Fix AppendNextTokensToSequences heap overflow - #2111
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to prevent out-of-bounds writes into the preallocated sequence buffers during generation by adding explicit max_length bounds checks across CPU and CUDA search implementations, and by failing fast when GenerateNextToken() is called after reaching max_length.
Changes:
- Added early-return bounds checks in CPU greedy/beam append paths to prevent writing past the sequences buffer.
- Added CUDA-side guards to avoid launching append kernels once the sequence length reaches
max_length, and updated done/max-length checks. - Added an early
GenerateNextToken()runtime error when sequence length is already atmax_length.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/search.cpp |
Adds CPU-side bounds checks and adjusts done/reset behavior around appending tokens. |
src/generators.cpp |
Adds a fast-fail guard in GenerateNextToken() when already at max_length. |
src/cuda/search_cuda.cpp |
Adds CUDA-side conditional guards to avoid appending once at/over max_length, plus logging/done handling. |
Contributor
|
The changes in this PR seem to indicate a larger issue inside ORT GenAI. The |
apsonawane
enabled auto-merge (squash)
May 20, 2026 21:45
baijumeswani
approved these changes
Jul 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix heap-buffer-overflow in
GenerateNextTokenwhen sequence is atmax_lengthSummary
Adds a fast-fail guard at the entry of
Generator::GenerateNextToken()that throws a clearstd::runtime_errorwhen the sequence has already reachedmax_length. This prevents a heap-buffer-overflow write into the preallocated sequences buffer that is otherwise reachable via the C API.Reported bug (AddressSanitizer)
A libFuzzer harness driving the
OgaGenerator_*C API triggered the following crash:Root cause
Inside
GreedySearch_Cpu::AppendTokens,ResetDone()is called unconditionally at the end. If the previous call filled the sequences buffer up to exactlymax_length,done_is cleared. A subsequentGenerateNextToken()then:SampleTopK().!done_check falsely passes (buffer is actually full).AppendNextTokensToSequences()writes at indexmax_length→ OOB write into the redzone immediately following the 64-byte sequences buffer.Fix
Add an early runtime error at the top of
Generator::GenerateNextToken():This sits directly above frame #2 of the crash stack, so the OOB path in frames #0 and #1 is unreachable. The exception propagates through
OgaGenerator_GenerateNextTokenand is surfaced to the caller as a normal API error instead of memory corruption.