indirect_draw_stats: guard null FILE* in report and out-of-range Event in note/skip #2309

Merged
leonida/mandy
narekshaghoyandev merged 1 commit from ubiq-indirect-draw-stats-guards into main 2026-11-29 14:26:31 +00:00

Pull request successfully merged and closed

narekshaghoyandev commented 2026-11-26 13:40:49 +00:00

Fixes null-pointer and out-of-bounds bugs in runtime/src/indirect_draw_stats.h:

  • Guard Counts::report(std::FILE* out) against out == nullptr before calling std::fprintf(out, ...).
  • Bounds-check static_cast<unsigned>(event) < 4u in note(Event) and skip(Event, Reason) so an out-of-range Event cannot index past Counts::values[4] or record a stray reason skip.
  • Added standalone unit test runtime/test/indirect_draw_stats_test.cpp.

Verification

  • g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/indirect_draw_stats_test.cpp passed
  • g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/indirect_draw_stats_test.cpp passed
  • Negative control: new test failed with UBSan null-argument + ASan SEGV against unmodified ubiq/main
  • python3 -I scripts/check_no_game_data.py runtime/src/indirect_draw_stats.h runtime/test/indirect_draw_stats_test.cpp clean

Claimed in the coordination issue as: RT-INDIRECT-DRAW-STATS-BOUNDS-AND-NULL (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193658432)
AI-assisted: yes (Gemini)

Fixes null-pointer and out-of-bounds bugs in `runtime/src/indirect_draw_stats.h`: - Guard `Counts::report(std::FILE* out)` against `out == nullptr` before calling `std::fprintf(out, ...)`. - Bounds-check `static_cast<unsigned>(event) < 4u` in `note(Event)` and `skip(Event, Reason)` so an out-of-range `Event` cannot index past `Counts::values[4]` or record a stray reason skip. - Added standalone unit test `runtime/test/indirect_draw_stats_test.cpp`. ### Verification - `g++ -std=c++20 -D_GLIBCXX_DEBUG -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/indirect_draw_stats_test.cpp` passed - `g++ -std=c++20 -fsanitize=address,undefined -Wall -Wextra -Werror -pedantic -Iruntime/src runtime/test/indirect_draw_stats_test.cpp` passed - Negative control: new test failed with UBSan null-argument + ASan SEGV against unmodified `ubiq/main` - `python3 -I scripts/check_no_game_data.py runtime/src/indirect_draw_stats.h runtime/test/indirect_draw_stats_test.cpp` clean Claimed in the coordination issue as: RT-INDIRECT-DRAW-STATS-BOUNDS-AND-NULL (https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7193658432) AI-assisted: yes (Gemini)
wavehs reviewed 2026-11-26 19:43:03 +00:00
wavehs left a comment
No description provided.
**macOS arm64 check of #2309** (indirect_draw_stats guards). Portable header plus its CPU test; runs here without a GPU. Tested head `25aed3fcb` merged onto `main` `bdbc7e923` as merge `9d8a3484d` (clean, no conflicts). Environment: Apple M3 Pro arm64, macOS 27.2.1, Apple clang 21.0.0, CMake 4.5.0, CPython 3.15.2. **On the merge** - Compiling the new test directly (`clang++ -std=c++20 -I runtime ... runtime/test/indirect_draw_stats_test.cpp`) and running it prints `indirect_draw_stats_test: ok`, exit 0. The valid-skip path formats `record_skips=1 [topology=1]`, and `report(nullptr)` is a silent no-op. - The three guards are the right ones: `Counts::report` checks `out`, `note` rejects `Event >= 4` before indexing `values[]`, and `skip` rejects an out-of-range `Event` as well as `Reason` (it then calls `note(event)`, so without the first check the second would still index `values[event]`). **Negative control (test kept, `runtime/src/indirect_draw_stats.h` reverted to `main`)** - Plain build: run exits 139 (SIGSEGV) at `local.report(nullptr)`. - To show the other guard is real, I reverted the header and re-applied only the `if (!out) return;` line, then rebuilt with `-fsanitize=address`: `AddressSanitizer: global-buffer-overflow` at `main+0x528`, i.e. `note(Event(99))` writing `values[99]` past the 4-element array. So the `Event` check is load-bearing, and like the `memoryTypeCount` case in #2308 it only diagnoses itself under a sanitizer once the earlier crash is fixed. **Finding: the new test is never built or run - it is not in `runtime/CMakeLists.txt`** `runtime/test/indirect_draw_stats_test.cpp` is new, but `runtime/CMakeLists.txt` has no target or `add_test` for it (`grep` finds nothing; `ctest -N` has no `indirect_draw_stats` entry). Every test in `runtime/test` is registered explicitly, so this file is dead in ctest, the Windows runtime job and Wine. `tests/test_ctest_registration.py` still passes, because it only checks that a target which exists has an `add_test`; it cannot see a `.cpp` with no target at all. This is the second PR in this batch with the same shape (#2333 has `vulkan_memory_budget_test.cpp` unregistered the same way); I filed the guard gap in the coordination issue (#1608) as `finding: tests/test_ctest_registration.py cannot see a test file that was never given a CMake target`. For this PR the fix is the usual three lines: ``` add_executable(indirect_draw_stats_test test/indirect_draw_stats_test.cpp) target_link_libraries(indirect_draw_stats_test PRIVATE Vulkan::Headers) # not even needed; header is standalone add_test(NAME indirect_draw_stats COMMAND indirect_draw_stats_test) ``` and it then runs on every platform. **Nits**: the `4u` in `note`/`skip` duplicates the array size (`values[4]`); a named constant would keep them in step if another event is added. Same for the test's `Event(4)`/`Event(99)` literals, which is fine as a regression value. **Not run**: the D3D12 path that consumes these counters (Windows-only); the header itself is stage-independent and no GPU is needed. `python -I scripts/check_no_game_data.py` on both changed files: clean. No game files. AI-assisted.
zmasi commented 2026-11-28 12:16:05 +00:00

