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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
use super::*;

use crate::decompiler::analysis::call_graph::CallTarget;
use crate::instruction::OpCode;

#[test]
fn decompilation_includes_call_graph_syscalls() {
    // Script: SYSCALL System.Runtime.GetTime, RET
    let script = [
        OpCode::Syscall.byte(),
        0xB7,
        0xC3,
        0x88,
        0x03,               // SYSCALL 0x0388C3B7
        OpCode::Ret.byte(), // RET
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    assert_eq!(decompilation.call_graph.edges.len(), 1);
    let edge = &decompilation.call_graph.edges[0];
    assert_eq!(edge.opcode, "SYSCALL");
    match &edge.target {
        CallTarget::Syscall {
            hash,
            name,
            returns_value,
        } => {
            assert_eq!(*hash, 0x0388C3B7);
            assert_eq!(
                name.as_deref(),
                Some("System.Runtime.GetTime"),
                "expected syscall name to resolve"
            );
            assert!(*returns_value);
        }
        other => panic!("unexpected call target: {other:?}"),
    }
}

#[test]
fn decompilation_includes_call_graph_internal_calls() {
    // Script layout:
    // 0x0000: CALL +4 (target=0x0004)
    // 0x0002: RET
    // 0x0003: NOP
    // 0x0004: RET
    let script = [
        OpCode::Call.byte(),
        0x04,
        OpCode::Ret.byte(),
        OpCode::Nop.byte(),
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let manifest = ContractManifest::from_json_str(
        r#"
        {
            "name": "ExampleContract",
            "supportedstandards": [],
            "features": {},
            "abi": {
                "methods": [
                    {
                        "name": "main",
                        "parameters": [],
                        "returntype": "Void",
                        "offset": 0,
                        "safe": false
                    },
                    {
                        "name": "helper",
                        "parameters": [],
                        "returntype": "Void",
                        "offset": 4,
                        "safe": false
                    }
                ],
                "events": []
            },
            "permissions": [],
            "trusts": "*"
        }
        "#,
    )
    .expect("manifest parsed");

    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, Some(manifest), OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    assert_eq!(decompilation.call_graph.methods.len(), 2);
    assert_eq!(decompilation.call_graph.methods[0].offset, 0);
    assert_eq!(decompilation.call_graph.methods[0].name, "main");
    assert_eq!(decompilation.call_graph.methods[1].offset, 4);
    assert_eq!(decompilation.call_graph.methods[1].name, "helper");

    assert_eq!(decompilation.call_graph.edges.len(), 1);
    let edge = &decompilation.call_graph.edges[0];
    assert_eq!(edge.opcode, "CALL");
    assert_eq!(edge.call_offset, 0);
    assert_eq!(edge.caller.offset, 0);
    assert_eq!(edge.caller.name, "main");
    match &edge.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 4);
            assert_eq!(method.name, "helper");
        }
        other => panic!("unexpected call target: {other:?}"),
    }
}

#[test]
fn call_graph_resolves_relative_call_from_opcode_offset() {
    // Neo VM relative call offsets are resolved from opcode position.
    // 0x0000: CALL +2 (target=0x0002)
    // 0x0002: RET
    let script = [OpCode::Call.byte(), 0x02, OpCode::Ret.byte()];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    assert_eq!(decompilation.call_graph.edges.len(), 1);
    let edge = &decompilation.call_graph.edges[0];
    assert_eq!(edge.opcode, "CALL");
    assert_eq!(edge.call_offset, 0);
    match &edge.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 2);
            assert_eq!(method.name, "sub_0x0002");
        }
        other => panic!("unexpected call target: {other:?}"),
    }
}

#[test]
fn call_graph_out_of_range_call_target_is_unresolved() {
    // Regression (adversarial): a positive CALL target past the script end is
    // unresolvable and must report UnresolvedInternal — not fabricate a synthetic
    // method and a real-looking Internal edge. Mirrors the negative-target path.
    // 0x0000: CALL +127 (target=0x007F, far past the 3-byte script); 0x0002: RET
    let script = [OpCode::Call.byte(), 0x7F, OpCode::Ret.byte()];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    assert_eq!(decompilation.call_graph.edges.len(), 1);
    match &decompilation.call_graph.edges[0].target {
        CallTarget::UnresolvedInternal { target } => assert_eq!(*target, 127),
        other => panic!("out-of-range CALL target should be unresolved, got {other:?}"),
    }
    // No synthetic method fabricated for the bogus target.
    assert!(
        decompilation
            .call_graph
            .methods
            .iter()
            .all(|method| method.offset != 127),
        "out-of-range CALL must not fabricate a method: {:?}",
        decompilation.call_graph.methods
    );
}

#[test]
fn call_graph_out_of_range_calla_target_is_indirect() {
    // Regression (adversarial): a PUSHA+CALLA whose pointer lands past the
    // script end must report an Indirect edge and must NOT fabricate a phantom
    // method at the out-of-range offset (the MethodTable filters it too).
    // PUSHA +127 (target 0x7F, far past the 7-byte script); CALLA; RET
    let script = [
        OpCode::PushA.byte(),
        0x7F,
        0x00,
        0x00,
        0x00,
        OpCode::CallA.byte(),
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    let edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA")
        .expect("CALLA edge present");
    assert!(
        matches!(edge.target, CallTarget::Indirect { .. }),
        "out-of-range CALLA target should be Indirect, got {:?}",
        edge.target
    );
    assert!(
        decompilation
            .call_graph
            .methods
            .iter()
            .all(|method| method.offset != 127),
        "out-of-range CALLA must not fabricate a method: {:?}",
        decompilation.call_graph.methods
    );
}

#[test]
fn decompilation_includes_call_graph_method_tokens() {
    // Script: CALLT 0, RET.
    let script = [OpCode::CallT.byte(), 0x00, 0x00, OpCode::Ret.byte()];
    let hash: [u8; 20] = [
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
        0x0F, 0x10, 0x11, 0x12, 0x13,
    ];
    let nef_bytes = build_nef_with_single_token(&script, hash, "transfer", 2, true, 0x0F);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    assert_eq!(decompilation.call_graph.edges.len(), 1);
    let edge = &decompilation.call_graph.edges[0];
    assert_eq!(edge.opcode, "CALLT");
    match &edge.target {
        CallTarget::MethodToken {
            index,
            hash_le,
            hash_be,
            method,
            parameters_count,
            has_return_value,
            call_flags,
            call_flags_description,
        } => {
            assert_eq!(*index, 0);
            assert_eq!(method, "transfer");
            assert_eq!(*parameters_count, 2);
            assert!(*has_return_value);
            assert_eq!(*call_flags, 0x0F);
            assert_eq!(
                call_flags_description,
                "ReadStates|WriteStates|AllowCall|AllowNotify"
            );

            let expected_le = hex::encode_upper(hash);
            let mut reversed = hash;
            reversed.reverse();
            let expected_be = hex::encode_upper(reversed);
            assert_eq!(hash_le, &expected_le);
            assert_eq!(hash_be, &expected_be);
        }
        other => panic!("unexpected call target: {other:?}"),
    }
}

#[test]
fn decompilation_includes_indirect_calls() {
    // Script:
    // CALLA          (no operand — pops Pointer from stack)
    // CALLT 0x0001   (U16 token index)
    // RET
    let script = [
        OpCode::CallA.byte(),
        OpCode::CallT.byte(),
        0x01,
        0x00,
        OpCode::Ret.byte(),
    ];
    let hash = [0x42u8; 20];
    let nef_bytes = build_nef_with_single_token(&script, hash, "stub", 0, false, 0x0F);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    assert_eq!(decompilation.call_graph.edges.len(), 2);
    assert_eq!(decompilation.call_graph.edges[0].opcode, "CALLA");
    match &decompilation.call_graph.edges[0].target {
        CallTarget::Indirect { opcode, operand } => {
            assert_eq!(opcode, "CALLA");
            assert_eq!(*operand, None);
        }
        other => panic!("unexpected call target: {other:?}"),
    }

    assert_eq!(decompilation.call_graph.edges[1].opcode, "CALLT");
    match &decompilation.call_graph.edges[1].target {
        CallTarget::Indirect { opcode, operand } => {
            assert_eq!(opcode, "CALLT");
            assert_eq!(*operand, Some(1));
        }
        other => panic!("unexpected call target: {other:?}"),
    }
}

#[test]
fn decompilation_resolves_pusha_calla_to_internal_call_edge() {
    // Script layout:
    // 0x0000: PUSHA +10 (target = 0x000A)
    // 0x0005: CALLA
    // 0x0006: RET
    // 0x0007..0x0009: NOP padding
    // 0x000A: INITSLOT 0,0
    // 0x000D: RET
    let script = [
        OpCode::PushA.byte(),
        0x0A,
        0x00,
        0x00,
        0x00,                 // PUSHA +10
        OpCode::CallA.byte(), // CALLA
        OpCode::Ret.byte(),   // RET
        OpCode::Nop.byte(),
        OpCode::Nop.byte(),
        OpCode::Nop.byte(), // NOP x3
        OpCode::Initslot.byte(),
        0x00,
        0x00,               // INITSLOT 0,0
        OpCode::Ret.byte(), // RET
    ];

    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    let edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA")
        .expect("CALLA edge present");

    match &edge.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 0x000A);
            assert_eq!(method.name, "sub_0x000A");
        }
        other => panic!("expected resolved internal CALLA target, got: {other:?}"),
    }
}

