Fix signed-integer overflow (UB) in roll and tile shape arithmetic#3604
Open
devYRPauli wants to merge 1 commit into
Open
Fix signed-integer overflow (UB) in roll and tile shape arithmetic#3604devYRPauli wants to merge 1 commit into
devYRPauli wants to merge 1 commit into
Conversation
roll and tile computed shifts/sizes in 32-bit int without overflow checks, so large-but-valid int32 inputs hit signed-integer overflow (UB) reachable from the public API: tile's reps[i]*shape[i], roll's INT_MIN negation, and roll's int shift-sum accumulator. Compute these in int64 and use safe_cast to reject out-of-range results with a graceful error. Add a regression test.
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.
Summary
Fixes #3601. Several shape ops computed sizes/shifts in 32-bit
intwithout overflow checks, so large-or-degenerate but otherwise valid (int32-range) inputs triggered C++ signed-integer overflow (UB), reachable from the safe public API. UBSan-confirmed sites on currentmain:tile:reps[i] * shape[i]overflowsint(ops.cpp:1352).roll: negating a shift ofINT_MINin(-sh) % size(ops.cpp:6353).roll: summing shifts into anint total_shiftaccumulator (both theShape-shift andShape-shift +axisoverloads).The
flatten/Flatten::output_shapeproduct path the issue also mentions is already 64-bit-safe onmain, so it is not touched here.Fix
Compute these in
int64_tand narrow with the existingsafe_casthelper, which raises a gracefulstd::overflow_errorfor out-of-int32results instead of overflowing.roll'sINT_MINnegation is performed in 64-bit so it no longer overflows.Verification
Built CPU-only with
-DUSE_UBSAN=ON:signed integer overflow/negation of -2147483648 cannot be representedat the sites above.std::overflow_error, androllbyINT_MINreturns the correct (identity) result for a size-4 axis.Added
TEST_CASE("roll and tile shape overflow").