runtime: keep new thread stacks out of the 2^40 boundary window #3458

Merged
Not-Saii merged 1 commit from upstream-pr/3458 into main 2026-11-29 14:17:30 +00:00
Not-Saii commented 2026-11-29 13:59:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Not-Saii on 2026-11-29T13:59:18Z.


Change

xmem_startup_below_40 failed once in the Debug job of main at 0614f68c: the boundary allocation was refused with error 8 after 4 blocker reservations (check_failed: boundary_allocated). I could not reproduce that failure on its own, so this is hardening, not a confirmed fix. Refs #3426.

check_boundary() reserves every free range below 2^40 except the 4 MiB window [2^40 - 4 MiB, 2^40), then asks xmem.dll for exactly 4 MiB there. Anything else that lands in the window between those two steps makes the call fail with that output.

What I measured: if the fixture starts a thread right after the window is set up, the thread's stack lands at the base of the window, and the test fails 200 of 200 times with the CI output. An unconstrained VirtualAlloc lands above 2^40 and doesn't interfere. What I did not measure: that any thread actually starts in this process at that moment on CI. Locally the process has exactly one thread at start, at the boundary setup and at exit (1000 runs under load). A loader or thread-pool worker on the slower runner is a hypothesis.

The test now leaves eight free ranges of 4 MiB - 64 KiB directly below the window, separated by reserved 64 KiB. A new stack takes one of those first. None of them can hold a 2 MiB-aligned block of 4 MiB or more, so the checks are unchanged: 4 MiB + 64 KiB is still refused, and the 4 MiB block must still land exactly at 2^40 - 4 MiB. The LowRangeBlockers constructor now takes a list of holes instead of one; check_exhausted still passes none. The runtime is unchanged.

Verification

Measured at base 212d4b5f with the change as b16519ab. The branch is rebased onto e3f9a64f without conflicts (6f10a2be); main has not changed the below-2^40 path since. Windows 11 build 26300; Windows SDK 10.0.26100. Builds: dev (Debug) and release (/O2) presets.

Each loop runs ctest --preset <dev|release> --no-tests=error -R '^xmem_startup_below_40$' a fixed number of times and counts failures. "load 24" adds 24 busy-loop processes on a 24-thread CPU. "2 cores" pins ctest, the test and 4 busy-loop processes to affinity mask 0x3.

xmem_startup_below_40, failures out of 1000:

Build Condition Before (212d4b5f) After
Debug idle 0 0
Debug load 24 0 0
Debug 2 cores 0 0
/O2 idle 0 0
/O2 load 24 0 0
/O2 2 cores 0 0

One after-fix run failed with error 32 loading xmem.dll because I relinked the DLL during the loop; it is excluded.

Forced control (Debug, 200 runs, an experiment-only block that starts N threads right after the blockers are reserved, not part of this PR):

Threads started Before After
1 200/200 fail, the CI output 0/200
12 not run 0/200

All five xmem_startup_below_40* tests pass in both presets with the change, including the mutant controls and xmem_startup_below_40_deployed_ignores_mutants.

Evidence and follow-up

If the CI failure came from something other than a new thread stack, this change does not cover it. The other two failures in #3426 are separate; the retire one is #3457.

AI-assisted.

