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

Open
github-bot wants to merge 1 commit from github-bot/gh-2058 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 19:12 UTC · original
IlyassRS3:rt/thread-stacks-bounds-and-wrap-guards → main · +38 −13 · 2 files · 1 commits · open

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).
**@⁠IlyassRS3** on GitHub · 2026-11-25 19:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2058) `IlyassRS3:rt/thread-stacks-bounds-and-wrap-guards` → `main` · +38 −13 · 2 files · 1 commits · open 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).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:16:25 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 19:12 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 19:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2058#pullrequestreview-6063484402) · **reviewed** 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.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2058/head:github-bot/gh-2058'
git switch 'github-bot/gh-2058'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2058'
git switch 'github-bot/gh-2058'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2058'
git switch 'github-bot/gh-2058'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2058'
git switch 'main'
git merge --squash 'github-bot/gh-2058'
git switch 'main'
git merge --ff-only 'github-bot/gh-2058'
git switch 'main'
git merge 'github-bot/gh-2058'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.