Skip to content

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

Merged
copybara-service[bot] merged 1 commit into
masterfrom
exported_pr_956346424
Jul 30, 2026
Merged

copybara-service[bot] merged 1 commit into
masterfrom
exported_pr_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 openxla/xla#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:

--
9ea3faf61a3396a9da49abd09199d9cb296adebf by Prasanna Dabi [email protected]:

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.

--
4a1d7d96afe8061ea8ba8d8331654854d2c16f91 by Prasanna Dabi [email protected]:

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=openxla/xla#46403 from prasanna8585:fix/ifrt-shape-fromproto-overflow-check 4a1d7d96afe8061ea8ba8d8331654854d2c16f91

@copybara-service
copybara-service Bot force-pushed the exported_pr_956346424 branch from cf92586 to 52c1a76 Compare July 30, 2026 10:41
… in Shape::FromProto

Imported from GitHub PR openxla/xla#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:

--
9ea3faf61a3396a9da49abd09199d9cb296adebf by Prasanna Dabi <[email protected]>:

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.

--
4a1d7d96afe8061ea8ba8d8331654854d2c16f91 by Prasanna Dabi <[email protected]>:

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

PiperOrigin-RevId: 956425279
@copybara-service
copybara-service Bot force-pushed the exported_pr_956346424 branch from 52c1a76 to f18a776 Compare July 30, 2026 11:00
@copybara-service
copybara-service Bot merged commit f18a776 into master Jul 30, 2026
4 checks passed
@copybara-service
copybara-service Bot deleted the exported_pr_956346424 branch July 30, 2026 11:00
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.

IndexError webcam object detection

1 participant