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

Closed
Decodere wants to merge 1 commit from upstream-pr/2673 into main
Decodere commented 2026-11-26 05:45:06 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Decodere on 2026-11-26T05:45:06Z.


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.

<!-- greyfinch-sync:ivall/xcl:issue:5504727467 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2673). Originally posted by [Decodere](https://github.com/Decodere) on 2026-11-26T05:45:06Z. --- **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.
luckyteagirl commented 2026-11-26 05:47:00 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by luckyteagirl on 2026-11-26T05:47:00Z.

Review: COMMENTED


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.

<!-- greyfinch-sync:ivall/xcl:review:6069134094 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2673#pullrequestreview-6069134094). Originally posted by [luckyteagirl](https://github.com/luckyteagirl) on 2026-11-26T05:47:00Z. **Review:** COMMENTED --- 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.
SlopTart commented 2026-11-26 07:03:43 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-26T06:50:51Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7184841641 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2673#issuecomment-7184841641). Originally posted by [SlopTart](https://github.com/SlopTart) on 2026-11-26T06:50:51Z. --- **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.
sage-sage commented 2026-11-26 08:04:35 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by sage-sage on 2026-11-26T07:05:53Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7185092972 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2673#issuecomment-7185092972). Originally posted by [sage-sage](https://github.com/sage-sage) on 2026-11-26T07:05:53Z. --- **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.
Foniass commented 2026-12-01 08:39:10 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T08:39:10Z.


Triage (#1607) – head 8660a45

Main commit e1761585 ("Loader: reject out-of-module addresses in constant time", merged with #2959) says it is adapted from this PR. modules.rs on main has the same RangeTable with the min/max envelope, the overflow-safe contains, and range publication under the MAPPED write lock, plus a test that unregistering the same base waits for registration. git diff pr/2673 origin/main -- crates/xcl/src/modules.rs only adds lines.
Recommendation: close (superseded by main commit e1761585).

AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.

<!-- greyfinch-sync:ivall/xcl:comment:7305601115 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2673#issuecomment-7305601115). Originally posted by [Foniass](https://github.com/Foniass) on 2026-12-01T08:39:10Z. --- **Triage (#1607)** – head 8660a45 Main commit e1761585 ("Loader: reject out-of-module addresses in constant time", merged with #2959) says it is adapted from this PR. `modules.rs` on main has the same `RangeTable` with the min/max envelope, the overflow-safe `contains`, and range publication under the `MAPPED` write lock, plus a test that unregistering the same base waits for registration. `git diff pr/2673 origin/main -- crates/xcl/src/modules.rs` only adds lines. Recommendation: close (superseded by main commit e1761585). AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
throny commented 2026-12-01 12:57:34 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by throny on 2026-12-01T12:57:34Z.


Closing as superseded: this change is already on main (e1761585). all 154 added lines occur verbatim on main; e1761585 'Adapted from PR #2673' in modules.rs

Two independent checks found this: Foniass's close list on #1607 and ours against current main. If something here is still missing on main, say so and reopen it, or ask and I will.

AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

<!-- greyfinch-sync:ivall/xcl:comment:7311896178 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/2673#issuecomment-7311896178). Originally posted by [throny](https://github.com/throny) on 2026-12-01T12:57:34Z. --- Closing as superseded: this change is already on `main` (e1761585). all 154 added lines occur verbatim on main; e1761585 'Adapted from PR #2673' in modules.rs Two independent checks found this: Foniass's close list on #1607 and ours against current `main`. If something here is still missing on `main`, say so and reopen it, or ask and I will. AI use: Claude Code (Claude Opus 5.5); the per-PR comparison was done by Claude Sonnet 5.5 sub-agents.

Pull request closed

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
xcl-dev/xcl!2663
No description provided.