-
Notifications
You must be signed in to change notification settings - Fork 0
/
DumpNef.cs
314 lines (296 loc) · 15 KB
/
DumpNef.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
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Native;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Text.RegularExpressions;
namespace Neo.Optimizer
{
static class DumpNef
{
#pragma warning disable SYSLIB1045 // Convert to 'GeneratedRegexAttribute'.
private static readonly Regex DocumentRegex = new(@"\[(\d+)\](\d+)\:(\d+)\-(\d+)\:(\d+)", RegexOptions.Compiled);
private static readonly Regex RangeRegex = new(@"(\d+)\-(\d+)", RegexOptions.Compiled);
private static readonly Regex SequencePointRegex = new(@"(\d+)(\[\d+\]\d+\:\d+\-\d+\:\d+)", RegexOptions.Compiled);
#pragma warning restore SYSLIB1045 // Convert to 'GeneratedRegexAttribute'.
static readonly Lazy<IReadOnlyDictionary<uint, string>> sysCallNames = new(
() => ApplicationEngine.Services.ToImmutableDictionary(kvp => kvp.Value.Hash, kvp => kvp.Value.Name));
public static string Unzip(byte[] zippedBuffer)
{
using var zippedStream = new MemoryStream(zippedBuffer);
using var archive = new ZipArchive(zippedStream, ZipArchiveMode.Read, false, Encoding.UTF8);
var entry = archive.Entries.FirstOrDefault();
if (entry != null)
{
using var unzippedEntryStream = entry.Open();
using var ms = new MemoryStream();
unzippedEntryStream.CopyTo(ms);
var unzippedArray = ms.ToArray();
return Encoding.UTF8.GetString(unzippedArray);
}
throw new ArgumentException("No file found in zip archive");
}
public static byte[] Zip(byte[] content, string innerFilename)
{
using (var compressedFileStream = new MemoryStream())
{
using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false))
{
var zipEntry = zipArchive.CreateEntry(innerFilename);
using (var originalFileStream = new MemoryStream(content))
{
using (var zipEntryStream = zipEntry.Open())
{
originalFileStream.CopyTo(zipEntryStream);
}
}
}
return compressedFileStream.ToArray();
}
}
public static string GetInstructionAddressPadding(this Script script)
{
var digitCount = EnumerateInstructions(script).Last().address switch
{
var x when x < 10 => 1,
var x when x < 100 => 2,
var x when x < 1000 => 3,
var x when x < 10000 => 4,
var x when x <= ushort.MaxValue => 5,
_ => throw new Exception($"Max script length is {ushort.MaxValue} bytes"),
};
return new string('0', digitCount);
}
public static string WriteInstruction(int address, Instruction instruction, string padString, MethodToken[] tokens)
{
string result = "";
try
{
result += $"{address.ToString(padString)}";
result += $" {instruction.OpCode}";
if (!instruction.Operand.IsEmpty)
result += $" {instruction.GetOperandString()}";
var comment = instruction.GetComment(address, tokens);
if (comment.Length > 0)
result += $" # {comment}";
}
finally { }
return result;
}
public static IEnumerable<(int address, Instruction instruction)> EnumerateInstructions(this Script script, bool print = false)
{
int address = 0;
OpCode opcode = OpCode.PUSH0;
Instruction instruction;
for (; address < script.Length; address += instruction.Size)
{
instruction = script.GetInstruction(address);
opcode = instruction.OpCode;
if (print)
Console.WriteLine(WriteInstruction(address, instruction, "0000", Array.Empty<MethodToken>()));
yield return (address, instruction);
}
if (opcode != OpCode.RET)
yield return (address, Instruction.RET);
}
public static string GetOperandString(this Instruction instruction) => BitConverter.ToString(instruction.Operand.Span.ToArray());
public static string GetComment(this Instruction instruction, int ip, MethodToken[]? tokens = null)
{
tokens ??= Array.Empty<MethodToken>();
switch (instruction.OpCode)
{
case OpCode.PUSHINT8:
case OpCode.PUSHINT16:
case OpCode.PUSHINT32:
case OpCode.PUSHINT64:
case OpCode.PUSHINT128:
case OpCode.PUSHINT256:
return $"{new BigInteger(instruction.Operand.Span)}";
case OpCode.PUSHA:
return $"{checked(ip + instruction.TokenI32)}";
case OpCode.PUSHDATA1:
case OpCode.PUSHDATA2:
case OpCode.PUSHDATA4:
{
var text = System.Text.Encoding.UTF8.GetString(instruction.Operand.Span)
.Replace("\r", "\"\\r\"").Replace("\n", "\"\\n\"");
if (instruction.Operand.Length == 20)
{
return $"as script hash: {new UInt160(instruction.Operand.Span)}, as text: \"{text}\"";
}
return $"as text: \"{text}\"";
}
case OpCode.JMP:
case OpCode.JMPIF:
case OpCode.JMPIFNOT:
case OpCode.JMPEQ:
case OpCode.JMPNE:
case OpCode.JMPGT:
case OpCode.JMPGE:
case OpCode.JMPLT:
case OpCode.JMPLE:
case OpCode.CALL:
return OffsetComment(instruction.TokenI8);
case OpCode.JMP_L:
case OpCode.JMPIF_L:
case OpCode.JMPIFNOT_L:
case OpCode.JMPEQ_L:
case OpCode.JMPNE_L:
case OpCode.JMPGT_L:
case OpCode.JMPGE_L:
case OpCode.JMPLT_L:
case OpCode.JMPLE_L:
case OpCode.CALL_L:
return OffsetComment(instruction.TokenI32);
case OpCode.CALLT:
{
int index = instruction.TokenU16;
if (index >= tokens.Length)
return $"Unknown token {instruction.TokenU16}";
var token = tokens[index];
var contract = NativeContract.Contracts.SingleOrDefault(c => c.Hash == token.Hash);
var tokenName = contract is null ? $"{token.Hash}" : contract.Name;
return $"{tokenName}.{token.Method} token call";
}
case OpCode.TRY:
return TryComment(instruction.TokenI8, instruction.TokenI8_1);
case OpCode.TRY_L:
return TryComment(instruction.TokenI32, instruction.TokenI32_1);
case OpCode.ENDTRY:
return OffsetComment(instruction.TokenI8);
case OpCode.ENDTRY_L:
return OffsetComment(instruction.TokenI32);
case OpCode.SYSCALL:
return sysCallNames.Value.TryGetValue(instruction.TokenU32, out var name)
? $"{name} SysCall"
: $"Unknown SysCall {instruction.TokenU32}";
case OpCode.INITSSLOT:
return $"{instruction.TokenU8} static variables";
case OpCode.INITSLOT:
return $"{instruction.TokenU8} local variables, {instruction.TokenU8_1} arguments";
case OpCode.LDSFLD:
case OpCode.STSFLD:
case OpCode.LDLOC:
case OpCode.STLOC:
case OpCode.LDARG:
case OpCode.STARG:
return $"Slot index {instruction.TokenU8}";
case OpCode.NEWARRAY_T:
case OpCode.ISTYPE:
case OpCode.CONVERT:
return $"{(VM.Types.StackItemType)instruction.TokenU8} type";
default:
return string.Empty;
}
string OffsetComment(int offset) => $"pos: {checked(ip + offset)} (offset: {offset})";
string TryComment(int catchOffset, int finallyOffset)
{
var builder = new System.Text.StringBuilder();
builder.Append(catchOffset == 0 ? "no catch block, " : $"catch {OffsetComment(catchOffset)}, ");
builder.Append(finallyOffset == 0 ? "no finally block" : $"finally {OffsetComment(finallyOffset)}");
return builder.ToString();
}
}
public static (int start, int end) GetMethodStartEndAddress(string name, JToken debugInfo)
{
name = name.Length == 0 ? string.Empty : name[0].ToString().ToUpper() + name.Substring(1); // first letter uppercase
int start = -1, end = -1;
foreach (JToken? method in (JArray)debugInfo["methods"]!)
{
string methodName = method!["name"]!.AsString().Split(",")[1];
if (methodName == name)
{
GroupCollection rangeGroups = RangeRegex.Match(method!["range"]!.AsString()).Groups;
(start, end) = (int.Parse(rangeGroups[1].ToString()), int.Parse(rangeGroups[2].ToString()));
}
}
return (start, end);
}
public static List<int> OpCodeAddressesInMethod(NefFile nef, JToken DebugInfo, string method, OpCode opcode)
{
(int start, int end) = GetMethodStartEndAddress(method, DebugInfo);
List<(int a, VM.Instruction i)> instructions = EnumerateInstructions(nef.Script).ToList();
return instructions.Where(
ai => ai.i.OpCode == opcode &&
ai.a >= start && ai.a <= end
).Select(ai => ai.a).ToList();
}
public static string GenerateDumpNef(NefFile nef, JToken debugInfo)
{
Script script = nef.Script;
List<(int, Instruction)> addressAndInstructionsList = script.EnumerateInstructions().ToList();
Dictionary<int, Instruction> addressToInstruction = new();
foreach ((int a, Instruction i) in addressAndInstructionsList)
addressToInstruction.Add(a, i);
Dictionary<int, string> methodStartAddrToName = new();
Dictionary<int, string> methodEndAddrToName = new();
Dictionary<int, List<(int docId, int startLine, int startCol, int endLine, int endCol)>> newAddrToSequencePoint = new();
foreach (JToken? method in (JArray)debugInfo["methods"]!)
{
GroupCollection rangeGroups = RangeRegex.Match(method!["range"]!.AsString()).Groups;
(int methodStartAddr, int methodEndAddr) = (int.Parse(rangeGroups[1].ToString()), int.Parse(rangeGroups[2].ToString()));
methodStartAddrToName.Add(methodStartAddr, method!["id"]!.AsString()); // TODO: same format of method name as dumpnef
methodEndAddrToName.Add(methodEndAddr, method["id"]!.AsString());
foreach (JToken? sequencePoint in (JArray)method!["sequence-points"]!)
{
GroupCollection sequencePointGroups = SequencePointRegex.Match(sequencePoint!.AsString()).Groups;
GroupCollection documentGroups = DocumentRegex.Match(sequencePointGroups[2].ToString()).Groups;
int addr = int.Parse(sequencePointGroups[1].Value);
if (!newAddrToSequencePoint.ContainsKey(addr))
newAddrToSequencePoint.Add(addr, new());
newAddrToSequencePoint[addr].Add((
int.Parse(documentGroups[1].ToString()),
int.Parse(documentGroups[2].ToString()),
int.Parse(documentGroups[3].ToString()),
int.Parse(documentGroups[4].ToString()),
int.Parse(documentGroups[5].ToString())
));
}
}
Dictionary<string, string[]> docPathToContent = new();
string dumpnef = "";
foreach ((int a, Instruction i) in script.EnumerateInstructions(/*print: true*/).ToList())
{
if (methodStartAddrToName.ContainsKey(a))
dumpnef += $"# Method Start {methodStartAddrToName[a]}\n";
if (methodEndAddrToName.ContainsKey(a))
dumpnef += $"# Method End {methodEndAddrToName[a]}\n";
if (newAddrToSequencePoint.ContainsKey(a))
{
foreach ((int docId, int startLine, int startCol, int endLine, int endCol) in newAddrToSequencePoint[a])
{
string docPath = debugInfo["documents"]![docId]!.AsString();
if (debugInfo["document-root"] != null)
docPath = Path.Combine(debugInfo["document-root"]!.AsString(), docPath);
if (!docPathToContent.ContainsKey(docPath))
docPathToContent.Add(docPath, File.ReadAllLines(docPath).ToArray());
if (startLine == endLine)
dumpnef += $"# Code {Path.GetFileName(docPath)} line {startLine}: \"{docPathToContent[docPath][startLine - 1][(startCol - 1)..(endCol - 1)]}\"\n";
else
for (int lineIndex = startLine; lineIndex <= endLine; lineIndex++)
{
string src;
if (lineIndex == startLine)
src = docPathToContent[docPath][lineIndex - 1][(startCol - 1)..].Trim();
else if (lineIndex == endLine)
src = docPathToContent[docPath][lineIndex - 1][..(endCol - 1)].Trim();
else
src = docPathToContent[docPath][lineIndex - 1].Trim();
dumpnef += $"# Code {Path.GetFileName(docPath)} line {startLine}: \"{src}\"\n";
}
}
}
if (a < script.Length)
dumpnef += $"{WriteInstruction(a, script.GetInstruction(a), script.GetInstructionAddressPadding(), nef.Tokens)}\n";
}
return dumpnef;
}
}
}