runtime: validate chad permissive out-handle and add lifecycle checks #1864

Merged
Ammmae0x merged 2 commits from upstream-pr/1871 into main 2026-11-24 20:31:07 +00:00
Ammmae0x commented 2026-11-24 19:52:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T19:52:06Z.


Change

Validate the handle output pointer in XDspConnectWithMaximumStreamLimit inside src/startup_stubs/chad.cpp when X2W_AUDIO_PERMISSIVE=1 is set, and add tests/startup_chad_check.cpp (startup_chad_check in CMakeLists.txt).

  • When handle is null or fails x2w_local::writable(handle, sizeof(void*)), XDspConnectWithMaximumStreamLimit logs the refusal and returns E_POINTER (0x80004003) instead of skipping the write and returning S_OK.
  • Non-permissive mode still traps via x2w_trap::fire before checking handle.
  • tests/startup_chad_check.cpp verifies:
    • Permissive negative controls (nullptr, PAGE_READONLY, and RW->RO page-boundary straddling void**) return E_POINTER.
    • Permissive positive lifecycle (XDspActivate, XDspConnectWithMaximumStreamLimit, XDspSubmitCommand, XDspDeactivate, XDspDisconnect) returns S_OK, populates the handle, and emits zero x2w-trap log lines.
    • Non-permissive child-process controls trap with expected 0xE2570200..0xE2570204 exit codes across all five exports.

Verification

Commit 4e6f2c81 (rebased onto upstream/main at 7c96def1), Windows x64 (dev Debug and release /O2):

  • cmake --preset dev & cmake --preset release -> configured cleanly (Total Tests: 1137).
  • cmake --build --preset dev --target startup_stub_chad startup_chad_check startup_stub_consumer -> 0 errors, 0 warnings.
  • cmake --build --preset release --target startup_stub_chad startup_chad_check startup_stub_consumer -> 0 errors, 0 warnings.
  • ctest --preset dev --output-on-failure -R "^(startup_chad_check|startup_stubs)$" -E "cpu_native_archive_" -> 2/2 passed (startup_chad_check, startup_stubs).
  • ctest --preset release --output-on-failure -R "^(startup_chad_check|startup_stubs)$" -E "cpu_native_archive_" -> 2/2 passed (startup_chad_check, startup_stubs).
  • python -B tests/form_coverage_check.py -> passed.
  • python -B tests/docs_links_check.py -> passed (18 Markdown files, 370 links).
  • python -B tests/bidi_controls_check.py -> passed (1325 tracked files, no bidirectional controls).

Evidence and follow-up

  • tests/startup_chad_check.cpp is a source-built fixture (MIT) loading the built chad.dll directly with no external assets.
  • XDspConnectWithMaximumStreamLimit still populates a static placeholder connection pointer under X2W_AUDIO_PERMISSIVE=1; full DSP stream semantics remain unimplemented until public ABI evidence exists.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6590132888 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1871). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T19:52:06Z. --- ## Change Validate the `handle` output pointer in `XDspConnectWithMaximumStreamLimit` inside `src/startup_stubs/chad.cpp` when `X2W_AUDIO_PERMISSIVE=1` is set, and add `tests/startup_chad_check.cpp` (`startup_chad_check` in `CMakeLists.txt`). - When `handle` is null or fails `x2w_local::writable(handle, sizeof(void*))`, `XDspConnectWithMaximumStreamLimit` logs the refusal and returns `E_POINTER` (`0x80004003`) instead of skipping the write and returning `S_OK`. - Non-permissive mode still traps via `x2w_trap::fire` before checking `handle`. - `tests/startup_chad_check.cpp` verifies: - Permissive negative controls (`nullptr`, `PAGE_READONLY`, and RW->RO page-boundary straddling `void**`) return `E_POINTER`. - Permissive positive lifecycle (`XDspActivate`, `XDspConnectWithMaximumStreamLimit`, `XDspSubmitCommand`, `XDspDeactivate`, `XDspDisconnect`) returns `S_OK`, populates the handle, and emits zero `x2w-trap` log lines. - Non-permissive child-process controls trap with expected `0xE2570200..0xE2570204` exit codes across all five exports. ## Verification Commit `4e6f2c81` (rebased onto `upstream/main` at `7c96def1`), Windows x64 (`dev` Debug and `release` `/O2`): - `cmake --preset dev` & `cmake --preset release` -> configured cleanly (`Total Tests: 1137`). - `cmake --build --preset dev --target startup_stub_chad startup_chad_check startup_stub_consumer` -> 0 errors, 0 warnings. - `cmake --build --preset release --target startup_stub_chad startup_chad_check startup_stub_consumer` -> 0 errors, 0 warnings. - `ctest --preset dev --output-on-failure -R "^(startup_chad_check|startup_stubs)$" -E "cpu_native_archive_"` -> 2/2 passed (`startup_chad_check`, `startup_stubs`). - `ctest --preset release --output-on-failure -R "^(startup_chad_check|startup_stubs)$" -E "cpu_native_archive_"` -> 2/2 passed (`startup_chad_check`, `startup_stubs`). - `python -B tests/form_coverage_check.py` -> passed. - `python -B tests/docs_links_check.py` -> passed (18 Markdown files, 370 links). - `python -B tests/bidi_controls_check.py` -> passed (1325 tracked files, no bidirectional controls). ## Evidence and follow-up - `tests/startup_chad_check.cpp` is a source-built fixture (MIT) loading the built `chad.dll` directly with no external assets. - `XDspConnectWithMaximumStreamLimit` still populates a static placeholder connection pointer under `X2W_AUDIO_PERMISSIVE=1`; full DSP stream semantics remain unimplemented until public ABI evidence exists. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets. AI-assisted.
tedin7 commented 2026-11-24 20:01:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T19:55:03Z.

Review: COMMENTED


Review of head 4e6f2c81086b22383fe19e2eed17b06bf2eb7c95 (base origin/main at dd753176 when I fetched). Comment only, not an approval.

Verdict: 0 P1 / 1 P2 (test strength) / a few P3. The production change looks correct. One of the new test assertions cannot fail, so it does not guard what it claims to.

Linux limits. This change is Windows-only C++ (windows.h, VirtualAlloc, SEH, CreateProcess). On Linux I could not compile or run startup_chad_check, startup_stubs or any mutation of the C++. Everything below about the code is from reading it; the author's Windows results (dev and release, 2/2 each) are theirs and not reproduced by me. What I did run: tests/docs_links_check.py (18 files, 370 links), tests/bidi_controls_check.py (1325 files) and git diff --check origin/main...HEAD, all clean.

Production change (src/startup_stubs/chad.cpp)

  • Order is right: accept() runs first, so in non-permissive mode x2w_trap::fire still terminates the process before handle is touched; in permissive mode the handle is validated before the write.
  • !handle || !x2w_local::writable(handle, sizeof(void*)) is the right guard. accessible() rejects addresses below 0x10000, a non-committed or guard page, a non-writable protection, and a range that crosses the end of the queried region. So the RW->RO straddle control (8 bytes starting 4 bytes before a read-only page) is refused by the bytes > end - start test, as the author intends.
  • E_POINTER is 0x80004003, cast to long (32-bit on Windows). The failure path logs through x2w_local::logf and returns before any write. The success path is unchanged except that the %llx/%llu arguments are now cast to unsigned long long, which fixes the old mismatch with u64.
  • Behaviour change to note: a caller that passed a null or unwritable out-handle used to get 0 in permissive mode and now gets E_POINTER. Only tests/startup_chad_check.cpp, chad.def and docs reference the export in this tree, so I found no in-tree caller that breaks.

Findings

  1. P2, test strength: tests/startup_chad_check.cpp asserts log_text.find("x2w-trap") == std::string::npos for the permissive lifecycle. In permissive mode accept() records with the tag "x2w-audio", and the only code that writes "x2w-trap" is x2w_trap::fire, which terminates the process. So the assertion is true even if the log is empty, unreachable or never written, and a regression would end the test process rather than reach this line. As written it cannot fail for a logging or lifecycle regression. Suggest asserting something that can fail: that the log contains the expected x2w-audio XDspConnectWithMaximumStreamLimit ... -> 0 line for the success call, and the -> E_POINTER line for the refusals. That would also pin the new log message, which nothing asserts today.
  2. P3, missing mutation proof: I could not run it here, but the useful one is to put back the old if (handle && writable(...)) *handle = &connection; and confirm the three negative controls fail. Please list that mutation and its result in the PR.
  3. P3, stale comment: the header comment of chad.cpp still says the export returns 0 on success and "the others ... succeed (0)"; it does not mention the new E_POINTER for a bad out-handle.
  4. P3, redundancy: the non-permissive null and valid child cases for XDspConnectWithMaximumStreamLimit both trap before handle is read, so they check the same thing twice. Harmless.
  5. P3, robustness: the straddle control uses VirtualProtect and a PAGE_READONLY allocation; both are checked for failure and report a FAIL instead of passing silently, which is good.

