runtime: guard DescriptorTable 9-way collision, read_slots width, and change-tracking OOM #1739

Merged
IlyassRS3 merged 4 commits from upstream-pr/1739 into main 2026-11-29 15:42:16 +00:00
IlyassRS3 commented 2026-11-25 10:37:37 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T10:37:37Z.


Item ID: RT-DESCRIPTOR-TABLE-GUARDS
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7159904197

Summary

Hardens runtime/src/descriptor_table.h (the bindless set 0, binding 2 T#/S# hash table) against three edge-case bugs:

  1. Unresolvable 9-way full-hash collision checked before load-factor growth (insert_locked()): previously, (occupied_count_ + 1) * 2 > entries_.size() called grow_locked() before checking whether all 8 probe slots already held keys with full hash h. Inserting a 9th colliding key into a 16-slot table needlessly doubled capacity (16 -> 32) and set all_changed_ = true before throwing std::length_error. Checking the 8-slot full-hash collision first refuses immediately without mutating capacity or dirty state.
  2. 64-bit slot offset arithmetic in read_slots(): widened dest + std::size_t(i) * 10 and entries_[std::size_t(first) + i] so large counts cannot wrap 32-bit uint32_t arithmetic.
  3. Strong exception safety for dirty-slot tracking & skip redundant tracking under all_changed_:
    • Previously mark(slot) pushed to changed_slots_ after mutating entries_ and occupied_count_ (and before --occupied_count_ in erase_locked()). A std::bad_alloc inside changed_slots_.push_back left CPU entries mutated (and occupied_count_ stale on erase) while dropping the dirty slot from take_changes(), desynchronizing the GPU descriptor table from the CPU table.
    • reserve_marks(count) now reserves changed_slots_ capacity before mutating entries_/occupied_count_ (and before make_probe_slot() relocates a resident), and both reserve_marks() and mark() are no-ops when all_changed_ is already set (since the next BindlessSet upload writes the full table).

Verification

  • AI-assisted: yes
  • GPUs tested: none (portable CPU-only C++20 header and unit test)
  • Commands run (macOS, Apple Clang, C++20):
    • clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=undefined runtime/test/descriptor_table_test.cpp -pthread: PASS (0 failures).
    • clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/descriptor_table_test.cpp -pthread: PASS (0 failures).
    • Mutation test running the updated descriptor_table_test.cpp against unpatched main:runtime/src/descriptor_table.h: FAILS as expected (exit 1 on test_unresolvable_collision_does_not_grow_at_load_threshold).
    • python3 -I scripts/check_no_game_data.py runtime/src/descriptor_table.h runtime/test/descriptor_table_test.cpp: PASS.
    • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5489676694 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1739). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T10:37:37Z. --- Item ID: `RT-DESCRIPTOR-TABLE-GUARDS` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7159904197 ## Summary Hardens `runtime/src/descriptor_table.h` (the bindless set 0, binding 2 T#/S# hash table) against three edge-case bugs: 1. **Unresolvable 9-way full-hash collision checked before load-factor growth** (`insert_locked()`): previously, `(occupied_count_ + 1) * 2 > entries_.size()` called `grow_locked()` before checking whether all 8 probe slots already held keys with full hash `h`. Inserting a 9th colliding key into a 16-slot table needlessly doubled capacity (`16 -> 32`) and set `all_changed_ = true` before throwing `std::length_error`. Checking the 8-slot full-hash collision first refuses immediately without mutating capacity or dirty state. 2. **64-bit slot offset arithmetic in `read_slots()`**: widened `dest + std::size_t(i) * 10` and `entries_[std::size_t(first) + i]` so large counts cannot wrap 32-bit `uint32_t` arithmetic. 3. **Strong exception safety for dirty-slot tracking & skip redundant tracking under `all_changed_`**: - Previously `mark(slot)` pushed to `changed_slots_` *after* mutating `entries_` and `occupied_count_` (and before `--occupied_count_` in `erase_locked()`). A `std::bad_alloc` inside `changed_slots_.push_back` left CPU entries mutated (and `occupied_count_` stale on erase) while dropping the dirty slot from `take_changes()`, desynchronizing the GPU descriptor table from the CPU table. - `reserve_marks(count)` now reserves `changed_slots_` capacity before mutating `entries_`/`occupied_count_` (and before `make_probe_slot()` relocates a resident), and both `reserve_marks()` and `mark()` are no-ops when `all_changed_` is already set (since the next `BindlessSet` upload writes the full table). ## Verification - **AI-assisted**: yes - **GPUs tested**: none (portable CPU-only C++20 header and unit test) - **Commands run** (macOS, Apple Clang, C++20): - `clang++ -std=c++20 -Wall -Wextra -Werror -O1 -g -fsanitize=undefined runtime/test/descriptor_table_test.cpp -pthread`: **PASS** (`0 failures`). - `clang++ -std=c++20 -Wall -Wextra -Werror -O2 runtime/test/descriptor_table_test.cpp -pthread`: **PASS** (`0 failures`). - Mutation test running the updated `descriptor_table_test.cpp` against unpatched `main:runtime/src/descriptor_table.h`: **FAILS as expected** (`exit 1` on `test_unresolvable_collision_does_not_grow_at_load_threshold`). - `python3 -I scripts/check_no_game_data.py runtime/src/descriptor_table.h runtime/test/descriptor_table_test.cpp`: **PASS**. - `python3 -I tests/test_text_write_line_endings.py`: **3/3 OK**.
