Skip to content

PR #46403: fix(ifrt): reject dimension products that overflow int64_t in Shape::FromProto - #46495

Merged
copybara-service[bot] merged 1 commit into
mainfrom
test_956346424
Jul 30, 2026
Merged

copybara-service[bot] merged 1 commit into
mainfrom
test_956346424

Conversation

@copybara-service

Copy link
Copy Markdown

PR #46403: fix(ifrt): reject dimension products that overflow int64_t 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

FUTURE_COPYBARA_INTEGRATE_REVIEW=#46403 from prasanna8585:fix/ifrt-shape-fromproto-overflow-check 4a1d7d9

… 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
copybara-service Bot merged commit ab8124b into main Jul 30, 2026
@copybara-service
copybara-service Bot deleted the test_956346424 branch July 30, 2026 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant