libmwemu 0.25.4

x86 32/64bits and system internals emulator, for securely emulating malware and other stuff.
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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
use crate::config::Config;
use crate::emu::Emu;
use crate::emu::decoded_instruction::DecodedInstruction;
use crate::maps::mem64::Permission;
use crate::{tests::helpers, *};
use std::cell::RefCell;
use std::rc::Rc;

#[test]
pub fn test_unified_step_and_run_methods() {
    helpers::setup();

    // Test 1: Single-threaded mode (default)
    let mut emu = emu64();
    assert_eq!(
        emu.is_threading_enabled(),
        false,
        "Threading should be disabled by default"
    );

    // Load some simple code - NOP instructions
    let code = vec![0x90, 0x90, 0x90]; // 3 NOP instructions
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x1000, &code);
    emu.regs_mut().rip = 0x1000;

    // Test step() in single-threaded mode
    let result = emu.step();
    assert!(result, "Step should succeed in single-threaded mode");
    assert_eq!(emu.regs().rip, 0x1001, "RIP should advance after NOP");

    // Test 2: Enable threading and verify it's set
    emu.enable_threading(true);
    assert_eq!(
        emu.is_threading_enabled(),
        true,
        "Threading should be enabled"
    );

    // Step again with threading enabled (but still only 1 thread)
    let result = emu.step();
    assert!(result, "Step should succeed with threading enabled");
    assert_eq!(
        emu.regs().rip,
        0x1002,
        "RIP should advance after second NOP"
    );

    // Test 3: Verify run() method works
    let mut emu2 = emu32();
    emu2.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    let code32 = vec![0x90, 0x90, 0xC3]; // 2 NOPs and RET
    emu2.maps.write_bytes(0x1000, &code32);
    emu2.regs_mut().set_eip(0x1000);

    // Create a minimal stack for the RET instruction
    emu2.maps
        .create_map("stack", 0x2000, 0x1000, Permission::READ_WRITE);
    emu2.regs_mut().set_esp(0x2500);
    emu2.maps.write_dword(0x2500, 0x3000); // Return address

    // Run until RET
    let result = emu2.run(Some(0x1002));
    assert!(result.is_ok(), "Run should succeed");

    // Test 4: Verify threading can be toggled
    let mut cfg = Config::new();
    cfg.enable_threading = false;
    assert_eq!(cfg.enable_threading, false);
    cfg.enable_threading = true;
    assert_eq!(cfg.enable_threading, true);
}

