runtime: guard stack region wrap/underflow and null inputs in thread_stacks #2058
No reviewers
No assignees
Labels
No labels
bug
dependencies
documentation
enhancement
missing claim
needs rebase
needs-triage
question
ready-for-agent
submodules
No project
No milestone
Dependencies
No dependencies set
Notifications
1 participant
Due date
No due date set.
Reference
leonida/mandy!2058
Loading…
Reference in a new issue
No description provided.
Delete branch "rt/thread-stacks-bounds-and-wrap-guards"
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?
Pull request successfully merged and closed
Claim ID:
RT-THREAD-STACKS-BOUNDS-AND-WRAP-GUARDSSummary
Hardens stack-region arithmetic, pointer bounds checks, and null-argument handling in
runtime/src/thread_stacks.cpp:top - stack.originalunderflow incopy_context_and_stack(): Checksregion.RegionSize > 0 && region.RegionSize <= UINT64_MAX - baseandstack.original >= base && top > stack.originalbefore computingtop - stack.originaland callingReadProcessMemory. Previously, a zero/wrappingRegionSizeortop <= stack.originalunderflowedtop - stack.originalnearUINT64_MAXand requested an 8 MiBReadProcessMemory.in_copy()check inwalk_copy(): Rewritesaddress - stack.copy + sizeof(DWORD64) <= stack.sizeasstack.size >= sizeof(DWORD64) && address >= stack.copy && address - stack.copy <= stack.size - sizeof(DWORD64)so addition cannot wrap.StackSampler::sample()now returns0immediately on nullthreadorframes;start_thread_stacks()checksGetModuleHandleWnon-null beforeGetProcAddress;thread_label()clampswcslen(description)toINT_MAXbefore casting toint.runtime/test/thread_stacks_test.cppwith null-handle / null-framesStackSampler::sample()checks andparse_stack_timeszero/overflow (4294967296) token checks.Verification
runtime/src/thread_stacks.cppwith minimal Win32 stubs; Windows MSVC CI run ofthread_stackspending)clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -Wall -Wextra -Werror: PASS (failures=0)clang++ -std=c++20 -O1 -g -fsanitize=thread -Wall -Wextra -Werror: PASS (failures=0)clang++ -std=c++20 -O2 -Wall -Wextra -Werror: PASS (failures=0)main: failed bothtop <= stack.originalunderflow andBaseAddress + RegionSizewraparound assertions (ReadProcessMemorycalled with bogus length,main_exit=1).python3 -I scripts/check_no_game_data.pyclean;python3 -I tests/test_text_write_line_endings.py(3/3 OK).Verified on Windows 11 (
10.0.26200.9974) with MSVC19.44.35228(x64,/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) againstmain4a54aedcb+0bba07bf1(claim#2570#issuecomment-7238486276):runtime/src/thread_stacks.cpp,runtime/test/thread_stacks_test.cpp); guardsbase + RegionSizewraparound andstack.original >= base && top > stack.originalincopy_context_and_stack, avoids underflow/overflow inin_copy, clampswcslentoINT_MAX, guards nullthread/framesinStackSampler::sample, and checkskernel32/ntdllmodule handles beforeGetProcAddress.check_no_game_data.pyclean.4a54aedcb+ PR test):thread_stacks_test.exehangs/self-suspends (TIMEOUT) becausesampler.sample(GetCurrentThread(), nullptr)lacks the!framesguard and entersSuspendThread(GetCurrentThread()).4a54aedcb+0bba07bf1merge): Auto-merges cleanly (rc=0), compiles with0/W4warnings, andthread_stacks_test.exepasses (13 checks, 0 failures,300/300live thread stack samples,rc=0).AI-assisted.
Red/green: On main thread_stacks_test times out after 60 s (it loops on a stack region that wraps around), and with the PR ctest -R "^thread_stacks$" passes in 0.1 s.
Independent tester (OreoFR, RTX 4070, Windows 11): PR head merged with the other PRs of its batch on main c1ecf04ed, full ctest run (485 tests, tests that launch the game excluded): no new failure compared with main.
Verified by OreoFR
c1ecf04ed+ PR head0bba07bf1; green run in a merge package of 16 PRs, no new ctest failure vs mainctest -R "^thread_stacks$"AI-assisted (Claude Code, Claude Opus 5.5).