startup: stop the stand-ins' first-calls log counters at their limit #1980

Merged
rasptum merged 1 commit from upstream-pr/1987 into main 2026-11-24 23:04:11 +00:00
rasptum commented 2026-11-24 22:37:05 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by rasptum on 2026-11-24T22:37:05Z.


The stand-ins limit many log lines to the first N calls with InterlockedIncrement(&counter) <= N, and x2w_local::first_calls in src/startup_stubs/local_log.h uses the same expression. The counter counts every call. A consumer that polls a status in a loop drives it past 2^31; it turns negative, and from then on every call is logged again. In a local run this produced millions of lines a minute and stalled the process.

  • x2w_local::first_calls now stops incrementing at the limit, using a compare-exchange loop with no lock. Output for the first N calls is unchanged.
  • These raw gates now use the helper: xcrdapi.cpp (13), xg_xs.cpp (3), d3d12_draw.inl (3), runtime_proxy.cpp (tracked_logged) and the X2W_D3D12_DUMP_PSO per-slot gate in d3d12_xs_device.cpp. The device's own first_calls forwards to the helper.
  • Three counters also feed statistics, so they keep counting and only log while the count is positive: render_draws, shown_frames and the runtime proxy's per-slot call_counts.
  • Left as they are:
    • shape_checks: it checks < 32 before incrementing, so it cannot wrap.
    • The checks & 63 periodic device check: the mask still works after a wrap.
    • Counters used as indexes, tokens or frame numbers: they are not log gates.
  • startup_local_log now expects the counter to stay at the limit (3, 0 for a zero or negative limit, exactly 10 under 4 threads). It also checks a counter set to 0x7FFFFFFE: with a limit of 4, none of 16 calls may pass.
  • The new negative control startup_local_log_wrap_control (--mutate wrap) runs that wrap case with the old counting expression. It passes only on the line FAIL first_calls passes no call at the wrap point, so any other failure makes the control fail.

A local check, not part of this change: putting the old expression back into the header made startup_local_log fail its saturation and wrap cases.

Test (both presets, throttled build of the touched targets, plus d3d12_xs_shim to compile the device file):

ctest --preset dev --no-tests=error -R '^(startup_local_log.*|startup_stubs|startup_acphal_apu_free|startup_dstorage_destination|startup_gameinput_(history|keys)|startup_heartbeat_commit|startup_storage_(alloc_failure|directory_batch|refusal_order|failure_counts.*)|startup_xapu_queue)$' -V
  • dev: 17 passed, 0 failed, 0 skipped (startup_local_log: 92 cases; startup_local_log_wrap_control passed on its expected FAIL line).
  • release: the same command with --preset release: 17 passed, 0 failed, 0 skipped.
  • tests/docs_links_check.py and tests/bidi_controls_check.py pass.

