From 7246ab5ea60f5b722177dc9b731db04a20b6a905 Mon Sep 17 00:00:00 2001 From: Arthur Lacoste Date: Tue, 24 Nov 2026 20:57:41 +0100 Subject: [PATCH 1/2] loader: add Win64 service ABI preservation tests (#201) Add dispatch and DBT-backed runtime service tests that verify Win64 callee entry alignment, caller-owned shadow space, return state, and preservation of nonvolatile GPRs/XMM registers across registered service calls. AI-Assisted: Codex (GPT-5) helped draft the tests; I reviewed the diff and ran fmt/clippy(loader)/loader tests locally. Signed-off-by: Arthur Lacoste --- CHANGELOG.md | 1 + crates/loader/tests/runtime_services.rs | 163 ++++++++++++++++++++++++ 2 files changed, 164 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e686c9c..d853bd1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ remains unsupported. - Extended the read-only RDNA2 planning CLI (`python3 -m qa rdna2`) with family/status filters, compact summaries, Codex-ready manifests under `artifacts/`, active-claim exclusion (family claims exclude those families; path-only claims block overlapping permitted files), coverage reconcile against `semantic_coverage` without inflating counts, and inventory diffs. Covered by `tests/qa/test_rdna2_plan.py`; planning only, no GPU production edits. ### Added +- Added Win64 service-call ABI tests in `crates/loader/tests/runtime_services.rs` for #201. A dispatch-level test and a DBT guest-call test verify 16-byte-aligned call entry (`RSP % 16 == 8`), caller-owned 32-byte shadow space accessibility/integrity, return-value/stack/RIP return state, and preservation of RBX/RBP/RDI/RSI/R12-R15 and XMM6-XMM15 across registered service calls. Expected values are derived from the Microsoft x64 calling convention, not from current implementation behavior. - Added the VOP3A (`_e64`) encodings of eleven gfx1030 VOP2 integer forms (`v_mul_{i32_i24,hi_i32_i24,u32_u24,hi_u32_u24}`, `v_{min,max}_{i32,u32}`, `v_xnor_b32`, `v_sub_nc_u32`, `v_subrev_nc_u32`) and of the sixteen Wave32 integer compares (explicit SGPR mask destination, `s_cbranch_vccz` through `vcc_lo`, a translation error for reads of runtime-only SGPR results) as semantic-registry encoding variants, with VGPR sources only. New fixtures `vop3-integer` and `vop3-compare` with IR and MSL goldens, and an independent guide-derived oracle (`tests/validation/tests/vop3_{integer,compare}.rs`) that agrees on both reference interpreters and on native Metal. Part of #215. - Added guest texture binding through D3D12-style descriptor tables (#303): shader-visible CBV_SRV_UAV and sampler heaps, `CreateShaderResourceView` for one-level RGBA8 and BC1 2D textures, a point/clamp `CreateSampler`, a root signature with one pixel-stage SRV table and one sampler table, and `SetDescriptorHeaps`/`SetGraphicsRootDescriptorTable` in `crates/loader/src/graphics.rs`, plus `NativeCompute::render_rgba8_sampled_bc1`. Verified locally on an Apple M5 Pro under Metal validation: a guest draw with the translated `image_sample_lz` pixel stage matches the software reference and hand-derived images on 16 of 16 pixels for two RGBA8 textures and 64 of 64 for an 8×8 BC1 texture. A wrong descriptor slot changes the image, off-centre and out-of-range coordinates point-sample with clamping, and a rejected binding, draw shape or resource state never reaches the backend. The software reference samples BC1 with the spec-exact decoder, while Apple's hardware rounds BC1's interpolated palette entries differently (171/84 against 170/85), so the BC1 fixtures use only endpoint colours. Not yet reachable from guest imports. Not yet independently accepted. - Added four Wave32 gfx1030 VOP2 select and carry forms through the semantic registry. Conditional select, add with carry and both subtract orders with borrow use a per-lane VCC bit and have a guide-derived oracle that passes on both CPU interpreters and on native Metal (local Apple M5, API and shader validation); independent acceptance is pending. Part of #215. diff --git a/crates/loader/tests/runtime_services.rs b/crates/loader/tests/runtime_services.rs index 5d5d2fa..1dd24de 100644 --- a/crates/loader/tests/runtime_services.rs +++ b/crates/loader/tests/runtime_services.rs @@ -7,6 +7,7 @@ //! convention documentation, not from the registry or the handlers. use std::sync::Arc; +use std::sync::Mutex; use std::sync::atomic::{AtomicUsize, Ordering}; use xenora_loader::cpu::{CpuState, ExitReason, Register}; use xenora_loader::dbt::DbtEngine; @@ -696,6 +697,79 @@ fn floating_arguments_and_results_use_xmm_registers() { assert_eq!(float("fabsf", garbage | 0xBF80_0000) as u32, 0x3F80_0000); // -1 } +#[test] +fn guest_call_through_dbt_preserves_win64_abi_state() { + use Register::{R8, R9, R12, R13, R14, R15, Rax, Rbp, Rbx, Rcx, Rdi, Rdx, Rsi, Rsp}; + let observed = Arc::new(Mutex::new(None::<(u64, u64, [u64; 4])>)); + let seen = observed.clone(); + let mut registry = ImportRegistry::new(); + registry.register("t.dll", "F", move |_args, cpu, mem| { + let rsp = cpu.get(Rsp); + let shadow = [ + mem.read_u64(rsp + 8).unwrap(), + mem.read_u64(rsp + 16).unwrap(), + mem.read_u64(rsp + 24).unwrap(), + mem.read_u64(rsp + 32).unwrap(), + ]; + *seen.lock().unwrap() = Some((rsp, cpu.rip, shadow)); + cpu.set(Rcx, 0x11); + cpu.set(Rdx, 0x22); + cpu.set(R8, 0x33); + cpu.set(R9, 0x44); + for reg in 0..6 { + cpu.xmm[reg] = u128::from(0x55_u64 + reg as u64); + } + Ok(0x99AA_BBCC_DDEE_F001) + }); + let image = build(&[("t.dll", Symbol::Name("F"))]); + let pe = ParsedPe::parse(&image.bytes).unwrap(); + let mut memory = pe.load_into_memory(&image.bytes).unwrap().memory; + memory + .map_region(STACK, 0x1000, MemoryPermissions::READ_WRITE) + .unwrap(); + registry + .bind_imports(&pe, &image.bytes, BASE, &mut memory) + .unwrap(); + let mut engine = DbtEngine::new(256 * 1024).unwrap(); + engine.set_import_registry(registry); + // Before CALL RSP is 16-byte aligned; CALL pushes return address so the + // callee sees RSP % 16 == 8. Shadow space is [entry_rsp+8, entry_rsp+40). + let initial_rsp = STACK_TOP; + assert_eq!(initial_rsp % 16, 0); + let entry_rsp_expected = initial_rsp - 8; + for slot in 0..4u64 { + memory + .write_u64(entry_rsp_expected + 8 + slot * 8, 0x7000 + slot) + .unwrap(); + } + let mut cpu = CpuState::new(BASE + u64::from(CODE_RVA), initial_rsp); + let nonvolatile_gpr = [Rbx, Rbp, Rdi, Rsi, R12, R13, R14, R15]; + for (index, register) in nonvolatile_gpr.into_iter().enumerate() { + cpu.set(register, 0x6100 + index as u64); + } + for index in 6..16 { + cpu.xmm[index] = u128::from(0x6200_u64 + index as u64); + } + let result = engine.run(&mut cpu, &mut memory, 1000); + assert_eq!(result.exit_reason, ExitReason::Halt); + let (entry_rsp, _, shadow) = observed.lock().unwrap().expect("service ran"); + assert_eq!(entry_rsp, entry_rsp_expected); + assert_eq!(entry_rsp % 16, 8); + assert_eq!(shadow, [0x7000, 0x7001, 0x7002, 0x7003]); + assert_eq!(cpu.get(Rax), 0x99AA_BBCC_DDEE_F001); + assert_eq!(cpu.get(Rsp), initial_rsp); + for (index, register) in nonvolatile_gpr.into_iter().enumerate() { + assert_eq!(cpu.get(register), 0x6100 + index as u64, "{register:?}"); + } + for index in 6..16 { + assert_eq!( + cpu.xmm[index], + u128::from(0x6200_u64 + index as u64), + "xmm{index}" + ); + } +} + #[test] fn arguments_are_assigned_by_position_and_read_the_stack_after_the_fourth() { let mut memory = GuestMemory::new(); @@ -957,6 +1031,95 @@ fn a_service_that_changes_a_nonvolatile_register_stops_the_guest() { } } +#[test] +fn service_calls_preserve_win64_abi_state() { + // Microsoft x64 calling convention: at entry RSP % 16 == 8 (return address + // pushed by CALL). Callee preserves RBX,RBP,RDI,RSI,R12-R15 and XMM6-XMM15. + // Caller owns 32-byte shadow/home space at [RSP+8, RSP+40). RAX carries the + // return value; RCX,RDX,R8,R9,R10,R11,XMM0-XMM5 are volatile. + use Register::{R8, R9, R10, R11, R12, R13, R14, R15, Rax, Rbp, Rbx, Rcx, Rdi, Rdx, Rsi, Rsp}; + let observed = Arc::new(Mutex::new(None::<(u64, u64, [u64; 4], u64)>)); + let seen = observed.clone(); + let (registry, mut memory, trampoline) = bound_service(move |args, cpu, mem| { + let rsp = cpu.get(Rsp); + let shadow = [ + mem.read_u64(rsp + 8).unwrap(), + mem.read_u64(rsp + 16).unwrap(), + mem.read_u64(rsp + 24).unwrap(), + mem.read_u64(rsp + 32).unwrap(), + ]; + *seen.lock().unwrap() = Some((rsp, cpu.rip, shadow, args.rcx)); + // Clobber volatile registers; dispatcher must still return correctly and + // nonvolatile state must be preserved after return. + cpu.set(Rcx, 0xA1); + cpu.set(Rdx, 0xA2); + cpu.set(R8, 0xA3); + cpu.set(R9, 0xA4); + cpu.set(R10, 0xA5); + cpu.set(R11, 0xA6); + for reg in 0..6 { + cpu.xmm[reg] = u128::from(0xB0_u64 + reg as u64); + } + Ok(0x1122_3344_5566_7788) + }); + // Win64: before CALL RSP is 16-byte aligned; CALL pushes return address, + // so callee entry sees RSP % 16 == 8. + let call_rsp = STACK_TOP - 8; + assert_eq!(call_rsp % 16, 8); + memory.write_u64(call_rsp, 0x1234).unwrap(); + for slot in 0..4u64 { + memory + .write_u64(call_rsp + 8 + slot * 8, 0xC000 + slot) + .unwrap(); + } + let mut cpu = CpuState::new(trampoline, call_rsp); + let nonvolatile_gpr = [Rbx, Rbp, Rdi, Rsi, R12, R13, R14, R15]; + for (index, register) in nonvolatile_gpr.into_iter().enumerate() { + cpu.set(register, 0x5AA5_0000_0001_0000 + index as u64); + } + for index in 6..16 { + cpu.xmm[index] = u128::from(0xD000_0000_0000_0000_u64 + index as u64); + } + cpu.set(Rcx, 0x1357_9BDF_2468_ACE0); + let result = registry.dispatch(trampoline, &mut cpu, &mut memory); + assert_eq!(result, Ok(())); + let (entry_rsp, entry_rip, shadow, entry_rcx) = observed.lock().unwrap().expect("service ran"); + assert_eq!(entry_rip, trampoline); + assert_eq!( + entry_rsp, call_rsp, + "service sees caller RSP at return address" + ); + assert_eq!(entry_rsp % 16, 8, "Win64 entry RSP must be 8 mod 16"); + assert_eq!(entry_rcx, 0x1357_9BDF_2468_ACE0); + // Shadow/home space is caller-owned and must be addressable by the service. + assert_eq!(shadow, [0xC000, 0xC001, 0xC002, 0xC003]); + for slot in 0..4u64 { + assert_eq!( + memory.read_u64(call_rsp + 8 + slot * 8).unwrap(), + 0xC000 + slot, + "service call must not mutate caller shadow space" + ); + } + // Post-return ABI state. + assert_eq!(cpu.get(Rax), 0x1122_3344_5566_7788); + assert_eq!(cpu.get(Rsp), call_rsp + 8); + assert_eq!(cpu.rip, 0x1234); + for (index, register) in nonvolatile_gpr.into_iter().enumerate() { + assert_eq!( + cpu.get(register), + 0x5AA5_0000_0001_0000 + index as u64, + "nonvolatile {register:?} must be preserved" + ); + } + for index in 6..16 { + assert_eq!( + cpu.xmm[index], + u128::from(0xD000_0000_0000_0000_u64 + index as u64), + "nonvolatile xmm{index} must be preserved" + ); + } +} + #[test] fn a_call_that_cannot_return_runs_no_service() { let calls = Arc::new(AtomicUsize::new(0)); From af1b84ad75f23bca616b030ea30523c37e892928 Mon Sep 17 00:00:00 2001 From: Arthur Lacoste Date: Tue, 24 Nov 2026 22:14:29 +0100 Subject: [PATCH 2/2] loader: fix needless_range_loop in Win64 ABI tests (#201) Address review feedback from zachdoxsee on PR #369: replace index-only XMM range loops with iterator-based loops so strict clippy passes on Rust 1.100. Signed-off-by: Arthur Lacoste --- crates/loader/tests/runtime_services.rs | 30 ++++++++++++------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/crates/loader/tests/runtime_services.rs b/crates/loader/tests/runtime_services.rs index 1dd24de..466d2ca 100644 --- a/crates/loader/tests/runtime_services.rs +++ b/crates/loader/tests/runtime_services.rs @@ -716,8 +716,8 @@ fn guest_call_through_dbt_preserves_win64_abi_state() { cpu.set(Rdx, 0x22); cpu.set(R8, 0x33); cpu.set(R9, 0x44); - for reg in 0..6 { - cpu.xmm[reg] = u128::from(0x55_u64 + reg as u64); + for (reg, lane) in cpu.xmm[..6].iter_mut().enumerate() { + *lane = u128::from(0x55_u64 + reg as u64); } Ok(0x99AA_BBCC_DDEE_F001) }); @@ -747,8 +747,8 @@ fn guest_call_through_dbt_preserves_win64_abi_state() { for (index, register) in nonvolatile_gpr.into_iter().enumerate() { cpu.set(register, 0x6100 + index as u64); } - for index in 6..16 { - cpu.xmm[index] = u128::from(0x6200_u64 + index as u64); + for (offset, lane) in cpu.xmm[6..16].iter_mut().enumerate() { + *lane = u128::from(0x6200_u64 + (offset + 6) as u64); } let result = engine.run(&mut cpu, &mut memory, 1000); assert_eq!(result.exit_reason, ExitReason::Halt); @@ -761,12 +761,9 @@ fn guest_call_through_dbt_preserves_win64_abi_state() { for (index, register) in nonvolatile_gpr.into_iter().enumerate() { assert_eq!(cpu.get(register), 0x6100 + index as u64, "{register:?}"); } - for index in 6..16 { - assert_eq!( - cpu.xmm[index], - u128::from(0x6200_u64 + index as u64), - "xmm{index}" - ); + for (offset, lane) in cpu.xmm[6..16].iter().enumerate() { + let index = offset + 6; + assert_eq!(*lane, u128::from(0x6200_u64 + index as u64), "xmm{index}"); } } @@ -1057,8 +1054,8 @@ fn service_calls_preserve_win64_abi_state() { cpu.set(R9, 0xA4); cpu.set(R10, 0xA5); cpu.set(R11, 0xA6); - for reg in 0..6 { - cpu.xmm[reg] = u128::from(0xB0_u64 + reg as u64); + for (reg, lane) in cpu.xmm[..6].iter_mut().enumerate() { + *lane = u128::from(0xB0_u64 + reg as u64); } Ok(0x1122_3344_5566_7788) }); @@ -1077,8 +1074,8 @@ fn service_calls_preserve_win64_abi_state() { for (index, register) in nonvolatile_gpr.into_iter().enumerate() { cpu.set(register, 0x5AA5_0000_0001_0000 + index as u64); } - for index in 6..16 { - cpu.xmm[index] = u128::from(0xD000_0000_0000_0000_u64 + index as u64); + for (offset, lane) in cpu.xmm[6..16].iter_mut().enumerate() { + *lane = u128::from(0xD000_0000_0000_0000_u64 + (offset + 6) as u64); } cpu.set(Rcx, 0x1357_9BDF_2468_ACE0); let result = registry.dispatch(trampoline, &mut cpu, &mut memory); @@ -1111,9 +1108,10 @@ fn service_calls_preserve_win64_abi_state() { "nonvolatile {register:?} must be preserved" ); } - for index in 6..16 { + for (offset, lane) in cpu.xmm[6..16].iter().enumerate() { + let index = offset + 6; assert_eq!( - cpu.xmm[index], + *lane, u128::from(0xD000_0000_0000_0000_u64 + index as u64), "nonvolatile xmm{index} must be preserved" );