-
-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathISILControlFlowGraph.cs
More file actions
291 lines (246 loc) · 9.86 KB
/
ISILControlFlowGraph.cs
File metadata and controls
291 lines (246 loc) · 9.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Cpp2IL.Core.ISIL;
namespace Cpp2IL.Core.Graphs;
public class ISILControlFlowGraph
{
public Block EntryBlock => entryBlock;
public Block ExitBlock => exitBlock;
public int Count => blockSet.Count;
public Collection<Block> Blocks => blockSet;
private int idCounter;
private Collection<Block> blockSet;
private Block exitBlock;
private Block entryBlock;
public ISILControlFlowGraph()
{
entryBlock = new Block() { ID = idCounter++ };
entryBlock.BlockType = BlockType.Entry;
exitBlock = new Block() { ID = idCounter++ };
exitBlock.BlockType = BlockType.Exit;
blockSet =
[
entryBlock,
exitBlock
];
}
private bool TryGetTargetJumpInstructionIndex(InstructionSetIndependentInstruction instruction, out uint jumpInstructionIndex)
{
jumpInstructionIndex = 0;
try
{
jumpInstructionIndex = ((InstructionSetIndependentInstruction)instruction.Operands[0].Data).InstructionIndex;
return true;
}
catch
{
// ignore
}
return false;
}
public void Build(List<InstructionSetIndependentInstruction> instructions)
{
if (instructions == null)
throw new ArgumentNullException(nameof(instructions));
var currentBlock = new Block() { ID = idCounter++ };
AddNode(currentBlock);
AddDirectedEdge(entryBlock, currentBlock);
for (var i = 0; i < instructions.Count; i++)
{
var isLast = i == instructions.Count - 1;
switch (instructions[i].FlowControl)
{
case IsilFlowControl.UnconditionalJump:
currentBlock.AddInstruction(instructions[i]);
if (!isLast)
{
var newNodeFromJmp = new Block() { ID = idCounter++ };
AddNode(newNodeFromJmp);
if (TryGetTargetJumpInstructionIndex(instructions[i], out uint jumpTargetIndex))
{
// var result = instructions.Any(instruction => instruction.InstructionIndex == jumpTargetIndex);
currentBlock.Dirty = true;
}
else
{
AddDirectedEdge(currentBlock, exitBlock);
}
currentBlock.CaculateBlockType();
currentBlock = newNodeFromJmp;
}
else
{
AddDirectedEdge(currentBlock, exitBlock);
currentBlock.Dirty = true;
}
break;
case IsilFlowControl.MethodCall:
currentBlock.AddInstruction(instructions[i]);
if (!isLast)
{
var newNodeFromCall = new Block() { ID = idCounter++ };
AddNode(newNodeFromCall);
AddDirectedEdge(currentBlock, newNodeFromCall);
currentBlock.CaculateBlockType();
currentBlock = newNodeFromCall;
}
else
{
AddDirectedEdge(currentBlock, exitBlock);
currentBlock.CaculateBlockType();
}
break;
case IsilFlowControl.Continue:
currentBlock.AddInstruction(instructions[i]);
if (isLast)
{
// TODO: Investiage
/* This shouldn't happen, we've either smashed into another method or random data such as a jump table */
}
break;
case IsilFlowControl.MethodReturn:
currentBlock.AddInstruction(instructions[i]);
if (!isLast)
{
var newNodeFromReturn = new Block() { ID = idCounter++ };
AddNode(newNodeFromReturn);
AddDirectedEdge(currentBlock, exitBlock);
currentBlock.CaculateBlockType();
currentBlock = newNodeFromReturn;
}
else
{
AddDirectedEdge(currentBlock, exitBlock);
currentBlock.CaculateBlockType();
}
break;
case IsilFlowControl.ConditionalJump:
currentBlock.AddInstruction(instructions[i]);
if (!isLast)
{
var newNodeFromConditionalBranch = new Block() { ID = idCounter++ };
AddNode(newNodeFromConditionalBranch);
AddDirectedEdge(currentBlock, newNodeFromConditionalBranch);
currentBlock.CaculateBlockType();
currentBlock.Dirty = true;
currentBlock = newNodeFromConditionalBranch;
}
else
{
AddDirectedEdge(currentBlock, exitBlock);
}
break;
case IsilFlowControl.Interrupt:
currentBlock.AddInstruction(instructions[i]);
var newNodeFromInterrupt = new Block() { ID = idCounter++ };
AddNode(newNodeFromInterrupt);
AddDirectedEdge(currentBlock, exitBlock);
currentBlock.CaculateBlockType();
currentBlock = newNodeFromInterrupt;
break;
case IsilFlowControl.IndexedJump:
// This could be a part of either 2 things, a jmp to a jump table (switch statement) or a tail call to another function maybe? I dunno
throw new NotImplementedException("Indirect branch not implemented currently");
default:
throw new NotImplementedException($"{instructions[i]} {instructions[i].FlowControl}");
}
}
for (var index = 0; index < blockSet.Count; index++)
{
var node = blockSet[index];
if (node.Dirty)
FixBlock(node);
}
}
public void CalculateDominations()
{
foreach (var block in blockSet)
{
throw new NotImplementedException();
}
}
private void FixBlock(Block block, bool removeJmp = false)
{
if (block.BlockType is BlockType.Fall)
return;
var jump = block.isilInstructions.Last();
var targetInstruction = jump.Operands[0].Data as InstructionSetIndependentInstruction;
var destination = FindNodeByInstruction(targetInstruction);
if (destination == null)
{
//We assume that we're tail calling another method somewhere. Need to verify if this breaks anywhere but it shouldn't in general
block.BlockType = BlockType.Call;
return;
}
int index = destination.isilInstructions.FindIndex(instruction => instruction == targetInstruction);
var targetNode = SplitAndCreate(destination, index);
AddDirectedEdge(block, targetNode);
block.Dirty = false;
if (removeJmp)
block.isilInstructions.Remove(jump);
}
protected Block? FindNodeByInstruction(InstructionSetIndependentInstruction? instruction)
{
if (instruction == null)
return null;
for (var i = 0; i < blockSet.Count; i++)
{
var block = blockSet[i];
for (var j = 0; j < block.isilInstructions.Count; j++)
{
var instr = block.isilInstructions[j];
if (instr == instruction)
{
return block;
}
}
}
return null;
}
private Block SplitAndCreate(Block target, int index)
{
if (index < 0 || index >= target.isilInstructions.Count)
throw new ArgumentOutOfRangeException(nameof(index));
// Don't need to split...
if (index == 0)
return target;
var newNode = new Block() { ID = idCounter++ };
// target split in two
// targetFirstPart -> targetSecondPart aka newNode
// Take the instructions for the secondPart
var instructions = target.isilInstructions.GetRange(index, target.isilInstructions.Count - index);
target.isilInstructions.RemoveRange(index, target.isilInstructions.Count - index);
// Add those to the newNode
newNode.isilInstructions.AddRange(instructions);
// Transfer control flow
newNode.BlockType = target.BlockType;
target.BlockType = BlockType.Fall;
// Transfer successors
newNode.Successors = target.Successors;
if (target.Dirty)
newNode.Dirty = true;
target.Dirty = false;
target.Successors = [];
// Correct the predecessors for all the successors
foreach (var successor in newNode.Successors)
{
for (int i = 0; i < successor.Predecessors.Count; i++)
{
if (successor.Predecessors[i].ID == target.ID)
successor.Predecessors[i] = newNode;
}
}
// Add newNode and connect it
AddNode(newNode);
AddDirectedEdge(target, newNode);
return newNode;
}
private void AddDirectedEdge(Block from, Block to)
{
from.Successors.Add(to);
to.Predecessors.Add(from);
}
protected void AddNode(Block block) => blockSet.Add(block);
}