Repository navigation
Conversation
A num_samples value near INT_MAX overflows the int arithmetic (num_samples + num_threads - 1) / num_threads in BalanceTrajectory and in the noisy ops' ComputeSmall. The resulting negative chunk sizes make every worker thread skip its sampling loop, so TfqNoisySamples returns its output tensor unwritten (CWE-200/CWE-908). Add kMaxNumSamples (10,000,000) and enforce it in GetNumSamples and GetIndividualSample, the two functions through which all seven ops that accept num_samples receive the input. GetIndividualSample previously had no bound at all; a negative value reached TensorShape::AddDim, which CHECK-fails and aborts the process. It now returns InvalidArgument. Add tests for the new bounds to the input-validation test of each of the seven ops.
mhucka
marked this pull request as ready for review
October 13, 2026 04:50
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces an upper bound of 10,000,000 (kMaxNumSamples) on the number of samples accepted for any single circuit or Pauli sum to prevent integer overflow during multi-threaded work splitting. It updates the validation logic in GetNumSamples and GetIndividualSample to enforce this limit and adds corresponding unit tests across several simulation and noisy operator test suites. No review comments were provided, so there is no additional feedback to address.
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.
This fixes b/510431411, "Uninitialized Tensor Heap Memory Leak via Integer Overflow in BalanceTrajectory", which is an instance of CWE-200/CWE-908. The problem is this: a
num_samplesvalue nearINT_MAXoverflows the int arithmetic(num_samples + num_threads - 1)/num_threadsinBalanceTrajectoryand in the noisy ops'ComputeSmall. The resulting negative chunk sizes make every worker thread skip its sampling loop, soTfqNoisySamplesreturns its output tensor unwritten.This PR adds a check on number of samples in
GetNumSamplesandGetIndividualSample. The latter previously had no bound at all; a negative value reachedTensorShape::AddDim, which would CHECK-fail and terminate the process. It now returnsInvalidArgument.The real changes are in the files
tensorflow_quantum/core/ops/parse_context.ccandtensorflow_quantum/core/ops/parse_context.h. The remaining file changes are to test files.