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

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

📋 Pull Request Information

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

Base: main ← Head: fix/rt-pipeline-workers-oom-uaf


📝 Commits (1)

  • 703dfb4 runtime: fix PipelineWorkers stack use-after-free on OOM in start() (RT-PIPELINE-WORKERS-OOM-UAF)

📊 Changes

2 files changed (+112 additions, -2 deletions)

View changed files

📝 runtime/src/pipeline_workers.h (+11 -2)
📝 runtime/test/pipeline_workers_test.cpp (+101 -0)

📄 Description

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.

🔄 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/1821 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/rt-pipeline-workers-oom-uaf` --- ### 📝 Commits (1) - [`703dfb4`](https://github.com/ubiquitarianism/leonida/commit/703dfb493b0e2edccdaafe44041aba1932a6712d) runtime: fix PipelineWorkers stack use-after-free on OOM in start() (RT-PIPELINE-WORKERS-OOM-UAF) ### 📊 Changes **2 files changed** (+112 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `runtime/src/pipeline_workers.h` (+11 -2) 📝 `runtime/test/pipeline_workers_test.cpp` (+101 -0) </details> ### 📄 Description ## 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. --- <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#1833
No description provided.