VERIFY-2309 — verified head 25aed3fcb1 merged cleanly with main d48a957bb on Windows 11 Pro (build 26200), MSVC 19.44 (x64, /std:c++20 /W4 /WX /permissive- /utf-8 /EHsc), synthetic indirect-draw counters only.

Red (main d48a957bb) -> Green (d48a957bb + 25aed3fcb1)

  • Red on main:
    • Compiling and running runtime/test/indirect_draw_stats_test.cpp against main's runtime/src/indirect_draw_stats.h -> FAILED with 0xC0000409 (STATUS_STACK_BUFFER_OVERRUN / UCRT invalid-parameter fastfail) when local.report(nullptr) passed a null FILE* to std::fprintf.
  • Green on merged PR:
    • cl.exe /nologo /std:c++20 /W4 /WX /permissive- /utf-8 /EHsc /I"runtime/src" runtime/test/indirect_draw_stats_test.cpp -> 0 warnings, exit 0 (indirect_draw_stats_test: ok), confirming both report(nullptr) no-op behavior and out-of-range Event (>= 4) bounds guards in note() / skip().
  • Non-blocking Windows UCRT note: runtime/test/indirect_draw_stats_test.cpp uses std::tmpfile(), which on Windows UCRT opens a file in the root of the current working drive (passes on writable non-system drives, though non-elevated runs rooted on a protected system drive root can get nullptr).

AI-assisted.

`VERIFY-2309` — verified head `25aed3fcb1` merged cleanly with `main` `d48a957bb` on Windows 11 Pro (build 26200), MSVC `19.44` (`x64`, `/std:c++20 /W4 /WX /permissive- /utf-8 /EHsc`), synthetic indirect-draw counters only. ### Red (`main` `d48a957bb`) -> Green (`d48a957bb` + `25aed3fcb1`) - **Red on `main`**: - Compiling and running `runtime/test/indirect_draw_stats_test.cpp` against `main`'s `runtime/src/indirect_draw_stats.h` -> **FAILED** with **`0xC0000409` (`STATUS_STACK_BUFFER_OVERRUN` / UCRT invalid-parameter fastfail)** when `local.report(nullptr)` passed a null `FILE*` to `std::fprintf`. - **Green on merged PR**: - `cl.exe /nologo /std:c++20 /W4 /WX /permissive- /utf-8 /EHsc /I"runtime/src" runtime/test/indirect_draw_stats_test.cpp` -> **0 warnings**, exit **`0`** (`indirect_draw_stats_test: ok`), confirming both `report(nullptr)` no-op behavior and out-of-range `Event` (`>= 4`) bounds guards in `note()` / `skip()`. - **Non-blocking Windows UCRT note**: `runtime/test/indirect_draw_stats_test.cpp` uses `std::tmpfile()`, which on Windows UCRT opens a file in the root of the current working drive (passes on writable non-system drives, though non-elevated runs rooted on a protected system drive root can get `nullptr`). AI-assisted.
OreoFR commented 2026-11-28 22:01:46 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 25aed3fcb; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: standalone build of indirect_draw_stats_test.exe (the test is not registered in CMake)
  • Red on main: crash 0xC0000409
  • Green with the PR: OK
  • Verdict: READY (the test is not in CMake: ctest will never run it)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `25aed3fcb`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `standalone build of indirect_draw_stats_test.exe (the test is not registered in CMake)` - Red on main: crash 0xC0000409 - Green with the PR: OK - Verdict: READY (the test is not in CMake: ctest will never run it) - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
