forked from neo-project/neo-devpack-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReachability.cs
409 lines (394 loc) · 22.3 KB
/
Reachability.cs
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using static Neo.Optimizer.JumpTarget;
using static Neo.Optimizer.OpCodeTypes;
using static Neo.Optimizer.Optimizer;
namespace Neo.Optimizer
{
static class Reachability
{
#pragma warning disable SYSLIB1045 // Convert to 'GeneratedRegexAttribute'.
private static readonly Regex RangeRegex = new(@"(\d+)\-(\d+)", RegexOptions.Compiled);
private static readonly Regex SequencePointRegex = new(@"(\d+)(\[\d+\]\d+\:\d+\-\d+\:\d+)", RegexOptions.Compiled);
private static readonly Regex DocumentRegex = new(@"\[(\d+)\](\d+)\:(\d+)\-(\d+)\:(\d+)", RegexOptions.Compiled);
#pragma warning restore SYSLIB1045 // Convert to 'GeneratedRegexAttribute'.
public enum TryStack
{
ENTRY,
TRY,
CATCH,
FINALLY,
}
public enum BranchType
{
OK, // One of the branches may return without exception
THROW, // All branches surely has exceptions, but can be catched
ABORT, // All branches abort, and cannot be catched
UNCOVERED,
}
[Strategy(Priority = int.MaxValue)]
public static (NefFile, ContractManifest, JObject) RemoveUncoveredInstructions(NefFile nef, ContractManifest manifest, JObject debugInfo)
{
Dictionary<int, BranchType> coveredMap = FindCoveredInstructions(nef, manifest, debugInfo);
Script oldScript = nef.Script;
List<(int, Instruction)> oldAddressAndInstructionsList = oldScript.EnumerateInstructions().ToList();
Dictionary<int, Instruction> oldAddressToInstruction = new();
foreach ((int a, Instruction i) in oldAddressAndInstructionsList)
oldAddressToInstruction.Add(a, i);
//DumpNef.GenerateDumpNef(nef, debugInfo);
//coveredMap.Where(kv => !kv.Value).Select(kv => (kv.Key, oldAddressToInstruction[kv.Key].OpCode)).ToList();
System.Collections.Specialized.OrderedDictionary simplifiedInstructionsToAddress = new();
int currentAddress = 0;
foreach ((int a, Instruction i) in oldAddressAndInstructionsList)
{
if (coveredMap[a] != BranchType.UNCOVERED)
{
simplifiedInstructionsToAddress.Add(i, currentAddress);
currentAddress += i.Size;
}
else
continue;
}
(Dictionary<Instruction, Instruction> jumpInstructionSourceToTargets,
Dictionary<Instruction, (Instruction, Instruction)> tryInstructionSourceToTargets, _)
= FindAllJumpAndTrySourceToTargets(oldAddressAndInstructionsList);
List<byte> simplifiedScript = new();
foreach (DictionaryEntry item in simplifiedInstructionsToAddress)
{
(Instruction i, int a) = ((Instruction)item.Key, (int)item.Value!);
simplifiedScript.Add((byte)i.OpCode);
int operandSizeLength = OperandSizePrefixTable[(int)i.OpCode];
simplifiedScript = simplifiedScript.Concat(BitConverter.GetBytes(i.Operand.Length)[0..operandSizeLength]).ToList();
if (jumpInstructionSourceToTargets.TryGetValue(i, out Instruction? dst))
{
int delta;
if (simplifiedInstructionsToAddress.Contains(dst)) // target instruction not deleted
delta = (int)simplifiedInstructionsToAddress[dst]! - a;
else if (i.OpCode == OpCode.PUSHA)
delta = 0; // TODO: decide a good target
else
throw new BadScriptException($"Target instruction of {i.OpCode} at address {a} is deleted");
if (i.OpCode == OpCode.JMP || conditionalJump.Contains(i.OpCode) || i.OpCode == OpCode.CALL || i.OpCode == OpCode.ENDTRY)
simplifiedScript.Add(BitConverter.GetBytes(delta)[0]);
if (i.OpCode == OpCode.PUSHA || i.OpCode == OpCode.JMP_L || conditionalJump_L.Contains(i.OpCode) || i.OpCode == OpCode.CALL_L || i.OpCode == OpCode.ENDTRY_L)
simplifiedScript = simplifiedScript.Concat(BitConverter.GetBytes(delta)).ToList();
continue;
}
if (tryInstructionSourceToTargets.ContainsKey(i))
{
(Instruction dst1, Instruction dst2) = tryInstructionSourceToTargets[i];
(int delta1, int delta2) = ((int)simplifiedInstructionsToAddress[dst1]! - a, (int)simplifiedInstructionsToAddress[dst2]! - a);
if (i.OpCode == OpCode.TRY)
{
simplifiedScript.Add(BitConverter.GetBytes(delta1)[0]);
simplifiedScript.Add(BitConverter.GetBytes(delta2)[0]);
}
if (i.OpCode == OpCode.TRY_L)
{
simplifiedScript = simplifiedScript.Concat(BitConverter.GetBytes(delta1)).ToList();
simplifiedScript = simplifiedScript.Concat(BitConverter.GetBytes(delta2)).ToList();
}
continue;
}
if (i.Operand.Length != 0)
simplifiedScript = simplifiedScript.Concat(i.Operand.ToArray()).ToList();
}
//Dictionary<int, (int docId, int startLine, int startCol, int endLine, int endCol)> newAddrToSequencePoint = new();
HashSet<JToken> methodsToRemove = new();
foreach (JToken? method in (JArray)debugInfo["methods"]!)
{
GroupCollection rangeGroups = RangeRegex.Match(method!["range"]!.AsString()).Groups;
(int oldMethodStart, int oldMethodEnd) = (int.Parse(rangeGroups[1].ToString()), int.Parse(rangeGroups[2].ToString()));
if (!simplifiedInstructionsToAddress.Contains(oldAddressToInstruction[oldMethodStart]))
{
methodsToRemove.Add(method);
continue;
}
int methodStart = (int)simplifiedInstructionsToAddress[oldAddressToInstruction[oldMethodStart]]!;
// The instruction at the end of the method may have been deleted.
// We need to find the last instruction that is not deleted.
//int methodEnd = (int)simplifiedInstructionsToAddress[oldAddressToInstruction[oldMethodEnd]]!;
int oldMethodEndNotDeleted = oldAddressToInstruction.Where(kv =>
kv.Key >= oldMethodStart && kv.Key <= oldMethodEnd &&
simplifiedInstructionsToAddress.Contains(kv.Value)
).Max(kv => kv.Key);
int methodEnd = (int)simplifiedInstructionsToAddress[oldAddressToInstruction[oldMethodEndNotDeleted]]!;
method["range"] = $"{methodStart}-{methodEnd}";
int previousSequencePoint = methodStart;
JArray newSequencePoints = new();
foreach (JToken? sequencePoint in (JArray)method!["sequence-points"]!)
{
GroupCollection sequencePointGroups = SequencePointRegex.Match(sequencePoint!.AsString()).Groups;
int startingInstructionAddress = int.Parse(sequencePointGroups[1].ToString());
Instruction oldInstruction = oldAddressToInstruction[startingInstructionAddress];
if (simplifiedInstructionsToAddress.Contains(oldInstruction))
{
startingInstructionAddress = (int)simplifiedInstructionsToAddress[oldInstruction]!;
newSequencePoints.Add(new JString($"{startingInstructionAddress}{sequencePointGroups[2]}"));
previousSequencePoint = startingInstructionAddress;
}
else
newSequencePoints.Add(new JString($"{previousSequencePoint}{sequencePointGroups[2]}"));
}
method["sequence-points"] = newSequencePoints;
}
JArray methods = (JArray)debugInfo["methods"]!;
foreach (JToken method in methodsToRemove)
methods.Remove(method);
foreach (ContractMethodDescriptor method in manifest.Abi.Methods)
method.Offset = (int)simplifiedInstructionsToAddress[oldAddressToInstruction[method.Offset]]!;
Script newScript = new(simplifiedScript.ToArray());
nef.Script = newScript;
nef.Compiler = AppDomain.CurrentDomain.FriendlyName;
nef.CheckSum = NefFile.ComputeChecksum(nef);
return (nef, manifest, debugInfo);
}
public static Dictionary<int, BranchType>
FindCoveredInstructions(NefFile nef, ContractManifest manifest, JToken debugInfo)
{
Script script = nef.Script;
Dictionary<int, BranchType> coveredMap = new();
foreach ((int addr, Instruction _) in script.EnumerateInstructions())
coveredMap.Add(addr, BranchType.UNCOVERED);
// It is unsafe to go parallel, because the coveredMap value is not true/false
//Parallel.ForEach(manifest.Abi.Methods, method =>
// CoverInstruction(method.Offset, script, coveredMap)
//);
foreach ((int addr, _) in EntryPoint.EntryPointsByMethod(manifest, debugInfo))
CoverInstruction(addr, script, coveredMap);
return coveredMap;
}
/// <summary>
///
/// </summary>
/// <param name="addr"></param>
/// <param name="script"></param>
/// <param name="coveredMap"></param>
/// <returns>Whether it is possible to return without exception</returns>
/// <exception cref="BadScriptException"></exception>
/// <exception cref="NotImplementedException"></exception>
public static BranchType CoverInstruction(int addr, Script script, Dictionary<int, BranchType> coveredMap, Stack<((int returnAddr, int finallyAddr), TryStack stackType)>? stack = null, bool throwed = false)
{
int entranceAddr = addr;
stack ??= new();
if (stack.Count == 0)
stack.Push(((-1, -1), TryStack.ENTRY));
while (stack.Count > 0)
{
if (!throwed)
goto HANDLE_NORMAL_CASE;
HANDLE_THROW:
throwed = true;
TryStack stackType;
int catchAddr; int finallyAddr;
do
((catchAddr, finallyAddr), stackType) = stack.Pop();
while (stackType != TryStack.TRY && stackType != TryStack.CATCH && stack.Count > 0);
if (stackType == TryStack.TRY) // goto CATCH or FINALLY
{
throwed = false;
if (catchAddr != -1)
{
addr = catchAddr;
stack.Push(((-1, finallyAddr), TryStack.CATCH));
}
else if (finallyAddr != -1)
{
addr = finallyAddr;
stack.Push(((-1, -1), TryStack.FINALLY));
}
}
if (stackType == TryStack.CATCH) // goto FINALLY
{
throwed = false;
if (finallyAddr != -1)
{
addr = finallyAddr;
stack.Push(((-1, -1), TryStack.FINALLY));
}
}
continue;
HANDLE_NORMAL_CASE:
if (!coveredMap.ContainsKey(addr))
throw new BadScriptException($"wrong address {addr}");
if (coveredMap[addr] != BranchType.UNCOVERED)
// We have visited the code. Skip it.
return coveredMap[addr];
Instruction instruction = script.GetInstruction(addr);
if (instruction.OpCode != OpCode.NOP)
coveredMap[addr] = BranchType.OK;
// TODO: ABORTMSG may THROW instead of ABORT. Just throw new NotImplementedException for ABORTMSG?
if (instruction.OpCode == OpCode.ABORT || instruction.OpCode == OpCode.ABORTMSG)
{
// See if we are in a try. There may still be runtime exceptions
((catchAddr, finallyAddr), stackType) = stack.Peek();
if (stackType == TryStack.TRY && catchAddr != -1)
{
// Visit catchAddr because there may still be exceptions at runtime
coveredMap[entranceAddr] = CoverInstruction(catchAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
return coveredMap[entranceAddr];
}
if (stackType == TryStack.CATCH && finallyAddr != -1)
{
// Visit finallyAddr because there may still be exceptions at runtime
coveredMap[entranceAddr] = CoverInstruction(finallyAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
return coveredMap[entranceAddr];
}
coveredMap[entranceAddr] = BranchType.ABORT;
return coveredMap[entranceAddr];
}
if (callWithJump.Contains(instruction.OpCode))
{
int callTarget = ComputeJumpTarget(addr, instruction);
BranchType returnedType = CoverInstruction(callTarget, script, coveredMap);
if (returnedType == BranchType.OK)
{
addr += instruction.Size;
continue;
}
if (returnedType == BranchType.ABORT)
{
// See if we are in a try. There may still be runtime exceptions
((catchAddr, finallyAddr), stackType) = stack.Peek();
if (stackType == TryStack.TRY && catchAddr != -1)
{
// Visit catchAddr because there may still be exceptions at runtime
coveredMap[entranceAddr] = CoverInstruction(catchAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
return coveredMap[entranceAddr];
}
if (stackType == TryStack.CATCH && finallyAddr != -1)
{
// Visit finallyAddr because there may still be exceptions at runtime
coveredMap[entranceAddr] = CoverInstruction(finallyAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
return coveredMap[entranceAddr];
}
coveredMap[entranceAddr] = BranchType.ABORT;
return coveredMap[entranceAddr];
}
if (returnedType == BranchType.THROW)
goto HANDLE_THROW;
}
if (instruction.OpCode == OpCode.RET)
{
// See if we are in a try. There may still be runtime exceptions
((catchAddr, finallyAddr), stackType) = stack.Peek();
if (stackType == TryStack.TRY && catchAddr != -1)
// Visit catchAddr because there may still be exceptions at runtime
CoverInstruction(catchAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
if (stackType == TryStack.CATCH && finallyAddr != -1)
// Visit finallyAddr because there may still be exceptions at runtime
CoverInstruction(finallyAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
coveredMap[entranceAddr] = BranchType.OK;
return coveredMap[entranceAddr];
}
if (tryThrowFinally.Contains(instruction.OpCode))
{
if (instruction.OpCode == OpCode.TRY || instruction.OpCode == OpCode.TRY_L)
stack.Push((ComputeTryTarget(addr, instruction), TryStack.TRY));
if (instruction.OpCode == OpCode.THROW)
goto HANDLE_THROW;
if (instruction.OpCode == OpCode.ENDTRY)
{
((catchAddr, finallyAddr), stackType) = stack.Peek();
if (stackType != TryStack.TRY && stackType != TryStack.CATCH)
throw new BadScriptException("No try stack on ENDTRY");
if (stackType == TryStack.TRY && catchAddr != -1)
// Visit catchAddr because there may still be exceptions at runtime
CoverInstruction(catchAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
if (stackType == TryStack.CATCH && finallyAddr != -1)
// Visit finallyAddr because there may still be exceptions at runtime
CoverInstruction(finallyAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
stack.Pop();
int endPointer = addr + instruction.TokenI8;
if (finallyAddr != -1)
{
stack.Push(((-1, endPointer), TryStack.FINALLY));
addr = finallyAddr;
}
else
addr = endPointer;
continue;
}
if (instruction.OpCode == OpCode.ENDTRY_L)
{
((_, finallyAddr), stackType) = stack.Pop();
if (stackType != TryStack.TRY) throw new BadScriptException("No try stack on ENDTRY");
int endPointer = addr + instruction.TokenI32;
if (finallyAddr != -1)
{
stack.Push(((-1, endPointer), TryStack.FINALLY));
addr = finallyAddr;
}
else
addr = endPointer;
continue;
}
if (instruction.OpCode == OpCode.ENDFINALLY)
{
((_, _), stackType) = stack.Pop();
if (stackType != TryStack.FINALLY)
throw new BadScriptException("No finally stack on ENDFINALLY");
addr += instruction.Size;
continue;
}
}
if (unconditionalJump.Contains(instruction.OpCode))
{
addr = ComputeJumpTarget(addr, instruction);
continue;
}
if (conditionalJump.Contains(instruction.OpCode) || conditionalJump_L.Contains(instruction.OpCode))
{
int targetAddress = ComputeJumpTarget(addr, instruction);
BranchType noJump = CoverInstruction(addr + instruction.Size, script, coveredMap, stack: new(stack.Reverse()));
BranchType jump = CoverInstruction(targetAddress, script, coveredMap, stack: new(stack.Reverse()));
if (noJump == BranchType.OK || jump == BranchType.OK)
{
// See if we are in a try. There may still be runtime exceptions
((catchAddr, finallyAddr), stackType) = stack.Peek();
if (stackType == TryStack.TRY && catchAddr != -1)
// Visit catchAddr because there may still be exceptions at runtime
CoverInstruction(catchAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
if (stackType == TryStack.CATCH && finallyAddr != -1)
// Visit finallyAddr because there may still be exceptions at runtime
CoverInstruction(finallyAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
coveredMap[entranceAddr] = BranchType.OK;
return coveredMap[entranceAddr];
}
if (noJump == BranchType.ABORT && jump == BranchType.ABORT)
{
// See if we are in a try. There may still be runtime exceptions
((catchAddr, finallyAddr), stackType) = stack.Peek();
if (stackType == TryStack.TRY && catchAddr != -1)
{
// Visit catchAddr because there may still be exceptions at runtime
coveredMap[entranceAddr] = CoverInstruction(catchAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
return coveredMap[entranceAddr];
}
if (stackType == TryStack.CATCH && finallyAddr != -1)
{
// Visit finallyAddr because there may still be exceptions at runtime
coveredMap[entranceAddr] = CoverInstruction(finallyAddr, script, coveredMap, stack: new(stack.Reverse()), throwed: true);
return coveredMap[entranceAddr];
}
coveredMap[entranceAddr] = BranchType.ABORT;
return coveredMap[entranceAddr];
}
if (noJump == BranchType.THROW || jump == BranchType.THROW) // THROW, ABORT => THROW
goto HANDLE_THROW;
throw new Exception($"Unknown {nameof(BranchType)} {noJump} {jump}");
}
addr += instruction.Size;
}
coveredMap[entranceAddr] = throwed ? BranchType.THROW : BranchType.OK;
return coveredMap[entranceAddr];
}
}
}