Clean-room: the new test is a source-built fixture loading the built stub DLL, with no game files or dumps. The only vendor-style names are the export names already in chad.def.

Not run here: any compile, ctest, or mutation of the C++.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046336131 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1871#pullrequestreview-6046336131). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T19:55:03Z. **Review:** COMMENTED --- Review of head `4e6f2c81086b22383fe19e2eed17b06bf2eb7c95` (base `origin/main` at `dd753176` when I fetched). Comment only, not an approval. **Verdict: 0 P1 / 1 P2 (test strength) / a few P3.** The production change looks correct. One of the new test assertions cannot fail, so it does not guard what it claims to. **Linux limits.** This change is Windows-only C++ (`windows.h`, `VirtualAlloc`, SEH, `CreateProcess`). On Linux I could not compile or run `startup_chad_check`, `startup_stubs` or any mutation of the C++. Everything below about the code is from reading it; the author's Windows results (dev and release, 2/2 each) are theirs and not reproduced by me. What I did run: `tests/docs_links_check.py` (18 files, 370 links), `tests/bidi_controls_check.py` (1325 files) and `git diff --check origin/main...HEAD`, all clean. **Production change** (`src/startup_stubs/chad.cpp`) - Order is right: `accept()` runs first, so in non-permissive mode `x2w_trap::fire` still terminates the process before `handle` is touched; in permissive mode the handle is validated before the write. - `!handle || !x2w_local::writable(handle, sizeof(void*))` is the right guard. `accessible()` rejects addresses below 0x10000, a non-committed or guard page, a non-writable protection, and a range that crosses the end of the queried region. So the RW->RO straddle control (8 bytes starting 4 bytes before a read-only page) is refused by the `bytes > end - start` test, as the author intends. - `E_POINTER` is `0x80004003`, cast to `long` (32-bit on Windows). The failure path logs through `x2w_local::logf` and returns before any write. The success path is unchanged except that the `%llx`/`%llu` arguments are now cast to `unsigned long long`, which fixes the old mismatch with `u64`. - Behaviour change to note: a caller that passed a null or unwritable out-handle used to get 0 in permissive mode and now gets `E_POINTER`. Only `tests/startup_chad_check.cpp`, `chad.def` and docs reference the export in this tree, so I found no in-tree caller that breaks. **Findings** 1. **P2, test strength:** `tests/startup_chad_check.cpp` asserts `log_text.find("x2w-trap") == std::string::npos` for the permissive lifecycle. In permissive mode `accept()` records with the tag `"x2w-audio"`, and the only code that writes `"x2w-trap"` is `x2w_trap::fire`, which terminates the process. So the assertion is true even if the log is empty, unreachable or never written, and a regression would end the test process rather than reach this line. As written it cannot fail for a logging or lifecycle regression. Suggest asserting something that can fail: that the log contains the expected `x2w-audio XDspConnectWithMaximumStreamLimit ... -> 0` line for the success call, and the `-> E_POINTER` line for the refusals. That would also pin the new log message, which nothing asserts today. 2. **P3, missing mutation proof:** I could not run it here, but the useful one is to put back the old `if (handle && writable(...)) *handle = &connection;` and confirm the three negative controls fail. Please list that mutation and its result in the PR. 3. **P3, stale comment:** the header comment of `chad.cpp` still says the export returns 0 on success and "the others ... succeed (0)"; it does not mention the new `E_POINTER` for a bad out-handle. 4. **P3, redundancy:** the non-permissive `null` and `valid` child cases for `XDspConnectWithMaximumStreamLimit` both trap before `handle` is read, so they check the same thing twice. Harmless. 5. **P3, robustness:** the straddle control uses `VirtualProtect` and a `PAGE_READONLY` allocation; both are checked for failure and report a FAIL instead of passing silently, which is good. Clean-room: the new test is a source-built fixture loading the built stub DLL, with no game files or dumps. The only vendor-style names are the export names already in `chad.def`. Not run here: any compile, `ctest`, or mutation of the C++. AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-24 21:01:00 +00:00
tedin7 commented 2026-11-24 21:01:01 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T20:07:09Z.

