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

Open
opened 2026-11-23 23:29:56 +00:00 by mastrr9 · 0 comments
mastrr9 commented 2026-11-23 23:29:56 +00:00 (Migrated from github.com)
Contributor

Imported from ivall/xcl. Originally posted by mastrr9 on 2026-11-23T23:29:56Z.


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).

<!-- greyfinch-sync:ivall/xcl:issue:6571021255 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/issues/329). Originally posted by [mastrr9](https://github.com/mastrr9) on 2026-11-23T23:29:56Z. --- 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).
Sign in to join this conversation.
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl#326
No description provided.