runtime: harden submit_phases period parsing and skip Scope overhead when disabled #2031
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2031"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T18:35:21Z.
Coordination item:
RT-SUBMIT-PHASES-PERIOD-PARSE-AND-ZERO-OVERHEAD-SCOPE(claimed on #1608).Why
leonida::submit_phases::period_ns()parsedLEONIDA_SUBMIT_PHASES_SECONDSwithstd::strtoulland multipliedstd::int64_t(s) * 1000000000unchecked. A negative value like-1wrapped toUINT64_MAXand yieldedperiod_ns() == -1000000000, makingnow - last < periodpermanently false sosubmitted()printed tostderron every single submission; huge decimal strings triggered signed 64-bit overflow UB; non-numeric strings silently yielded0(disabling logging) instead of falling back to the 5 s default.submit_phases::Scopeunconditionally ran twosteady_clock::now()calls and two atomic RMWs (fetch_add+compare_exchange_weak) on every submission phase (wait_room,heaps,shadows,geometry,textures) even whenLEONIDA_SUBMIT_PHASES_SECONDS=0turned the periodic report off.add(Phase, ns)did not bounds-checkp < kCount,Scope::~Scope()castnow_ns() - startdirectly touint64_twithout guarding negative deltas, andprocess_age()subtractedFILETIMEticks without guarding clock step-back.Changes
runtime/src/submit_phases.h:parse_period_ns(const char*)that handles0(disable), positive decimal seconds with safe saturation atINT64_MAX / 1e9, and falls back to the 5 s default on null, empty, negative, or malformed input.steady_clock::now()andadd()insideScopewhenperiod_ns() <= 0, and guarddt > 0in~Scope().p >= kCountinadd(),period <= 0insubmitted(), andn >= cinprocess_age().runtime/test/submit_phases_test.cpp+runtime/CMakeLists.txt: add portable pure-stdlibsubmit_phasesCTest covering period parsing edge cases, saturation, accumulation, phase bounds, andScope/submitted().Verification
runtime/test/submit_phases_test.cppwithclang++ -std=c++20 -Wall -Wextra -Werror:-O1 -g -fsanitize=address,undefined, with unset,0, and-1):submit_phases_test: OK (0 failures).-O1 -g -fsanitize=thread):submit_phases_test: OK (0 failures).-O2):submit_phases_test: OK (0 failures).LEONIDA_SUBMIT_PHASES_SECONDS=-1):mainreturnedperiod_ns=-1000000000; this branch returnsperiod_ns=5000000000(5 s default).-O2benchmark (LEONIDA_SUBMIT_PHASES_SECONDS=0, 3,000,000Scopelifetimes):main:82.255 ms(ns_heaps=66940813)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.Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T18:35:33Z.
Review: COMMENTED
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.
Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T02:51:52Z.
Hi @IlyassRS3, thanks for this pull request. It currently conflicts with
main(conflicting file(s):runtime/src/submit_phases.h), so it carries theneeds rebaselabel. CONTRIBUTING.md asks authors to rebase their branch onmain; 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).
Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-30T13:40:37Z.
Carried as #2738 because of the conflict with main after #2566; @IlyassRS3, if you come back, say so and the carry is dropped.