neo-decompiler 0.10.1

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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
use super::*;

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

#[test]
fn call_graph_includes_direct_calla_targets_as_methods() {
    let nef_bytes = build_nef(&[
        OpCode::PushA.byte(),
        0x07,
        0x00,
        0x00,
        0x00,                 // PUSHA +7 -> target at 0x0007
        OpCode::CallA.byte(), // CALLA
        OpCode::Ret.byte(),   // RET
        OpCode::Syscall.byte(),
        0xB7,
        0xC3,
        0x88,
        0x03,               // SYSCALL System.Runtime.GetTime
        OpCode::Ret.byte(), // RET
    ]);
    let decompilation = Decompiler::new()
        .decompile_bytes_with_manifest(&nef_bytes, None, OutputFormat::Pseudocode)
        .expect("decompile succeeds");

    assert!(
        decompilation
            .call_graph
            .methods
            .iter()
            .any(|method| method.offset == 0x0007),
        "resolved CALLA target must be listed as a method: {:?}",
        decompilation.call_graph.methods
    );

    let calla = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA")
        .expect("CALLA edge present");
    match &calla.target {
        CallTarget::Internal { method } => assert_eq!(method.offset, 0x0007),
        other => panic!("CALLA target should be internal, got {other:?}"),
    }

    let syscall = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "SYSCALL")
        .expect("SYSCALL edge present");
    assert_eq!(syscall.caller.offset, 0x0007);
}

#[test]
fn call_graph_attributes_helper_syscall_to_inferred_helper_method() {
    // Script layout:
    // 0x0000: CALL +4 (target=0x0004)
    // 0x0002: RET
    // 0x0003: NOP
    // 0x0004: SYSCALL System.Runtime.GetTime
    // 0x0009: RET
    let script = [
        OpCode::Call.byte(),
        0x04,               // CALL +4
        OpCode::Ret.byte(), // RET
        OpCode::Nop.byte(), // NOP
        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(), 2);
    let helper_edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "SYSCALL")
        .expect("helper syscall edge");
    assert_eq!(helper_edge.call_offset, 4);
    assert_eq!(helper_edge.caller.offset, 4);
    assert_eq!(helper_edge.caller.name, "sub_0x0004");
}

#[test]
fn call_graph_attributes_pusha_calla_helper_syscall_to_inferred_helper_method() {
    // Script layout:
    // 0x0000: PUSHA +8 (target=0x0008)
    // 0x0005: CALLA
    // 0x0006: RET
    // 0x0007: NOP
    // 0x0008: SYSCALL System.Runtime.GetTime
    // 0x000D: RET
    let script = [
        OpCode::PushA.byte(),
        0x08,
        0x00,
        0x00,
        0x00,                 // PUSHA +8
        OpCode::CallA.byte(), // CALLA
        OpCode::Ret.byte(),   // RET
        OpCode::Nop.byte(),   // NOP
        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");

    let helper_edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "SYSCALL")
        .expect("helper syscall edge");
    assert_eq!(helper_edge.call_offset, 8);
    assert_eq!(helper_edge.caller.offset, 8);
    assert_eq!(helper_edge.caller.name, "sub_0x0008");
}

#[test]
fn call_graph_attributes_ldarg_calla_helper_syscall_to_inferred_helper_method() {
    // Script layout:
    // 0x0000: PUSHA +15 (target=0x000F)
    // 0x0005: CALL +4 (target=0x0009)
    // 0x0007: RET
    // 0x0008: NOP
    // 0x0009: INITSLOT 0,1
    // 0x000C: LDARG0
    // 0x000D: CALLA
    // 0x000E: RET
    // 0x000F: SYSCALL System.Runtime.GetTime
    // 0x0014: RET
    let script = [
        OpCode::PushA.byte(),
        0x0F,
        0x00,
        0x00,
        0x00, // PUSHA +15
        OpCode::Call.byte(),
        0x04,               // CALL +4
        OpCode::Ret.byte(), // RET
        OpCode::Nop.byte(), // NOP
        OpCode::Initslot.byte(),
        0x00,
        0x01,                  // INITSLOT 0,1
        OpCode::Ldarg0.byte(), // LDARG0
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        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");

    let helper_edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "SYSCALL")
        .expect("helper syscall edge");
    assert_eq!(helper_edge.call_offset, 0x000F);
    assert_eq!(helper_edge.caller.offset, 0x000F);
    assert_eq!(helper_edge.caller.name, "sub_0x000F");
}

#[test]
fn call_graph_attributes_ldloc_from_argument_calla_helper_syscall_to_inferred_helper_method() {
    // Script layout:
    // 0x0000: PUSHA +17 (target=0x0011)
    // 0x0005: CALL +4 (target=0x0009)
    // 0x0007: RET
    // 0x0008: NOP
    // 0x0009: INITSLOT 1,1
    // 0x000C: LDARG0
    // 0x000D: STLOC0
    // 0x000E: LDLOC0
    // 0x000F: CALLA
    // 0x0010: RET
    // 0x0011: SYSCALL System.Runtime.GetTime
    // 0x0016: RET
    let script = [
        OpCode::PushA.byte(),
        0x11,
        0x00,
        0x00,
        0x00, // PUSHA +17
        OpCode::Call.byte(),
        0x04,               // CALL +4
        OpCode::Ret.byte(), // RET
        OpCode::Nop.byte(), // NOP
        OpCode::Initslot.byte(),
        0x01,
        0x01,                  // INITSLOT 1,1
        OpCode::Ldarg0.byte(), // LDARG0
        OpCode::Stloc0.byte(), // STLOC0
        OpCode::Ldloc0.byte(), // LDLOC0
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        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");

    let helper_edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "SYSCALL")
        .expect("helper syscall edge");
    assert_eq!(helper_edge.call_offset, 0x0011);
    assert_eq!(helper_edge.caller.offset, 0x0011);
    assert_eq!(helper_edge.caller.name, "sub_0x0011");
}

#[test]
fn call_graph_resolves_nested_pusha_argument_through_calla_helper() {
    // Script layout:
    // 0x0000: PUSHA +18 (target = 0x0012)  // helper2 pointer argument
    // 0x0005: PUSHA +7  (target = 0x000C)  // helper1 callee
    // 0x000A: CALLA
    // 0x000B: RET
    // 0x000C: INITSLOT 0,1
    // 0x000F: LDARG0
    // 0x0010: CALLA
    // 0x0011: RET
    // 0x0012: SYSCALL System.Runtime.GetTime
    // 0x0017: RET
    let script = [
        OpCode::PushA.byte(),
        0x12,
        0x00,
        0x00,
        0x00, // PUSHA +18
        OpCode::PushA.byte(),
        0x07,
        0x00,
        0x00,
        0x00,                 // PUSHA +7
        OpCode::CallA.byte(), // CALLA
        OpCode::Ret.byte(),   // RET
        OpCode::Initslot.byte(),
        0x00,
        0x01,                  // INITSLOT 0,1
        OpCode::Ldarg0.byte(), // LDARG0
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        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");

    let nested_calla = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA" && edge.call_offset == 0x0010)
        .expect("nested CALLA edge present");
    match &nested_calla.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 0x0012);
            assert_eq!(method.name, "sub_0x0012");
        }
        other => panic!("expected nested CALLA to resolve helper target, got: {other:?}"),
    }

    let helper_edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "SYSCALL")
        .expect("helper syscall edge");
    assert_eq!(helper_edge.call_offset, 0x0012);
    assert_eq!(helper_edge.caller.offset, 0x0012);
    assert_eq!(helper_edge.caller.name, "sub_0x0012");
}

