Skip to content

Commit 250285d

Browse files
authored
feat: add MLIL SSA builders (#58)
1 parent c2eb97b commit 250285d

7 files changed

Lines changed: 560 additions & 5 deletions

‎Handle/BNMediumLevelILFunction.cs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2910,18 +2910,31 @@ public MediumLevelILExpressionIndex EmitIntrinsic(
29102910
}
29112911

29122912
public MediumLevelILExpressionIndex EmitFreeVarSlot(
2913-
ulong size,
29142913
Variable variable,
29152914
SourceLocation? location = null
29162915
)
29172916
{
29182917
return this.AddExpression(
29192918
MediumLevelILOperation.MLIL_FREE_VAR_SLOT,
29202919
location,
2921-
size,
2920+
0,
29222921
variable.Identifier
29232922
);
29242923
}
2924+
2925+
/// <summary>
2926+
/// Compatibility overload for the former signature whose size argument is not part of
2927+
/// the official MLIL free-variable-slot operation.
2928+
/// </summary>
2929+
[Obsolete("Use the EmitFreeVarSlot overload without a size argument.")]
2930+
public MediumLevelILExpressionIndex EmitFreeVarSlot(
2931+
ulong size,
2932+
Variable variable,
2933+
SourceLocation? location = null
2934+
)
2935+
{
2936+
return this.EmitFreeVarSlot(variable, location);
2937+
}
29252938

29262939
public MediumLevelILExpressionIndex EmitBreakpoint(
29272940
SourceLocation? location = null

‎MediumLevelIL/MLILLoadStructSSA.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public MediumLevelILInstruction Source
1515
{
1616
get
1717
{
18-
return this.GetOperandAsExpression((OperandIndex)1);
18+
return this.GetOperandAsExpression((OperandIndex)0);
1919
}
2020
}
2121

‎MediumLevelIL/MLILStoreStructSSA.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public MediumLevelILInstruction Source
4747
{
4848
get
4949
{
50-
return this.GetOperandAsExpression((OperandIndex)1);
50+
return this.GetOperandAsExpression((OperandIndex)4);
5151
}
5252
}
5353
}

‎MediumLevelIL/MLILTailCallUntypedSSA.cs‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,21 @@ public MediumLevelILInstruction[] Parameters
6565
return instruction.Source;
6666
}
6767
}
68+
69+
public ulong ParametersSourceMemory
70+
{
71+
get
72+
{
73+
MLILCallParamSSA? instruction = this.GetOperandAsExpression((OperandIndex)2) as MLILCallParamSSA;
74+
75+
if (null == instruction)
76+
{
77+
throw new Exception("must be MediumLevelILCallParamSSA");
78+
}
79+
80+
return instruction.SourceMemory;
81+
}
82+
}
6883