🤖 Generated with Claude Code

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:5551428769 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3458). Originally posted by [Not-Saii](https://github.com/Not-Saii) on 2026-11-29T13:59:18Z. --- ## Change `xmem_startup_below_40` failed once in the Debug job of main at `0614f68c`: the boundary allocation was refused with error 8 after 4 blocker reservations (`check_failed: boundary_allocated`). I could not reproduce that failure on its own, so this is hardening, not a confirmed fix. Refs #3426. `check_boundary()` reserves every free range below 2^40 except the 4 MiB window `[2^40 - 4 MiB, 2^40)`, then asks `xmem.dll` for exactly 4 MiB there. Anything else that lands in the window between those two steps makes the call fail with that output. What I measured: if the fixture starts a thread right after the window is set up, the thread's stack lands at the base of the window, and the test fails 200 of 200 times with the CI output. An unconstrained `VirtualAlloc` lands above 2^40 and doesn't interfere. What I did not measure: that any thread actually starts in this process at that moment on CI. Locally the process has exactly one thread at start, at the boundary setup and at exit (1000 runs under load). A loader or thread-pool worker on the slower runner is a hypothesis. The test now leaves eight free ranges of 4 MiB - 64 KiB directly below the window, separated by reserved 64 KiB. A new stack takes one of those first. None of them can hold a 2 MiB-aligned block of 4 MiB or more, so the checks are unchanged: 4 MiB + 64 KiB is still refused, and the 4 MiB block must still land exactly at `2^40 - 4 MiB`. The `LowRangeBlockers` constructor now takes a list of holes instead of one; `check_exhausted` still passes none. The runtime is unchanged. ## Verification Measured at base `212d4b5f` with the change as `b16519ab`. The branch is rebased onto `e3f9a64f` without conflicts (`6f10a2be`); main has not changed the below-2^40 path since. Windows 11 build 26300; Windows SDK 10.0.26100. Builds: `dev` (Debug) and `release` (/O2) presets. Each loop runs `ctest --preset <dev|release> --no-tests=error -R '^xmem_startup_below_40$'` a fixed number of times and counts failures. "load 24" adds 24 busy-loop processes on a 24-thread CPU. "2 cores" pins ctest, the test and 4 busy-loop processes to affinity mask `0x3`. `xmem_startup_below_40`, failures out of 1000: | Build | Condition | Before (`212d4b5f`) | After | |---|---|---|---| | Debug | idle | 0 | 0 | | Debug | load 24 | 0 | 0 | | Debug | 2 cores | 0 | 0 | | /O2 | idle | 0 | 0 | | /O2 | load 24 | 0 | 0 | | /O2 | 2 cores | 0 | 0 | One after-fix run failed with error 32 loading `xmem.dll` because I relinked the DLL during the loop; it is excluded. **Forced control** (Debug, 200 runs, an experiment-only block that starts N threads right after the blockers are reserved, not part of this PR): | Threads started | Before | After | |---|---|---| | 1 | 200/200 fail, the CI output | 0/200 | | 12 | not run | 0/200 | All five `xmem_startup_below_40*` tests pass in both presets with the change, including the mutant controls and `xmem_startup_below_40_deployed_ignores_mutants`. ## Evidence and follow-up If the CI failure came from something other than a new thread stack, this change does not cover it. The other two failures in #3426 are separate; the retire one is #3457. AI-assisted. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
JohnConnorNPC commented 2026-11-29 14:16:18 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by JohnConnorNPC on 2026-11-29T14:16:18Z.


Review of head 6f10a2be (one commit on main e3f9a64f). git merge-tree --write-tree origin/main 6f10a2be against main 0ed385dc exits 0. The diff is in tests/xmem_startup_below_40_check.cpp only. The runtime is unchanged. X2W_XMEM_STARTUP_BELOW_40 stays opt-in: run.ps1 -StartupBelow40, run.sh --startup-below-40, and startup_below_40 in the base run preset.

Semantics

  • decoys_below(hole, 8) makes eight ranges [hole - k·4 MiB, hole - (k-1)·4 MiB - 64 KiB). Each is 4 MiB - 64 KiB. The blockers reserve the 64 KiB above each range, including the 64 KiB just below the window, and everything below the lowest one. So no free range below 2^40 other than the window can hold 4 MiB, aligned or not. boundary_overflow_* (4 MiB + 64 KiB refused) and boundary_last_byte (4 MiB placed exactly at 2^40 - 4 MiB) test the same thing as before.
  • The new hole loop in LowRangeBlockers handles a hole that overlaps a region's start or end: reserve(start, hole) is a no-op when hole <= start, and so is the final reserve(start, stop) when hole_end >= stop. check_exhausted passes no holes and is unchanged.
  • The CMake expectations for the three X2W_XMEM_TEST_BELOW_40 mutants only name exhausted_* checks. Under Wine they also fail boundary_overflow_*, with the same check names at both heads (table below). So the decoys hide nothing the overflow check caught before.

Measured under Wine (Linux, Proton Experimental wine-11.0, private prefix). The test was built with clang-cl 19 (/O2 /MT) from main and from this head. xmem.dll came from a cross build of main, and the mutants used a cross-built xmem-test-controls DLL. In both DLLs the below-2^40 code matches main. "Forced" is the control from your body: one CreateThread right after boundary_setup, waited until the thread runs.

Variant main test this head
unforced 0/200 fail 0/200 fail
forced, 1 thread 50/50 fail, check_failed: boundary_allocated (error 8 after 4 blocker reservations, as in CI) 0/50 fail
mutant no_limit / no_limit+no_check / no_limit+no_release 2 / 6 / 4 failed checks the same check names

So Wine places a new thread's stack the way your Windows run did. In the main test it lands in the window. Its region starts at 0xFFFFC00000 (1 MiB committed), with the stack at 0xFFFFD00000. With the decoys it lands in the lowest decoy (0xFFFDD00000), and the 4 MiB block still goes to 0xFFFFC00000.

MUST: none.

NIT

  1. The decoys hold a bounded number of threads, and the comment above decoys_below doesn't say so. Under Wine each new thread takes 2 MiB (1 MiB committed, then the 1 MiB stack), split across the decoys. With 12 forced threads the test passes. With 13 the 13th lands in the window (allocation base 0xFFFFD00000) and boundary_allocated fails again. You measured 12 on Windows. One line such as "about a dozen thread stacks fit" would tell the next reader when this hardening runs out. Optional.
  2. The subject runtime: keep new thread stacks … reads like a runtime change. The change is test-only. tests: would match how this repo labels test-only commits. Optional.

CI note: CI on 6f10a2be is green. But the Windows job runs the 82-test pr preset, and its log has no xmem_startup_below_40 line (grep -c gives 0). So CI did not run this test. Your Windows loops and the Wine runs above are the evidence. There are no WARP reports on the PR yet (this test doesn't need a GPU).

No reviews or change requests. No binary files, no title-specific inputs.

Merging.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7257173422 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/3458#issuecomment-7257173422). Originally posted by [JohnConnorNPC](https://github.com/JohnConnorNPC) on 2026-11-29T14:16:18Z. --- Review of head `6f10a2be` (one commit on `main` `e3f9a64f`). `git merge-tree --write-tree origin/main 6f10a2be` against `main` `0ed385dc` exits 0. The diff is in `tests/xmem_startup_below_40_check.cpp` only. The runtime is unchanged. `X2W_XMEM_STARTUP_BELOW_40` stays opt-in: `run.ps1 -StartupBelow40`, `run.sh --startup-below-40`, and `startup_below_40` in the `base` run preset. **Semantics** - `decoys_below(hole, 8)` makes eight ranges `[hole - k·4 MiB, hole - (k-1)·4 MiB - 64 KiB)`. Each is 4 MiB - 64 KiB. The blockers reserve the 64 KiB above each range, including the 64 KiB just below the window, and everything below the lowest one. So no free range below 2^40 other than the window can hold 4 MiB, aligned or not. `boundary_overflow_*` (4 MiB + 64 KiB refused) and `boundary_last_byte` (4 MiB placed exactly at `2^40 - 4 MiB`) test the same thing as before. - The new hole loop in `LowRangeBlockers` handles a hole that overlaps a region's start or end: `reserve(start, hole)` is a no-op when `hole <= start`, and so is the final `reserve(start, stop)` when `hole_end >= stop`. `check_exhausted` passes no holes and is unchanged. - The CMake expectations for the three `X2W_XMEM_TEST_BELOW_40` mutants only name `exhausted_*` checks. Under Wine they also fail `boundary_overflow_*`, with the same check names at both heads (table below). So the decoys hide nothing the overflow check caught before. **Measured under Wine** (Linux, Proton Experimental `wine-11.0`, private prefix). The test was built with clang-cl 19 (`/O2 /MT`) from `main` and from this head. `xmem.dll` came from a cross build of `main`, and the mutants used a cross-built `xmem-test-controls` DLL. In both DLLs the below-2^40 code matches `main`. "Forced" is the control from your body: one `CreateThread` right after `boundary_setup`, waited until the thread runs. | Variant | `main` test | this head | |---|---|---| | unforced | 0/200 fail | 0/200 fail | | forced, 1 thread | 50/50 fail, `check_failed: boundary_allocated` (error 8 after 4 blocker reservations, as in CI) | 0/50 fail | | mutant `no_limit` / `no_limit+no_check` / `no_limit+no_release` | 2 / 6 / 4 failed checks | the same check names | So Wine places a new thread's stack the way your Windows run did. In the `main` test it lands in the window. Its region starts at `0xFFFFC00000` (1 MiB committed), with the stack at `0xFFFFD00000`. With the decoys it lands in the lowest decoy (`0xFFFDD00000`), and the 4 MiB block still goes to `0xFFFFC00000`. **MUST:** none. **NIT** 1. The decoys hold a bounded number of threads, and the comment above `decoys_below` doesn't say so. Under Wine each new thread takes 2 MiB (1 MiB committed, then the 1 MiB stack), split across the decoys. With 12 forced threads the test passes. With 13 the 13th lands in the window (allocation base `0xFFFFD00000`) and `boundary_allocated` fails again. You measured 12 on Windows. One line such as "about a dozen thread stacks fit" would tell the next reader when this hardening runs out. Optional. 2. The subject `runtime: keep new thread stacks …` reads like a runtime change. The change is test-only. `tests:` would match how this repo labels test-only commits. Optional. **CI note:** CI on `6f10a2be` is green. But the Windows job runs the 82-test `pr` preset, and its log has no `xmem_startup_below_40` line (`grep -c` gives 0). So CI did not run this test. Your Windows loops and the Wine runs above are the evidence. There are no WARP reports on the PR yet (this test doesn't need a GPU). No reviews or change requests. No binary files, no title-specific inputs. Merging. 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!3458
No description provided.