#[test]
fn call_graph_resolves_nested_pusha_argument_through_calla_helper_without_initslot() {
    // Script layout:
    // 0x0000: PUSHA +11 (target = 0x000B) // helper2 pointer argument
    // 0x0005: CALL +3  (target = 0x0008) // helper1 callee, no INITSLOT
    // 0x0007: RET
    // 0x0008: LDARG0
    // 0x0009: CALLA
    // 0x000A: RET
    // 0x000B: SYSCALL System.Runtime.GetTime
    // 0x0010: RET
    let script = [
        OpCode::PushA.byte(),
        0x0B,
        0x00,
        0x00,
        0x00, // PUSHA +11
        OpCode::Call.byte(),
        0x03,                  // CALL +3
        OpCode::Ret.byte(),    // RET
        OpCode::Ldarg0.byte(), // LDARG0
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        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");

    let nested_calla = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA" && edge.call_offset == 0x0009)
        .expect("nested CALLA edge present");
    match &nested_calla.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 0x000B);
            assert_eq!(method.name, "sub_0x000B");
        }
        other => panic!("expected CALLA without INITSLOT helper to resolve target, got: {other:?}"),
    }
}

#[test]
fn call_graph_resolves_two_level_nested_calla_argument_chain() {
    // Script layout:
    // 0x0000: PUSHA +20 (target = 0x0014) // helper2 pointer
    // 0x0005: PUSHA +21 (target = 0x001A) // helper3 pointer
    // 0x000A: CALL +3  (target = 0x000D) // helper1
    // 0x000C: RET
    // 0x000D: INITSLOT 0,2
    // 0x0010: LDARG0   // helper3
    // 0x0011: LDARG1   // helper2
    // 0x0012: CALLA    // helper2(helper3)
    // 0x0013: RET
    // 0x0014: INITSLOT 0,1
    // 0x0017: LDARG0   // helper3
    // 0x0018: CALLA    // helper3()
    // 0x0019: RET
    // 0x001A: SYSCALL System.Runtime.GetTime
    // 0x001F: RET
    let script = [
        OpCode::PushA.byte(),
        0x14,
        0x00,
        0x00,
        0x00, // PUSHA +20
        OpCode::PushA.byte(),
        0x15,
        0x00,
        0x00,
        0x00, // PUSHA +21
        OpCode::Call.byte(),
        0x03,               // CALL +3
        OpCode::Ret.byte(), // RET
        OpCode::Initslot.byte(),
        0x00,
        0x02,                  // INITSLOT 0,2
        OpCode::Ldarg0.byte(), // LDARG0
        OpCode::Ldarg1.byte(), // LDARG1
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        OpCode::Initslot.byte(),
        0x00,
        0x01,                  // INITSLOT 0,1
        OpCode::Ldarg0.byte(), // LDARG0
        OpCode::CallA.byte(),  // CALLA
        OpCode::Ret.byte(),    // RET
        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");

    let nested_calla = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "CALLA" && edge.call_offset == 0x0018)
        .expect("second-level CALLA edge present");
    match &nested_calla.target {
        CallTarget::Internal { method } => {
            assert_eq!(method.offset, 0x001A);
            assert_eq!(method.name, "sub_0x001A");
        }
        other => panic!("expected second-level CALLA to resolve helper target, got: {other:?}"),
    }

    let helper_edge = decompilation
        .call_graph
        .edges
        .iter()
        .find(|edge| edge.opcode == "SYSCALL")
        .expect("helper syscall edge");
    assert_eq!(helper_edge.call_offset, 0x001A);
    assert_eq!(helper_edge.caller.offset, 0x001A);
    assert_eq!(helper_edge.caller.name, "sub_0x001A");
}

#[test]
fn inferred_method_starts_tolerate_malformed_tryl_operand() {
    use crate::instruction::{Instruction, OpCode, Operand};

    let instructions = vec![
        Instruction::new(
            0,
            OpCode::TryL,
            Some(Operand::Bytes(vec![0x01, 0x02, 0x03])),
        ),
        Instruction::new(1, OpCode::Ret, None),
    ];

    let inferred = crate::decompiler::helpers::inferred_method_starts(&instructions, None);
    assert_eq!(inferred, vec![0]);
}

#[test]
fn decompilation_resolves_pickitem_delegate_array_into_calla_edge() {
    // Script layout:
    // 0x0000: NEWARRAY0
    // 0x0001: STLOC0
    // 0x0002: LDLOC0
    // 0x0003: PUSHA +11 (target = 0x000E)
    // 0x0008: APPEND
    // 0x0009: LDLOC0
    // 0x000A: PUSH0
    // 0x000B: PICKITEM
    // 0x000C: CALLA
    // 0x000D: RET
    // 0x000E: INITSLOT 0,0
    // 0x0011: RET
    let script = [
        OpCode::Newarray0.byte(), // NEWARRAY0
        OpCode::Stloc0.byte(),    // STLOC0
        OpCode::Ldloc0.byte(),    // LDLOC0
        OpCode::PushA.byte(),
        0x0B,
        0x00,
        0x00,
        0x00,                    // PUSHA +11
        OpCode::Append.byte(),   // APPEND
        OpCode::Ldloc0.byte(),   // LDLOC0
        OpCode::Push0.byte(),    // PUSH0
        OpCode::Pickitem.byte(), // PICKITEM
        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, 0x000E);
            assert_eq!(method.name, "sub_0x000E");
        }
        other => {
            panic!("expected PICKITEM delegate CALLA to resolve helper target, got: {other:?}")
        }
    }
}

#[test]
fn decompilation_resolves_pickitem_delegate_array_through_local_alias() {
    // Script layout:
    // NEWARRAY0; STLOC0
    // LDLOC0; DUP; STLOC1
    // PUSHA +13 (target=0x0012, the INITSLOT helper); APPEND
    // LDLOC1; PUSH0; PICKITEM; STLOC2
    // LDLOC2; CALLA; RET
    // target: INITSLOT 0,0; RET
    let script = [
        OpCode::Newarray0.byte(), // NEWARRAY0
        OpCode::Stloc0.byte(),    // STLOC0
        OpCode::Ldloc0.byte(),    // LDLOC0
        OpCode::Dup.byte(),       // DUP
        OpCode::Stloc1.byte(),    // STLOC1
        OpCode::PushA.byte(),
        0x0D,
        0x00,
        0x00,
        0x00,                    // PUSHA +13 (target=0x0012, a valid in-range helper)
        OpCode::Append.byte(),   // APPEND
        OpCode::Ldloc1.byte(),   // LDLOC1
        OpCode::Push0.byte(),    // PUSH0
        OpCode::Pickitem.byte(), // PICKITEM
        OpCode::Stloc2.byte(),   // STLOC2
        OpCode::Ldloc2.byte(),   // LDLOC2
        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, 0x0012);
            assert_eq!(method.name, "sub_0x0012");
        }
        other => {
            panic!("expected aliased delegate-array CALLA to resolve helper target, got: {other:?}")
        }
    }
}

#[test]
fn decompilation_resolves_duplicated_pointer_into_calla_edge() {
    // Script layout:
    // 0x0000: PUSHA +8 (target = 0x0008)
    // 0x0005: DUP
    // 0x0006: CALLA
    // 0x0007: RET
    // 0x0008: INITSLOT 0,0
    // 0x000B: RET
    let script = [
        OpCode::PushA.byte(),
        0x08,
        0x00,
        0x00,
        0x00,                 // PUSHA +8
        OpCode::Dup.byte(),   // DUP
        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, 0x0008);
            assert_eq!(method.name, "sub_0x0008");
        }
        other => panic!("expected DUP-fed CALLA target to resolve, got: {other:?}"),
    }
}