copilot-pull-request-reviewer[bot] commented 2026-11-25 10:37:52 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T10:37:52Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6055994886 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1739#pullrequestreview-6055994886). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T10:37:52Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
potatoe0100 commented 2026-11-25 10:57:20 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by potatoe0100 on 2026-11-25T10:57:20Z.


Windows/MSVC check: f41be1995 merged onto main bcaa13dad (clean), native Windows 11 Pro 26H2 (build 26300), MSVC 19.44 (VS 2022) RelWithDebInfo + Ninja, NVIDIA GeForce RTX 4060, driver 621.14.

  • Builds cleanly; descriptor_table passes.
  • Full ctest -j 6: 409 of 409 passed, same as main.
  • run_gpu_tests.py: 209/263 passed, 53 skipped, identical to main (the one failure, cs_imgatomic_ext, is on main too).

AI-assisted testing (non-developer tester).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7160394543 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/1739#issuecomment-7160394543). Originally posted by [potatoe0100](https://github.com/potatoe0100) on 2026-11-25T10:57:20Z. --- Windows/MSVC check: `f41be1995` merged onto `main` `bcaa13dad` (clean), native Windows 11 Pro 26H2 (build 26300), MSVC 19.44 (VS 2022) RelWithDebInfo + Ninja, NVIDIA GeForce RTX 4060, driver 621.14. - Builds cleanly; `descriptor_table` passes. - Full `ctest -j 6`: 409 of 409 passed, same as `main`. - `run_gpu_tests.py`: 209/263 passed, 53 skipped, identical to `main` (the one failure, `cs_imgatomic_ext`, is on `main` too). AI-assisted testing (non-developer tester).
abnalstlll-byte commented 2026-11-25 10:58:27 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by abnalstlll-byte on 2026-11-25T10:58:27Z.

Review: APPROVED


### Clean-Room & Technical Verification

  • Mergeability: 3-way merge against upstream main (bcaa13dad) is 100% clean with zero merge conflicts.
  • Clean-Room Compliance: 100% clean-room. Descriptor table data structures and unit tests only; zero retail binaries, game data, or keys (check_no_game_data.py clean).
  • Technical Rigor & Edge-Case Hardening:
    • Checks for 9-way unresolvable full-hash collisions before evaluating load-factor growth, preventing spurious table growth (16 -> 32) and unnecessary all_changed_ dirty flags prior to throwing std::length_error.
    • Widens slot offset arithmetic in read_slots() to 64-bit (std::size_t), safeguarding against 32-bit integer overflow.
    • Guarantees strong exception safety in dirty-slot tracking by reserving capacity via reserve_marks() before modifying entries_ or occupied_count_, eliminating CPU/GPU state desynchronization under OOM.
    • Skips redundant individual slot tracking when all_changed_ is already active.
    • Comprehensive unit test coverage added in runtime/test/descriptor_table_test.cpp with OOM fault injection testing.

LGTM! Approved.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6056317627 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1739#pullrequestreview-6056317627). Originally posted by [abnalstlll-byte](https://github.com/abnalstlll-byte) on 2026-11-25T10:58:27Z. **Review:** APPROVED --- ### Clean-Room & Technical Verification - **Mergeability**: 3-way merge against upstream `main` (`bcaa13dad`) is 100% clean with zero merge conflicts. - **Clean-Room Compliance**: 100% clean-room. Descriptor table data structures and unit tests only; zero retail binaries, game data, or keys (`check_no_game_data.py` clean). - **Technical Rigor & Edge-Case Hardening**: - Checks for 9-way unresolvable full-hash collisions *before* evaluating load-factor growth, preventing spurious table growth (`16 -> 32`) and unnecessary `all_changed_` dirty flags prior to throwing `std::length_error`. - Widens slot offset arithmetic in `read_slots()` to 64-bit (`std::size_t`), safeguarding against 32-bit integer overflow. - Guarantees strong exception safety in dirty-slot tracking by reserving capacity via `reserve_marks()` before modifying `entries_` or `occupied_count_`, eliminating CPU/GPU state desynchronization under OOM. - Skips redundant individual slot tracking when `all_changed_` is already active. - Comprehensive unit test coverage added in `runtime/test/descriptor_table_test.cpp` with OOM fault injection testing. LGTM! Approved.
MoonEyes commented 2026-11-28 02:51:04 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T02:51:04Z.


Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/src/descriptor_table.h, runtime/test/descriptor_table_test.cpp), so it carries the needs rebase label. CONTRIBUTING.md asks authors to rebase their branch on main; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here.

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7232634078 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1739#issuecomment-7232634078). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T02:51:04Z. --- Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/src/descriptor_table.h`, `runtime/test/descriptor_table_test.cpp`), so it carries the `needs rebase` label. CONTRIBUTING.md asks authors to rebase their branch on `main`; once it merges cleanly the label goes within the hour, and reviews start with the pull requests that do. Could you update the branch when you have a moment? If you no longer plan to continue with it, please say so here. AI-assisted (Claude Code, Claude Opus 5.5).
OreoFR commented 2026-11-28 21:53:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:53:59Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head f41be1995 (tested at f41be1995, head is now b6b41e4c5)
  • Test: not run (merge conflict with main)
  • Red on main: not run
  • Green with the PR: not run
  • Verdict: CONFLICT (runtime/src/descriptor_table.h and runtime/test/descriptor_table_test.cpp conflict with main)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246353924 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1739#issuecomment-7246353924). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:53:59Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `f41be1995` (tested at `f41be1995`, head is now `b6b41e4c5`) - Test: not run (merge conflict with main) - Red on main: not run - Green with the PR: not run - Verdict: CONFLICT (runtime/src/descriptor_table.h and runtime/test/descriptor_table_test.cpp conflict with main) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
zmasi commented 2026-11-28 23:25:45 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T23:25:45Z.


Windows 11 Native Verification Report (b6b41e4c51 merged onto origin/main adfb3821a2)

  • Merge Status: Clean merge onto origin/main (adfb3821a2) and clean in a 5-PR runtime batch merge.
  • Environment: Windows 11 (10.0.26200, x64), MSVC 19.44.35228.

Diff Audit (2 files, +183/-11)

  • runtime/src/descriptor_table.h:
    1. read_slots 64-bit indexing (descriptor_table.h:285-289): promotes first + i and i * 10 to std::size_t, preventing 32-bit uint32_t overflow when serializing >= 429,496,730 slots (>= 2^29 capacity tables).
    2. 9-way full-hash collision ordering (descriptor_table.h:369-384): checks whether all 8 probe slots already hold distinct keys with the exact same 32-bit hash before (occupied_count_ + 1) * 2 > entries_.size() triggers grow_locked(), preventing futile table doubling and all_changed_ invalidation at the 50% load threshold.
    3. Dirty-tracking elision & OOM strong exception guarantee (descriptor_table.h:312-319, 357, 386, 419): skips changed_slots_.push_back while all_changed_ is already set, and calls reserve_marks(...) prior to mutating entries_ or occupied_count_ across insert, replace, 8-probe resident relocation (reserve_marks(2)), and erase so std::bad_alloc cannot leave CPU entries_ and GPU dirty tracking out of sync.
  • runtime/test/descriptor_table_test.cpp: adds ScopedAllocFailure operator new OOM fault-injection tests, 50%-load 9-way collision tests (fnv1a and fnv1a_fmix32), and all_changed_ slot-tracking elision/resumption checks.

