neo-decompiler 0.11.0

Neo N3 NEF decompiler: parse, disassemble, lift bytecode to high-level pseudocode and C# skeletons, with a CLI, JSON reports, and optional WebAssembly bindings.
Documentation
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
use super::*;

#[test]
fn csharpize_statement_converts_known_forms() {
    assert_eq!(csharpize_statement("   "), "");
    assert_eq!(csharpize_statement("// note"), "// note");
    assert_eq!(csharpize_statement("let x = 1;"), "var x = 1;");
    // Helper rewrites must apply inside `let` initialisers too —
    // earlier the `let` branch early-returned before
    // `csharpize_expression` ran, so `let t0 = min(x, y);` came
    // out as `var t0 = min(x, y);` (uncompilable).
    assert_eq!(
        csharpize_statement("let t0 = min(x, y);"),
        "var t0 = BigInteger.Min(x, y);"
    );
    assert_eq!(
        csharpize_statement("let t0 = is_null(loc0);"),
        "var t0 = (loc0 is null);"
    );
    assert_eq!(csharpize_statement("let t0 = a cat b;"), "var t0 = a + b;");
    // Helper rewrites must also apply inside throw / abort / assert
    // operands. Same bug class as the `let` branch fix — these
    // branches were extracting their bodies but not running them
    // through the expression rewriter.
    // Non-string operands get wrapped in `$"{...}"` so the
    // implicit ToString call satisfies `Exception(string)`. Without
    // this, `new Exception(BigInteger.Min(a, b))` had no matching
    // constructor.
    assert_eq!(
        csharpize_statement("throw(min(a, b));"),
        "throw new Exception($\"{BigInteger.Min(a, b)}\");"
    );
    // String-concat results pass through unwrapped — any `"…"`
    // somewhere in the operand signals a string-typed result, so
    // wrapping in `$"{...}"` would be redundant noise. The
    // resulting `Exception("err" + code)` compiles because `+` on
    // a `string` left operand always produces a `string`.
    assert_eq!(
        csharpize_statement("abort(\"err\" cat code);"),
        "throw new Exception(\"err\" + code);"
    );
    assert_eq!(
        csharpize_statement("assert(is_null(loc0));"),
        "if (!((loc0 is null))) throw new Exception();"
    );
    // Assert message is also coerced to satisfy `Exception(string)`:
    // a string-concat result passes through unwrapped (it has a `"`
    // somewhere), while a non-string message gets the same
    // `$"{value}"` wrap as throw/abort.
    assert_eq!(
        csharpize_statement("assert(min(a, b) > 0, \"e\" cat code);"),
        "if (!(BigInteger.Min(a, b) > 0)) throw new Exception(\"e\" + code);"
    );
    assert_eq!(
        csharpize_statement("assert(x > 0, code);"),
        "if (!(x > 0)) throw new Exception($\"{code}\");"
    );
    assert_eq!(csharpize_statement("if t0 {"), "if (t0) {");
    assert_eq!(csharpize_statement("while t1 {"), "while (t1) {");
    assert_eq!(csharpize_statement("loop {"), "while (true) {");
    assert_eq!(
        csharpize_statement("else if loc0 < 3 {"),
        "else if (loc0 < 3) {"
    );
    assert_eq!(
        csharpize_statement("} else if loc0 == 1 {"),
        "} else if (loc0 == 1) {"
    );
    assert_eq!(
        csharpize_statement("for (let i = 0; i < 3; i++) {"),
        "for (var i = 0; i < 3; i++) {"
    );
    assert_eq!(
        csharpize_statement("leave label_0x0010;"),
        "goto label_0x0010;"
    );
    // CAT operator (high-level pseudocode) → C# `+`. The translation
    // only fires for ` cat ` tokens outside string literals.
    assert_eq!(
        csharpize_statement("return \"b:\" cat addr;"),
        "return \"b:\" + addr;"
    );
    assert_eq!(
        csharpize_statement("var x = a cat b cat c;"),
        "var x = a + b + c;"
    );
    assert_eq!(
        csharpize_statement("var msg = \"says cat ok\";"),
        "var msg = \"says cat ok\";"
    );
    // `throw(value);` (high-level pseudocode for NEO's THROW opcode)
    // becomes `throw new Exception(value);` in C# — NEO accepts any
    // stack value, but C# requires an `Exception`.
    assert_eq!(
        csharpize_statement("throw(\"oops\");"),
        "throw new Exception(\"oops\");"
    );
    // Non-string-literal identifier — wrapped to coerce via
    // ToString. The user can hand-strip `$"{...}"` if they know
    // `error_msg` is already a string.
    assert_eq!(
        csharpize_statement("throw(error_msg);"),
        "throw new Exception($\"{error_msg}\");"
    );
    // ABORT / ABORTMSG also map to `throw new Exception(...);` —
    // closest C# analogue to a NEO VM abort.
    assert_eq!(csharpize_statement("abort();"), "throw new Exception();");
    assert_eq!(
        csharpize_statement("abort(\"fatal\");"),
        "throw new Exception(\"fatal\");"
    );
    // Identifier operand — same wrapping rule as `throw(error_msg)`
    // since we don't know its static type.
    assert_eq!(
        csharpize_statement("abort(reason);"),
        "throw new Exception($\"{reason}\");"
    );
    // ASSERT / ASSERTMSG also need a compilable C# form. There is no
    // built-in `assert` keyword/function; the universal translation
    // is `if (!(cond)) throw new Exception(...);`.
    assert_eq!(
        csharpize_statement("assert(x > 0);"),
        "if (!(x > 0)) throw new Exception();"
    );
    assert_eq!(
        csharpize_statement("assert(x > 0, \"must be positive\");"),
        "if (!(x > 0)) throw new Exception(\"must be positive\");"
    );
    // Don't be fooled by commas inside the condition expression.
    assert_eq!(
        csharpize_statement("assert(foo(a, b));"),
        "if (!(foo(a, b))) throw new Exception();"
    );
    // NEO arithmetic helpers — the high-level lift emits `abs/min/max/pow`
    // as bare function calls, but C# has no `abs` etc. in scope. Rewrite
    // to `BigInteger.X(...)`. For `pow`, the second argument must be
    // `int` per `BigInteger.Pow`'s signature.
    assert_eq!(
        csharpize_statement("var x = abs(loc0);"),
        "var x = BigInteger.Abs(loc0);"
    );
    assert_eq!(
        csharpize_statement("var x = min(a, b);"),
        "var x = BigInteger.Min(a, b);"
    );
    assert_eq!(
        csharpize_statement("var x = max(a, b);"),
        "var x = BigInteger.Max(a, b);"
    );
    assert_eq!(
        csharpize_statement("var x = pow(base, exp);"),
        "var x = BigInteger.Pow(base, (int)(exp));"
    );
    // Literal exponent skips the redundant `(int)` cast — same idea
    // as `wrap_int_cast_unless_literal`. `pow(2, 8)` lifts cleanly to
    // `BigInteger.Pow(2, 8)` rather than `BigInteger.Pow(2, (int)(8))`.
    assert_eq!(
        csharpize_statement("var x = pow(2, 8);"),
        "var x = BigInteger.Pow(2, 8);"
    );
    assert_eq!(
        csharpize_statement("var x = left(buf, 4);"),
        "var x = Helper.Left(buf, 4);"
    );
    assert_eq!(
        csharpize_statement("var x = substr(buf, 0, 16);"),
        "var x = Helper.Substr(buf, 0, 16);"
    );
    // Identifier-boundary respect: `mypow(x)` is NOT `pow(x)`.
    assert_eq!(
        csharpize_statement("var x = mypow(2);"),
        "var x = mypow(2);"
    );
    // String-literal preservation: `"min(a)"` inside a string stays
    // verbatim.
    assert_eq!(
        csharpize_statement("var x = \"min(a, b)\";"),
        "var x = \"min(a, b)\";"
    );
    // Nested helpers compose: `max(abs(a), b)` → `BigInteger.Max(BigInteger.Abs(a), b)`.
    assert_eq!(
        csharpize_statement("var x = max(abs(a), b);"),
        "var x = BigInteger.Max(BigInteger.Abs(a), b);"
    );
    // Extended NEO arithmetic / buffer helpers — `BigInteger.X` for
    // ones .NET provides directly, `Helper.X` (Neo SmartContract
    // Framework) for the rest. Args at int-typed positions get an
    // `(int)(...)` cast so the C# overload signature matches.
    assert_eq!(
        csharpize_statement("var x = sign(loc0);"),
        "var x = Helper.Sign(loc0);"
    );
    assert_eq!(
        csharpize_statement("var x = sqrt(loc0);"),
        "var x = Helper.Sqrt(loc0);"
    );
    assert_eq!(
        csharpize_statement("var x = modmul(a, b, m);"),
        "var x = Helper.ModMul(a, b, m);"
    );
    assert_eq!(
        csharpize_statement("var x = modpow(b, e, m);"),
        "var x = BigInteger.ModPow(b, e, m);"
    );
    assert_eq!(
        csharpize_statement("var x = within(v, lo, hi);"),
        "var x = Helper.Within(v, lo, hi);"
    );
    assert_eq!(
        csharpize_statement("var x = left(buf, n);"),
        "var x = Helper.Left(buf, (int)(n));"
    );
    assert_eq!(
        csharpize_statement("var x = right(buf, n);"),
        "var x = Helper.Right(buf, (int)(n));"
    );
    assert_eq!(
        csharpize_statement("var x = substr(buf, start, len);"),
        "var x = Helper.Substr(buf, (int)(start), (int)(len));"
    );
    // `is_null(x)` is a unary check, not a function call — it lifts
    // to the idiomatic C# pattern `(x is null)` instead of trying to
    // resolve a (non-existent) `IsNull` helper on the framework.
    assert_eq!(
        csharpize_statement("if is_null(loc0) {"),
        "if ((loc0 is null)) {"
    );
    assert_eq!(
        csharpize_statement("var x = is_null(loc0);"),
        "var x = (loc0 is null);"
    );
    // Nested into another helper: `if (!is_null(x))` style usages.
    assert_eq!(
        csharpize_statement("var y = !is_null(loc0);"),
        "var y = !(loc0 is null);"
    );
    // Identifier-boundary respect: `assert_is_null(x)` must NOT pick
    // up the `is_null` rewrite (it's a different identifier).
    assert_eq!(
        csharpize_statement("var x = my_is_null(loc0);"),
        "var x = my_is_null(loc0);"
    );
    // Empty collection constructors lifted from NEWMAP / NEWARRAY0 /
    // NEWSTRUCT0 — the lift emits `Map()`, `[]`, `Struct()` which
    // don't compile as-is. Rewrite to explicit `new` forms with
    // best-effort type defaults (`object` for Map's generic args
    // since we don't have key/value type info; `object[0]` for the
    // bare-literal array case).
    assert_eq!(
        csharpize_statement("var t0 = Map();"),
        "var t0 = new Map<object, object>();"
    );
    assert_eq!(
        csharpize_statement("var t0 = [];"),
        "var t0 = new object[0];"
    );
    assert_eq!(
        csharpize_statement("var t0 = Struct();"),
        "var t0 = new Struct();"
    );
    // Identifier-boundary respect — a user-named `MyMap()` factory
    // must NOT be rewritten to `new MyMap<...>()`.
    assert_eq!(
        csharpize_statement("var t0 = MyMap();"),
        "var t0 = MyMap();"
    );
    // String-literal preservation — `"Map()"` inside a quoted
    // string stays verbatim.
    assert_eq!(
        csharpize_statement("var t0 = \"Map()\";"),
        "var t0 = \"Map()\";"
    );
    // Size-operand constructors lifted from NEWBUFFER / NEWARRAY
    // — `new_buffer(n)` and `new_array(n)` aren't valid C#
    // identifiers; rewrite to explicit `new byte[...]` /
    // `new object[...]`. The size operand needs a defensive
    // `(int)` cast for any expression that could carry BigInteger
    // semantics, but bare integer literals are unambiguously `int`
    // to the C# parser, so `wrap_int_cast_unless_literal` skips the
    // cast for them — yielding `new object[3]` instead of the noisier
    // `new object[(int)(3)]`. Variable / expression operands still
    // get the cast.
    assert_eq!(
        csharpize_statement("var t0 = new_buffer(8);"),
        "var t0 = new byte[8];"
    );
    assert_eq!(
        csharpize_statement("var t0 = new_buffer(loc0);"),
        "var t0 = new byte[(int)(loc0)];"
    );
    assert_eq!(
        csharpize_statement("var t0 = new_array(3);"),
        "var t0 = new object[3];"
    );
    // Negative literals also pass through without cast. Negative
    // sizes don't make sense for `new T[]` but the cast wouldn't
    // help anyway — `new T[-3]` and `new T[(int)(-3)]` are both
    // accepted by the C# compiler and reject at runtime alike.
    assert_eq!(
        csharpize_statement("var t0 = new_array(-3);"),
        "var t0 = new object[-3];"
    );
    // Identifier-boundary respect — `my_new_buffer(8)` is NOT the
    // NEWBUFFER lift output.
    assert_eq!(
        csharpize_statement("var t0 = my_new_buffer(8);"),
        "var t0 = my_new_buffer(8);"
    );
    // CONVERT lifts (`convert_to_bool` / `convert_to_integer` /
    // `convert_to_bytestring` / `convert_to_buffer`) — rewrite to
    // explicit C# casts.
    assert_eq!(
        csharpize_statement("var t0 = convert_to_bool(loc0);"),
        "var t0 = (bool)(loc0);"
    );
    assert_eq!(
        csharpize_statement("var t0 = convert_to_integer(loc0);"),
        "var t0 = (BigInteger)(loc0);"
    );
    assert_eq!(
        csharpize_statement("var t0 = convert_to_bytestring(loc0);"),
        "var t0 = (ByteString)(loc0);"
    );
    assert_eq!(
        csharpize_statement("var t0 = convert_to_buffer(loc0);"),
        "var t0 = (byte[])(loc0);"
    );
    // ISTYPE lifts — rewrite to C# pattern matches.
    assert_eq!(
        csharpize_statement("if is_type_bool(loc0) {"),
        "if ((loc0 is bool)) {"
    );
    assert_eq!(
        csharpize_statement("var t0 = is_type_integer(loc0);"),
        "var t0 = (loc0 is BigInteger);"
    );
    assert_eq!(
        csharpize_statement("var t0 = is_type_bytestring(loc0);"),
        "var t0 = (loc0 is ByteString);"
    );
    assert_eq!(
        csharpize_statement("var t0 = is_type_buffer(loc0);"),
        "var t0 = (loc0 is byte[]);"
    );
    // The other CONVERT / ISTYPE variants (any, pointer, array,
    // struct, map, interopinterface) deliberately keep the lifted
    // form — silently rewriting them would require type info the
    // lift doesn't supply. Leave a clear hint that the user has to
    // pick the right cast.
    assert_eq!(
        csharpize_statement("var t0 = convert_to_array(loc0);"),
        "var t0 = convert_to_array(loc0);"
    );
    assert_eq!(
        csharpize_statement("var t0 = is_type_map(loc0);"),
        "var t0 = is_type_map(loc0);"
    );
    // Collection helpers — `clear_items(c)`, `remove_item(c, k)`,
    // `keys(m)`, `values(m)`, `reverse_items(arr)` are NEO-flavoured
    // identifiers that don't compile. Rewrite to standard
    // .NET / Neo Map accessors.
    assert_eq!(csharpize_statement("clear_items(loc0);"), "loc0.Clear();");
    assert_eq!(
        csharpize_statement("remove_item(loc0, key);"),
        "loc0.Remove(key);"
    );
    assert_eq!(
        csharpize_statement("var t0 = keys(loc0);"),
        "var t0 = loc0.Keys;"
    );
    assert_eq!(
        csharpize_statement("var t0 = values(loc0);"),
        "var t0 = loc0.Values;"
    );
    assert_eq!(
        csharpize_statement("reverse_items(loc0);"),
        "loc0.Reverse();"
    );
    // Identifier-boundary respect — `my_keys(loc0)` is NOT KEYS.
    assert_eq!(
        csharpize_statement("var t0 = my_keys(loc0);"),
        "var t0 = my_keys(loc0);"
    );
    // APPEND lift — `append(arr, item)` → `arr.Add(item)`.
    assert_eq!(csharpize_statement("append(loc0, 42);"), "loc0.Add(42);");
    // HASKEY lift — `has_key(c, k)` → `c.ContainsKey(k)`.
    assert_eq!(
        csharpize_statement("var t0 = has_key(loc0, key);"),
        "var t0 = loc0.ContainsKey(key);"
    );
    assert_eq!(
        csharpize_statement("if has_key(loc0, key) {"),
        "if (loc0.ContainsKey(key)) {"
    );
}