#[test]
pub fn test_run_until_ret_32_ret_imm_updates_ip_and_stack() {
    helpers::setup();

    let mut emu = emu32();
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps
        .create_map("stack", 0x2000, 0x1000, Permission::READ_WRITE);
    emu.maps
        .create_map("target", 0x3000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps
        .write_bytes(0x1000, &[0xc2, 0x0c, 0x00, 0xb8, 0xef, 0xbe, 0xad, 0xde]);
    emu.maps.write_bytes(0x3000, &[0x90]);
    emu.regs_mut().set_eip(0x1000);
    emu.regs_mut().set_esp(0x2500);
    emu.maps.write_dword(0x2500, 0x3000);
    emu.maps.write_dword(0x2504, 0x1111_1111);
    emu.maps.write_dword(0x2508, 0x2222_2222);
    emu.maps.write_dword(0x250c, 0x3333_3333);

    let result = emu.run_until_ret();

    assert_eq!(result.unwrap(), 0x3000);
    assert_eq!(emu.regs().get_eip(), 0x3000);
    assert_eq!(emu.regs().get_esp(), 0x2510);
    assert_ne!(emu.regs().get_eax(), 0xdead_beef);
    assert!(!emu.run_until_ret);
}

#[test]
pub fn test_run_until_ret_64_ret_imm_updates_ip_and_stack() {
    helpers::setup();

    let mut emu = emu64();
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps
        .create_map("stack", 0x4000, 0x1000, Permission::READ_WRITE);
    emu.maps
        .create_map("target", 0x6000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps
        .write_bytes(0x1000, &[0xc2, 0x10, 0x00, 0xb8, 0xef, 0xbe, 0xad, 0xde]);
    emu.maps.write_bytes(0x6000, &[0x90]);
    emu.regs_mut().rip = 0x1000;
    emu.regs_mut().rsp = 0x4500;
    emu.maps.write_qword(0x4500, 0x6000);
    emu.maps.write_qword(0x4508, 0x1111_1111_1111_1111);
    emu.maps.write_qword(0x4510, 0x2222_2222_2222_2222);

    let result = emu.run_until_ret();

    assert_eq!(result.unwrap(), 0x6000);
    assert_eq!(emu.regs().rip, 0x6000);
    assert_eq!(emu.regs().rsp, 0x4518);
    assert_ne!(emu.regs().rax, 0xdead_beef);
    assert!(!emu.run_until_ret);
}

#[test]
pub fn test_run_until_ret_multithreaded_ret_imm_updates_ip_and_stack() {
    helpers::setup();

    let mut emu = emu32();
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps
        .create_map("stack", 0x2000, 0x1000, Permission::READ_WRITE);
    emu.maps
        .create_map("target", 0x3000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x1000, &[0xc2, 0x0c, 0x00]);
    emu.maps.write_bytes(0x3000, &[0x90]);
    emu.regs_mut().set_eip(0x1000);
    emu.regs_mut().set_esp(0x2500);
    emu.maps.write_dword(0x2500, 0x3000);

    let mut second_thread = crate::threading::context::ThreadContext::new(0x1001, emu.cfg.arch);
    second_thread.suspended = true;
    emu.threads.push(second_thread);
    emu.enable_threading(true);

    let result = emu.run_until_ret();

    assert_eq!(result.unwrap(), 0x3000);
    assert_eq!(emu.regs().get_eip(), 0x3000);
    assert_eq!(emu.regs().get_esp(), 0x2510);
    assert!(!emu.run_until_ret);
}

#[test]
pub fn test_step_32_ret_imm_updates_ip_and_stack() {
    helpers::setup();

    let mut emu = emu32();
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps
        .create_map("stack", 0x2000, 0x1000, Permission::READ_WRITE);
    emu.maps
        .create_map("target", 0x3000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x1000, &[0xc2, 0x0c, 0x00]);
    emu.maps.write_bytes(0x3000, &[0x90]);
    emu.regs_mut().set_eip(0x1000);
    emu.regs_mut().set_esp(0x2500);
    emu.maps.write_dword(0x2500, 0x3000);

    assert!(emu.step());
    assert_eq!(emu.regs().get_eip(), 0x3000);
    assert_eq!(emu.regs().get_esp(), 0x2510);
    assert!(!emu.force_reload);
}

#[test]
pub fn test_run_no_observer_leaves_last_decoded_empty() {
    helpers::setup();

    let mut emu = emu64();
    let code_base = 0x1000;
    let code = vec![0x90, 0x90, 0x90];
    emu.maps
        .create_map("code", code_base, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(code_base, &code);
    emu.regs_mut().rip = code_base;
    emu.cfg.verbose = 0;
    emu.cfg.trace_regs = false;
    emu.cfg.trace_filename = None;

    let result = emu.run(Some(code_base + code.len() as u64));

    assert!(result.is_ok(), "Run should succeed without observers");
    assert_eq!(emu.regs().rip, code_base + code.len() as u64);
    assert_eq!(emu.last_decoded_addr, 0);
    assert!(emu.last_decoded.is_none());
}

#[test]
pub fn test_run_hooks_receive_fresh_decoded_instruction() {
    helpers::setup();

    let mut emu = emu64();
    let code_base = 0x2000;
    let code = vec![0x90, 0x90, 0x90];
    emu.maps
        .create_map("code", code_base, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(code_base, &code);
    emu.regs_mut().rip = code_base;
    emu.cfg.verbose = 0;

    let pre_addresses = Rc::new(RefCell::new(Vec::new()));
    let post_addresses = Rc::new(RefCell::new(Vec::new()));
    let pre_capture = Rc::clone(&pre_addresses);
    let post_capture = Rc::clone(&post_addresses);

    emu.hooks.on_pre_instruction(
        move |_emu: &mut Emu, _ip: u64, ins: &DecodedInstruction, _sz: usize| -> bool {
            pre_capture.borrow_mut().push(ins.as_x86().ip());
            true
        },
    );
    emu.hooks.on_post_instruction(
        move |_emu: &mut Emu, _ip: u64, ins: &DecodedInstruction, _sz: usize, _ok: bool| {
            post_capture.borrow_mut().push(ins.as_x86().ip());
        },
    );

    let result = emu.run(Some(code_base + code.len() as u64));

    assert!(result.is_ok(), "Run should succeed with hooks installed");
    assert_eq!(
        *pre_addresses.borrow(),
        vec![code_base, code_base + 1, code_base + 2]
    );
    assert_eq!(
        *post_addresses.borrow(),
        vec![code_base, code_base + 1, code_base + 2]
    );
    assert_eq!(emu.last_decoded_addr, code_base + 2);
    assert_eq!(emu.last_decoded.unwrap().as_x86().ip(), code_base + 2);
}

#[test]
pub fn test_run_trace_file_preserves_disassembly() {
    helpers::setup();

    let mut emu = emu64();
    let code_base = 0x3000;
    let code = vec![0x90, 0x90];
    emu.maps
        .create_map("code", code_base, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(code_base, &code);
    emu.regs_mut().rip = code_base;
    emu.cfg.verbose = 0;
    emu.cfg.trace_regs = true;
    emu.cfg.trace_start = 0;

    let trace_path = std::env::temp_dir().join(format!(
        "libmwemu-lazy-decoded-trace-{}-{}.csv",
        std::process::id(),
        code_base
    ));
    let _ = std::fs::remove_file(&trace_path);
    emu.cfg.trace_filename = Some(trace_path.to_string_lossy().into_owned());
    emu.open_trace_file();

    let result = emu.run(Some(code_base + code.len() as u64));
    drop(emu);

    assert!(result.is_ok(), "Run should succeed with trace file enabled");
    let trace = std::fs::read_to_string(&trace_path).expect("trace file should be readable");
    let _ = std::fs::remove_file(&trace_path);
    let rows: Vec<&str> = trace.lines().collect();
    assert!(
        rows.len() > 1,
        "trace file should contain instruction rows: {trace}"
    );
    assert!(
        rows.iter().skip(1).all(|row| !row.contains("???")),
        "trace rows should contain disassembly: {trace}"
    );
    assert!(
        rows.iter()
            .skip(1)
            .any(|row| row.to_ascii_lowercase().contains("nop")),
        "trace should include NOP disassembly: {trace}"
    );
}

// ---------------------------------------------------------------------------
// ISA-specific API tests
// ---------------------------------------------------------------------------
//
// The following tests exercise the new `step_x86` / `step_aarch64` /
// `run_x86` / `run_aarch64` public APIs and the mis-architecture guard
// contract. They also confirm that the threaded scheduler dispatches
// through the new ISA-specific paths.

#[test]
pub fn test_step_x86_advances_rip() {
    helpers::setup();

    let mut emu = emu64();
    let code = vec![0x90, 0x90, 0x90]; // 3 NOP instructions
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x1000, &code);
    emu.regs_mut().rip = 0x1000;

    assert!(emu.step_x86());
    assert_eq!(emu.regs().rip, 0x1001);
    assert!(emu.step_x86());
    assert_eq!(emu.regs().rip, 0x1002);
    assert!(emu.step_x86());
    assert_eq!(emu.regs().rip, 0x1003);
}

#[test]
pub fn test_run_x86_executes_until_end_addr() {
    helpers::setup();

    let mut emu = emu64();
    let code = vec![0x90, 0x90, 0x90, 0x90];
    emu.maps
        .create_map("code", 0x2000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x2000, &code);
    emu.regs_mut().rip = 0x2000;

    let result = emu.run_x86(Some(0x2004));
    assert!(result.is_ok(), "run_x86 should succeed: {result:?}");
    assert_eq!(emu.regs().rip, 0x2004);
}

#[test]
pub fn test_step_aarch64_advances_pc() {
    helpers::setup();

    // mov x0, #1; mov x1, #1; add x2, x0, x1
    let code: [u8; 12] = [
        0x20, 0x00, 0x80, 0xd2, 0x21, 0x00, 0x80, 0xd2, 0x02, 0x00, 0x01, 0x8b,
    ];

    let mut emu = emu_aarch64();
    emu.load_code_bytes(&code);

    let base = emu.regs_aarch64().pc;
    assert!(emu.step_aarch64());
    assert_eq!(emu.regs_aarch64().pc, base + 4);
    assert_eq!(emu.regs_aarch64().x[0], 1);

    assert!(emu.step_aarch64());
    assert_eq!(emu.regs_aarch64().pc, base + 8);
    assert_eq!(emu.regs_aarch64().x[1], 1);

    assert!(emu.step_aarch64());
    assert_eq!(emu.regs_aarch64().pc, base + 12);
    assert_eq!(emu.regs_aarch64().x[2], 2);
}

#[test]
pub fn test_run_aarch64_executes_until_end_addr() {
    helpers::setup();

    let code: [u8; 16] = [
        0x20, 0x00, 0x80, 0xd2, // mov x0, #1
        0x21, 0x00, 0x80, 0xd2, // mov x1, #1
        0x02, 0x00, 0x01, 0x8b, // add x2, x0, x1
        0xd5, 0x03, 0x20, 0x1f, // nop
    ];

    let mut emu = emu_aarch64();
    emu.load_code_bytes(&code);
    let base = emu.regs_aarch64().pc;

    let result = emu.run_aarch64(Some(base + 12));
    assert!(result.is_ok(), "run_aarch64 should succeed: {result:?}");
    assert_eq!(emu.regs_aarch64().pc, base + 12);
    assert_eq!(emu.regs_aarch64().x[2], 2);
}

#[test]
#[should_panic(
    expected = "step_x86 called on non-x86 emulator (arch=Aarch64); use the AArch64 API instead"
)]
pub fn test_step_x86_panics_on_aarch64() {
    helpers::setup();

    let mut emu = emu_aarch64();
    let _ = emu.step_x86();
}

#[test]
#[should_panic(
    expected = "run_x86 called on non-x86 emulator (arch=Aarch64); use the AArch64 API instead"
)]
pub fn test_run_x86_panics_on_aarch64() {
    helpers::setup();

    let mut emu = emu_aarch64();
    let _ = emu.run_x86(None);
}

