runtime: guard stack region wrap/underflow and null inputs in thread_stacks #2058

Merged
leonida/mandy
quietfold merged 1 commit from rt/thread-stacks-bounds-and-wrap-guards into main 2026-11-28 18:13:17 +00:00

Pull request successfully merged and closed

quietfold commented 2026-11-25 19:12:12 +00:00

Claim ID: RT-THREAD-STACKS-BOUNDS-AND-WRAP-GUARDS

Summary

Hardens stack-region arithmetic, pointer bounds checks, and null-argument handling in runtime/src/thread_stacks.cpp:

  1. Guard region wraparound and top - stack.original underflow in copy_context_and_stack(): Checks region.RegionSize > 0 && region.RegionSize <= UINT64_MAX - base and stack.original >= base && top > stack.original before computing top - stack.original and calling ReadProcessMemory. Previously, a zero/wrapping RegionSize or top <= stack.original underflowed top - stack.original near UINT64_MAX and requested an 8 MiB ReadProcessMemory.
  2. Overflow-free in_copy() check in walk_copy(): Rewrites address - stack.copy + sizeof(DWORD64) <= stack.size as stack.size >= sizeof(DWORD64) && address >= stack.copy && address - stack.copy <= stack.size - sizeof(DWORD64) so addition cannot wrap.
  3. Guard null arguments and module handles: StackSampler::sample() now returns 0 immediately on null thread or frames; start_thread_stacks() checks GetModuleHandleW non-null before GetProcAddress; thread_label() clamps wcslen(description) to INT_MAX before casting to int.
  4. Regression tests: Extends runtime/test/thread_stacks_test.cpp with null-handle / null-frames StackSampler::sample() checks and parse_stack_times zero/overflow (4294967296) token checks.

Verification

  • AI-assisted: yes (Antigravity agent by Google DeepMind)
  • GPUs tested: none (verified locally on macOS arm64 compiling real runtime/src/thread_stacks.cpp with minimal Win32 stubs; Windows MSVC CI run of thread_stacks pending)
  • Sanitizers & tests:
    • 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)
    • Negative control on main: failed both top <= stack.original underflow and BaseAddress + RegionSize wraparound assertions (ReadProcessMemory called with bogus length, main_exit=1).
  • Repository checks: python3 -I scripts/check_no_game_data.py clean; python3 -I tests/test_text_write_line_endings.py (3/3 OK).
Claim ID: `RT-THREAD-STACKS-BOUNDS-AND-WRAP-GUARDS` ### Summary Hardens stack-region arithmetic, pointer bounds checks, and null-argument handling in `runtime/src/thread_stacks.cpp`: 1. **Guard region wraparound and `top - stack.original` underflow in `copy_context_and_stack()`:** Checks `region.RegionSize > 0 && region.RegionSize <= UINT64_MAX - base` and `stack.original >= base && top > stack.original` before computing `top - stack.original` and calling `ReadProcessMemory`. Previously, a zero/wrapping `RegionSize` or `top <= stack.original` underflowed `top - stack.original` near `UINT64_MAX` and requested an 8 MiB `ReadProcessMemory`. 2. **Overflow-free `in_copy()` check in `walk_copy()`:** Rewrites `address - stack.copy + sizeof(DWORD64) <= stack.size` as `stack.size >= sizeof(DWORD64) && address >= stack.copy && address - stack.copy <= stack.size - sizeof(DWORD64)` so addition cannot wrap. 3. **Guard null arguments and module handles:** `StackSampler::sample()` now returns `0` immediately on null `thread` or `frames`; `start_thread_stacks()` checks `GetModuleHandleW` non-null before `GetProcAddress`; `thread_label()` clamps `wcslen(description)` to `INT_MAX` before casting to `int`. 4. **Regression tests:** Extends `runtime/test/thread_stacks_test.cpp` with null-handle / null-frames `StackSampler::sample()` checks and `parse_stack_times` zero/overflow (`4294967296`) token checks. ### Verification - **AI-assisted:** yes (Antigravity agent by Google DeepMind) - **GPUs tested:** none (verified locally on macOS arm64 compiling real `runtime/src/thread_stacks.cpp` with minimal Win32 stubs; Windows MSVC CI run of `thread_stacks` pending) - **Sanitizers & tests:** - `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`) - Negative control on `main`: failed both `top <= stack.original` underflow and `BaseAddress + RegionSize` wraparound assertions (`ReadProcessMemory` called with bogus length, `main_exit=1`). - **Repository checks:** `python3 -I scripts/check_no_game_data.py` clean; `python3 -I tests/test_text_write_line_endings.py` (3/3 OK).
copilot-pull-request-reviewer[bot] reviewed 2026-11-25 19:12:25 +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.
zmasi commented 2026-11-28 11:28:38 +00:00

