Loader: add O(1) envelope fast-reject, packed RangeEntry lookup, and synchronized registration in modules #2673

Open
github-bot wants to merge 1 commit from github-bot/gh-2673 into main AGit
Owner

@⁠Decodere on GitHub · 2026-11-26 05:45 UTC · original
Decodere:fix-modules-range-table-fast-reject-and-sync → main · +212 −24 · 1 files · 1 commits · open

What is in it

Improves lock-free module address lookup performance, fixes high-address overflow bugs, and eliminates a registration/unregistration race in crates/xcl/src/modules.rs:

  • O(1) envelope fast-reject and packed RangeEntry table (RangeTable): Replaced split RANGE_BASE / RANGE_SIZE / RANGE_COUNT statics with a self-contained RangeTable packing #[repr(C)] struct RangeEntry { base: AtomicUsize, size: AtomicUsize } per slot and maintaining a monotonic [min_addr, max_inclusive] envelope. Out-of-envelope probes (such as PC D3D12/driver callers in 0x7ff... during guarded COM calls or VEH dispatch) reject in O(1) without scanning all registered slots, and in-envelope scans only load size when b != 0 && addr >= b.
  • Synchronized registration/unregistration and one-time saturation warning: register_mapped and unregister_mapped now mutate RANGES strictly while holding MAPPED.write(), preventing concurrent unregister_mapped calls from missing an in-flight register_mapped slot and leaving zombie entries. When MAX_RANGES (512) is reached, RangeTable::insert clamps count and signals a one-time xlog_warn! logged strictly after dropping MAPPED.write().
  • Overflow-safe containment checks: Replaced addr < self.base + self.size in MappedModule::contains with addr.checked_sub(self.base).is_some_and(|off| off < self.size), and replaced addr < b + s in RangeTable::lookup with addr - b < s (after addr >= b) and base.saturating_add(size - 1) for max_inclusive, avoiding debug overflow panics and release wraparound near usize::MAX.
  • Leaves describe_address, MAIN, and existing tests untouched.

Part of #1833