#[test]
#[should_panic(
    expected = "step_aarch64 called on non-AArch64 emulator (arch=X86_64); use the x86 API instead"
)]
pub fn test_step_aarch64_panics_on_x86_64() {
    helpers::setup();

    let mut emu = emu64();
    let _ = emu.step_aarch64();
}

#[test]
#[should_panic(
    expected = "run_aarch64 called on non-AArch64 emulator (arch=X86_64); use the x86 API instead"
)]
pub fn test_run_aarch64_panics_on_x86_64() {
    helpers::setup();

    let mut emu = emu64();
    let _ = emu.run_aarch64(None);
}

#[test]
#[should_panic(
    expected = "decode_and_execute_x86 called on non-x86 emulator (arch=Aarch64); use the AArch64 API instead"
)]
pub fn test_decode_and_execute_x86_panics_on_aarch64() {
    helpers::setup();

    let mut emu = emu_aarch64();
    let _ = emu.decode_and_execute_x86();
}

#[test]
#[should_panic(
    expected = "advance_pc_aarch64 called on non-AArch64 emulator (arch=X86_64); use the x86 API instead"
)]
pub fn test_advance_pc_aarch64_panics_on_x86_64() {
    helpers::setup();

    let mut emu = emu64();
    emu.advance_pc_aarch64(4);
}

