Skip to content

Add overflow detection to shape multiplication functions#9987

Open
TristanInSec wants to merge 1 commit intogoogle:masterfrom
TristanInSec:fix-shape-multiply-overflow
Open

Add overflow detection to shape multiplication functions#9987
TristanInSec wants to merge 1 commit intogoogle:masterfrom
TristanInSec:fix-shape-multiply-overflow

Conversation

@TristanInSec
Copy link
Copy Markdown

Summary

  • Add overflow detection to all five xnn_shape_multiply_* variants in tensor.c
  • Use __builtin_mul_overflow to detect when dimension products exceed SIZE_MAX
  • Return SIZE_MAX on overflow instead of a silently wrapped value
  • Prevents undersized buffer allocations on platforms where size_t is 32-bit (ARM32, WebAssembly)

Test plan

  • Verified overflow is detected for shapes with dimension products exceeding 2^32 on 32-bit
  • Verified normal (non-overflowing) shapes return correct results
  • All five variants use the same checked multiplication pattern

@google-cla
Copy link
Copy Markdown

google-cla Bot commented Apr 17, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

The five shape multiplication functions multiply size_t dimensions
in a loop without checking for overflow. On platforms where size_t
is 32-bit (ARM32, WebAssembly), dimension products exceeding 2^32
silently wrap, causing get_tensor_size() to return undersized
allocation sizes.

Add a checked_mul helper using __builtin_mul_overflow that returns
SIZE_MAX on overflow. This propagates through the multiplication
chain so callers see SIZE_MAX instead of a wrapped value.
@TristanInSec TristanInSec force-pushed the fix-shape-multiply-overflow branch from 65d4bc0 to 20114ad Compare April 17, 2026 16:23
@TristanInSec
Copy link
Copy Markdown
Author

Hi, friendly ping on this PR (and the related #9988). Let me know if any changes are needed. Thanks!

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