fix: reject zero-slot CIRCULAR_BUFFER outputs#3517
Open
Sakura-501 wants to merge 1 commit intotensorflow:mainfrom
Open
fix: reject zero-slot CIRCULAR_BUFFER outputs#3517Sakura-501 wants to merge 1 commit intotensorflow:mainfrom
Sakura-501 wants to merge 1 commit intotensorflow:mainfrom
Conversation
The CIRCULAR_BUFFER kernel accepted output tensors with num_slots == 0. During Eval this value flows into (num_slots - 1) * depth and then into memmove/memcpy, where the negative length is converted to size_t and causes out-of-bounds access. Add a Prepare-time guard that rejects zero-slot outputs before Eval can run, and add a focused regression test that verifies InitAndPrepare fails for the malformed shape.
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.
Summary
This PR fixes a memory-safety issue in the
CIRCULAR_BUFFERcustom kernel.A malformed model can set the output shape to
[1, 0, 1, depth], which makesnum_slots == 0. DuringCircularBufferEval(), that value flows into(num_slots - 1) * depthand is then passed tomemmove()/memcpy(). The negative length is implicitly converted tosize_t, producing an out-of-bounds access.Root cause
CircularBufferPrepare()validated only partial dimension relationships and did not reject zero-slot output tensors, so malformed shape metadata could reachEvalInt8()unchecked.Fix
output->dims->data[1] <= 0duringCircularBufferPrepare()InitAndPrepare()fails whennum_slots == 0Verification
git diff --checkbazel test //tensorflow/lite/micro/kernels:circular_buffer_test --test_output=errorsPATH="/opt/homebrew/opt/llvm@21/bin:/opt/homebrew/opt/make/libexec/gnubin:$PATH" bash tensorflow/lite/micro/tools/ci_build/test_code_style.shBUG=None