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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use super::*;

use crate::instruction::OpCode;

#[test]
fn decompile_syscall_includes_human_name() {
    // Script: SYSCALL(System.Runtime.Platform) ; RET
    let script = [
        OpCode::Syscall.byte(),
        0xB2,
        0x79,
        0xFC,
        0xF6,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    assert!(decompilation
        .pseudocode
        .as_deref()
        .expect("pseudocode output")
        .contains("System.Runtime.Platform"));
    assert!(decompilation
        .high_level
        .as_deref()
        .expect("high-level output")
        .contains("System.Runtime.Platform()"));
}

#[test]
fn void_syscall_does_not_push_stack_value() {
    // Script: PUSH0 ; PUSH0 ; SYSCALL(System.Runtime.Notify) ; RET
    // System.Runtime.Notify takes 2 args (event_name, state)
    let script = [
        OpCode::Push0.byte(),
        OpCode::Push0.byte(),
        OpCode::Syscall.byte(),
        0x95,
        0x01,
        0x6F,
        0x61,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Runtime.Notify("),
        "void syscall should be emitted as a statement"
    );
    assert!(
        !high_level.contains("let t0 = System.Runtime.Notify()"),
        "void syscall should not push a temp onto the stack"
    );
}

#[test]
fn unknown_syscall_is_assumed_to_return_value() {
    let unknown_hash = 0xDEADBEEF;
    assert!(
        crate::syscalls::lookup(unknown_hash).is_none(),
        "fixture hash should not be present in the syscall catalog"
    );

    // Script: SYSCALL(unknown) ; RET
    let script = [
        OpCode::Syscall.byte(),
        0xEF,
        0xBE,
        0xAD,
        0xDE,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    // Unknown syscalls return a value to the simulated stack; the
    // single-use-temps pass collapses `let t0 = syscall(0xDEADBEEF);
    // return t0;` to `return syscall(0xDEADBEEF);` in clean mode.
    // Earlier this was blocked by a trailing `// unknown syscall`
    // annotation (which acted as an inliner barrier); the warning
    // now renders as a leading `// warning: unknown syscall 0xHASH`
    // line (iteration 97 unification), so the inliner can run.
    assert!(
        high.contains("syscall(0xDEADBEEF)"),
        "unknown syscalls should conservatively push a stack value: {high}"
    );
    assert!(
        high.contains("// warning: unknown syscall 0xDEADBEEF"),
        "unknown-syscall warning should be visible inline: {high}"
    );
}

#[test]
fn syscall_arguments_render_in_declaration_order() {
    // Script: NEWARRAY0 ; PUSHDATA1 "evt" ; SYSCALL(System.Runtime.Notify) ; RET
    // The devpack pushes syscall arguments right-to-left, so the state
    // array goes first and the event name ends on top: pop order equals
    // the declared signature Notify(eventName, state). A reversal bug
    // used to render this as syscall("System.Runtime.Notify", [], "evt").
    let script = [
        OpCode::Newarray0.byte(), // NEWARRAY0 (state)
        OpCode::Pushdata1.byte(),
        0x03,
        b'e',
        b'v',
        b't', // PUSHDATA1 "evt" (eventName, on top)
        OpCode::Syscall.byte(),
        0x95,
        0x01,
        0x6F,
        0x61,               // SYSCALL System.Runtime.Notify
        OpCode::Ret.byte(), // RET
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .with_inline_single_use_temps(true)
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Runtime.Notify(\"evt\","),
        "event name must render as the first Notify argument: {high_level}"
    );
}

#[test]
fn storage_put_arguments_render_in_pop_order() {
    // Script: PUSH1 ; PUSH2 ; PUSH3 ; SYSCALL(System.Storage.Put) ; RET
    // Pop order is 3, 2, 1 (top first) and equals declaration order; the
    // old code reversed it to 1, 2, 3.
    let script = [
        OpCode::Push1.byte(),
        OpCode::Push2.byte(),
        OpCode::Push3.byte(),
        OpCode::Syscall.byte(),
        0xE6,
        0x3F,
        0x18,
        0x84,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .with_inline_single_use_temps(true)
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Storage.Put(3, 2, 1)"),
        "Storage.Put arguments must render in pop order: {high_level}"
    );
}

#[test]
fn void_storage_syscall_is_emitted_as_statement() {
    // Script: PUSH0 ; PUSH0 ; PUSH0 ; SYSCALL(System.Storage.Put) ; RET
    // System.Storage.Put takes 3 args (context, key, value)
    let script = [
        OpCode::Push0.byte(),
        OpCode::Push0.byte(),
        OpCode::Push0.byte(),
        OpCode::Syscall.byte(),
        0xE6,
        0x3F,
        0x18,
        0x84,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Storage.Put("),
        "void storage syscall should be emitted as a statement"
    );
    assert!(
        !high_level.contains("let t0 = System.Storage.Put()"),
        "void storage syscall should not push a temp onto the stack"
    );
}

#[test]
fn void_storage_local_syscall_is_emitted_as_statement() {
    // Script: PUSH0 ; PUSH0 ; SYSCALL(System.Storage.Local.Put) ; RET
    // System.Storage.Local.Put takes 2 args (key, value)
    let script = [
        OpCode::Push0.byte(),
        OpCode::Push0.byte(),
        OpCode::Syscall.byte(),
        0x39,
        0x0C,
        0xE3,
        0x0A,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Storage.Local.Put("),
        "void storage local syscall should be emitted as a statement"
    );
    assert!(
        !high_level.contains("let t0 = System.Storage.Local.Put()"),
        "void storage local syscall should not push a temp onto the stack"
    );
}

#[test]
fn syscall_contract_call_expands_to_native_contract_form() {
    // Mirrors a real `GasToken.Transfer(from, to, amount)` call from the
    // devpack: push the args array, the call flags (15 = All), the method
    // string, and the contract hash, then SYSCALL System.Contract.Call.
    // The decompiler must lift this to `GasToken.Transfer(args)` so the
    // reader sees the contract and method, not the syscall wrapper.
    //
    // Stack layout (top → bottom) before SYSCALL:
    //   args_array, call_flags, "transfer", GasToken_hash
    let gas_token_hash: [u8; 20] = [
        0xCF, 0x76, 0xE2, 0x8B, 0xD0, 0x06, 0x2C, 0x4A, 0x47, 0x8E, 0xE3, 0x55, 0x61, 0x01, 0x13,
        0x19, 0xF3, 0xCF, 0xA4, 0xD2,
    ];
    let mut script = vec![
        // args array: empty PACK (any[] in devpack)
        OpCode::Newarray0.byte(),
        // call_flags = 15 (CallFlags.All)
        OpCode::Pushint8.byte(),
        0x0F,
        // method = "transfer"
        OpCode::Pushdata1.byte(),
        0x08,
        b't',
        b'r',
        b'a',
        b'n',
        b's',
        b'f',
        b'e',
        b'r',
        // contract hash = GasToken (20 bytes, PUSHDATA1)
        OpCode::Pushdata1.byte(),
        0x14,
    ];
    script.extend_from_slice(&gas_token_hash);
    script.extend_from_slice(&[
        OpCode::Syscall.byte(),
        0x62,
        0x7D,
        0x5B,
        0x52, // System.Contract.Call
        OpCode::Ret.byte(),
    ]);

    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("GasToken.transfer("),
        "native contract call should expand to `GasToken.transfer(args)`: {high_level}"
    );
    assert!(
        !high_level.contains("syscall(\"System.Contract.Call\""),
        "native contract call must not surface the raw syscall: {high_level}"
    );
    // The call returns a value (Transaction / hash), so it must be bound
    // to a temp on the evaluation stack.
    assert!(
        high_level.contains("= GasToken.transfer("),
        "native contract call returns a value, must push a temp: {high_level}"
    );
}

#[test]
fn syscall_contract_call_with_unknown_hash_uses_hex_form() {
    // Custom (non-native) contract: hash is 20 bytes but does not match
    // any bundled native contract. The decompiler should still produce
    // a readable call site (`0xHASH::method(args)`) instead of falling
    // back to the opaque syscall wrapper.
    let custom_hash: [u8; 20] = [
        0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
        0x00, 0x12, 0x34, 0x56, 0x78,
    ];
    let mut script = vec![
        OpCode::Newarray0.byte(),
        OpCode::Pushint8.byte(),
        0x0F,
        OpCode::Pushdata1.byte(),
        0x04,
        b't',
        b'e',
        b's',
        b't',
        OpCode::Pushdata1.byte(),
        0x14,
    ];
    script.extend_from_slice(&custom_hash);
    script.extend_from_slice(&[
        OpCode::Syscall.byte(),
        0x62,
        0x7D,
        0x5B,
        0x52,
        OpCode::Ret.byte(),
    ]);

    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("0x112233445566778899AABBCCDDEEFF0012345678.test("),
        "unknown-hash contract call must show the hex form so the reader can grep the manifest: {high_level}"
    );
    assert!(
        !high_level.contains("syscall(\"System.Contract.Call\""),
        "unknown-hash contract call must not fall back to the syscall wrapper: {high_level}"
    );
}

#[test]
fn syscall_contract_call_returns_value() {
    // Script: PUSH0 x4 ; SYSCALL(System.Contract.Call) hash=0x525B7D62 ; RET
    // The four PUSH0s populate the contract call arguments with literal
    // zeros (non-realistic — real bytecode uses a real hash and method).
    // Because no contract hash / method literal is tracked, the
    // expander falls back to the legacy `syscall("System.Contract.Call", …)`
    // form so the reader still sees the devpack's intended syscall instead
    // of a synthetic `0::0(0)` style call.
    let script = [
        OpCode::Push0.byte(),
        OpCode::Push0.byte(),
        OpCode::Push0.byte(),
        OpCode::Push0.byte(),
        OpCode::Syscall.byte(),
        0x62,
        0x7D,
        0x5B,
        0x52,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Contract.Call("),
        "untracked contract-call hash should fall back to the syscall form: {high_level}"
    );
    assert!(
        decompilation
            .warnings
            .iter()
            .any(|warning| warning.contains("contract-call hash was not a tracked 20-byte literal")),
        "structured warning should fire when the hash isn't a tracked 20-byte literal: {:?}",
        decompilation.warnings
    );
}

#[test]
fn syscall_runtime_log_is_void() {
    // Script: PUSH0 ; SYSCALL(System.Runtime.Log) hash=0x9647E7CF ; RET
    // System.Runtime.Log takes 1 arg (message)
    let script = [
        OpCode::Push0.byte(),
        OpCode::Syscall.byte(),
        0xCF,
        0xE7,
        0x47,
        0x96,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Runtime.Log("),
        "System.Runtime.Log is void and should be emitted as statement: {high_level}"
    );
    assert!(
        !high_level.contains("let t0 = System.Runtime.Log()"),
        "void syscall should not push a temp: {high_level}"
    );
}

#[test]
fn syscall_runtime_log_missing_argument_emits_warning() {
    // Script: SYSCALL(System.Runtime.Log) hash=0x9647E7CF ; RET
    // No message is pushed, so the decompiler must surface a warning.
    let script = [
        OpCode::Syscall.byte(),
        0xCF,
        0xE7,
        0x47,
        0x96,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Runtime.Log(???)"),
        "missing syscall argument should still be shown in output: {high_level}"
    );
    assert!(
        high_level.contains("missing syscall argument values for System.Runtime.Log"),
        "missing syscall argument should be called out inline in the output: {high_level}"
    );
    assert!(
        decompilation
            .warnings
            .iter()
            .any(|warning| warning.contains("missing syscall argument values")),
        "missing syscall argument should emit a structured warning: {:?}",
        decompilation.warnings
    );
}

#[test]
fn syscall_runtime_log_after_packed_store_reports_consumed_slot_context() {
    // Script:
    //   INITSLOT 1,0
    //   PUSHDATA1 "Hello"
    //   PUSH1
    //   PACK
    //   STLOC0
    //   SYSCALL(System.Runtime.Log)
    //   RET
    // The preceding store consumes the packed value, so the decompiler should
    // explain that the stack is empty because STLOC0 stored the last produced value.
    let script = [
        OpCode::Initslot.byte(),
        0x01,
        0x00, // INITSLOT 1,0
        OpCode::Pushdata1.byte(),
        0x05,
        b'H',
        b'e',
        b'l',
        b'l',
        b'o',                  // PUSHDATA1 "Hello"
        OpCode::Push1.byte(),  // PUSH1
        OpCode::Pack.byte(),   // PACK
        OpCode::Stloc0.byte(), // STLOC0
        OpCode::Syscall.byte(),
        0xCF,
        0xE7,
        0x47,
        0x96,               // SYSCALL(System.Runtime.Log)
        OpCode::Ret.byte(), // RET
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Runtime.Log(???)"),
        "missing syscall argument should still be rendered: {high_level}"
    );
    assert!(
        high_level.contains("preceding STLOC0 stored a packed value into loc0"),
        "packed-store context should be surfaced inline: {high_level}"
    );
    assert!(
        decompilation.warnings.iter().any(|warning| {
            warning.contains("preceding STLOC0 stored a packed value into loc0")
        }),
        "packed-store context should also be emitted as a structured warning: {:?}",
        decompilation.warnings
    );
}

#[test]
fn syscall_check_witness_returns_value() {
    // Script: PUSH0 ; SYSCALL(System.Runtime.CheckWitness) hash=0x8CEC27F8 ; RET
    // System.Runtime.CheckWitness takes 1 arg (hash_or_pubkey)
    let script = [
        OpCode::Push0.byte(),
        OpCode::Syscall.byte(),
        0xF8,
        0x27,
        0xEC,
        0x8C,
        OpCode::Ret.byte(),
    ];
    let nef_bytes = build_nef(&script);
    let decompilation = Decompiler::new()
        .decompile_bytes(&nef_bytes)
        .expect("decompile succeeds");

    let high_level = decompilation
        .high_level
        .as_deref()
        .expect("high-level output");
    assert!(
        high_level.contains("System.Runtime.CheckWitness("),
        "CheckWitness should resolve to human name: {high_level}"
    );
    assert!(
        high_level.contains("= System.Runtime.CheckWitness("),
        "CheckWitness returns a value: {high_level}"
    );
}