runtime: fix PipelineWorkers stack use-after-free on OOM in start() (RT-PIPELINE-WORKERS-OOM-UAF) #1821

Merged
IlyassRS3 merged 1 commit from upstream-pr/1821 into main 2026-11-28 17:57:12 +00:00
IlyassRS3 commented 2026-11-25 12:32:44 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by IlyassRS3 on 2026-11-25T12:32:44Z.


Summary

Item: RT-PIPELINE-WORKERS-OOM-UAF
Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162472188

Fixes a stack use-after-free and OOM submission failure in leonida::PipelineWorkers::run() (runtime/src/pipeline_workers.h):

  1. Start workers before publishing stack &batch to queue_: previously run() pushed the address of its stack-local Batch onto queue_ and updated batches_/jobs_ before calling start(count - 1). Because start() only caught std::system_error and not std::bad_alloc, an allocation failure growing threads_ (or allocating a std::thread control block) unwound run() and destroyed batch on the stack while leaving &batch dangling in queue_. The next wakeup of any existing worker dereferenced the freed stack frame (AddressSanitizer: stack-use-after-return at pipeline_workers.h:120).
  2. Handle std::bad_alloc in start() and queue_.push_back():
    • start() catches std::bad_alloc and breaks out of the growth loop without permanently latching exhausted_, so the existing workers and caller complete the current batch and later batches can scale up again once memory is available.
    • run() guards queue_.push_back(&batch) against std::bad_alloc; if enqueuing fails, it unlocks mutex_ and runs the batch inline on the calling thread without mutating batches_/jobs_ or publishing a dangling pointer.
  3. Regression tests: added oom_during_start_and_enqueue() to runtime/test/pipeline_workers_test.cpp using a thread-local operator new failure injector covering both mid-growth OOM (which deterministically triggers ASan stack-use-after-return on main) and hard first-batch OOM with inline fallback and post-OOM recovery.

