diff --git a/CHANGELOG.md b/CHANGELOG.md index a99346d..9bef06f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -120,6 +120,8 @@ remains unsupported. - Added non-alertable extended event waits through both Win64 wait APIs. +- Added the C runtime's `exit`, `_exit` and `abort` (#395, part of Gate A2), on top of the process exit from #329. A C program ends through `exit`, not `ExitProcess`, and a program built with the mingw-w64 runtime imports all three; the loader refused it before its entry point because they did not resolve. `exit` calls the functions registered with `atexit` and `_crt_atexit`, most recently registered first, and then ends the guest with `ExitReason::ProcessExit(status)`; `_exit` ends it with its status and `abort` with exit code 3, and neither calls a registered function. All three go through the same cleanup as `ExitProcess` and never end the host. A function registered while `exit` runs is called next, as ISO C requires; `exit` called again from a registered function, which ISO C leaves undefined, runs the remaining functions once and ends with the later status. All three are partial: no stream is flushed because there are none, `abort` prints no message and calls no `SIGABRT` handler, and thread-local destructors are not run. Nine tests in `crt_exit.rs` call them from guest code. `docs/testing/gate-a.md` now describes A2 as in progress and not passing. Not compared with Windows; awaiting independent review. + - Added immediate wait-any and wait-all for guest event handles. - Added guest CRT signed and unsigned 64-bit conversions for narrow and wide strings. diff --git a/crates/loader/src/services/crt_callbacks.rs b/crates/loader/src/services/crt_callbacks.rs index bb404b6..12cb56d 100644 --- a/crates/loader/src/services/crt_callbacks.rs +++ b/crates/loader/src/services/crt_callbacks.rs @@ -400,6 +400,57 @@ impl CallbackTask for ExitFunctions { } } +/// `exit`: calls the functions registered with `atexit` and `_crt_atexit`, +/// most recently registered first, and then ends the guest process. +/// +/// It takes one function at a time from the shared list. A function that is +/// registered while `exit` runs is therefore called next, before the older +/// ones that have not run yet, as ISO C requires (7.22.4.2, 7.22.4.4). A +/// registered function that calls `exit` again, which ISO C leaves undefined, +/// starts a second task on the same list: the remaining functions run once +/// each and the process ends with the later status. +struct Exit { + state: Arc, + status: u32, +} + +impl Exit { + fn next(&mut self) -> Result { + let function = self + .state + .atexit + .lock() + .expect("guest atexit functions") + .pop(); + match function { + Some(entry) => Ok(CallbackStep::Call { + entry, + args: [0; 4], + }), + None => Err(ExitReason::ProcessExit( + self.state.exit_process(self.status), + )), + } + } + + fn start(mut self) -> Result { + match self.next()? { + CallbackStep::Call { entry, args } => Ok(CallbackStart::Call { + task: Box::new(self), + entry, + args, + }), + CallbackStep::Return(value) => Ok(CallbackStart::Return(value)), + } + } +} + +impl CallbackTask for Exit { + fn resume(&mut self, _: u64, _: u64, _: &mut GuestMemory) -> Result { + self.next() + } +} + fn register_exit_tables(registry: &mut ImportRegistry, state: &Arc) { registry.register(UCRT, "_initialize_onexit_table", |args, _, mem| { let table = args.rcx; @@ -473,6 +524,24 @@ fn register_exit_tables(registry: &mut ImportRegistry, state: &Arc) { // ExitProcess does not return (processthreadsapi.h). It stops the guest @@ -20,4 +24,26 @@ pub(super) fn register(registry: &mut ImportRegistry, state: &Arc }); registry.alias(PROCESS_THREADS_API, KERNEL32, "ExitProcess"); registry.set_status(KERNEL32, "ExitProcess", ServiceStatus::Partial); + + // _exit ends the process without calling the functions registered with + // atexit (Microsoft Learn, "exit, _Exit, _exit"). `exit`, which does call + // them, is beside `_cexit` in crt_callbacks.rs. + let s = Arc::clone(state); + registry.register(UCRT, "_exit", move |args, _, _| { + // The status is an int, the low half of the first argument. + Err(ExitReason::ProcessExit(s.exit_process(args.rcx as u32))) + }); + + // abort ends the process with exit code 3 and calls no registered + // function. It is partial: it prints no message, and it does not call a + // SIGABRT handler, which the documentation says it does when one is set. + let s = Arc::clone(state); + registry.register(UCRT, "abort", move |_, _, _| { + Err(ExitReason::ProcessExit(s.exit_process(ABORT_STATUS))) + }); + + for symbol in ["_exit", "abort"] { + registry.alias(CRT_RUNTIME_API, UCRT, symbol); + registry.set_status(UCRT, symbol, ServiceStatus::Partial); + } } diff --git a/crates/loader/tests/crt_exit.rs b/crates/loader/tests/crt_exit.rs new file mode 100644 index 0000000..5bddfbb --- /dev/null +++ b/crates/loader/tests/crt_exit.rs @@ -0,0 +1,332 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +//! The C runtime's `exit`, `_exit` and `abort` (issue #395), called from +//! translated guest code through a bound import table. +//! +//! Expected behaviour is from the Microsoft Learn pages "exit, _Exit, _exit" +//! (`exit` calls the functions registered by `atexit` "in last-in-first-out +//! (LIFO) order"; `_exit` terminates "without ... processing atexit or _onexit +//! functions") and "abort" ("calls _exit to terminate the process with exit +//! code 3"), and from ISO C 7.22.4.2 and 7.22.4.4 for a function registered +//! while `exit` runs. Calling `exit` again from a registered function is +//! undefined in ISO C; the test for it states Xenora's rule. The order each +//! test expects is the order in which it registered its functions. The guest +//! code is original. + +mod support; + +use support::*; +use xenora_loader::cpu::ExitReason; +use xenora_loader::import_table::ImportName; +use xenora_loader::imports::{ImportRegistry, ServiceStatus}; +use xenora_loader::services::{MockClock, ServicesState, register_standard_services}; + +const KERNEL32: &str = "kernel32.dll"; +const UCRT: &str = "ucrtbase.dll"; +const RUNTIME: &str = "api-ms-win-crt-runtime-l1-1-0.dll"; + +const EXIT: usize = 0; +const ATEXIT: usize = 3; +const IMPORTS: &[(&str, &str)] = &[ + (UCRT, "exit"), + (UCRT, "_exit"), + (UCRT, "abort"), + (UCRT, "atexit"), + (RUNTIME, "_crt_atexit"), + (RUNTIME, "_cexit"), + (KERNEL32, "CreateEventW"), + (KERNEL32, "SetLastError"), +]; +/// The same imports as a program built with mingw-w64 names the three: by the +/// runtime API set. +const BY_API_SET: &[(&str, &str)] = &[ + (RUNTIME, "exit"), + (RUNTIME, "_exit"), + (RUNTIME, "abort"), + (UCRT, "atexit"), + (RUNTIME, "_crt_atexit"), + (RUNTIME, "_cexit"), + (KERNEL32, "CreateEventW"), + (KERNEL32, "SetLastError"), +]; + +/// A decimal log of the registered functions that ran, oldest digit first. +const LOG: u64 = SCRATCH; +/// Set to one by code placed after a call that must not return. +const RETURNED: u64 = SCRATCH + 0x100; + +/// Guest address of the stub `call [slot]; hlt` for import `index`. +fn stub(index: usize) -> u64 { + BASE + u64::from(CODE_RVA) + 16 * index as u64 +} + +/// Guest address of the import address table slot of import `index`, read +/// from the displacement of its stub. +fn slot(guest: &Guest, index: usize) -> u64 { + let displacement = guest.u32_at(stub(index) + 2) as i32; + (stub(index) + 6).wrapping_add_signed(i64::from(displacement)) +} + +// Routine indices. Routines are 32 bytes apart. +const APPEND_1: usize = 0; +const APPEND_2: usize = 1; +const APPEND_3: usize = 2; +const APPEND_4: usize = 3; +const APPEND_5: usize = 4; +const REGISTER_4_THEN_APPEND_5: usize = 5; +const EXIT_AGAIN: usize = 6; +/// The status the `EXIT_AGAIN` routine exits with. +const SECOND_STATUS: u32 = 77; + +/// `log = log * 10 + digit; return 0` +fn append(digit: u8) -> Vec { + let mut code = vec![0x48, 0xB8]; // mov rax, LOG + code.extend(LOG.to_le_bytes()); + code.extend([ + 0x48, 0x8B, 0x08, // mov rcx, [rax] + 0x48, 0x6B, 0xC9, 0x0A, // imul rcx, rcx, 10 + 0x48, 0x83, 0xC1, digit, // add rcx, digit + 0x48, 0x89, 0x08, // mov [rax], rcx + 0x31, 0xC0, // xor eax, eax + 0xC3, // ret + ]); + code +} + +fn rel32(from_end: u64, to: u64) -> [u8; 4] { + (to.wrapping_sub(from_end) as u32).to_le_bytes() +} + +/// A registered function that registers `APPEND_4` with `atexit`, through the +/// import address table as compiled code would, and then appends 5. +fn register_4_then_append_5(atexit_slot: u64) -> Vec { + let at = routine(REGISTER_4_THEN_APPEND_5); + let mut code = vec![0x48, 0x83, 0xEC, 0x28]; // sub rsp, 0x28 + code.extend([0x48, 0x8D, 0x0D]); // lea rcx, [rip + APPEND_4] + code.extend(rel32(at + 11, routine(APPEND_4))); + code.extend([0xFF, 0x15]); // call [rip + atexit slot] + code.extend(rel32(at + 17, atexit_slot)); + code.extend([0x48, 0x83, 0xC4, 0x28]); // add rsp, 0x28 + code.push(0xE9); // jmp APPEND_5 + code.extend(rel32(at + 26, routine(APPEND_5))); + code +} + +/// A registered function that calls `exit(SECOND_STATUS)`. The code after the +/// call would record that `exit` returned. +fn exit_again() -> Vec { + let at = routine(EXIT_AGAIN); + let mut code = vec![0x48, 0x83, 0xEC, 0x28]; // sub rsp, 0x28 + code.push(0xB9); // mov ecx, SECOND_STATUS + code.extend(SECOND_STATUS.to_le_bytes()); + code.push(0xE8); // call + code.extend(rel32(at + 14, stub(EXIT))); + code.extend([0x48, 0xB8]); // mov rax, RETURNED + code.extend(RETURNED.to_le_bytes()); + code.extend([ + 0xC6, 0x00, 0x01, // mov byte ptr [rax], 1 + 0x48, 0x83, 0xC4, 0x28, // add rsp, 0x28 + 0xC3, // ret + ]); + code +} + +fn guest_with(imports: &'static [(&'static str, &'static str)]) -> Guest { + // The slot of `atexit` depends only on the import list, so a guest built + // without routines tells where it is. + let atexit_slot = slot(&Guest::new(imports, &[]), ATEXIT); + let routines = [ + append(1), + append(2), + append(3), + append(4), + append(5), + register_4_then_append_5(atexit_slot), + exit_again(), + ]; + let guest = Guest::new(imports, &routines); + assert_eq!(slot(&guest, ATEXIT), atexit_slot); + guest +} + +fn guest() -> Guest { + guest_with(IMPORTS) +} + +fn register(guest: &mut Guest, symbol: &str, index: usize) { + assert_eq!(guest.call(symbol, &[routine(index)]) as u32, 0, "{symbol}"); +} + +#[test] +fn exit_runs_registered_functions_most_recent_first_then_ends_the_process() { + for imports in [IMPORTS, BY_API_SET] { + let mut guest = guest_with(imports); + // atexit and _crt_atexit feed one list. + register(&mut guest, "atexit", APPEND_1); + register(&mut guest, "_crt_atexit", APPEND_2); + register(&mut guest, "atexit", APPEND_3); + assert_eq!(guest.u64_at(LOG), 0); + assert_eq!(guest.state.exit_status(), None); + // The status is an int: the upper half of RCX is not part of it. + let (_, exit) = guest.run("exit", &[0xAAAA_BBBB_1234_5678]); + assert_eq!(exit, ExitReason::ProcessExit(0x1234_5678)); + assert_eq!(guest.u64_at(LOG), 321, "last registered runs first"); + assert_eq!(guest.state.exit_status(), Some(0x1234_5678)); + } +} + +#[test] +fn exit_with_nothing_registered_ends_the_process_with_its_status() { + for (argument, status) in [ + (0, 0), + (1, 1), + (300, 300), + (0xFFFF_FFFF, 0xFFFF_FFFF), + (0x1_0000_0000, 0), + ] { + let mut guest = guest(); + // The stub is `call [exit]; hlt`: a service that returned would reach + // the HLT and report Halt. + let (_, exit) = guest.run("exit", &[argument]); + assert_eq!(exit, ExitReason::ProcessExit(status), "{argument:#x}"); + assert_eq!(guest.u64_at(LOG), 0); + assert_eq!(guest.state.exit_status(), Some(status)); + } +} + +/// ISO C 7.22.4.4: registered functions are called in reverse order of +/// registration, "except that a function is called after any previously +/// registered functions that had already been called at the time it was +/// registered". A function registered by the second function to run is +/// therefore the third to run, ahead of the one registered first. +#[test] +fn a_function_registered_while_exit_runs_is_called_next() { + let mut guest = guest(); + register(&mut guest, "atexit", APPEND_1); + register(&mut guest, "atexit", REGISTER_4_THEN_APPEND_5); + register(&mut guest, "atexit", APPEND_3); + let (_, exit) = guest.run("exit", &[6]); + assert_eq!(exit, ExitReason::ProcessExit(6)); + // 3 ran first; then the function that registered 4 and logged 5; then 4, + // registered during exit; then 1, the oldest. + assert_eq!(guest.u64_at(LOG), 3541); +} + +/// Calling `exit` from a registered function is undefined in ISO C. Xenora's +/// rule: the functions that have not run yet run once each, and the process +/// ends with the status of the later call. Nothing runs twice and the inner +/// call does not return. +#[test] +fn exit_called_again_from_a_registered_function_finishes_with_the_later_status() { + let mut guest = guest(); + register(&mut guest, "atexit", APPEND_1); + register(&mut guest, "atexit", EXIT_AGAIN); + register(&mut guest, "atexit", APPEND_3); + let (_, exit) = guest.run("exit", &[5]); + assert_eq!(exit, ExitReason::ProcessExit(SECOND_STATUS)); + // 3, then the function that called exit again, then 1 from inside that + // second call. A status of 5 would mean the function never ran. + assert_eq!(guest.u64_at(LOG), 31); + assert_eq!(guest.memory.read_u8(RETURNED).unwrap(), 0); + assert_eq!(guest.state.exit_status(), Some(SECOND_STATUS)); +} + +#[test] +fn underscore_exit_and_abort_run_no_registered_function() { + for imports in [IMPORTS, BY_API_SET] { + // _exit ends the process with its status, an int. + for (argument, status) in [(9, 9), (0xAAAA_BBBB_0000_0102, 0x102), (0, 0)] { + let mut guest = guest_with(imports); + register(&mut guest, "atexit", APPEND_1); + register(&mut guest, "_crt_atexit", APPEND_2); + let (_, exit) = guest.run("_exit", &[argument]); + assert_eq!(exit, ExitReason::ProcessExit(status), "{argument:#x}"); + assert_eq!(guest.u64_at(LOG), 0); + assert_eq!(guest.state.exit_status(), Some(status)); + } + // abort takes no argument and ends the process with exit code 3, + // whatever the argument registers hold. + for junk in [0, 0x1234] { + let mut guest = guest_with(imports); + register(&mut guest, "atexit", APPEND_1); + let (_, exit) = guest.run("abort", &[junk, junk, junk, junk]); + assert_eq!(exit, ExitReason::ProcessExit(3)); + assert_eq!(guest.u64_at(LOG), 0); + assert_eq!(guest.state.exit_status(), Some(3)); + } + } +} + +#[test] +fn each_way_out_closes_the_guest_handles() { + for symbol in ["exit", "_exit", "abort"] { + let mut guest = guest(); + assert_ne!(guest.call("CreateEventW", &[0, 1, 0, 0]), 0); + assert_ne!(guest.call("CreateEventW", &[0, 0, 1, 0]), 0); + assert_eq!(guest.state.open_handle_count(), 2); + let (_, exit) = guest.run(symbol, &[4]); + assert!(matches!(exit, ExitReason::ProcessExit(_)), "{symbol}"); + assert_eq!(guest.state.open_handle_count(), 0, "{symbol}"); + } + // With registered functions: they run while the handles are still open, + // and the handles are closed once the last one has returned. + let mut guest = guest(); + assert_ne!(guest.call("CreateEventW", &[0, 1, 0, 0]), 0); + register(&mut guest, "atexit", APPEND_1); + assert_eq!(guest.run("exit", &[0]).1, ExitReason::ProcessExit(0)); + assert_eq!(guest.u64_at(LOG), 1); + assert_eq!(guest.state.open_handle_count(), 0); +} + +#[test] +fn an_exited_guest_runs_nothing_further() { + for symbol in ["exit", "_exit", "abort"] { + let mut guest = guest(); + let first = guest.run(symbol, &[8]).1; + assert!(matches!(first, ExitReason::ProcessExit(_)), "{symbol}"); + // Neither another service nor a second exit takes effect. + for (later, argument) in [("SetLastError", 5), ("exit", 9), ("_exit", 9), ("abort", 0)] { + assert_eq!(guest.run(later, &[argument]).1, first, "{symbol}, {later}"); + } + assert_eq!(guest.state.last_error(THREAD_A), 0, "{symbol}"); + } +} + +#[test] +fn functions_already_run_by_cexit_are_not_run_again_by_exit() { + let mut guest = guest(); + register(&mut guest, "atexit", APPEND_1); + register(&mut guest, "atexit", APPEND_2); + // _cexit runs the registered functions and returns to the caller. + assert_eq!(guest.call("_cexit", &[]), 0); + assert_eq!(guest.u64_at(LOG), 21); + // One registered afterwards is the only one exit has left to run. + register(&mut guest, "atexit", APPEND_3); + assert_eq!(guest.run("exit", &[2]).1, ExitReason::ProcessExit(2)); + assert_eq!(guest.u64_at(LOG), 213); +} + +#[test] +fn the_three_resolve_under_the_library_and_the_runtime_api_set() { + let state = std::sync::Arc::new(ServicesState::new(std::sync::Arc::new(MockClock::new(0)))); + let mut registry = ImportRegistry::new(); + register_standard_services(&mut registry, state); + let named = |name: &str| ImportName::Name(name.to_owned()); + for symbol in ["exit", "_exit", "abort"] { + let service = registry + .resolve(UCRT, &named(symbol)) + .unwrap_or_else(|| panic!("{symbol}")); + let aliased = registry.resolve(RUNTIME, &named(symbol)).unwrap(); + assert!(std::ptr::eq(service, aliased), "{symbol}"); + assert_eq!(service.status, ServiceStatus::Partial, "{symbol}"); + // Not exported by kernel32, and not through an unrelated API set. + assert!(registry.resolve(KERNEL32, &named(symbol)).is_none()); + assert!( + registry + .resolve("api-ms-win-crt-heap-l1-1-0.dll", &named(symbol)) + .is_none() + ); + } + // The spelling is exact: _Exit is another function and is not provided. + assert!(registry.resolve(UCRT, &named("_Exit")).is_none()); +} diff --git a/docs/interfaces.md b/docs/interfaces.md index 342db01..5f8dafa 100644 --- a/docs/interfaces.md +++ b/docs/interfaces.md @@ -78,11 +78,35 @@ Windows while the process has one thread. The exit closes every guest handle: kernel objects, Winsock sockets and task-queue handles, which `ServicesState::open_handle_count` counts together. Files stay on the synthetic `C:` drive, guest memory stays mapped so the host can inspect it, and `atexit` functions are not run: that belongs to the C -runtime's `exit`, which is not implemented. `ServicesState::exit_status` +runtime's `exit`, described below. `ServicesState::exit_status` reports the status. After an exit the `DbtEngine` that ran the guest executes no further guest instruction and answers every later `run` with the same `ProcessExit`. A service callback that was in progress is never resumed. +A C program ends through the C runtime instead. `ucrtbase.dll` and +`api-ms-win-crt-runtime-l1-1-0.dll` provide three ways out, each ending the +guest with the same `ProcessExit` and the same cleanup as `ExitProcess`: + +| Service | Behaviour | +| --- | --- | +| `exit(status)` | Calls the functions registered with `atexit` and `_crt_atexit`, most recently registered first, then ends the process with `status` | +| `_exit(status)` | Ends the process with `status` and calls no registered function | +| `abort()` | Ends the process with exit code 3 and calls no registered function | + +`exit` takes one function at a time from the registered list. A function +registered while `exit` runs is therefore called next, before the older ones +that have not run yet, which is ISO C's rule. A registered function that calls +`exit` again is undefined in ISO C; here the functions that have not run yet +run once each and the process ends with the status of the later call. +Functions that `_cexit` has already run are not run again. + +All three are partial. `exit` flushes no stream, because there are none, and +runs no thread-local destructor. It does not walk a module's own `_onexit` +table; that module's startup code runs it through `_execute_onexit_table`. +`abort` prints no message and does not call a `SIGABRT` handler, which the +documentation says it does when one is set. `_Exit`, `quick_exit` and +`_c_exit` are not provided. None of this has been compared with Windows. + A TLS callback may end the process too. Startup then reports the exit as its outcome, with the status, and runs neither the remaining callbacks nor the entry point; `StartupResult::tls_callbacks` counts the callbacks that finished. @@ -450,8 +474,8 @@ The CRT uses this for: on the default guest heap, and they hold at most 65,536 entries. Execution resets the table first, then calls the functions in reverse order of registration. `atexit` and `_crt_atexit` record process exit functions, - which `_cexit` runs in reverse order. Process termination through `exit` is - not modeled. + which `_cexit` runs in reverse order and returns. `exit` runs them and + then ends the process; see "PE32+ fixture startup" above. Null comparators, null arrays with a nonzero count, and reversed or misaligned tables would reach the CRT invalid-parameter handler. That diff --git a/docs/testing/gate-a.md b/docs/testing/gate-a.md index 91a85eb..cbd0a83 100644 --- a/docs/testing/gate-a.md +++ b/docs/testing/gate-a.md @@ -8,7 +8,7 @@ when both pass. | Milestone | What it is | Status | | --- | --- | --- | | A1 | An original C program with no C library linked, importing documented `kernel32.dll` and `ucrtbase.dll` functions, run through the loader and the ARM64 DBT with proper process exit | Passes locally; awaiting independent review | -| A2 | A hosted C program: a real C runtime startup, the standard C library, `argc` and `argv`, stdio and formatted output, normal exit and runtime cleanup | Not started | +| A2 | A hosted C program: a real C runtime startup, the standard C library, `argc` and `argv`, stdio and formatted output, normal exit and runtime cleanup | In progress; does not pass | ## A1 @@ -47,7 +47,22 @@ whose entry point returns its status gives the same result as `ExitProcess`. ## A2 -Not started. It needs at least the C standard I/O functions, `exit` with -`atexit` handlers, command-line and environment access, and a decision on -which C runtime startup code may be linked and under what licence. None of -those are registered today. +In progress. It does not pass, and there is no A2 test in the repository yet. + +The target is an original C program built with the mingw-w64 runtime (UCRT), +whose own startup code runs before `main`. Such a program needs pieces that +are being added one issue at a time. This page records only what is in the +tree it is part of: + +| Piece | State here | +| --- | --- | +| Normal exit: `exit` with its `atexit` functions, `_exit` and `abort` | Present (#395), on top of the process exit A1 added | +| The PE headers mapped at the image base, which the startup code reads | Separate change (#333) | +| `FNINIT`, the one instruction the startup code needs that was refused | Separate change (#378) | +| The C runtime startup calls, and the functions a program imports but does not call | Separate changes (#382, #388) | +| `argc`, `argv` and the environment table | Separate change (#392) | +| Standard streams, files and formatted output | Not started | +| The A2 program itself, its build and its test | Not started | + +Until the last two rows exist nothing here shows a hosted C program running, +and A2 must not be reported as passing.