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

Merged
leonida/mandy
quietfold merged 1 commit from fix/rt-submit-phases-period-parse-and-zero-overhead-scope into main 2026-11-30 17:15:37 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 18:35:21 +00:00

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.
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.
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 18:35:33 +00:00
copilot-pull-request-reviewer[bot] left a comment
No description provided.
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.
MoonEyes commented 2026-11-28 02:51:52 +00:00

Hi @IlyassRS3, thanks for this pull request. It currently conflicts with main (conflicting file(s): runtime/src/submit_phases.h), 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).

Hi @IlyassRS3, thanks for this pull request. It currently conflicts with `main` (conflicting file(s): `runtime/src/submit_phases.h`), 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-30 13:40:37 +00:00

Carried as #2738 because of the conflict with main after #2566; @IlyassRS3, if you come back, say so and the carry is dropped.

Carried as #2738 because of the conflict with main after #2566; @IlyassRS3, if you come back, say so and the carry is dropped.
Sign in to join this conversation.
No description provided.