Skip to content

Reject out-of-range num_samples in the op input parsers - #1119

Open
mhucka wants to merge 2 commits into
tensorflow:masterfrom
mhucka:fix-num-samples-bounds
Open

mhucka wants to merge 2 commits into
tensorflow:masterfrom
mhucka:fix-num-samples-bounds

Conversation

@mhucka

@mhucka mhucka commented Oct 13, 2026

Copy link
Copy Markdown
Member

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_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.

This PR adds a check on number of samples in GetNumSamples and GetIndividualSample. The latter previously had no bound at all; a negative value reached TensorShape::AddDim, which would CHECK-fail and terminate the process. It now returns InvalidArgument.

The real changes are in the files tensorflow_quantum/core/ops/parse_context.cc and tensorflow_quantum/core/ops/parse_context.h. The remaining file changes are to test files.

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
mhucka marked this pull request as ready for review October 13, 2026 04:50

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

@mhucka mhucka added the priority/before-0.7.7 To be done before releasing TFQ version 0.7.7 label Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

priority/before-0.7.7 To be done before releasing TFQ version 0.7.7

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant