Skip to content

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

Closed
prasanna8585 wants to merge 2 commits into
openxla:mainfrom
prasanna8585:fix/ifrt-shape-fromproto-overflow-check
Closed

fix(ifrt): reject dimension products that overflow int64_t in Shape::FromProto#46403
prasanna8585 wants to merge 2 commits into
openxla:mainfrom
prasanna8585:fix/ifrt-shape-fromproto-overflow-check

Conversation

@prasanna8585

Copy link
Copy Markdown
Contributor

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.

…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.
Comment thread xla/python/ifrt/shape.cc Outdated
…d 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.
copybara-service Bot pushed a commit that referenced this pull request Jul 30, 2026
… 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
PiperOrigin-RevId: 956346424
copybara-service Bot pushed a commit to tensorflow/tensorflow that referenced this pull request Jul 30, 2026
… 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 <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.

--
4a1d7d96afe8061ea8ba8d8331654854d2c16f91 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=openxla/xla#46403 from prasanna8585:fix/ifrt-shape-fromproto-overflow-check 4a1d7d96afe8061ea8ba8d8331654854d2c16f91
PiperOrigin-RevId: 956346424
copybara-service Bot pushed a commit to tensorflow/tensorflow that referenced this pull request Jul 30, 2026
… 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 <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.

--
4a1d7d96afe8061ea8ba8d8331654854d2c16f91 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

PiperOrigin-RevId: 956425279
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.

2 participants