TheGeogeo commented 2026-11-28 22:49:45 +00:00

Tested 25aed3fcb merged (no commit) with main aa918e5d9, MSVC 19.52 (Visual Studio 18), Windows 11 10.0.26300.

Blocker: runtime/test/indirect_draw_stats_test.cpp is new but is not registered in runtime/CMakeLists.txt, so neither CI nor ctest ever builds or runs it. ctest -N on the merge lists no such test, and tests/test_ctest_registration.py still passes, so nothing catches this. Registering it the way the other header-only tests are would fix that:

add_executable(indirect_draw_stats_test test/indirect_draw_stats_test.cpp)
add_test(NAME indirect_draw_stats COMMAND indirect_draw_stats_test)

Hand-compiled (cl /std:c++20 /EHsc /O2):

  • Green: indirect_draw_stats_test: ok, exit 0.
  • Red, with runtime/src/indirect_draw_stats.h back to main: crash, exit 0xC0000409.

I did not run the ctest tests relinked by the header change: test runs on this machine are paused for memory.

AI-assisted (Claude Code).

Tested `25aed3fcb` merged (no commit) with `main` `aa918e5d9`, MSVC 19.52 (Visual Studio 18), Windows 11 10.0.26300. **Blocker:** `runtime/test/indirect_draw_stats_test.cpp` is new but is not registered in `runtime/CMakeLists.txt`, so neither CI nor `ctest` ever builds or runs it. `ctest -N` on the merge lists no such test, and `tests/test_ctest_registration.py` still passes, so nothing catches this. Registering it the way the other header-only tests are would fix that: ```cmake add_executable(indirect_draw_stats_test test/indirect_draw_stats_test.cpp) add_test(NAME indirect_draw_stats COMMAND indirect_draw_stats_test) ``` Hand-compiled (`cl /std:c++20 /EHsc /O2`): - Green: `indirect_draw_stats_test: ok`, exit 0. - Red, with `runtime/src/indirect_draw_stats.h` back to `main`: crash, exit `0xC0000409`. I did not run the ctest tests relinked by the header change: test runs on this machine are paused for memory. AI-assisted (Claude Code).
timbot103 reviewed 2026-11-29 04:27:27 +00:00
timbot103 left a comment
No description provided.
Static audit of `25aed3fcb` against the base code. I ran no builds, tests, or GPU workloads. The guards in `runtime/src/indirect_draw_stats.h` are correct for the current enum on MSVC and Linux. `Event` is a scoped enum with a fixed, signed `int` underlying type, so `static_cast<Event>(-1)` is defined and converts to `UINT_MAX` when checked as `unsigned`. The comparison against `4u` matches the four counter slots, and `Event` has no `Count` sentinel. `skip` rejects invalid events before logging or incrementing a reason counter. `report(nullptr)` returns before formatting or calling `fprintf`, and the overlay reader in `runtime/src/dev_overlay.h` indexes the counters with named enumerators. - Please register `runtime/test/indirect_draw_stats_test.cpp` in the common `BUILD_TESTING` block at `runtime/CMakeLists.txt:861`. The Windows and Linux CTest jobs currently omit it, so the new regression cases receive no coverage in those jobs. - At `runtime/test/indirect_draw_stats_test.cpp:24`, use `reason_tag(Reason::reason_count)` to check the `"unknown"` fallback. `Reason` is an unfixed enum with a value range of 0 through 31, so casting 999 to it is undefined before the fallback can run. AI-assisted.
MoonEyes commented 2026-11-29 14:26:34 +00:00

Merged. Tested by OreoFR (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11) on this head: indirect_draw_stats_test red on main (crash 0xC0000409), green with the PR. Note: the test is not registered in CMake. AI-assisted: yes.

Merged. Tested by OreoFR (RTX 4070 Laptop 8 GB, NVIDIA 621.40, Windows 11) on this head: indirect_draw_stats_test red on main (crash 0xC0000409), green with the PR. Note: the test is not registered in CMake. AI-assisted: yes.
Sign in to join this conversation.
No description provided.