/// AArch64 multi-threaded smoke test: the prior AArch64 threading path
/// called `self.regs().rip` which panicked on AArch64 emulators. The
/// ISA-specific `run_multi_threaded_aarch64` must execute two threads
/// without touching x86-only register accessors.
#[test]
pub fn test_aarch64_multithreaded_runs_two_threads() {
    helpers::setup();

    // mov x0, #1; mov x1, #1; add x2, x0, x1; ret
    let code: [u8; 16] = [
        0x20, 0x00, 0x80, 0xd2, 0x21, 0x00, 0x80, 0xd2, 0x02, 0x00, 0x01, 0x8b, 0xc0, 0x03, 0x5f,
        0xd6,
    ];

    let mut emu = emu_aarch64();
    emu.load_code_bytes(&code);

    // Suspended second AArch64 thread parked at the same code base.
    let mut second = crate::threading::context::ThreadContext::new(0x1001, emu.cfg.arch);
    second.suspended = true;
    emu.threads.push(second);
    emu.enable_threading(true);

    let result = emu.run_until_ret();
    assert!(
        result.is_ok(),
        "run_until_ret with threading should succeed: {result:?}"
    );
    assert_eq!(emu.regs_aarch64().x[2], 2);
}

/// Threaded post-hook count assertion. The pre-refactor scheduler fired
/// the post hook twice for every multithreaded step (once in the decode
/// path and once in `ThreadScheduler::execute_thread_instruction`). After
/// the refactor each instruction delivered exactly one post hook.
#[test]
pub fn test_threaded_post_hook_fires_exactly_once_per_inst() {
    helpers::setup();

    let code: [u8; 12] = [
        0x20, 0x00, 0x80, 0xd2, // mov x0, #1
        0x21, 0x00, 0x80, 0xd2, // mov x1, #1
        0x02, 0x00, 0x01, 0x8b, // add x2, x0, x1
    ];

    let mut emu = emu_aarch64();
    emu.load_code_bytes(&code);

    let count = Rc::new(RefCell::new(0u32));
    let capture = Rc::clone(&count);
    emu.hooks.on_post_instruction(
        move |_emu: &mut Emu, _pc: u64, _ins: &DecodedInstruction, _sz: usize, _ok: bool| {
            *capture.borrow_mut() += 1;
        },
    );

    let _ = emu.step_aarch64();
    let _ = emu.step_aarch64();
    let _ = emu.step_aarch64();

    assert_eq!(
        *count.borrow(),
        3,
        "exactly one post hook per instruction (3 expected, got {})",
        *count.borrow()
    );
}