Review: COMMENTED


Re-review of head 60ee4c20a559814f029cb18deb3cbae0ac4fa017 (tests: assert permissive chad log entries and clarify header comment, on top of 4e6f2c81), checked out at exactly that SHA. Verdict: 0 P1, 0 P2. My earlier P2 is closed; remaining notes are P3. I could not compile or run this change (Windows-only C++: windows.h, VirtualAlloc, VirtualProtect, SEH, CreateProcess).

Earlier findings

  • P2 (assertion that could not fail): closed. The commit adds assertions that can fail: the permissive log must contain exactly one XDspConnectWithMaximumStreamLimit memory=0x1000 ... handle_out=... line ending -> 0 and exactly three ending -> E_POINTER, plus the XDspActivate, XDspSubmitCommand, XDspDeactivate and XDspDisconnect entries. To check that the counting loop discriminates, I copied it verbatim into a small program and fed it synthetic logs:
expected log (new behaviour)       ok_hits=1 epointer_hits=3 => assertions pass
old behaviour (refusals log -> 0)  ok_hits=4 epointer_hits=0 => assertions FAIL
empty log                          ok_hits=0 epointer_hits=0 => assertions FAIL
success line missing               ok_hits=0 epointer_hits=3 => assertions FAIL

So an empty or unreachable log, and a regression to the old "-> 0 on refusal" behaviour, now fail the test. This reproduces the parsing logic only, not the chad stub itself.

  • P3 (mutation proof): the author's comment reports reverting return hr_pointer; to return result; and getting 3 failing negative controls. That is consistent with the code (the three controls compare the return value with hr_pointer), but I did not reproduce it.
  • P3 (stale header comment): closed; chad.cpp now says E_POINTER when handle is null or unwritable.

Production change, re-read at this head

accept() still runs first, so in non-permissive mode the trap fires before handle is read. In permissive mode a null or unwritable handle logs the full argument line with -> E_POINTER and returns hr_pointer before any write; otherwise *handle = &connection and the -> 0 line is logged. The log arguments are now cast to unsigned long long, matching %llx/%llu. 0x80004003 to long is the usual 32-bit E_POINTER. Clean-room and title-neutral: a source-built fixture loads the built stub; no title is named.

Commands run (Linux)

git diff --check origin/main...HEAD                    -> clean
python3 -B tests/docs_links_check.py                   -> Checked 18 Markdown files and 370 links
python3 -B tests/bidi_controls_check.py                -> no bidirectional controls
x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrc tests/startup_chad_check.cpp   -> no errors
x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrc src/startup_stubs/chad.cpp    -> only the two __try/__except errors of local_log.h (cross toolchain), none in the changed lines
git merge-tree --write-tree HEAD origin/main           -> 0 conflicts
g++ logparse.cpp (the counting loop on synthetic logs) -> table above

P3

  • The old assertion log_text.find("x2w-trap") == npos is still there; it passes on an empty log, so it adds nothing next to the new ones (harmless).
  • A caller that passed a null or unwritable out-handle used to get 0 in permissive mode and now gets E_POINTER; no caller in the tree is affected, but a sentence in the PR description would record the change.
  • The non-permissive null and valid child cases both trap before handle is read, so they check the same thing twice.

Limits