Tested

  • Failing-first debug verification on unmodified origin/main (b49be183, cargo test -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 2 passed; 1 failed; 0 ignored — contains_handles_base_plus_size_overflow panicked at modules.rs:34:37 (attempt to add with overflow).
  • Post-fix debug WARP targeted tests (XCL_ADAPTER=warp, XCL_WARP=1, cargo test -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 4 passed; 0 failed; 0 ignored; 0 skipped (0.00s, zero compiler warnings in modules.rs).
  • Post-fix release WARP targeted tests (XCL_ADAPTER=warp, XCL_WARP=1, cargo test --release -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 4 passed; 0 failed; 0 ignored; 0 skipped (0.00s).
    • Release micro-benchmark (200,000 out-of-envelope 0x7FF9_1234_5678 lookups against 32 registered modules on an isolated RangeTable): 14.11 ns -> 0.73 ns (19.3x faster, WARP run) / 16.02 ns -> 0.71 ns (22.6x faster, RTX run).
  • Clippy check (cargo clippy --all-targets): 0 new warnings in crates/xcl/src/modules.rs (only 1 pre-existing unusual_byte_groupings lint on untouched test literal 0x1000_0000_4).
  • Post-fix release RTX 4060 targeted tests (cargo test --release -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture): 4 passed; 0 failed; 0 ignored; 0 skipped (0.01s).

Regression run

  • Full release workspace on RTX 4060 (cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture): 37 test binaries, 1437 passed; 0 failed; 47 ignored, 29 pre-existing environment-gated SKIPPED lines:

    • 21 skipped: no wave64 (RTX 4060 is wave32)
    • 3 skipped: wave32 per-lane form: refused by driver (f16 arithmetic, f16 packed, f32 conversions)
    • 3 skipped: no D3D12 debug layer (d3d12SDKLayers.dll unavailable on host: debug-layer dred: false/true and uploads_start_from_each_subresources_state)
    • 1 skipped: pso_measurement_compiles_an_exact_synthetic_record (cannot disable driver cache on Windows)
    • 1 skipped: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows)
  • Paired 60 s RTX 4060 smoke capture (scripts/smoke.ps1):

Base (origin/main b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7):

xcl-smoke v1
commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1552 (PresentX 1552)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13970 MB

PR (8660a4593d51dfbf26abf9a41580d3e896ef1bb2):

xcl-smoke v1
commit: 8660a4593d51dfbf26abf9a41580d3e896ef1bb2
gpu: NVIDIA GeForce RTX 4060
title: 29640639
seconds: 60
exit: 0x0
frames shown: 1538 (PresentX 1538)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 13980 MB

Not tested

  • Live retail scenarios loading more than 512 modules (saturation behavior is verified on an isolated RangeTable::new() instance to avoid polluting the process-wide table).
  • The 29 host-gated workspace tests skipped on this RTX 4060 machine (wave64 hardware, wave32 per-lane driver support, D3D12 SDK debug layer, driver cache disable, and heap windows).

AI use

Google Antigravity (Gemini 4 Neon) wrote the patch, unit tests, and PR description; all commands, test runs, micro-benchmarks, and paired 60 s GPU smoke captures were executed directly in this workspace.

**@⁠Decodere** on GitHub · 2026-11-26 05:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/2673) `Decodere:fix-modules-range-table-fast-reject-and-sync` → `main` · +212 −24 · 1 files · 1 commits · open **What is in it** Improves lock-free module address lookup performance, fixes high-address overflow bugs, and eliminates a registration/unregistration race in `crates/xcl/src/modules.rs`: - **O(1) envelope fast-reject and packed `RangeEntry` table (`RangeTable`):** Replaced split `RANGE_BASE` / `RANGE_SIZE` / `RANGE_COUNT` statics with a self-contained `RangeTable` packing `#[repr(C)] struct RangeEntry { base: AtomicUsize, size: AtomicUsize }` per slot and maintaining a monotonic `[min_addr, max_inclusive]` envelope. Out-of-envelope probes (such as PC D3D12/driver callers in `0x7ff...` during guarded COM calls or VEH dispatch) reject in O(1) without scanning all registered slots, and in-envelope scans only load `size` when `b != 0 && addr >= b`. - **Synchronized registration/unregistration and one-time saturation warning:** `register_mapped` and `unregister_mapped` now mutate `RANGES` strictly while holding `MAPPED.write()`, preventing concurrent `unregister_mapped` calls from missing an in-flight `register_mapped` slot and leaving zombie entries. When `MAX_RANGES` (`512`) is reached, `RangeTable::insert` clamps `count` and signals a one-time `xlog_warn!` logged strictly after dropping `MAPPED.write()`. - **Overflow-safe containment checks:** Replaced `addr < self.base + self.size` in `MappedModule::contains` with `addr.checked_sub(self.base).is_some_and(|off| off < self.size)`, and replaced `addr < b + s` in `RangeTable::lookup` with `addr - b < s` (after `addr >= b`) and `base.saturating_add(size - 1)` for `max_inclusive`, avoiding debug overflow panics and release wraparound near `usize::MAX`. - Leaves `describe_address`, `MAIN`, and existing tests untouched. Part of #1833 **Tested** - **Failing-first debug verification** on unmodified `origin/main` (`b49be183`, `cargo test -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture`): `2 passed; 1 failed; 0 ignored` — `contains_handles_base_plus_size_overflow` panicked at `modules.rs:34:37` (`attempt to add with overflow`). - **Post-fix debug WARP targeted tests** (`XCL_ADAPTER=warp`, `XCL_WARP=1`, `cargo test -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture`): `4 passed; 0 failed; 0 ignored; 0 skipped` (`0.00s`, zero compiler warnings in `modules.rs`). - **Post-fix release WARP targeted tests** (`XCL_ADAPTER=warp`, `XCL_WARP=1`, `cargo test --release -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture`): `4 passed; 0 failed; 0 ignored; 0 skipped` (`0.00s`). - Release micro-benchmark (200,000 out-of-envelope `0x7FF9_1234_5678` lookups against 32 registered modules on an isolated `RangeTable`): `14.11 ns` -> `0.73 ns` (`19.3x` faster, WARP run) / `16.02 ns` -> `0.71 ns` (`22.6x` faster, RTX run). - **Clippy check** (`cargo clippy --all-targets`): `0` new warnings in `crates/xcl/src/modules.rs` (only 1 pre-existing `unusual_byte_groupings` lint on untouched test literal `0x1000_0000_4`). - **Post-fix release RTX 4060 targeted tests** (`cargo test --release -p xcl --lib "modules::tests::" -- --test-threads=1 --nocapture`): `4 passed; 0 failed; 0 ignored; 0 skipped` (`0.01s`). **Regression run** - **Full release workspace on RTX 4060** (`cargo test --release --workspace --no-fail-fast -- --test-threads=4 --nocapture`): `37` test binaries, `1437 passed; 0 failed; 47 ignored`, `29` pre-existing environment-gated `SKIPPED` lines: - `21` skipped: `no wave64` (RTX 4060 is wave32) - `3` skipped: wave32 `per-lane form: refused` by driver (`f16 arithmetic`, `f16 packed`, `f32 conversions`) - `3` skipped: no D3D12 debug layer (`d3d12SDKLayers.dll` unavailable on host: debug-layer `dred: false`/`true` and `uploads_start_from_each_subresources_state`) - `1` skipped: `pso_measurement_compiles_an_exact_synthetic_record` (cannot disable driver cache on Windows) - `1` skipped: `heap_window_out_of_memory_falls_back_to_the_switch_form` (no heap windows) - **Paired 60 s RTX 4060 smoke capture (`scripts/smoke.ps1`):** Base (`origin/main` `b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7`): ``` xcl-smoke v1 commit: b49be1836844fe0b0cdb3211dce3cb7fb4e21ac7 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1552 (PresentX 1552) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 13970 MB ``` PR (`8660a4593d51dfbf26abf9a41580d3e896ef1bb2`): ``` xcl-smoke v1 commit: 8660a4593d51dfbf26abf9a41580d3e896ef1bb2 gpu: NVIDIA GeForce RTX 4060 title: 29640639 seconds: 60 exit: 0x0 frames shown: 1538 (PresentX 1538) presents per 2 s (last 3): 60 60 60 device removals: 0 crashes: 0 peak commit: 13980 MB ``` **Not tested** - Live retail scenarios loading more than `512` modules (saturation behavior is verified on an isolated `RangeTable::new()` instance to avoid polluting the process-wide table). - The `29` host-gated workspace tests skipped on this RTX 4060 machine (wave64 hardware, wave32 per-lane driver support, D3D12 SDK debug layer, driver cache disable, and heap windows). **AI use** Google Antigravity (Gemini 4 Neon) wrote the patch, unit tests, and PR description; all commands, test runs, micro-benchmarks, and paired 60 s GPU smoke captures were executed directly in this workspace.
Replace split RANGE_BASE/RANGE_SIZE/RANGE_COUNT arrays with a packed
RangeTable maintaining a monotonic [min_addr, max_inclusive] envelope for
O(1) lock-free fast-reject of out-of-range callers (e.g. PC D3D12/driver
addresses), synchronize RangeTable insert/remove under MAPPED.write() so
unregister_mapped cannot leave zombie slots, warn once outside the lock on
MAX_RANGES saturation, and make MappedModule::contains and RangeTable
lookup overflow-safe against high base + size wraparound.

Part of #1833
Assisted-by: Google Antigravity (Gemini 4 Neon)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:15:09 +00:00
Author
Owner

@⁠luckyteagirl on GitHub · 2026-11-26 05:47 UTC · original · reviewed

Could the concurrency test drive overlapping registration and unregistration of the same base? Its writers own disjoint addresses, and each completes register_mapped before its own unregister_mapped, so it cannot exercise the split publication race this patch fixes. On main, a registration can publish RANGE_BASE, a concurrent same-base unregister can clear it before MAPPED.push, and registration can then leave find_mapped_by_base populated while image_base_for_addr returns None. A controlled same-base interleaving with an assertion that both lookups agree after the threads finish would catch that regression. Source-based review at exact head 8660a4593d51dfbf26abf9a41580d3e896ef1bb2; no runtime race test was run. AI-assisted review with Codex.

**@⁠luckyteagirl** on GitHub · 2026-11-26 05:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/2673#pullrequestreview-6069134094) · **reviewed** Could the concurrency test drive overlapping registration and unregistration of the same base? Its writers own disjoint addresses, and each completes `register_mapped` before its own `unregister_mapped`, so it cannot exercise the split publication race this patch fixes. On main, a registration can publish `RANGE_BASE`, a concurrent same-base unregister can clear it before `MAPPED.push`, and registration can then leave `find_mapped_by_base` populated while `image_base_for_addr` returns `None`. A controlled same-base interleaving with an assertion that both lookups agree after the threads finish would catch that regression. Source-based review at exact head `8660a4593d51dfbf26abf9a41580d3e896ef1bb2`; no runtime race test was run. AI-assisted review with Codex.
Author
Owner

@⁠SlopTart on GitHub · 2026-11-26 06:50 UTC · original · edited 2026-11-26 07:02 UTC

Tested on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b overrides, official DXC kit, VKD3D_CONFIG=ignore_shared_fence, threads=2), head 8660a459, mergeable against main b49be183:
non-gpu suite (--skip gpu::): 598 passed, 3 failed, 4 ignored, 733 filtered.
The failure set is identical to bare main b49be183 measured on the same rig the same day (596 passed, 3 failed): gpu_promoted_uav_clear (our #2490), xma2_source_voice (#2481, vendored-FAudio gate). The third red, the sparse tile video-memory test, is a known batch flake (green solo).
Zero delta from this PR; every test it adds passes inside the suite.
Not tested: gpu suite, Windows native.
Correction: an earlier version of this row reported three extra reds; those runs lacked the d3d12,d3d12core=n,b DLL overrides and hit stub-DLL NOTIMPL artifacts. Re-ran with the standard env, artifacts gone.
AI use: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.

**@⁠SlopTart** on GitHub · 2026-11-26 06:50 UTC · [original](https://github.com/xcl-dev/xcl/pull/2673#issuecomment-7184841641) · edited 2026-11-26 07:02 UTC **Tested** on the Linux/Wine lane, full standard env (AMD rig: Ryzen 9 9900X, 96 GB DDR5, RX 7900 XT 20 GB, Arch kernel 7.2.15, Mesa 26.2.5 RADV, wine-11.0 Proton, rustc 1.94.0; `d3d12,d3d12core,dxgi,d3d11,d3d10core=n,b` overrides, official DXC kit, `VKD3D_CONFIG=ignore_shared_fence`, threads=2), head `8660a459`, mergeable against main `b49be183`: non-gpu suite (`--skip gpu::`): **598 passed, 3 failed, 4 ignored, 733 filtered**. The failure set is identical to bare main `b49be183` measured on the same rig the same day (596 passed, 3 failed): gpu_promoted_uav_clear (our #2490), xma2_source_voice (#2481, vendored-FAudio gate). The third red, the sparse tile video-memory test, is a known batch flake (green solo). Zero delta from this PR; every test it adds passes inside the suite. **Not tested**: gpu suite, Windows native. **Correction**: an earlier version of this row reported three extra reds; those runs lacked the `d3d12,d3d12core=n,b` DLL overrides and hit stub-DLL NOTIMPL artifacts. Re-ran with the standard env, artifacts gone. **AI use**: Hermes agent (local Qwen-class) ran the tests and drafted this; I checked it before posting.
Author
Owner

@⁠sage-sage on GitHub · 2026-11-26 07:05 UTC · original

Test report: passes on NVIDIA. Exact head 8660a459 (its base is current main b49be18, so head = merged).

Tested

  • RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build
  • cargo test --workspace -- --test-threads=4: 1437 passed, 0 failed, 47 ignored; the 2 new tests pass
  • SKIPPED (29 lines, the same as on main on this machine): 21 wave64 tests (NVIDIA is wave32 only), 3 per-lane wave32 f16/f32 forms (refused), 3 D3D12 debug layer (d3d12SDKLayers.dll not installed), 1 no heap windows, 1 pso_measurement_compiles_an_exact_synthetic_record (Developer Mode off)

Not tested

  • No game, so not module registration on a title.

Claude Code (Claude Opus 5.5) ran the tests and wrote this report.

**@⁠sage-sage** on GitHub · 2026-11-26 07:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/2673#issuecomment-7185092972) **Test report: passes on NVIDIA.** Exact head `8660a459` (its base is current `main` `b49be18`, so head = merged). **Tested** - RTX 4080 SUPER (driver 621.40), Ryzen 5 7600X, Windows 11 26H2 build 26300.10075, rustc 1.100.0 MSVC, DXC 1.9.2602.17 (Windows SDK 10.0.26100), debug build - `cargo test --workspace -- --test-threads=4`: 1437 passed, 0 failed, 47 ignored; the 2 new tests pass - SKIPPED (29 lines, the same as on `main` on this machine): 21 wave64 tests (NVIDIA is wave32 only), 3 per-lane wave32 f16/f32 forms (refused), 3 D3D12 debug layer (`d3d12SDKLayers.dll` not installed), 1 `no heap windows`, 1 `pso_measurement_compiles_an_exact_synthetic_record` (Developer Mode off) **Not tested** - No game, so not module registration on a title. Claude Code (Claude Opus 5.5) ran the tests and wrote this report.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2673/head:github-bot/gh-2673'
git switch 'github-bot/gh-2673'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2673'
git switch 'github-bot/gh-2673'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2673'
git switch 'github-bot/gh-2673'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2673'
git switch 'main'
git merge --squash 'github-bot/gh-2673'
git switch 'main'
git merge --ff-only 'github-bot/gh-2673'
git switch 'main'
git merge 'github-bot/gh-2673'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.