Testing

  • AI-assisted: yes
  • GPUs tested: none (CPU-only portable runtime header and test)
  • Host: macOS arm64, clang++ (C++20)
  • Verified runtime/test/pipeline_workers_test.cpp with -std=c++20 -Wall -Wextra -Werror:
    • ASan + UBSan (ASAN_OPTIONS=detect_stack_use_after_return=1): pipeline_workers_test: passed
    • TSan (-fsanitize=thread): pipeline_workers_test: passed (0 data races)
    • -O2 × 3 runs: pipeline_workers_test: passed
  • Mutation check against main (0d80bc5df): compiled the updated pipeline_workers_test.cpp against main:runtime/src/pipeline_workers.h under ASan (detect_stack_use_after_return=1); reproduced ERROR: AddressSanitizer: stack-use-after-return in PipelineWorkers::step (pipeline_workers.h:120, rc=134).
  • Ran python3 -I scripts/check_no_game_data.py runtime/src/pipeline_workers.h runtime/test/pipeline_workers_test.cpp and python3 -I tests/test_text_write_line_endings.py: both passed.
  • Windows MSVC build and full CTest suite rely on CI.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5491387430 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1821). Originally posted by [IlyassRS3](https://github.com/IlyassRS3) on 2026-11-25T12:32:44Z. --- ## Summary Item: `RT-PIPELINE-WORKERS-OOM-UAF` Claim: https://github.com/ubiquitarianism/leonida/issues/1608#issuecomment-7162472188 Fixes a stack use-after-free and OOM submission failure in `leonida::PipelineWorkers::run()` (`runtime/src/pipeline_workers.h`): 1. **Start workers before publishing stack `&batch` to `queue_`**: previously `run()` pushed the address of its stack-local `Batch` onto `queue_` and updated `batches_`/`jobs_` *before* calling `start(count - 1)`. Because `start()` only caught `std::system_error` and not `std::bad_alloc`, an allocation failure growing `threads_` (or allocating a `std::thread` control block) unwound `run()` and destroyed `batch` on the stack while leaving `&batch` dangling in `queue_`. The next wakeup of any existing worker dereferenced the freed stack frame (`AddressSanitizer: stack-use-after-return` at `pipeline_workers.h:120`). 2. **Handle `std::bad_alloc` in `start()` and `queue_.push_back()`**: - `start()` catches `std::bad_alloc` and breaks out of the growth loop without permanently latching `exhausted_`, so the existing workers and caller complete the current batch and later batches can scale up again once memory is available. - `run()` guards `queue_.push_back(&batch)` against `std::bad_alloc`; if enqueuing fails, it unlocks `mutex_` and runs the batch inline on the calling thread without mutating `batches_`/`jobs_` or publishing a dangling pointer. 3. **Regression tests**: added `oom_during_start_and_enqueue()` to `runtime/test/pipeline_workers_test.cpp` using a thread-local `operator new` failure injector covering both mid-growth OOM (which deterministically triggers ASan `stack-use-after-return` on `main`) and hard first-batch OOM with inline fallback and post-OOM recovery. ## Testing - **AI-assisted**: yes - **GPUs tested**: none (CPU-only portable runtime header and test) - **Host**: macOS arm64, `clang++` (C++20) - Verified `runtime/test/pipeline_workers_test.cpp` with `-std=c++20 -Wall -Wextra -Werror`: - ASan + UBSan (`ASAN_OPTIONS=detect_stack_use_after_return=1`): `pipeline_workers_test: passed` - TSan (`-fsanitize=thread`): `pipeline_workers_test: passed` (0 data races) - `-O2` × 3 runs: `pipeline_workers_test: passed` - **Mutation check against `main` (`0d80bc5df`)**: compiled the updated `pipeline_workers_test.cpp` against `main:runtime/src/pipeline_workers.h` under ASan (`detect_stack_use_after_return=1`); reproduced `ERROR: AddressSanitizer: stack-use-after-return` in `PipelineWorkers::step` (`pipeline_workers.h:120`, rc=134). - Ran `python3 -I scripts/check_no_game_data.py runtime/src/pipeline_workers.h runtime/test/pipeline_workers_test.cpp` and `python3 -I tests/test_text_write_line_endings.py`: both passed. - Windows MSVC build and full CTest suite rely on CI.
copilot-pull-request-reviewer[bot] commented 2026-11-25 12:32:59 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by copilot-pull-request-reviewer[bot] on 2026-11-25T12:32:59Z.

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.

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6057655116 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1821#pullrequestreview-6057655116). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-11-25T12:32:59Z. **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.
zmasi commented 2026-11-28 11:51:47 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by zmasi on 2026-11-28T11:51:47Z.


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 24549bdb1 + 703dfb493 (claim #2570#issuecomment-7238799031):

  • Line-by-line diff audit: Clean (runtime/src/pipeline_workers.h, runtime/test/pipeline_workers_test.cpp); calls start(count - 1) (catching std::bad_alloc cleanly) before pushing stack-allocated &batch onto queue_, and falls back to inline execution if queue_.push_back(&batch) throws std::bad_alloc, eliminating the dangling stack Batch* use-after-free on subsequent worker wakeups. check_no_game_data.py clean.
  • Red (24549bdb1 + PR test): pipeline_workers_test.exe crashes with 0xc0000005 (STATUS_ACCESS_VIOLATION from the dangling stack Batch* in queue_ after injected std::bad_alloc in start()).
  • Green (24549bdb1 + 703dfb493 merge): Merges cleanly (rc=0), compiles with 0 /W4 warnings, and pipeline_workers_test.exe passes (pipeline_workers_test: passed, rc=0).

AI-assisted.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7238831712 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1821#issuecomment-7238831712). Originally posted by [zmasi](https://github.com/zmasi) on 2026-11-28T11:51:47Z. --- 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` `24549bdb1` + `703dfb493` (claim `#2570#issuecomment-7238799031`): - **Line-by-line diff audit**: Clean (`runtime/src/pipeline_workers.h`, `runtime/test/pipeline_workers_test.cpp`); calls `start(count - 1)` (catching `std::bad_alloc` cleanly) before pushing stack-allocated `&batch` onto `queue_`, and falls back to inline execution if `queue_.push_back(&batch)` throws `std::bad_alloc`, eliminating the dangling stack `Batch*` use-after-free on subsequent worker wakeups. `check_no_game_data.py` clean. - **Red (`24549bdb1` + PR test)**: `pipeline_workers_test.exe` crashes with `0xc0000005` (`STATUS_ACCESS_VIOLATION` from the dangling stack `Batch*` in `queue_` after injected `std::bad_alloc` in `start()`). - **Green (`24549bdb1` + `703dfb493` merge)**: Merges cleanly (`rc=0`), compiles with `0` `/W4` warnings, and `pipeline_workers_test.exe` passes (`pipeline_workers_test: passed`, `rc=0`). AI-assisted.
MoonEyes commented 2026-11-28 17:57:10 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-28T17:57:10Z.


Red/green: On main pipeline_workers_test crashes with an access violation (a dangling stack Batch pointer left in the queue after an injected bad_alloc in start()), and with the PR ctest -R "^pipeline_workers$" passes.
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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7243414165 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1821#issuecomment-7243414165). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-28T17:57:10Z. --- Red/green: On main pipeline_workers_test crashes with an access violation (a dangling stack Batch pointer left in the queue after an injected bad_alloc in start()), and with the PR ctest -R "^pipeline_workers$" passes. 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:54:17 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by OreoFR on 2026-11-28T21:54:17Z.


Verified by OreoFR

  • Base: main c1ecf04ed + PR head 703dfb493; green run in a merge package of 15 PRs, no new ctest failure vs main
  • Test: ctest -R ^pipeline_workers$
  • Red on main: SegFault exception
  • Green with the PR: OK
  • Verdict: READY (merged during the test, at the tested head)
  • Machine: RTX 4070 Laptop 8 GB, NVIDIA 621.40, Intel Core Ultra 7 155H, Windows 11

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

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7246357426 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/1821#issuecomment-7246357426). Originally posted by [OreoFR](https://github.com/OreoFR) on 2026-11-28T21:54:17Z. --- **Verified by OreoFR** - Base: main `c1ecf04ed` + PR head `703dfb493`; green run in a merge package of 15 PRs, no new ctest failure vs main - Test: `ctest -R ^pipeline_workers$` - Red on main: SegFault exception - Green with the PR: OK - Verdict: READY (merged during the test, at the tested head) - 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 reviewers
No labels
No milestone
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.

Reference
ProjectLeonida/mandy!1821
No description provided.