Do scratch-buffer size math in size_t to avoid 32-bit overflow - #220
Open
EylonKrause wants to merge 1 commit into
Open
Do scratch-buffer size math in size_t to avoid 32-bit overflow#220EylonKrause wants to merge 1 commit into
EylonKrause wants to merge 1 commit into
Conversation
The scratch sizing for the public EightBitIntGemm float entry point computes `n * ldc * sizeof(std::int32_t)` (and the transpose_c case `m * ldc * ...`) in `int`, and Scratch::AssureSize takes an `std::int32_t required_size` and stores it in an `std::int32_t size_`. For large-but-legal shapes (e.g. m=1, k=1, n=50000, ldc=50000) the product overflows 32 bits and/or is truncated when passed to AssureSize, so the scratch buffer is under-allocated and the subsequent int32 result writes run past it -- a heap out-of-bounds write. The internal packing paths have the same latent overflow: PackedSideBlock reserves `params_.l2_width * params_.l2_depth` and PackedResult reserves `block_params_.l2_rows * block_params_.l2_cols`, both `int * int`, before the result is handed to Allocator::Reserve(std::size_t). Widen the dimension arithmetic to size_t at each site and make Scratch::AssureSize / Scratch::size_ size_t so the computed requirement is never truncated. No behavior change for in-range sizes.
EylonKrause
force-pushed
the
fix/scratch-size-overflow
branch
from
August 26, 2026 21:27
f2556ab to
06cbfb0
Compare
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.
Problem
The scratch-buffer sizing for the public
EightBitIntGemmfloat entry point doesits byte math in
int:and
Scratch::AssureSizeboth takes and stores the size asstd::int32_t:For large-but-legal shapes the product
n * ldc * 4overflows 32-bitint(and is further truncated on the
std::int32_tparameter).AssureSizethenunder-allocates the scratch buffer, and the subsequent int32 result writes run
past it — a heap out-of-bounds write. Example:
m=1, k=1, n=50000, ldc=50000needs 50000500004 = 10 GB but
n*ldc*4overflows to a small (even negative)int.The internal packing paths have the same latent
int * intoverflow before thesize reaches
Allocator::Reserve(std::size_t):internal/pack.h:Reserve<std::uint8_t>(params_.l2_width * params_.l2_depth)internal/unpack.h:Reserve<std::int32_t>(block_params_.l2_rows * block_params_.l2_cols)Fix
Do the dimension arithmetic in
size_tat each site, and makeScratch::AssureSize/Scratch::size_size_tso the computed requirement isnever truncated. No behavior change for in-range sizes.
Testing
test/test.cc(full GEMM correctness suite, built withtest/test.cc test/test_data.cc eight_bit_int_gemm/eight_bit_int_gemm.cc)passes:
All tests passed.The change is confined to size computations, socorrectness for in-range shapes is unaffected.
Disclosure: this contribution was authored with an AI coding assistant (Claude) and reviewed before submission.