use crate::tests::helpers;
use crate::*;
use iced_x86::Mnemonic;
use std::cell::RefCell;
use std::rc::Rc;
const MSG_SYSCALL_STEP: u64 = 25_000;
const MSG_SYSCALL_SCAN_CAP: u64 = 1_000_000;
#[test]
#[ignore = "SSDT mode only initializes cleanly on Win2022, whose DLLs are no longer bundled and \
aren't on the symbol server (winbindex lacks Server 2022). Auto-fetch provides Win11, \
whose real LdrInitializeThunk still stalls in console init. Re-enable once Win11 LdrInit \
completes."]
fn exe64win_msgbox_ssdt_reaches_cli_trace_window() {
helpers::setup();
let mut emu = emu64();
emu.cfg.maps_folder = helpers::win64_maps_folder();
emu.cfg.emulate_winapi = true;
let sample = helpers::test_data_path("exe64win_msgbox.bin");
assert!(
std::path::Path::new(&sample).is_file(),
"missing test sample (run from mwemu repo): {}",
sample
);
emu.load_code(&sample);
let target = 120u64;
emu.run_to(target).unwrap_or_else(|e| {
panic!(
"run_to({}) failed: {} (pos={} rip=0x{:x})",
target,
e,
emu.pos,
emu.regs().rip
);
});
assert!(emu.pos >= target);
}
#[test]
#[ignore = "slow: run with --release -- --ignored; may need MSG_SYSCALL_SCAN_CAP raised"]
fn exe64win_msgbox_ssdt_hits_first_windows_syscall() {
helpers::setup();
let mut emu = emu64();
if !helpers::set_winver_maps(&mut emu, "win11") {
return;
}
emu.cfg.emulate_winapi = true;
let sample = helpers::test_data_path("exe64win_msgbox.bin");
assert!(
std::path::Path::new(&sample).is_file(),
"missing {}",
sample
);
let hit = Rc::new(RefCell::new(false));
let hit_flag = Rc::clone(&hit);
emu.hooks
.on_post_instruction(move |emu, _rip, ins, _sz, _ok| {
if emu.os.is_windows() && ins.is_x86() && ins.as_x86().mnemonic() == Mnemonic::Syscall {
*hit_flag.borrow_mut() = true;
}
});
emu.load_code(&sample);
let mut next_goal = MSG_SYSCALL_STEP;
while !*hit.borrow() && emu.pos < MSG_SYSCALL_SCAN_CAP {
let goal = next_goal.min(MSG_SYSCALL_SCAN_CAP);
if goal <= emu.pos {
break;
}
emu.run_to(goal).unwrap_or_else(|e| {
panic!(
"run_to({}) failed: {} (pos={} rip=0x{:x})",
goal,
e,
emu.pos,
emu.regs().rip
);
});
if *hit.borrow() {
break;
}
next_goal += MSG_SYSCALL_STEP;
}
assert!(
*hit.borrow(),
"no Windows `syscall` before pos {} (rip=0x{:x}). Raise MSG_SYSCALL_SCAN_CAP if the CRT path grew.",
MSG_SYSCALL_SCAN_CAP,
emu.regs().rip
);
}
#[test]
#[ignore = "SSDT mode needs the Win2022 build (not bundled, not on winbindex); Win11 LdrInit still \
stalls in console init. Re-enable once Win11 LdrInit completes."]
fn exe64win_enigma_ssdt_reaches_unpacker() {
helpers::setup();
let mut emu = emu64();
emu.cfg.maps_folder = helpers::win64_maps_folder();
emu.cfg.emulate_winapi = true;
let sample = helpers::test_data_path("exe64win_enigma.bin");
assert!(
std::path::Path::new(&sample).is_file(),
"missing test sample: {}",
sample
);
emu.load_code(&sample);
assert!(
emu.ldr_init_done,
"ntdll!LdrInitializeThunk did not complete under --ssdt (pos={} rip=0x{:x})",
emu.pos,
emu.regs().rip
);
let target = emu.pos + 500_000;
let _ = emu.run_to(target);
assert!(
emu.pos >= target,
"ssdt enigma stalled at {} (target {}); rip=0x{:x}",
emu.pos,
target,
emu.regs().rip
);
}
#[test]
#[ignore = "slow: ~3s in release; run with --release -- --ignored"]
fn exe64win_enigma_ssdt_runs_deep() {
helpers::setup();
let mut emu = emu64();
if !helpers::set_winver_maps(&mut emu, "win11") {
return;
}
emu.cfg.emulate_winapi = true;
let sample = helpers::test_data_path("exe64win_enigma.bin");
assert!(
std::path::Path::new(&sample).is_file(),
"missing test sample: {}",
sample
);
emu.load_code(&sample);
let target = 50_000_000u64;
let _ = emu.run_to(target);
assert!(
emu.pos >= target,
"ssdt enigma only reached {} instructions (need >= {}); rip=0x{:x}",
emu.pos,
target,
emu.regs().rip
);
}
#[test]
#[ignore = "SSDT mode needs the Win2022 build (not bundled, not on winbindex); Win11 LdrInit still \
stalls in console init. Re-enable once Win11 LdrInit completes."]
fn exe64win_mingw_ssdt_reaches_early_execution_window() {
helpers::setup();
let mut emu = emu64();
emu.cfg.maps_folder = helpers::win64_maps_folder();
emu.cfg.emulate_winapi = true;
let sample = helpers::test_data_path("exe64win_mingw.bin");
assert!(
std::path::Path::new(&sample).is_file(),
"missing {}",
sample
);
emu.load_code(&sample);
emu.run_to(120)
.expect("ssdt mingw should reach early execution window");
assert!(emu.pos >= 120);
}
#[test]
#[ignore = "slow: run with --release -- --ignored --nocapture"]
fn ssdt_ldr_initialize_thunk() {
helpers::setup();
let mut emu = emu64();
if !helpers::set_winver_maps(&mut emu, "win11") {
return;
}
emu.cfg.emulate_winapi = true; emu.cfg.skip_unimplemented = true; emu.maps.set_banzai(true);
let sample = helpers::test_data_path("exe64win_msgbox.bin");
assert!(
std::path::Path::new(&sample).is_file(),
"missing test sample: {}",
sample
);
emu.load_code(&sample);
assert!(
emu.pos > 15_000,
"LdrInitializeThunk emulation only reached {} instructions (need > 15 000). \
rip=0x{:x} — check for early crash or unimplemented syscall.",
emu.pos,
emu.regs().rip
);
}
#[test]
#[ignore = "slow (~9 min): asserts the full MessageBoxA happy path under --winver win11, which \
needs LdrInitializeThunk to COMPLETE. As of 2026-06-14 this works (ConDrv console, \
movsd GS-cookie, and thread-pool loop blockers fixed); kept #[ignore] only for runtime. \
Run with: cargo test -p libmwemu --release ssdt_msgbox_reaches_messageboxa -- --ignored --nocapture"]
fn ssdt_msgbox_reaches_messageboxa() {
use std::cell::RefCell;
use std::rc::Rc;
helpers::setup();
let mut emu = emu64();
if !helpers::set_winver_maps(&mut emu, "win11") {
return;
}
emu.cfg.emulate_winapi = true;
let sample = helpers::test_data_path("exe64win_msgbox.bin");
assert!(
std::path::Path::new(&sample).is_file(),
"missing test sample: {}",
sample
);
let user32_loaded = Rc::new(RefCell::new(false));
let mba_returned = Rc::new(RefCell::new(false));
let user32_loaded_c = Rc::clone(&user32_loaded);
let mba_returned_c = Rc::clone(&mba_returned);
const POST_MBA_CALL_RIP: u64 = 0x140001241;
emu.hooks
.on_pre_instruction(move |emu, rip, _ins, _sz| {
if !*user32_loaded_c.borrow()
&& emu.maps.get_map_by_name("user32.pe").is_some()
{
*user32_loaded_c.borrow_mut() = true;
}
if rip == POST_MBA_CALL_RIP {
*mba_returned_c.borrow_mut() = true;
}
true
});
emu.load_code(&sample);
assert!(
emu.ldr_init_done,
"ntdll!LdrInitializeThunk did not complete (pos={} rip=0x{:x})",
emu.pos,
emu.regs().rip
);
let cap = 12_000_000u64;
let _ = emu.run_to(cap);
assert!(
*user32_loaded.borrow(),
"user32.dll was never mapped — LoadLibraryA('user32.dll') shim didn't fire \
(pos={} rip=0x{:x})",
emu.pos,
emu.regs().rip,
);
assert!(
*mba_returned.borrow(),
"EXE never returned from `call MessageBoxA` (no execution at 0x{:x}) — \
either the MessageBoxA shim didn't fire, didn't pop the return address \
correctly, or LdrInit / GetProcAddress regressed (pos={} rip=0x{:x}).",
POST_MBA_CALL_RIP,
emu.pos,
emu.regs().rip,
);
}