runtime: harden submit_phases period parsing and skip Scope overhead when disabled #2031

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

@⁠IlyassRS3 on GitHub · 2026-11-25 18:35 UTC · original
IlyassRS3:fix/rt-submit-phases-period-parse-and-zero-overhead-scope → main · +133 −10 · 3 files · 1 commits · open

Coordination item: RT-SUBMIT-PHASES-PERIOD-PARSE-AND-ZERO-OVERHEAD-SCOPE (claimed on #1608).

Why

  1. leonida::submit_phases::period_ns() parsed LEONIDA_SUBMIT_PHASES_SECONDS with std::strtoull and multiplied std::int64_t(s) * 1000000000 unchecked. A negative value like -1 wrapped to UINT64_MAX and yielded period_ns() == -1000000000, making now - last < period permanently false so submitted() printed to stderr on every single submission; huge decimal strings triggered signed 64-bit overflow UB; non-numeric strings silently yielded 0 (disabling logging) instead of falling back to the 5 s default.
  2. submit_phases::Scope unconditionally ran two steady_clock::now() calls and two atomic RMWs (fetch_add + compare_exchange_weak) on every submission phase (wait_room, heaps, shadows, geometry, textures) even when LEONIDA_SUBMIT_PHASES_SECONDS=0 turned the periodic report off.
  3. add(Phase, ns) did not bounds-check p < kCount, Scope::~Scope() cast now_ns() - start directly to uint64_t without guarding negative deltas, and process_age() subtracted FILETIME ticks without guarding clock step-back.

Changes

  • runtime/src/submit_phases.h:
    • Add pure helper parse_period_ns(const char*) that handles 0 (disable), positive decimal seconds with safe saturation at INT64_MAX / 1e9, and falls back to the 5 s default on null, empty, negative, or malformed input.
    • Skip steady_clock::now() and add() inside Scope when period_ns() <= 0, and guard dt > 0 in ~Scope().
    • Guard p >= kCount in add(), period <= 0 in submitted(), and n >= c in process_age().
  • runtime/test/submit_phases_test.cpp + runtime/CMakeLists.txt: add portable pure-stdlib submit_phases CTest covering period parsing edge cases, saturation, accumulation, phase bounds, and Scope/submitted().

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind).
  • GPUs tested: none (CPU-only portable unit tests & A/B benchmark on macOS arm64).
  • Compiled and ran runtime/test/submit_phases_test.cpp with clang++ -std=c++20 -Wall -Wextra -Werror:
    • ASan+UBSan (-O1 -g -fsanitize=address,undefined, with unset, 0, and -1): submit_phases_test: OK (0 failures).
    • TSan (-O1 -g -fsanitize=thread): submit_phases_test: OK (0 failures).
    • Release (-O2): submit_phases_test: OK (0 failures).
  • Negative control (LEONIDA_SUBMIT_PHASES_SECONDS=-1): main returned period_ns=-1000000000; this branch returns period_ns=5000000000 (5 s default).
  • A/B -O2 benchmark (LEONIDA_SUBMIT_PHASES_SECONDS=0, 3,000,000 Scope lifetimes):
    • main: 82.255 ms (ns_heaps=66940813)
    • this branch: 1.522 ms (ns_heaps=0, 54.04× faster, zero clock/atomic overhead when disabled).
  • python3 -I scripts/check_no_game_data.py runtime/src/submit_phases.h runtime/test/submit_phases_test.cpp runtime/CMakeLists.txt: clean.
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK.
**@⁠IlyassRS3** on GitHub · 2026-11-25 18:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2031) `IlyassRS3:fix/rt-submit-phases-period-parse-and-zero-overhead-scope` → `main` · +133 −10 · 3 files · 1 commits · open Coordination item: `RT-SUBMIT-PHASES-PERIOD-PARSE-AND-ZERO-OVERHEAD-SCOPE` (claimed on #1608). ### Why 1. `leonida::submit_phases::period_ns()` parsed `LEONIDA_SUBMIT_PHASES_SECONDS` with `std::strtoull` and multiplied `std::int64_t(s) * 1000000000` unchecked. A negative value like `-1` wrapped to `UINT64_MAX` and yielded `period_ns() == -1000000000`, making `now - last < period` permanently false so `submitted()` printed to `stderr` on every single submission; huge decimal strings triggered signed 64-bit overflow UB; non-numeric strings silently yielded `0` (disabling logging) instead of falling back to the 5 s default. 2. `submit_phases::Scope` unconditionally ran two `steady_clock::now()` calls and two atomic RMWs (`fetch_add` + `compare_exchange_weak`) on every submission phase (`wait_room`, `heaps`, `shadows`, `geometry`, `textures`) even when `LEONIDA_SUBMIT_PHASES_SECONDS=0` turned the periodic report off. 3. `add(Phase, ns)` did not bounds-check `p < kCount`, `Scope::~Scope()` cast `now_ns() - start` directly to `uint64_t` without guarding negative deltas, and `process_age()` subtracted `FILETIME` ticks without guarding clock step-back. ### Changes - `runtime/src/submit_phases.h`: - Add pure helper `parse_period_ns(const char*)` that handles `0` (disable), positive decimal seconds with safe saturation at `INT64_MAX / 1e9`, and falls back to the 5 s default on null, empty, negative, or malformed input. - Skip `steady_clock::now()` and `add()` inside `Scope` when `period_ns() <= 0`, and guard `dt > 0` in `~Scope()`. - Guard `p >= kCount` in `add()`, `period <= 0` in `submitted()`, and `n >= c` in `process_age()`. - `runtime/test/submit_phases_test.cpp` + `runtime/CMakeLists.txt`: add portable pure-stdlib `submit_phases` CTest covering period parsing edge cases, saturation, accumulation, phase bounds, and `Scope`/`submitted()`. ### Verification - AI-assisted: yes (Antigravity agent by Google DeepMind). - GPUs tested: none (CPU-only portable unit tests & A/B benchmark on macOS arm64). - Compiled and ran `runtime/test/submit_phases_test.cpp` with `clang++ -std=c++20 -Wall -Wextra -Werror`: - ASan+UBSan (`-O1 -g -fsanitize=address,undefined`, with unset, `0`, and `-1`): `submit_phases_test: OK (0 failures)`. - TSan (`-O1 -g -fsanitize=thread`): `submit_phases_test: OK (0 failures)`. - Release (`-O2`): `submit_phases_test: OK (0 failures)`. - Negative control (`LEONIDA_SUBMIT_PHASES_SECONDS=-1`): `main` returned `period_ns=-1000000000`; this branch returns `period_ns=5000000000` (5 s default). - A/B `-O2` benchmark (`LEONIDA_SUBMIT_PHASES_SECONDS=0`, 3,000,000 `Scope` lifetimes): - `main`: `82.255 ms` (`ns_heaps=66940813`) - this branch: `1.522 ms` (`ns_heaps=0`, **54.04× faster**, zero clock/atomic overhead when disabled). - `python3 -I scripts/check_no_game_data.py runtime/src/submit_phases.h runtime/test/submit_phases_test.cpp runtime/CMakeLists.txt`: clean. - `python3 -I tests/test_text_write_line_endings.py`: 3/3 OK.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:15:24 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 18:35 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 18:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2031#pullrequestreview-6062978277) · **reviewed** 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.
This pull request has changes conflicting with the target branch.
  • runtime/src/submit_phases.h
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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