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

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

@⁠IlyassRS3 on GitHub · 2026-11-25 12:32 UTC · original
IlyassRS3:fix/rt-pipeline-workers-oom-uaf → main · +112 −2 · 2 files · 1 commits · open

Summary

Item: RT-PIPELINE-WORKERS-OOM-UAF
Claim: #1608

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.
**@⁠IlyassRS3** on GitHub · 2026-11-25 12:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1821) `IlyassRS3:fix/rt-pipeline-workers-oom-uaf` → `main` · +112 −2 · 2 files · 1 commits · open ## Summary Item: `RT-PIPELINE-WORKERS-OOM-UAF` Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:07:09 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 12:32 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 12:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1821#pullrequestreview-6057655116) · **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/1821/head:github-bot/gh-1821'
git switch 'github-bot/gh-1821'

Merge

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