#[test]
fn csharpize_statement_does_not_panic_on_degenerate_headers() {
    // Regression: empty-condition `if {` / `while {` headers used raw byte
    // slicing (`trimmed[3..len-2]`) which panicked with begin > end. They must
    // be handled without panicking.
    let _ = csharpize_statement("if {");
    let _ = csharpize_statement("while {");
    // Regression (adversarial): a multibyte UTF-8 character outside a string
    // literal (e.g. a method-token name) previously panicked in the helper
    // rewriter via `&line[i..]` landing mid-character. It must be preserved.
    assert_eq!(csharpize_statement("é(t0);"), "é(t0);");
    assert_eq!(
        csharpize_statement("let t0 = \"café\";"),
        "var t0 = \"café\";"
    );
    let _ = csharpize_statement("naïve(abs(x));");
    // Well-formed headers still convert.
    assert_eq!(csharpize_statement("if loc0 == 1 {"), "if (loc0 == 1) {");
    assert_eq!(
        csharpize_statement("while loc0 < 3 {"),
        "while (loc0 < 3) {"
    );
}

#[test]
fn csharpize_nested_helper_calls_in_cast_path_helpers() {
    // pow/left/right/substr take the int-cast argument path; nested NEO helper
    // calls in their arguments must still be rewritten to compilable C# rather
    // than emitted verbatim.
    assert_eq!(
        csharpize_statement("let t0 = pow(abs(x), 2);"),
        "var t0 = BigInteger.Pow(BigInteger.Abs(x), 2);"
    );
}

#[test]
fn csharpize_expression_preserves_multibyte_in_cat_path() {
    // Regression (adversarial recheck): rewrite_cat_operator runs first in
    // csharpize_expression and previously mangled multibyte UTF-8 (push b as char
    // re-encodes as Latin-1) when the line contained ` cat `.
    assert_eq!(
        csharpize_statement("let t0 = café cat x;"),
        "var t0 = café + x;"
    );
    assert_eq!(
        csharpize_statement("let t0 = \"naïve\" cat y;"),
        "var t0 = \"naïve\" + y;"
    );
}