Skip to content

Fix dangling pointer in TextTokenGenerator non-kv-cache path#18725

Open
kirklandsign wants to merge 1 commit intomainfrom
export-D99408541
Open

Fix dangling pointer in TextTokenGenerator non-kv-cache path#18725
kirklandsign wants to merge 1 commit intomainfrom
export-D99408541

Conversation

@kirklandsign
Copy link
Copy Markdown
Contributor

Summary:
In the non-kv-cache branch of TextTokenGenerator::generate(), push_back()
on token_data can trigger vector reallocation, but the tensor created via
from_blob still points to the old data address. resize_tensor_ptr only
updates shape metadata, not the data pointer, resulting in a dangling
pointer.

Fix by pre-allocating the vector with reserve() before creating the
tensor, ensuring push_back never triggers reallocation during the
generate loop.

Differential Revision: D99408541

Summary:
In the non-kv-cache branch of TextTokenGenerator::generate(), push_back()
on token_data can trigger vector reallocation, but the tensor created via
from_blob still points to the old data address. resize_tensor_ptr only
updates shape metadata, not the data pointer, resulting in a dangling
pointer.

Fix by pre-allocating the vector with reserve() before creating the
tensor, ensuring push_back never triggers reallocation during the
generate loop.

Differential Revision: D99408541
Copilot AI review requested due to automatic review settings April 6, 2026 19:24
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 6, 2026
@meta-codesync
Copy link
Copy Markdown
Contributor

meta-codesync bot commented Apr 6, 2026

@kirklandsign has exported this pull request. If you are a Meta employee, you can view the originating Diff in D99408541.

@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Apr 6, 2026

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/18725

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure, 9 Pending

As of commit b5e15c8 with merge base 3d2c853 (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Apr 6, 2026

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a memory-safety issue in the LLM token generation loop when KV-cache is disabled: from_blob() wraps token_data.data(), but subsequent push_back() could reallocate the vector and leave the tensor with a dangling data pointer.

Changes:

  • Pre-reserve token_data capacity in the non-KV-cache path to prevent reallocation during generation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants