acphal: hal_pop Win32 BOOL ABI mismatch, stale MSG_DISCONNECTED on reconnect, and +125 ppm frame-clock drift #329

Open
opened 2026-11-26 04:20:09 +00:00 by github-bot · 0 comments
Owner

@⁠mastrr9 on GitHub · 2026-11-23 23:29 UTC · original

Static audit of crates/xcl/src/hle/acphal.rs on main (85bdf21) found three correctness bugs in acphal:

  1. hal_pop returns a 1-byte Rust bool across Windows extern "system" ABI (acphal.rs:434-445)

    • unsafe extern "system" fn hal_pop(h: *mut Hal, m: *mut Message) -> bool only guarantees the low byte (AL) is set to 0/1, leaving the upper 24 bits of EAX undefined.
    • Win32/Xbox C++ callers expecting a 32-bit BOOL inspect full EAX, so false can be observed as non-zero TRUE and cause a caller to process a stale/uninitialized Message or spin.
    • Suggested fix: Change the return type from bool to u32 (returning 1 / 0).
  2. Channel::connect() leaves stale MSG_DISCONNECTED queued (acphal.rs:354-362)

    • disconnect() pushes MSG_DISCONNECTED onto self.messages, but connect() does not clear self.messages. A title that disconnects and later reconnects immediately pops a stale MSG_DISCONNECTED on the fresh connection.
    • Suggested fix: Call self.messages.clear() in Channel::connect().
  3. Audio frame clock drifts +125 ppm (acphal.rs:39)

    • const FRAME: Duration = Duration::from_micros(2667); vs exact 128 / 48_000 s (2666.666... µs) accumulates +0.333 µs per frame (+125 ppm, ~450 ms/hour slow relative to 48 kHz audio).
    • Suggested fix: Anchor connect_time and compute frame k deadline as connect_time + Duration::from_nanos(k * 128_000_000_000 / 48_000) (or integer nanosecond accumulator) so there is zero long-term drift.

Not tested: on Windows (static code audit + clean-room timing arithmetic check on macOS; no game files).

AI use: Audited with Antigravity (Gemini 4 Neon) and Claude Sonnet 5.5 (static code review only).

**@⁠mastrr9** on GitHub · 2026-11-23 23:29 UTC · [original](https://github.com/xcl-dev/xcl/issues/329) Static audit of `crates/xcl/src/hle/acphal.rs` on `main` (`85bdf21`) found three correctness bugs in `acphal`: 1. **`hal_pop` returns a 1-byte Rust `bool` across Windows `extern "system"` ABI (`acphal.rs:434-445`)** - `unsafe extern "system" fn hal_pop(h: *mut Hal, m: *mut Message) -> bool` only guarantees the low byte (`AL`) is set to `0`/`1`, leaving the upper 24 bits of `EAX` undefined. - Win32/Xbox C++ callers expecting a 32-bit `BOOL` inspect full `EAX`, so `false` can be observed as non-zero `TRUE` and cause a caller to process a stale/uninitialized `Message` or spin. - **Suggested fix:** Change the return type from `bool` to `u32` (returning `1` / `0`). 2. **`Channel::connect()` leaves stale `MSG_DISCONNECTED` queued (`acphal.rs:354-362`)** - `disconnect()` pushes `MSG_DISCONNECTED` onto `self.messages`, but `connect()` does not clear `self.messages`. A title that disconnects and later reconnects immediately pops a stale `MSG_DISCONNECTED` on the fresh connection. - **Suggested fix:** Call `self.messages.clear()` in `Channel::connect()`. 3. **Audio frame clock drifts `+125 ppm` (`acphal.rs:39`)** - `const FRAME: Duration = Duration::from_micros(2667);` vs exact `128 / 48_000` s (`2666.666... µs`) accumulates `+0.333 µs` per frame (`+125 ppm`, ~450 ms/hour slow relative to 48 kHz audio). - **Suggested fix:** Anchor `connect_time` and compute frame `k` deadline as `connect_time + Duration::from_nanos(k * 128_000_000_000 / 48_000)` (or integer nanosecond accumulator) so there is zero long-term drift. **Not tested:** on Windows (static code audit + clean-room timing arithmetic check on macOS; no game files). **AI use:** Audited with Antigravity (Gemini 4 Neon) and Claude Sonnet 5.5 (static code review only).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:20:09 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.