Repository navigation
Fix packed weights size overflow in create_dwconv_path and create_vmulcaddc_path - #11240
Merged
copybara-service[bot] merged 1 commit intoSep 18, 2026
Conversation
…lcaddc_path Both paths in src/operators/convolution-nhwc.c computed packed_weights_size using raw C multiplication of per_channel_bytes * c_stride with no overflow check. On 32-bit targets (WebAssembly, ARM32) a model-supplied groups value near 107M causes the product to wrap, resulting in an undersized allocation. The subsequent pack_dwconv_hwg_w / pack_vmulcaddc_w call iterates over the real groups count and writes past the end of the buffer. Replace the raw multiplications with xnn_safe_mul / xnn_safe_add, consistent with the fix applied to create_igemm and create_deconvolution2d_nhwc in PR google#11184.
destro4evr-rgb
force-pushed
the
fix-packed-weights-overflow-dwconv-vmulcaddc
branch
from
September 18, 2026 11:36
4d998da to
4010442
Compare
copybara-service Bot
pushed a commit
that referenced
this pull request
Sep 22, 2026
…-nchw.c
The sparse-weights path in xnn_create_convolution2d_nchw (the block-sparse
"spmm" packing branch, guarded by kernel_height == 1, kernel_width == 1,
groups == 1) computed packed_weights_size using raw C multiplication and
addition, with no overflow check:
const size_t packed_weights_size =
num_nonzero_blocks * 2 * sizeof(int32_t) +
num_output_channel_blocks * sizeof(uint32_t) +
((context->group_output_channels + num_nonzero_values) << log2_filter_element_size) +
XNN_EXTRA_BYTES;
num_nonzero_blocks, num_output_channel_blocks, and num_nonzero_values are
all derived from xnn_analyze_spmm's count of non-zero values in the
model's own weight tensor (spmm_packing_params.num_nonzeroes and
friends), combined with context->group_output_channels -- both
model-supplied. On a 32-bit target (WebAssembly, ARM32), a model whose
sparse weight tensor yields large enough counts for this expression
causes it to wrap size_t, producing an undersized allocation via
xnn_allocate_simd_memory. xnn_pack_spmm, called immediately after, then
writes based on the real, un-wrapped num_nonzero_blocks and
num_output_channel_blocks (it iterates over the same sparsity structure
that produced these counts), writing past the end of the undersized
buffer.
This is the same bug shape already fixed for the dense paths in this
codebase: create_igemm and create_deconvolution2d_nhwc (PR #11184), and
create_dwconv_path / create_vmulcaddc_path in convolution-nhwc.c (PR
#11240, "a model-supplied groups value near 107M causes the product to
wrap ... writes past the end of the buffer"). Found by sweeping this
same file and convolution-nhwc.c for other packed_weights_size
computations after reviewing #11240: this file already has a properly
guarded packed_weights_size calculation in its dense
(conv2d_hwc2chw) path (chained xnn_safe_mul calls on
context->group_input_channels / kernel_height / kernel_width /
context->groups / packed_group_output_channels), confirming the
guarding convention is established here -- it just was not extended to
this sparse path.
I also traced a second candidate this same sweep surfaced,
resize-bilinear-nhwc.c's xnn_setup_resize_bilinear2d_nhwc, and confirmed
it is not concretely exploitable: its packed_weights_size recomputation
reads output_height/output_width from
resize_op->context.resize_nhwc_indirection_init, which is populated in
exactly one place in that file -- inside
xnn_reshape_resize_bilinear2d_nhwc, immediately after that function's
own xnn_safe_mul-guarded computation of the same product already
succeeded on the same values. So unlike this one, that recomputation
can never see values its own file's overflow check has not already
validated. No fix needed there.
Fix: replace the raw expression with chained xnn_safe_mul / xnn_safe_add
calls (matching the style already used a few functions away in this
same file, and in the two prior PRs above), returning
xnn_status_invalid_parameter with a logged error on overflow instead of
proceeding with an undersized allocation.
Verified:
- Reproduced the overflow dynamically: extracted the exact pre-fix
expression into a standalone harness using uint32_t to model size_t
on a 32-bit target. With num_nonzero_blocks =
num_output_channel_blocks = num_nonzero_values = 300000000 (a
plausible crafted-sparse-model value) and group_output_channels = 4,
the intended size is ~4.8 GB but the wrapped size_t computation
produces 505032736 -- undersized by exactly 2^32 bytes (a full
uint32_t wrap).
- Verified the fix closes it: transcribed the exact patch logic
(identical chain of xnn_safe_mul/xnn_safe_add calls) into the same
harness, using the real xnn_safe_mul/xnn_safe_add implementations
from src/xnnpack/math.h (__builtin_mul_overflow /
__builtin_add_overflow under the hood). The same malicious input is
correctly rejected; a normal, legitimate sparse-weights input (e.g.
1000 nonzero blocks, 64 output channel blocks) is correctly accepted
and produces the expected byte count.
- Syntax-verified the inserted block in isolation against the real
src/xnnpack/math.h header (correct xnn_safe_mul/xnn_safe_add
signatures, no typos, balanced braces).
- Could not complete a full project build in this environment: the
file transitively includes pthreadpool.h, an external dependency
fetched by XNNPACK's Bazel/CMake build that was not available
standalone in this sandbox. The verification above exercises the
exact inserted logic against the project's real overflow-checking
primitives rather than a reimplementation of them. Please confirm a
full build + the existing convolution-nchw sparse-path tests in CI.
melkap01-Arm
pushed a commit
to melkap01-Arm/XNNPACK
that referenced
this pull request
Oct 6, 2026
…-nchw.c
The sparse-weights path in xnn_create_convolution2d_nchw (the block-sparse
"spmm" packing branch, guarded by kernel_height == 1, kernel_width == 1,
groups == 1) computed packed_weights_size using raw C multiplication and
addition, with no overflow check:
const size_t packed_weights_size =
num_nonzero_blocks * 2 * sizeof(int32_t) +
num_output_channel_blocks * sizeof(uint32_t) +
((context->group_output_channels + num_nonzero_values) << log2_filter_element_size) +
XNN_EXTRA_BYTES;
num_nonzero_blocks, num_output_channel_blocks, and num_nonzero_values are
all derived from xnn_analyze_spmm's count of non-zero values in the
model's own weight tensor (spmm_packing_params.num_nonzeroes and
friends), combined with context->group_output_channels -- both
model-supplied. On a 32-bit target (WebAssembly, ARM32), a model whose
sparse weight tensor yields large enough counts for this expression
causes it to wrap size_t, producing an undersized allocation via
xnn_allocate_simd_memory. xnn_pack_spmm, called immediately after, then
writes based on the real, un-wrapped num_nonzero_blocks and
num_output_channel_blocks (it iterates over the same sparsity structure
that produced these counts), writing past the end of the undersized
buffer.
This is the same bug shape already fixed for the dense paths in this
codebase: create_igemm and create_deconvolution2d_nhwc (PR google#11184), and
create_dwconv_path / create_vmulcaddc_path in convolution-nhwc.c (PR
google#11240, "a model-supplied groups value near 107M causes the product to
wrap ... writes past the end of the buffer"). Found by sweeping this
same file and convolution-nhwc.c for other packed_weights_size
computations after reviewing google#11240: this file already has a properly
guarded packed_weights_size calculation in its dense
(conv2d_hwc2chw) path (chained xnn_safe_mul calls on
context->group_input_channels / kernel_height / kernel_width /
context->groups / packed_group_output_channels), confirming the
guarding convention is established here -- it just was not extended to
this sparse path.
I also traced a second candidate this same sweep surfaced,
resize-bilinear-nhwc.c's xnn_setup_resize_bilinear2d_nhwc, and confirmed
it is not concretely exploitable: its packed_weights_size recomputation
reads output_height/output_width from
resize_op->context.resize_nhwc_indirection_init, which is populated in
exactly one place in that file -- inside
xnn_reshape_resize_bilinear2d_nhwc, immediately after that function's
own xnn_safe_mul-guarded computation of the same product already
succeeded on the same values. So unlike this one, that recomputation
can never see values its own file's overflow check has not already
validated. No fix needed there.
Fix: replace the raw expression with chained xnn_safe_mul / xnn_safe_add
calls (matching the style already used a few functions away in this
same file, and in the two prior PRs above), returning
xnn_status_invalid_parameter with a logged error on overflow instead of
proceeding with an undersized allocation.
Verified:
- Reproduced the overflow dynamically: extracted the exact pre-fix
expression into a standalone harness using uint32_t to model size_t
on a 32-bit target. With num_nonzero_blocks =
num_output_channel_blocks = num_nonzero_values = 300000000 (a
plausible crafted-sparse-model value) and group_output_channels = 4,
the intended size is ~4.8 GB but the wrapped size_t computation
produces 505032736 -- undersized by exactly 2^32 bytes (a full
uint32_t wrap).
- Verified the fix closes it: transcribed the exact patch logic
(identical chain of xnn_safe_mul/xnn_safe_add calls) into the same
harness, using the real xnn_safe_mul/xnn_safe_add implementations
from src/xnnpack/math.h (__builtin_mul_overflow /
__builtin_add_overflow under the hood). The same malicious input is
correctly rejected; a normal, legitimate sparse-weights input (e.g.
1000 nonzero blocks, 64 output channel blocks) is correctly accepted
and produces the expected byte count.
- Syntax-verified the inserted block in isolation against the real
src/xnnpack/math.h header (correct xnn_safe_mul/xnn_safe_add
signatures, no typos, balanced braces).
- Could not complete a full project build in this environment: the
file transitively includes pthreadpool.h, an external dependency
fetched by XNNPACK's Bazel/CMake build that was not available
standalone in this sandbox. The verification above exercises the
exact inserted logic against the project's real overflow-checking
primitives rather than a reimplementation of them. Please confirm a
full build + the existing convolution-nchw sparse-path tests in CI.
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.
Both
create_dwconv_pathandcreate_vmulcaddc_pathinsrc/operators/convolution-nhwc.ccomputedpacked_weights_sizeusing raw C multiplication with no overflow check:On 32-bit targets (WebAssembly, ARM32) a model-supplied
groupsvalue near 107M causes the product to wrap to a small value, resulting in an undersized allocation. The subsequentpack_dwconv_hwg_w/pack_vmulcaddc_wcall iterates over the realgroupscount and writes past the end of the buffer.This fix replaces the raw multiplications with
xnn_safe_mul/xnn_safe_addguards, consistent with the approach used in PR #11184 forcreate_igemmandcreate_deconvolution2d_nhwc.