#[test]
fn decompilation_resolves_local_pointer_flow_into_calla_edge() {
    // Script layout:
    // 0x0000: PUSHA +9  (target = 0x0009)
    // 0x0005: STLOC0
    // 0x0006: LDLOC0
    // 0x0007: CALLA
    // 0x0008: RET
    // 0x0009: INITSLOT 0,0
    // 0x000C: RET
    let script = [
        OpCode::PushA.byte(),
        0x09,
        0x00,
        0x00,
        0x00,                  // PUSHA +9
        OpCode::Stloc0.byte(), // STLOC0
        OpCode::Ldloc0.byte(), // LDLOC0
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        OpCode::Initslot.byte(),
        0x00,
        0x00,               // INITSLOT 0,0
        OpCode::Ret.byte(), // RET
    ];

    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    let edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA")
        .expect("CALLA edge present");

    match &edge.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 0x0009);
            assert_eq!(method.name, "sub_0x0009");
        }
        other => panic!("expected resolved local-flow CALLA target, got: {other:?}"),
    }
}

#[test]
fn decompilation_resolves_local_pointer_flow_with_nop_before_calla() {
    // Script layout:
    // 0x0000: PUSHA +10 (target = 0x000A)
    // 0x0005: STLOC0
    // 0x0006: LDLOC0
    // 0x0007: NOP
    // 0x0008: CALLA
    // 0x0009: RET
    // 0x000A: INITSLOT 0,0
    // 0x000D: RET
    let script = [
        OpCode::PushA.byte(),
        0x0A,
        0x00,
        0x00,
        0x00,                  // PUSHA +10
        OpCode::Stloc0.byte(), // STLOC0
        OpCode::Ldloc0.byte(), // LDLOC0
        OpCode::Nop.byte(),    // NOP
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        OpCode::Initslot.byte(),
        0x00,
        0x00,               // INITSLOT 0,0
        OpCode::Ret.byte(), // RET
    ];

    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    let edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA")
        .expect("CALLA edge present");

    match &edge.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 0x000A);
            assert_eq!(method.name, "sub_0x000A");
        }
        other => panic!("expected resolved local-flow CALLA target, got: {other:?}"),
    }
}

#[test]
fn decompilation_resolves_multi_hop_local_pointer_flow_into_calla_edge() {
    // Script layout:
    // 0x0000: PUSHA +12 (target = 0x000C)
    // 0x0005: STLOC0
    // 0x0006: LDLOC0
    // 0x0007: STLOC1
    // 0x0008: LDLOC1
    // 0x0009: CALLA
    // 0x000A: RET
    // 0x000B: NOP
    // 0x000C: INITSLOT 0,0
    // 0x000F: RET
    let script = [
        OpCode::PushA.byte(),
        0x0C,
        0x00,
        0x00,
        0x00,                  // PUSHA +12
        OpCode::Stloc0.byte(), // STLOC0
        OpCode::Ldloc0.byte(), // LDLOC0
        OpCode::Stloc1.byte(), // STLOC1
        OpCode::Ldloc1.byte(), // LDLOC1
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        OpCode::Nop.byte(),    // NOP
        OpCode::Initslot.byte(),
        0x00,
        0x00,               // INITSLOT 0,0
        OpCode::Ret.byte(), // RET
    ];

    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    let edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA")
        .expect("CALLA edge present");

    match &edge.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 0x000C);
            assert_eq!(method.name, "sub_0x000C");
        }
        other => panic!("expected resolved multi-hop local CALLA target, got: {other:?}"),
    }
}

#[test]
fn decompilation_does_not_resolve_local_pointer_across_method_boundary() {
    // Script layout:
    // 0x0000: INITSLOT 1,0
    // 0x0003: PUSHA +14 (target = 0x0011)
    // 0x0008: STLOC0
    // 0x0009: RET
    // 0x000A: INITSLOT 1,0
    // 0x000D: LDLOC0
    // 0x000E: CALLA
    // 0x000F: RET
    // 0x0010: NOP
    // 0x0011: INITSLOT 0,0
    // 0x0014: RET
    let script = [
        OpCode::Initslot.byte(),
        0x01,
        0x00, // INITSLOT 1,0
        OpCode::PushA.byte(),
        0x0E,
        0x00,
        0x00,
        0x00,                  // PUSHA +14
        OpCode::Stloc0.byte(), // STLOC0
        OpCode::Ret.byte(),    // RET
        OpCode::Initslot.byte(),
        0x01,
        0x00,                  // INITSLOT 1,0
        OpCode::Ldloc0.byte(), // LDLOC0
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        OpCode::Nop.byte(),    // NOP
        OpCode::Initslot.byte(),
        0x00,
        0x00,               // INITSLOT 0,0
        OpCode::Ret.byte(), // RET
    ];

    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    let edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA")
        .expect("CALLA edge present");

    match &edge.target {
        CallTarget::Indirect { opcode, operand } => {
            assert_eq!(opcode, "CALLA");
            assert_eq!(*operand, None);
        }
        other => panic!("expected cross-method local CALLA to remain indirect, got: {other:?}"),
    }
}