Skip to content

Fix packed weights size overflow in create_dwconv_path and create_vmulcaddc_path - #11240

Merged
copybara-service[bot] merged 1 commit into
google:masterfrom
destro4evr-rgb:fix-packed-weights-overflow-dwconv-vmulcaddc
Sep 18, 2026
Merged

copybara-service[bot] merged 1 commit into
google:masterfrom
destro4evr-rgb:fix-packed-weights-overflow-dwconv-vmulcaddc

Conversation

@destro4evr-rgb

@destro4evr-rgb destro4evr-rgb commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Both create_dwconv_path and create_vmulcaddc_path in src/operators/convolution-nhwc.c computed packed_weights_size using raw C multiplication with no overflow check:

packed_weights_size = ((primary_tile << log2_filter_element_size) +
                       bias_element_size + extra_weights_bytes) *
                      c_stride;

On 32-bit targets (WebAssembly, ARM32) a model-supplied groups value near 107M causes the product to wrap to a small value, 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.

This fix replaces the raw multiplications with xnn_safe_mul / xnn_safe_add guards, consistent with the approach used in PR #11184 for create_igemm and create_deconvolution2d_nhwc.

…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
destro4evr-rgb force-pushed the fix-packed-weights-overflow-dwconv-vmulcaddc branch from 4d998da to 4010442 Compare September 18, 2026 11:36
@copybara-service
copybara-service Bot merged commit 434b6e2 into google:master Sep 18, 2026
8 checks passed
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.
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