Red -> Green Verification

  1. Red (origin/main adfb3821a2):
    • #1739's runtime/test/descriptor_table_test.cpp compiled against origin/main descriptor_table.h exited 1:
      assertion failed: table.capacity() == 16 && table.size() == 8 (test_unresolvable_collision_does_not_grow_at_load_threshold, where origin/main grew capacity from 16 to 32 and set all_changed_ before throwing std::length_error).
  2. Green (#1739 + origin/main):
    • descriptor_table_test.exe (/O2 /EHsc /W4 /permissive- /utf-8, 0 new warnings): descriptor_table_test: all tests passed (0 failures) (0.07 s, rc=0).

Verdict: PASS — ready to merge.

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7247424286 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1739#issuecomment-7247424286). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T23:25:45Z. --- ### Windows 11 Native Verification Report (`b6b41e4c51` merged onto `origin/main` `adfb3821a2`) - **Merge Status**: Clean merge onto `origin/main` (`adfb3821a2`) and clean in a 5-PR runtime batch merge. - **Environment**: Windows 11 (`10.0.26200`, x64), MSVC `19.44.35228`. #### Diff Audit (`2` files, `+183/-11`) - `runtime/src/descriptor_table.h`: 1. **`read_slots` 64-bit indexing (`descriptor_table.h:285-289`)**: promotes `first + i` and `i * 10` to `std::size_t`, preventing 32-bit `uint32_t` overflow when serializing `>= 429,496,730` slots (`>= 2^29` capacity tables). 2. **9-way full-hash collision ordering (`descriptor_table.h:369-384`)**: checks whether all 8 probe slots already hold distinct keys with the exact same 32-bit hash *before* `(occupied_count_ + 1) * 2 > entries_.size()` triggers `grow_locked()`, preventing futile table doubling and `all_changed_` invalidation at the 50% load threshold. 3. **Dirty-tracking elision & OOM strong exception guarantee (`descriptor_table.h:312-319, 357, 386, 419`)**: skips `changed_slots_.push_back` while `all_changed_` is already set, and calls `reserve_marks(...)` prior to mutating `entries_` or `occupied_count_` across insert, replace, 8-probe resident relocation (`reserve_marks(2)`), and erase so `std::bad_alloc` cannot leave CPU `entries_` and GPU dirty tracking out of sync. - `runtime/test/descriptor_table_test.cpp`: adds `ScopedAllocFailure` `operator new` OOM fault-injection tests, 50%-load 9-way collision tests (`fnv1a` and `fnv1a_fmix32`), and `all_changed_` slot-tracking elision/resumption checks. #### Red -> Green Verification 1. **Red (`origin/main` `adfb3821a2`)**: - `#1739`'s `runtime/test/descriptor_table_test.cpp` compiled against `origin/main` `descriptor_table.h` exited `1`: `assertion failed: table.capacity() == 16 && table.size() == 8` (`test_unresolvable_collision_does_not_grow_at_load_threshold`, where `origin/main` grew capacity from `16` to `32` and set `all_changed_` before throwing `std::length_error`). 2. **Green (`#1739` + `origin/main`)**: - `descriptor_table_test.exe` (`/O2 /EHsc /W4 /permissive- /utf-8`, `0` new warnings): **`descriptor_table_test: all tests passed (0 failures)`** (`0.07 s`, `rc=0`). **Verdict**: **PASS** — ready to merge. AI-assisted.
MoonEyes commented 2026-11-29 15:42:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-29T15:42:19Z.


Merged. Tested by @zmasi on head b6b41e4c5 (CPU only (Windows 11, MSVC)): red: PR descriptor_table_test vs main header fails capacity()==16 (grows to 32); green all tests pass. The CI jobs that are red on this head were also red on main at the time of the run. Thanks @IlyassRS3!

AI-assisted: yes

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7258300237 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1739#issuecomment-7258300237). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-29T15:42:19Z. --- Merged. Tested by @zmasi on head b6b41e4c5 (CPU only (Windows 11, MSVC)): red: PR descriptor_table_test vs main header fails capacity()==16 (grows to 32); green all tests pass. The CI jobs that are red on this head were also red on main at the time of the run. Thanks @IlyassRS3! AI-assisted: yes
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
ProjectLeonida/mandy!1739
No description provided.