[PR #2031] runtime: harden submit_phases period parsing and skip Scope overhead when disabled #2038

Open
opened 2026-11-26 21:19:49 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2031
Author: @IlyassRS3
Created: 11/25/2026
Status: 🔄 Open

Base: main ← Head: fix/rt-submit-phases-period-parse-and-zero-overhead-scope


📝 Commits (1)

  • ed34b99 runtime: harden submit_phases period parsing and skip Scope overhead when disabled

📊 Changes

3 files changed (+133 additions, -10 deletions)

View changed files

📝 runtime/CMakeLists.txt (+3 -0)
📝 runtime/src/submit_phases.h (+41 -10)
➕ runtime/test/submit_phases_test.cpp (+89 -0)

📄 Description

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/2031 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-submit-phases-period-parse-and-zero-overhead-scope` --- ### 📝 Commits (1) - [`ed34b99`](https://github.com/ubiquitarianism/leonida/commit/ed34b996ce2aef1ae2800b0f0840ca6878b89b73) runtime: harden submit_phases period parsing and skip Scope overhead when disabled ### 📊 Changes **3 files changed** (+133 additions, -10 deletions) <details> <summary>View changed files</summary> 📝 `runtime/CMakeLists.txt` (+3 -0) 📝 `runtime/src/submit_phases.h` (+41 -10) ➕ `runtime/test/submit_phases_test.cpp` (+89 -0) </details> ### 📄 Description 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#2038
No description provided.