Not built and not run on Windows. Please run ctest -R startup_chad_check (dev and release) or rely on the Windows CI job; my verdict rests on reading the code and on the log-parsing reproduction above.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6046518676 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1871#pullrequestreview-6046518676). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T20:07:09Z. **Review:** COMMENTED --- Re-review of head `60ee4c20a559814f029cb18deb3cbae0ac4fa017` (`tests: assert permissive chad log entries and clarify header comment`, on top of `4e6f2c81`), checked out at exactly that SHA. Verdict: 0 P1, 0 P2. My earlier P2 is closed; remaining notes are P3. **I could not compile or run this change** (Windows-only C++: `windows.h`, `VirtualAlloc`, `VirtualProtect`, SEH, `CreateProcess`). ## Earlier findings - **P2 (assertion that could not fail):** closed. The commit adds assertions that can fail: the permissive log must contain exactly one `XDspConnectWithMaximumStreamLimit memory=0x1000 ... handle_out=...` line ending `-> 0` and exactly three ending `-> E_POINTER`, plus the `XDspActivate`, `XDspSubmitCommand`, `XDspDeactivate` and `XDspDisconnect` entries. To check that the counting loop discriminates, I copied it verbatim into a small program and fed it synthetic logs: ``` expected log (new behaviour) ok_hits=1 epointer_hits=3 => assertions pass old behaviour (refusals log -> 0) ok_hits=4 epointer_hits=0 => assertions FAIL empty log ok_hits=0 epointer_hits=0 => assertions FAIL success line missing ok_hits=0 epointer_hits=3 => assertions FAIL ``` So an empty or unreachable log, and a regression to the old "-> 0 on refusal" behaviour, now fail the test. This reproduces the parsing logic only, not the chad stub itself. - **P3 (mutation proof):** the author's comment reports reverting `return hr_pointer;` to `return result;` and getting 3 failing negative controls. That is consistent with the code (the three controls compare the return value with `hr_pointer`), but I did not reproduce it. - **P3 (stale header comment):** closed; `chad.cpp` now says `E_POINTER when handle is null or unwritable`. ## Production change, re-read at this head `accept()` still runs first, so in non-permissive mode the trap fires before `handle` is read. In permissive mode a null or unwritable `handle` logs the full argument line with `-> E_POINTER` and returns `hr_pointer` before any write; otherwise `*handle = &connection` and the `-> 0` line is logged. The log arguments are now cast to `unsigned long long`, matching `%llx`/`%llu`. `0x80004003` to `long` is the usual 32-bit `E_POINTER`. Clean-room and title-neutral: a source-built fixture loads the built stub; no title is named. ## Commands run (Linux) ``` git diff --check origin/main...HEAD -> clean python3 -B tests/docs_links_check.py -> Checked 18 Markdown files and 370 links python3 -B tests/bidi_controls_check.py -> no bidirectional controls x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrc tests/startup_chad_check.cpp -> no errors x86_64-w64-mingw32-g++ -std=c++17 -fsyntax-only -Isrc src/startup_stubs/chad.cpp -> only the two __try/__except errors of local_log.h (cross toolchain), none in the changed lines git merge-tree --write-tree HEAD origin/main -> 0 conflicts g++ logparse.cpp (the counting loop on synthetic logs) -> table above ``` ## P3 - The old assertion `log_text.find("x2w-trap") == npos` is still there; it passes on an empty log, so it adds nothing next to the new ones (harmless). - A caller that passed a null or unwritable out-handle used to get 0 in permissive mode and now gets `E_POINTER`; no caller in the tree is affected, but a sentence in the PR description would record the change. - The non-permissive `null` and `valid` child cases both trap before `handle` is read, so they check the same thing twice. ## Limits Not built and not run on Windows. Please run `ctest -R startup_chad_check` (dev and release) or rely on the Windows CI job; my verdict rests on reading the code and on the log-parsing reproduction above. AI-assisted; commands listed were executed locally.
Ammmae0x commented 2026-11-24 21:01:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-24T20:03:19Z.


Updated tests/startup_chad_check.cpp to assert the positive -> 0 log entry, all three -> E_POINTER refusal log entries, and the permissive lifecycle export entries, and updated the header comment in src/startup_stubs/chad.cpp. Mutation check reverting return hr_pointer; to return result; failed all 3 negative controls (startup_chad_check: 3 failure(s)). Re-verified startup_chad_check in dev and release, plus docs_links_check.py, bidi_controls_check.py, and form_coverage_check.py. AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7142976997 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1871#issuecomment-7142976997). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-24T20:03:19Z. --- Updated `tests/startup_chad_check.cpp` to assert the positive `-> 0` log entry, all three `-> E_POINTER` refusal log entries, and the permissive lifecycle export entries, and updated the header comment in `src/startup_stubs/chad.cpp`. Mutation check reverting `return hr_pointer;` to `return result;` failed all 3 negative controls (`startup_chad_check: 3 failure(s)`). Re-verified `startup_chad_check` in `dev` and `release`, plus `docs_links_check.py`, `bidi_controls_check.py`, and `form_coverage_check.py`. AI-assisted.
Sign in to join this conversation.
No reviewers
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
R0Y00/Xbox2Windows!1864
No description provided.