docs/startup-runtime.md (Local switches) describes the saturating gate and names the new control.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6592802497 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1987). Originally posted by [rasptum](https://github.com/rasptum) on 2026-11-24T22:37:05Z. --- The stand-ins limit many log lines to the first N calls with `InterlockedIncrement(&counter) <= N`, and `x2w_local::first_calls` in `src/startup_stubs/local_log.h` uses the same expression. The counter counts every call. A consumer that polls a status in a loop drives it past 2^31; it turns negative, and from then on every call is logged again. In a local run this produced millions of lines a minute and stalled the process. - `x2w_local::first_calls` now stops incrementing at the limit, using a compare-exchange loop with no lock. Output for the first N calls is unchanged. - These raw gates now use the helper: `xcrdapi.cpp` (13), `xg_xs.cpp` (3), `d3d12_draw.inl` (3), `runtime_proxy.cpp` (`tracked_logged`) and the `X2W_D3D12_DUMP_PSO` per-slot gate in `d3d12_xs_device.cpp`. The device's own `first_calls` forwards to the helper. - Three counters also feed statistics, so they keep counting and only log while the count is positive: `render_draws`, `shown_frames` and the runtime proxy's per-slot `call_counts`. - Left as they are: - `shape_checks`: it checks `< 32` before incrementing, so it cannot wrap. - The `checks & 63` periodic device check: the mask still works after a wrap. - Counters used as indexes, tokens or frame numbers: they are not log gates. - `startup_local_log` now expects the counter to stay at the limit (3, 0 for a zero or negative limit, exactly 10 under 4 threads). It also checks a counter set to `0x7FFFFFFE`: with a limit of 4, none of 16 calls may pass. - The new negative control `startup_local_log_wrap_control` (`--mutate wrap`) runs that wrap case with the old counting expression. It passes only on the line `FAIL first_calls passes no call at the wrap point`, so any other failure makes the control fail. A local check, not part of this change: putting the old expression back into the header made `startup_local_log` fail its saturation and wrap cases. Test (both presets, throttled build of the touched targets, plus `d3d12_xs_shim` to compile the device file): ```powershell ctest --preset dev --no-tests=error -R '^(startup_local_log.*|startup_stubs|startup_acphal_apu_free|startup_dstorage_destination|startup_gameinput_(history|keys)|startup_heartbeat_commit|startup_storage_(alloc_failure|directory_batch|refusal_order|failure_counts.*)|startup_xapu_queue)$' -V ``` - dev: 17 passed, 0 failed, 0 skipped (`startup_local_log`: 92 cases; `startup_local_log_wrap_control` passed on its expected FAIL line). - release: the same command with `--preset release`: 17 passed, 0 failed, 0 skipped. - `tests/docs_links_check.py` and `tests/bidi_controls_check.py` pass. `docs/startup-runtime.md` (Local switches) describes the saturating gate and names the new control. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ChazY11 commented 2026-11-24 23:03:41 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by ChazY11 on 2026-11-24T22:48:41Z.


Built and ran it; both ways back to the bug are caught

Windows 11 build 26300.9974, MSVC 19.44, dev preset:

cmake --build --preset dev --target startup_local_log_check    # exit 0
ctest --preset dev -R 'local_log'
1/5 startup_local_log ......................... Passed 3.03
2/5 startup_local_log_guard_drop_control ...... Passed 3.04
3/5 startup_local_log_reopen_control .......... Passed 0.73
4/5 startup_local_log_cache_failure_control ... ***Not Run
5/5 startup_local_log_wrap_control ............ Passed 0.03

startup_local_log_cache_failure_control did not run because its executable was not in my build selection — my environment, not the PR. The
four that ran are green, and the log-flood output the fix is about is visible in their output:

recovery child: 1144774 lines on an unusable path in 1000 ms took 4 open attempts

~1.1 million lines per second on the unusable path is the rate that made the counter matter; the fix keeps that burst to the configured
limit instead of letting it resume at 2^31.

Mutations

mutation in src/startup_stubs/local_log.h result
first_calls back to return InterlockedIncrement(&counter) <= limit; (the original bug) caught
the compare-exchange loop bound seen < limit → seen <= limit (off-by-one at the cap) caught

The first is the important one: restoring the exact pre-change expression makes the fixture fail, so the regression it prevents is pinned
rather than assumed. The second shows the cap is the limit, not "somewhere near it" — an off-by-one would have let one extra line through
and left the counter at limit + 1, which is the same class of runaway the fix exists to stop, just slower.

Why the helper and not the call sites

The same expression appeared at 13 + 3 + 3 + 1 + 1 sites. Capping it once in first_calls is the only version that cannot drift, and the
call sites that were converted are listed in the description so a reviewer can check the count rather than trust it. The three counters that
also feed statistics (render_draws, shown_frames, runtime proxy slot counts) keeping their full count while logging only while positive
is the right split: a counter that stops counting breaks the statistic, a counter that keeps counting breaks the gate.

What is unverified

Nothing process-level beyond this fixture: the 2^31 turn is exercised by the counter arithmetic, not by driving a real provider past two
billion calls. Whether a title's own polling loop hits the same path is a claim about the provider, not about this header.

Verdict

Reviewed. The bug is real, the fix is in the one place that cannot be forgotten, and both the old expression and an off-by-one fail the
fixture.

Notes

  • AI-assisted review.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7146671410 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1987#issuecomment-7146671410). Originally posted by [ChazY11](https://github.com/ChazY11) on 2026-11-24T22:48:41Z. --- ## Built and ran it; both ways back to the bug are caught Windows 11 build 26300.9974, MSVC 19.44, `dev` preset: ```powershell cmake --build --preset dev --target startup_local_log_check # exit 0 ctest --preset dev -R 'local_log' 1/5 startup_local_log ......................... Passed 3.03 2/5 startup_local_log_guard_drop_control ...... Passed 3.04 3/5 startup_local_log_reopen_control .......... Passed 0.73 4/5 startup_local_log_cache_failure_control ... ***Not Run 5/5 startup_local_log_wrap_control ............ Passed 0.03 ``` `startup_local_log_cache_failure_control` did not run because its executable was not in my build selection — my environment, not the PR. The four that ran are green, and the log-flood output the fix is about is visible in their output: ```text recovery child: 1144774 lines on an unusable path in 1000 ms took 4 open attempts ``` ~1.1 million lines per second on the unusable path is the rate that made the counter matter; the fix keeps that burst to the configured limit instead of letting it resume at 2^31. ## Mutations | mutation in `src/startup_stubs/local_log.h` | result | |---|---| | `first_calls` back to `return InterlockedIncrement(&counter) <= limit;` (the original bug) | **caught** | | the compare-exchange loop bound `seen < limit` → `seen <= limit` (off-by-one at the cap) | **caught** | The first is the important one: restoring the exact pre-change expression makes the fixture fail, so the regression it prevents is pinned rather than assumed. The second shows the cap is the *limit*, not "somewhere near it" — an off-by-one would have let one extra line through and left the counter at `limit + 1`, which is the same class of runaway the fix exists to stop, just slower. ## Why the helper and not the call sites The same expression appeared at 13 + 3 + 3 + 1 + 1 sites. Capping it once in `first_calls` is the only version that cannot drift, and the call sites that were converted are listed in the description so a reviewer can check the count rather than trust it. The three counters that also feed statistics (`render_draws`, `shown_frames`, runtime proxy slot counts) keeping their full count while logging only while positive is the right split: a counter that stops counting breaks the statistic, a counter that keeps counting breaks the gate. ## What is unverified Nothing process-level beyond this fixture: the 2^31 turn is exercised by the counter arithmetic, not by driving a real provider past two billion calls. Whether a title's own polling loop hits the same path is a claim about the provider, not about this header. ## Verdict Reviewed. The bug is real, the fix is in the one place that cannot be forgotten, and both the old expression and an off-by-one fail the fixture. ## Notes - AI-assisted review.
upstream-sync closed this pull request 2026-11-25 00:04:48 +00:00
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!1980
No description provided.