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

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

@⁠narekshaghoyandev on GitHub · 2026-11-26 13:40 UTC · original
narekshaghoyandev:ubiq-indirect-draw-stats-guards → main · +58 −2 · 2 files · 1 commits · open

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 (#1608)
AI-assisted: yes (Gemini)

**@⁠narekshaghoyandev** on GitHub · 2026-11-26 13:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2309) `narekshaghoyandev:ubiq-indirect-draw-stats-guards` → `main` · +58 −2 · 2 files · 1 commits · open 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://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608) AI-assisted: yes (Gemini)
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 13:44:31 +00:00
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 19:43 UTC · original · reviewed

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.

**@⁠wavehs** on GitHub · 2026-11-26 19:43 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2309#pullrequestreview-6079687932) · **reviewed** **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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/2309/head:github-bot/gh-2309'
git switch 'github-bot/gh-2309'

Merge

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