6984
public MediumLevelILInstruction Stack
7085
{

‎MediumLevelIL/MediumLevelILDetailedOperandsTable.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ internal static class MediumLevelILDetailedOperandsTable
167167
{ MediumLevelILOperation.MLIL_CALL_SSA, new ILOperandDescriptor[] { new ILOperandDescriptor("output", ILOperandKind.CallOutputSsaVariables, 0, "List[SSAVariable]"), new ILOperandDescriptor("output_dest_memory", ILOperandKind.CallOutputSsaMemory, 0, "int"), new ILOperandDescriptor("dest", ILOperandKind.Expression, 1, "MediumLevelILInstruction"), new ILOperandDescriptor("params", ILOperandKind.ExpressionList, 2, "List[MediumLevelILInstruction]"), new ILOperandDescriptor("src_memory", ILOperandKind.Integer, 4, "int") } },
168168
{ MediumLevelILOperation.MLIL_CALL_UNTYPED_SSA, new ILOperandDescriptor[] { new ILOperandDescriptor("output", ILOperandKind.CallOutputSsaVariables, 0, "List[SSAVariable]"), new ILOperandDescriptor("output_dest_memory", ILOperandKind.CallOutputSsaMemory, 0, "int"), new ILOperandDescriptor("dest", ILOperandKind.Expression, 1, "MediumLevelILInstruction"), new ILOperandDescriptor("params", ILOperandKind.CallParamSsaExpressions, 2, "List[MediumLevelILInstruction]"), new ILOperandDescriptor("params_src_memory", ILOperandKind.CallParamSsaMemory, 2, "int"), new ILOperandDescriptor("stack", ILOperandKind.Expression, 3, "MediumLevelILInstruction") } },
169169
{ MediumLevelILOperation.MLIL_TAILCALL_SSA, new ILOperandDescriptor[] { new ILOperandDescriptor("output", ILOperandKind.CallOutputSsaVariables, 0, "List[SSAVariable]"), new ILOperandDescriptor("output_dest_memory", ILOperandKind.CallOutputSsaMemory, 0, "int"), new ILOperandDescriptor("dest", ILOperandKind.Expression, 1, "MediumLevelILInstruction"), new ILOperandDescriptor("params", ILOperandKind.ExpressionList, 2, "List[MediumLevelILInstruction]"), new ILOperandDescriptor("src_memory", ILOperandKind.Integer, 4, "int") } },
170-
{ MediumLevelILOperation.MLIL_TAILCALL_UNTYPED_SSA, new ILOperandDescriptor[] { new ILOperandDescriptor("output", ILOperandKind.CallOutputSsaVariables, 0, "List[SSAVariable]"), new ILOperandDescriptor("output_dest_memory", ILOperandKind.CallOutputSsaMemory, 0, "int"), new ILOperandDescriptor("dest", ILOperandKind.Expression, 1, "MediumLevelILInstruction"), new ILOperandDescriptor("params", ILOperandKind.CallParamSsaExpressions, 2, "List[MediumLevelILInstruction]"), new ILOperandDescriptor("stack", ILOperandKind.Expression, 3, "MediumLevelILInstruction") } },
170+
{ MediumLevelILOperation.MLIL_TAILCALL_UNTYPED_SSA, new ILOperandDescriptor[] { new ILOperandDescriptor("output", ILOperandKind.CallOutputSsaVariables, 0, "List[SSAVariable]"), new ILOperandDescriptor("output_dest_memory", ILOperandKind.CallOutputSsaMemory, 0, "int"), new ILOperandDescriptor("dest", ILOperandKind.Expression, 1, "MediumLevelILInstruction"), new ILOperandDescriptor("params", ILOperandKind.CallParamSsaExpressions, 2, "List[MediumLevelILInstruction]"), new ILOperandDescriptor("params_src_memory", ILOperandKind.CallParamSsaMemory, 2, "int"), new ILOperandDescriptor("stack", ILOperandKind.Expression, 3, "MediumLevelILInstruction") } },
171171
{ MediumLevelILOperation.MLIL_CALL, new ILOperandDescriptor[] { new ILOperandDescriptor("output", ILOperandKind.VariableList, 0, "List[Variable]"), new ILOperandDescriptor("dest", ILOperandKind.Expression, 2, "MediumLevelILInstruction"), new ILOperandDescriptor("params", ILOperandKind.ExpressionList, 3, "List[MediumLevelILInstruction]") } },
172172
{ MediumLevelILOperation.MLIL_IF, new ILOperandDescriptor[] { new ILOperandDescriptor("condition", ILOperandKind.Expression, 0, "MediumLevelILInstruction"), new ILOperandDescriptor("true", ILOperandKind.Integer, 1, "InstructionIndex"), new ILOperandDescriptor("false", ILOperandKind.Integer, 2, "InstructionIndex") } },
173173
{ MediumLevelILOperation.MLIL_STORE_SSA, new ILOperandDescriptor[] { new ILOperandDescriptor("dest", ILOperandKind.Expression, 0, "MediumLevelILInstruction"), new ILOperandDescriptor("dest_memory", ILOperandKind.Integer, 1, "int"), new ILOperandDescriptor("src_memory", ILOperandKind.Integer, 2, "int"), new ILOperandDescriptor("src", ILOperandKind.Expression, 3, "MediumLevelILInstruction") } },
Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
namespace BinaryNinja
2+
{
3+
public sealed partial class MediumLevelILFunction
4+
{
5+
public MediumLevelILExpressionIndex EmitCallOutputSSA(
6+
MediumLevelILSSAVariable[] outputs,
7+
ulong destinationMemoryVersion,
8+
SourceLocation? location = null)
9+
{
10+
return this.AddExpression(
11+
MediumLevelILOperation.MLIL_CALL_OUTPUT_SSA,
12+
location,
13+
0,
14+
destinationMemoryVersion,
15+
(ulong)outputs.Length * 2,
16+
(ulong)this.AddSSAVariableList(outputs));
17+
}
18+
19+
public MediumLevelILExpressionIndex EmitCallParamSSA(
20+
MediumLevelILExpressionIndex[] parameters,
21+
ulong sourceMemoryVersion,
22+
SourceLocation? location = null)
23+
{
24+
return this.AddExpression(
25+
MediumLevelILOperation.MLIL_CALL_PARAM_SSA,
26+
location,
27+
0,
28+
sourceMemoryVersion,
29+
(ulong)parameters.Length,
30+
(ulong)this.AddOperandList(parameters));
31+
}
32+
33+
public MediumLevelILExpressionIndex EmitCallSSA(
34+
MediumLevelILSSAVariable[] outputs,
35+
MediumLevelILExpressionIndex destination,
36+
MediumLevelILExpressionIndex[] parameters,
37+
ulong newMemoryVersion,
38+
ulong previousMemoryVersion,
39+
SourceLocation? location = null)
40+
{
41+
return this.AddExpression(
42+
MediumLevelILOperation.MLIL_CALL_SSA,
43+
location,
44+
0,
45+
(ulong)this.EmitCallOutputSSA(outputs, newMemoryVersion, location),
46+
(ulong)destination,
47+
(ulong)parameters.Length,
48+
(ulong)this.AddOperandList(parameters),
49+
previousMemoryVersion);
50+
}
51+
52+
public MediumLevelILExpressionIndex EmitCallUntypedSSA(
53+
MediumLevelILSSAVariable[] outputs,
54+
MediumLevelILExpressionIndex destination,
55+
MediumLevelILExpressionIndex[] parameters,
56+
ulong newMemoryVersion,
57+
ulong previousMemoryVersion,
58+
MediumLevelILExpressionIndex stack,
59+
SourceLocation? location = null)
60+
{
61+
return this.AddExpression(
62+
MediumLevelILOperation.MLIL_CALL_UNTYPED_SSA,
63+
location,
64+
0,
65+
(ulong)this.EmitCallOutputSSA(outputs, newMemoryVersion, location),
66+
(ulong)destination,
67+
(ulong)this.EmitCallParamSSA(parameters, previousMemoryVersion, location),
68+
(ulong)stack);
69+
}
70+
71+
public MediumLevelILExpressionIndex EmitSysCallSSA(
72+
MediumLevelILSSAVariable[] outputs,
73+
MediumLevelILExpressionIndex[] parameters,
74+
ulong newMemoryVersion,
75+
ulong previousMemoryVersion,
76+
SourceLocation? location = null)
77+
{
78+
return this.AddExpression(
79+
MediumLevelILOperation.MLIL_SYSCALL_SSA,
80+
location,
81+
0,
82+
(ulong)this.EmitCallOutputSSA(outputs, newMemoryVersion, location),
83+
(ulong)parameters.Length,
84+
(ulong)this.AddOperandList(parameters),
85+
previousMemoryVersion);
86+
}
87+
88+
public MediumLevelILExpressionIndex EmitSysCallUntypedSSA(
89+
MediumLevelILSSAVariable[] outputs,
90+
MediumLevelILExpressionIndex[] parameters,
91+
ulong newMemoryVersion,
92+
ulong previousMemoryVersion,
93+
MediumLevelILExpressionIndex stack,
94+
SourceLocation? location = null)
95+
{
96+
return this.AddExpression(
97+
MediumLevelILOperation.MLIL_SYSCALL_UNTYPED_SSA,
98+
location,
99+
0,
100+
(ulong)this.EmitCallOutputSSA(outputs, newMemoryVersion, location),
101+
(ulong)this.EmitCallParamSSA(parameters, previousMemoryVersion, location),
102+
(ulong)stack);
103+
}
104+
105+
public MediumLevelILExpressionIndex EmitTailCallSSA(
106+
MediumLevelILSSAVariable[] outputs,
107+
MediumLevelILExpressionIndex destination,
108+
MediumLevelILExpressionIndex[] parameters,
109+
ulong newMemoryVersion,
110+
ulong previousMemoryVersion,
111+
SourceLocation? location = null)
112+
{
113+
return this.AddExpression(
114+
MediumLevelILOperation.MLIL_TAILCALL_SSA,
115+
location,
116+
0,
117+
(ulong)this.EmitCallOutputSSA(outputs, newMemoryVersion, location),
118+
(ulong)destination,
119+
(ulong)parameters.Length,
120+
(ulong)this.AddOperandList(parameters),
121+
previousMemoryVersion);
122+
}
123+
124+
public MediumLevelILExpressionIndex EmitTailCallUntypedSSA(
125+
MediumLevelILSSAVariable[] outputs,
126+
MediumLevelILExpressionIndex destination,
127+
MediumLevelILExpressionIndex[] parameters,
128+
ulong newMemoryVersion,
129+
ulong previousMemoryVersion,
130+
MediumLevelILExpressionIndex stack,
131+
SourceLocation? location = null)
132+
{
133+
return this.AddExpression(
134+
MediumLevelILOperation.MLIL_TAILCALL_UNTYPED_SSA,
135+
location,
136+
0,
137+
(ulong)this.EmitCallOutputSSA(outputs, newMemoryVersion, location),
138+
(ulong)destination,
139+
(ulong)this.EmitCallParamSSA(parameters, previousMemoryVersion, location),
140+
(ulong)stack);
141+
}
142+
143+
public MediumLevelILExpressionIndex EmitIntrinsicSSA(
144+
MediumLevelILSSAVariable[] outputs,
145+
IntrinsicIndex intrinsic,
146+
MediumLevelILExpressionIndex[] parameters,
147+
SourceLocation? location = null)
148+
{
149+
return this.AddExpression(
150+
MediumLevelILOperation.MLIL_INTRINSIC_SSA,
151+
location,
152+
0,
153+
(ulong)outputs.Length * 2,
154+
(ulong)this.AddSSAVariableList(outputs),
155+
(ulong)intrinsic,
156+
(ulong)parameters.Length,
157+
(ulong)this.AddOperandList(parameters));
158+
}
159+
160+
public MediumLevelILExpressionIndex EmitMemoryIntrinsicOutputSSA(
161+
MediumLevelILSSAVariable[] outputs,
162+
ulong destinationMemoryVersion,
163+
SourceLocation? location = null)
164+
{
165+
return this.AddExpression(
166+
MediumLevelILOperation.MLIL_MEMORY_INTRINSIC_OUTPUT_SSA,
167+
location,
168+
0,
169+
destinationMemoryVersion,
170+
(ulong)outputs.Length * 2,
171+
(ulong)this.AddSSAVariableList(outputs));
172+
}
173+
174+
public MediumLevelILExpressionIndex EmitMemoryIntrinsicSSA(
175+
MediumLevelILSSAVariable[] outputs,
176+
IntrinsicIndex intrinsic,
177+
MediumLevelILExpressionIndex[] parameters,
178+
ulong newMemoryVersion,
179+
ulong previousMemoryVersion,
180+
SourceLocation? location = null)
181+
{
182+
return this.AddExpression(
183+
MediumLevelILOperation.MLIL_MEMORY_INTRINSIC_SSA,
184+
location,
185+
0,
186+
(ulong)this.EmitMemoryIntrinsicOutputSSA(outputs, newMemoryVersion, location),
187+
(ulong)intrinsic,
188+
(ulong)parameters.Length,
189+
(ulong)this.AddOperandList(parameters),
190+
previousMemoryVersion);
191+
}
192+
}
193+
}

0 commit comments

Comments
 (0)