diff --git a/crates/loader/src/services.rs b/crates/loader/src/services.rs index e3a39c6..1f588ae 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 8616549..5d5d2fa 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();