Verified on Windows 11 (10.0.26200.9974) with MSVC 19.44.35228 (x64, /std:c++20 /EHsc /W4 /permissive- /utf-8 /O2) against main 4a54aedcb + 0bba07bf1 (claim #2570#issuecomment-7238486276):

  • Line-by-line diff audit: Clean (runtime/src/thread_stacks.cpp, runtime/test/thread_stacks_test.cpp); guards base + RegionSize wraparound and stack.original >= base && top > stack.original in copy_context_and_stack, avoids underflow/overflow in in_copy, clamps wcslen to INT_MAX, guards null thread/frames in StackSampler::sample, and checks kernel32/ntdll module handles before GetProcAddress. check_no_game_data.py clean.
  • Red (4a54aedcb + PR test): thread_stacks_test.exe hangs/self-suspends (TIMEOUT) because sampler.sample(GetCurrentThread(), nullptr) lacks the !frames guard and enters SuspendThread(GetCurrentThread()).
  • Green (4a54aedcb + 0bba07bf1 merge): Auto-merges cleanly (rc=0), compiles with 0 /W4 warnings, and thread_stacks_test.exe passes (13 checks, 0 failures, 300/300 live thread stack samples, rc=0).

AI-assisted.

Verified on Windows 11 (`10.0.26200.9974`) with MSVC `19.44.35228` (`x64`, `/std:c++20 /EHsc /W4 /permissive- /utf-8 /O2`) against `main` `4a54aedcb` + `0bba07bf1` (claim `#2570#issuecomment-7238486276`): - **Line-by-line diff audit**: Clean (`runtime/src/thread_stacks.cpp`, `runtime/test/thread_stacks_test.cpp`); guards `base + RegionSize` wraparound and `stack.original >= base && top > stack.original` in `copy_context_and_stack`, avoids underflow/overflow in `in_copy`, clamps `wcslen` to `INT_MAX`, guards null `thread`/`frames` in `StackSampler::sample`, and checks `kernel32`/`ntdll` module handles before `GetProcAddress`. `check_no_game_data.py` clean. - **Red (`4a54aedcb` + PR test)**: `thread_stacks_test.exe` hangs/self-suspends (`TIMEOUT`) because `sampler.sample(GetCurrentThread(), nullptr)` lacks the `!frames` guard and enters `SuspendThread(GetCurrentThread())`. - **Green (`4a54aedcb` + `0bba07bf1` merge)**: Auto-merges cleanly (`rc=0`), compiles with `0` `/W4` warnings, and `thread_stacks_test.exe` passes (`13 checks, 0 failures`, `300/300` live thread stack samples, `rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 18:13:14 +00:00

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.

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.
OreoFR commented 2026-11-28 21:59:22 +00:00

Verified by OreoFR

  • Base: main c1ecf04ed + PR head 0bba07bf1; green run in a merge package of 16 PRs, no new ctest failure vs main
  • Test: ctest -R "^thread_stacks$"
  • Red on main: timeout (60 s, loops on a region that overflows)
  • Green with the PR: OK (0.1 s)
  • Verdict: READY
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

AI-assisted (Claude Code, Claude Opus 5.5).

**Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `0bba07bf1`; green run in a merge package of 16 PRs, no new ctest failure vs main - Test: `ctest -R "^thread_stacks$"` - Red on main: timeout (60 s, loops on a region that overflows) - Green with the PR: OK (0.1 s) - Verdict: READY - Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11 AI-assisted (Claude Code, Claude Opus 5.5).
Sign in to join this conversation.
No description provided.