From 7623fde0e77ae4ed24c06ff9e95058bd3a2dee7a Mon Sep 17 00:00:00 2001 From: LanaCodes-Boop <139008131+LanaCodes-Boop@users.noreply.github.com> Date: Tue, 24 Nov 2026 12:10:20 -0300 Subject: [PATCH] loader: retire a guest thread's last error and test the per-TEB contract (#202) main keeps the last error per TEB since #310. This adds the missing retirement step, so a reused TEB address starts at zero, and two conformance tests: a successful call keeps a nonzero error, and one thread's failure does not reach another. Signed-off-by: LanaCodes-Boop <139008131+LanaCodes-Boop@users.noreply.github.com> --- CHANGELOG.md | 1 + crates/loader/src/services.rs | 15 +++++++ crates/loader/tests/runtime_services.rs | 58 +++++++++++++++++++++++-- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d35d6370..a6b2d5ec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,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 `ServicesState::retire_thread_last_error`, which drops the last-error value of a guest thread that has ended so a reused TEB address starts at zero, and two conformance tests for the per-TEB last error from #310: a successful call keeps a nonzero error, and one thread's failure does not reach another (#202; awaiting independent review). No guest thread is scheduled yet, and the number of TEBs holding a value is not bounded. - Added an independent guide-derived oracle for the Wave32 16-bit (`v_cmp_{lt..ge}_{i16,u16}_e32`) and 64-bit (`v_cmp_{f..t}_{i64,u64}_e32`) integer compares in the reference and the second interpreter (6,212 lane cases, 1,208 rejections; contract in `docs/shaders/vector-compare.md` section 2a). The decoder and translator still reject these forms, and native Metal agreement is not yet shown. Part of #215. - Added `python3 -m qa rdna2 rank` to the read-only RDNA2 planner: it groups undeclared gfx1030 forms by the piece of work that would unlock them (an encoding variant of a declared form, a new operand rule, or a new primitive, from XML identity and operand shape only) and ranks the groups by forms per estimated cost, excluding claimed scopes, with `--no-float` to leave out floating-point groups until the FP contract exists (part of #215). - Added authoritative read-only x86-64 CPU inventory CLI (`python3 -m qa cpu-inventory`) over `crates/loader/x86/instructions.toml` via `generate_x86.load`, with opcode-map/prefix/operand summaries, native vs shared-semantic operation partitions from `dbt.rs`, fixture mnemonic evidence and a separate unsupported catalog. Does not count candidates as implemented. Covered by `tests/qa/test_cpu_inventory.py`. diff --git a/crates/loader/src/services.rs b/crates/loader/src/services.rs index e3a39c66..1f588ae8 100644 --- a/crates/loader/src/services.rs +++ b/crates/loader/src/services.rs @@ -251,6 +251,21 @@ impl ServicesState { .unwrap_or(0) } + /// Forget the last-error value of a guest thread that has ended, so a + /// later thread given the same TEB address starts at zero. Returns + /// whether a nonzero value was dropped. + /// + /// Each service call names its own TEB and the values sit behind one + /// lock, so calls for different threads may run concurrently. Retire a + /// TEB only once no service call for that thread is still running. + pub fn retire_thread_last_error(&self, teb: u64) -> bool { + self.last_errors + .lock() + .expect("guest last-error state") + .remove(&teb) + .is_some() + } + fn set_last_error(&self, teb: u64, error: u32) { let mut errors = self.last_errors.lock().expect("guest last-error state"); if error == 0 { diff --git a/crates/loader/tests/runtime_services.rs b/crates/loader/tests/runtime_services.rs index 8616549f..5d5d2fa7 100644 --- a/crates/loader/tests/runtime_services.rs +++ b/crates/loader/tests/runtime_services.rs @@ -120,17 +120,24 @@ fn build(imports: &[(&str, Symbol)]) -> Image { } fn standard(clock: u64) -> (ImportRegistry, Arc) { - let clock = Arc::new(MockClock::new(clock)); - let mut registry = ImportRegistry::new(); - register_standard_services(&mut registry, Arc::new(ServicesState::new(clock.clone()))); + let (registry, clock, _) = standard_with_state(clock); (registry, clock) } +fn standard_with_state(clock: u64) -> (ImportRegistry, Arc, Arc) { + let clock = Arc::new(MockClock::new(clock)); + let state = Arc::new(ServicesState::new(clock.clone())); + let mut registry = ImportRegistry::new(); + register_standard_services(&mut registry, state.clone()); + (registry, clock, state) +} + /// A guest whose image imports every generated service from its own library. struct Guest { engine: DbtEngine, memory: GuestMemory, clock: Arc, + state: Arc, teb: u64, /// Upper halves and XMM lanes of unrelated arguments hold this pattern. xmm: [u128; 4], @@ -151,7 +158,7 @@ impl Guest { memory .map_region(STACK, 0x1000, MemoryPermissions::READ_WRITE) .unwrap(); - let (mut registry, clock) = standard(0x01DA_0000_1234_5678); + let (mut registry, clock, state) = standard_with_state(0x01DA_0000_1234_5678); let bound = registry .bind_imports(&pe, &image.bytes, BASE, &mut memory) .unwrap(); @@ -162,6 +169,7 @@ impl Guest { engine, memory, clock, + state, teb: 0, xmm: [0xAAAA_BBBB_CCCC_DDDD_EEEE_FFFF_0123_4567; 4], } @@ -554,6 +562,48 @@ fn last_error_isolated_across_guest_thread_switches() { assert_eq!(guest.last_error(), 6); } +/// The last-error code is documented as kept per thread, and most functions +/// set it only when they fail (errhandlingapi.h GetLastError). +#[test] +fn a_successful_call_keeps_another_threads_error_and_its_own() { + let mut guest = Guest::new(); + guest.teb = 0x10000; + assert_eq!(guest.call("VirtualAlloc", &[0, 0, 0x3000, 4]), 0); + assert_eq!(guest.last_error(), 87); // ERROR_INVALID_PARAMETER + guest.teb = 0x20000; + guest.call("SetLastError", &[0xFFFF_FFFF]); + assert_eq!(guest.call("GetCurrentProcess", &[]), PROCESS); + assert_eq!(guest.call("GetCurrentThreadId", &[]), 0x4104); + assert_eq!( + guest.last_error(), + 0xFFFF_FFFF, + "success keeps a nonzero error" + ); + guest.teb = 0x10000; + assert_eq!(guest.last_error(), 87); +} + +#[test] +fn a_retired_thread_starts_clean_when_its_teb_address_is_reused() { + let mut guest = Guest::new(); + guest.teb = 0x10000; + guest.call("SetLastError", &[998]); + guest.teb = 0x20000; + guest.call("CloseHandle", &[0x1234]); + + assert!(guest.state.retire_thread_last_error(0x10000)); + assert!(!guest.state.retire_thread_last_error(0x10000)); + assert!( + !guest.state.retire_thread_last_error(0x30000), + "a thread that never failed holds no value" + ); + assert_eq!(guest.last_error(), 6, "the other thread is untouched"); + guest.teb = 0x10000; + assert_eq!(guest.last_error(), 0); + guest.call("SetLastError", &[5]); + assert_eq!(guest.last_error(), 5); +} + #[test] fn last_error_is_set_by_failures_and_by_the_guest() { let mut guest = Guest::new();