[PR #2058] runtime: guard stack region wrap/underflow and null inputs in thread_stacks #2064

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

📋 Pull Request Information

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

Base: main ← Head: rt/thread-stacks-bounds-and-wrap-guards


📝 Commits (1)

  • 0bba07b runtime: guard stack region wrap/underflow and null inputs in thread_stacks

📊 Changes

2 files changed (+38 additions, -13 deletions)

View changed files

📝 runtime/src/thread_stacks.cpp (+29 -11)
📝 runtime/test/thread_stacks_test.cpp (+9 -2)

📄 Description

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).

🔄 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/2058 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `rt/thread-stacks-bounds-and-wrap-guards` --- ### 📝 Commits (1) - [`0bba07b`](https://github.com/ubiquitarianism/leonida/commit/0bba07bf12dc327fc690b331814ebde0f3c1175f) runtime: guard stack region wrap/underflow and null inputs in thread_stacks ### 📊 Changes **2 files changed** (+38 additions, -13 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/thread_stacks.cpp` (+29 -11) 📝 `runtime/test/thread_stacks_test.cpp` (+9 -2) </details> ### 📄 Description 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). --- <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#2064
No description provided.