Repository navigation
PR #46403: fix(ifrt): reject dimension products that overflow int64_t in Shape::FromProto - #46495
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
test_956346424
branch
from
July 30, 2026 10:36
45a596a to
77e537b
Compare
… in Shape::FromProto Imported from GitHub PR #46403 `xla::ifrt::Shape::FromProto` only validated that individual dimensions were non-negative; the product of all dimensions was never checked. `xla::ShapeUtil::ValidateDimensions` already does this correctly for the core `xla::Shape` type via `OverflowSafeMultiply` -- this class is a separate type (used by the Python/IFRT layer, including `ifrt_proxy`) that didn't have the equivalent protection. `Shape::num_elements()` computes the dimension product without overflow detection, so a crafted `ShapeProto` with individually plausible but jointly overflowing dimensions silently produces a wrapped, incorrect element count rather than being rejected. This is reachable from `ifrt_proxy`'s deserialized RPC request protos (`ArraySpec::FromProto` -> `Shape::FromProto`), a client-server boundary distinct from IFRT's general in-process API. Fix mirrors the existing pattern in `ValidateDimensions`: detect overflow with `__builtin_mul_overflow` while walking dimensions, reject with `InvalidArgument` instead of returning a corrupted `Shape`. Two regression tests included. Copybara import of the project: -- 9ea3faf by Prasanna Dabi <65734642+prasanna8585@users.noreply.github.com>: fix(ifrt): reject dimension products that overflow int64_t in Shape::FromProto xla::ifrt::Shape (a class separate from xla::Shape/ShapeUtil, used by the Python/IFRT layer including ifrt_proxy) deserializes a ShapeProto and only validated that individual dimensions were non-negative -- the product of all dimensions was never checked, unlike xla::ShapeUtil::ValidateDimensions which already does this correctly for the core Shape type via OverflowSafeMultiply. Shape::num_elements() computes this product without overflow detection, so a crafted ShapeProto with individually plausible but jointly overflowing dimensions silently produces a wrapped, incorrect element count. Anything downstream that sizes a buffer from that count (e.g. byte-size computations derived from num_elements()) would be sized inconsistently with the shape's true declared dimensions. This is reachable from ifrt_proxy's deserialized RPC request protos (ArraySpec::FromProto -> Shape::FromProto), which is a genuine client-server trust boundary distinct from IFRT's general in-process API surface. Fix mirrors the existing, correct pattern in xla::ShapeUtil::ValidateDimensions: detect the overflow with __builtin_mul_overflow while walking the dimensions and reject with InvalidArgument, rather than silently returning a corrupted Shape. Two regression tests added: one confirming the overflow is rejected, one confirming ordinary non-overflowing shapes are unaffected. -- 4a1d7d9 by Prasanna Dabi <65734642+prasanna8585@users.noreply.github.com>: Address review: reuse xla::OverflowSafeMultiply instead of hand-rolled overflow check Per golechwierowicz's review comment on this PR: replace the manual __builtin_mul_overflow-based loop with xla::OverflowSafeMultiply (xla/overflow_util.h), the same utility xla::ShapeUtil::ValidateDimensions already uses for the core XLA Shape type's equivalent check. No behavioral change -- same detection, same rejection -- just reusing the existing, already-reviewed utility instead of duplicating its logic. Merging this change closes #46403 COPYBARA_INTEGRATE_REVIEW=#46403 from prasanna8585:fix/ifrt-shape-fromproto-overflow-check 4a1d7d9 PiperOrigin-RevId: 956425279
copybara-service
Bot
force-pushed
the
test_956346424
branch
from
July 30, 2026 10:58
77e537b to
ab8124b
Compare
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.
PR #46403: fix(ifrt): reject dimension products that overflow int64_t in Shape::FromProto
Imported from GitHub PR #46403
xla::ifrt::Shape::FromProtoonly validated that individual dimensions were non-negative; the product of all dimensions was never checked.xla::ShapeUtil::ValidateDimensionsalready does this correctly for the corexla::Shapetype viaOverflowSafeMultiply-- this class is a separate type (used by the Python/IFRT layer, includingifrt_proxy) that didn't have the equivalent protection.Shape::num_elements()computes the dimension product without overflow detection, so a craftedShapeProtowith individually plausible but jointly overflowing dimensions silently produces a wrapped, incorrect element count rather than being rejected.This is reachable from
ifrt_proxy's deserialized RPC request protos (ArraySpec::FromProto->Shape::FromProto), a client-server boundary distinct from IFRT's general in-process API.Fix mirrors the existing pattern in
ValidateDimensions: detect overflow with__builtin_mul_overflowwhile walking dimensions, reject withInvalidArgumentinstead of returning a corruptedShape. Two regression tests included.Copybara import of the project:
--
9ea3faf by Prasanna Dabi 65734642+prasanna8585@users.noreply.github.com:
fix(ifrt): reject dimension products that overflow int64_t in Shape::FromProto
xla::ifrt::Shape (a class separate from xla::Shape/ShapeUtil, used by the
Python/IFRT layer including ifrt_proxy) deserializes a ShapeProto and only
validated that individual dimensions were non-negative -- the product of
all dimensions was never checked, unlike xla::ShapeUtil::ValidateDimensions
which already does this correctly for the core Shape type via
OverflowSafeMultiply.
Shape::num_elements() computes this product without overflow detection, so
a crafted ShapeProto with individually plausible but jointly overflowing
dimensions silently produces a wrapped, incorrect element count. Anything
downstream that sizes a buffer from that count (e.g. byte-size
computations derived from num_elements()) would be sized inconsistently
with the shape's true declared dimensions.
This is reachable from ifrt_proxy's deserialized RPC request protos
(ArraySpec::FromProto -> Shape::FromProto), which is a genuine
client-server trust boundary distinct from IFRT's general in-process API
surface.
Fix mirrors the existing, correct pattern in
xla::ShapeUtil::ValidateDimensions: detect the overflow with
__builtin_mul_overflow while walking the dimensions and reject with
InvalidArgument, rather than silently returning a corrupted Shape.
Two regression tests added: one confirming the overflow is rejected, one
confirming ordinary non-overflowing shapes are unaffected.
--
4a1d7d9 by Prasanna Dabi 65734642+prasanna8585@users.noreply.github.com:
Address review: reuse xla::OverflowSafeMultiply instead of hand-rolled overflow check
Per golechwierowicz's review comment on this PR: replace the manual
__builtin_mul_overflow-based loop with xla::OverflowSafeMultiply
(xla/overflow_util.h), the same utility xla::ShapeUtil::ValidateDimensions
already uses for the core XLA Shape type's equivalent check. No behavioral
change -- same detection, same rejection -- just reusing the existing,
already-reviewed utility instead of duplicating its logic.
Merging this change closes #46403
FUTURE_COPYBARA_INTEGRATE_REVIEW=#46403 from prasanna8585:fix/ifrt-shape-fromproto-overflow-check 4a1d7d9