#[test]
pub fn test_x86_multithreaded_post_hook_fires_exactly_once_per_inst() {
    helpers::setup();

    let mut emu = emu64();
    let code = vec![0x90, 0x90, 0x90];
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x1000, &code);
    emu.regs_mut().rip = 0x1000;

    let count = Rc::new(RefCell::new(0u32));
    let capture = Rc::clone(&count);
    emu.hooks.on_post_instruction(
        move |_emu: &mut Emu, _pc: u64, _ins: &DecodedInstruction, _sz: usize, _ok: bool| {
            *capture.borrow_mut() += 1;
        },
    );

    let _ = emu.step_x86();
    let _ = emu.step_x86();
    let _ = emu.step_x86();

    assert_eq!(
        *count.borrow(),
        3,
        "exactly one post hook per instruction (3 expected, got {})",
        *count.borrow()
    );
}

// ---------------------------------------------------------------------------
// Typed vs generic dispatch parity tests (split-ISA execution refactor)
// ---------------------------------------------------------------------------
//
// These tests confirm the public generic facade (`step` / `run` /
// `decode_and_execute` / `advance_pc`) and the typed ISA entry points
// (`step_x86` / `step_aarch64` / `decode_and_execute_x86` / etc.) reach
// the same final state on a tiny program. They guard against silent
// regressions where the generic dispatcher ends up calling the wrong ISA
// path after the refactor.

#[test]
pub fn test_decode_and_execute_x86_advances_rip_and_records_size() {
    helpers::setup();

    let mut emu = emu64();
    // mov eax, 1  (5 bytes: B8 01 00 00 00)
    let code = [0xB8, 0x01, 0x00, 0x00, 0x00];
    emu.maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x1000, &code);
    emu.regs_mut().rip = 0x1000;

    let (size, ok) = emu.decode_and_execute_x86();
    assert!(ok, "decode_and_execute_x86 should succeed");
    assert_eq!(size, 5, "mov eax, imm32 must be 5 bytes");
    assert_eq!(
        emu.regs().rax & 0xFFFF_FFFF,
        1,
        "mov eax, 1 should set the low 32 bits of rax to 1"
    );
    emu.advance_pc_x86(size);
    assert_eq!(
        emu.regs().rip,
        0x1005,
        "advance_pc_x86 must move RIP past the decoded instruction"
    );
}

#[test]
pub fn test_decode_and_execute_aarch64_advances_pc_and_records_size() {
    helpers::setup();

    let mut emu = emu_aarch64();
    // mov x0, #1  (MOVZ W0, #1: 20 00 80 D2)
    let code: [u8; 4] = [0x20, 0x00, 0x80, 0xD2];
    emu.maps
        .create_map("code", 0x2000, 0x1000, Permission::READ_WRITE_EXECUTE);
    emu.maps.write_bytes(0x2000, &code);
    emu.regs_aarch64_mut().pc = 0x2000;

    let (size, ok) = emu.decode_and_execute_aarch64();
    assert!(ok, "decode_and_execute_aarch64 should succeed");
    assert_eq!(size, 4, "AArch64 mov is fixed 4 bytes");
    assert_eq!(emu.regs_aarch64().x[0], 1, "mov x0, #1 must load 1 into x0");

    emu.advance_pc_aarch64(size);
    assert_eq!(
        emu.regs_aarch64().pc,
        0x2004,
        "advance_pc_aarch64 must move PC past the decoded instruction"
    );
}

#[test]
pub fn test_generic_dispatch_matches_typed_x86_path() {
    helpers::setup();

    // Build a tiny x64 program: mov rax, 0x2A; ret
    let code: Vec<u8> = vec![
        0x48, 0xC7, 0xC0, 0x2A, 0x00, 0x00, 0x00, // mov rax, 0x2A
        0xC3, // ret
    ];
    let stack_addr = 0x4000u64;
    let ret_addr = 0x9000u64;

    let mut typed_emu = emu64();
    typed_emu.os = crate::arch::OperatingSystem::Linux;
    typed_emu
        .maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    typed_emu.maps.write_bytes(0x1000, &code);
    typed_emu
        .maps
        .create_map("stack", stack_addr, 0x1000, Permission::READ_WRITE);
    typed_emu.regs_mut().rip = 0x1000;
    typed_emu.regs_mut().rsp = stack_addr + 0x800;
    let typed_rsp = typed_emu.regs_mut().rsp;
    typed_emu.maps.write_qword(typed_rsp, ret_addr);
    let typed_result = typed_emu.run_x86(Some(ret_addr));
    let typed_rax = typed_emu.regs().rax;
    let typed_rip = typed_emu.regs().rip;
    assert!(
        typed_result.is_ok(),
        "typed run_x86 should succeed: {typed_result:?}"
    );
    assert_eq!(typed_rax, 0x2A, "typed run must compute 0x2A in rax");

    let mut generic_emu = emu64();
    generic_emu.os = crate::arch::OperatingSystem::Linux;
    generic_emu
        .maps
        .create_map("code", 0x1000, 0x1000, Permission::READ_WRITE_EXECUTE);
    generic_emu.maps.write_bytes(0x1000, &code);
    generic_emu
        .maps
        .create_map("stack", stack_addr, 0x1000, Permission::READ_WRITE);
    generic_emu.regs_mut().rip = 0x1000;
    generic_emu.regs_mut().rsp = stack_addr + 0x800;
    let generic_rsp = generic_emu.regs_mut().rsp;
    generic_emu.maps.write_qword(generic_rsp, ret_addr);
    let generic_result = generic_emu.run(Some(ret_addr));
    let generic_rax = generic_emu.regs().rax;
    let generic_rip = generic_emu.regs().rip;
    assert!(
        generic_result.is_ok(),
        "generic run should succeed: {generic_result:?}"
    );

    assert_eq!(
        typed_rax, generic_rax,
        "generic and typed x86 run must reach the same rax"
    );
    assert_eq!(
        typed_rip, generic_rip,
        "generic and typed x86 run must reach the same rip"
    );
}

#[test]
pub fn test_generic_dispatch_matches_typed_aarch64_path() {
    helpers::setup();

    // mov x0, #1; mov x1, #2; add x2, x0, x1
    let code: [u8; 16] = [
        0x20, 0x00, 0x80, 0xD2, // mov x0, #1
        0x41, 0x00, 0x80, 0xD2, // mov x1, #2
        0x02, 0x00, 0x01, 0x8B, // add x2, x0, x1
        0xd5, 0x03, 0x20, 0x1f, // nop
    ];

    let mut typed_emu = emu_aarch64();
    typed_emu.load_code_bytes(&code);
    let typed_result = typed_emu.run_aarch64(Some(typed_emu.regs_aarch64().pc + 12));
    let typed_x2 = typed_emu.regs_aarch64().x[2];
    let typed_pc = typed_emu.regs_aarch64().pc;
    assert!(
        typed_result.is_ok(),
        "typed run_aarch64 should succeed: {typed_result:?}"
    );
    assert_eq!(typed_x2, 3, "typed run must produce x2 = 1 + 2");

    let mut generic_emu = emu_aarch64();
    generic_emu.load_code_bytes(&code);
    let generic_result = generic_emu.run(Some(generic_emu.regs_aarch64().pc + 12));
    let generic_x2 = generic_emu.regs_aarch64().x[2];
    let generic_pc = generic_emu.regs_aarch64().pc;
    assert!(
        generic_result.is_ok(),
        "generic run should succeed: {generic_result:?}"
    );

    assert_eq!(
        typed_x2, generic_x2,
        "generic and typed aarch64 run must reach the same x2"
    );
    assert_eq!(
        typed_pc, generic_pc,
        "generic and typed aarch64 run must reach the same pc"
    );
}