Skip to content

[operators, subgraph] Harden operator reshape workspace initialization and subgraph tensor rank/axis validation - #11459

Open
anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-operator-workspace-and-subgraph-validation
Open

anagnorisis2peripeteia wants to merge 1 commit into
google:masterfrom
anagnorisis2peripeteia:fix-operator-workspace-and-subgraph-validation

Conversation

@anagnorisis2peripeteia

Copy link
Copy Markdown
Contributor

Summary

This PR addresses several edge-case defects across operator reshape routines and subgraph runtime reshaping:

  1. Operator Reshape Workspace Sizing & Safety:

    • fully-connected-nc, batch-matrix-multiply-nc: Initialize *workspace_size = 0 unconditionally upon entry when workspace_size != NULL.
    • dynamic-fully-connected-nc: Set *workspace_size = 0 when batch_size == 0, and check if (workspace_size != NULL) prior to computing workspace requirements.
    • resize-bilinear-nhwc: Set *workspace_size = 0 when batch_size == 0.
    • pack-lh: Set *output_size_bytes = 0 when num_groups == 0 || batch_size == 0.
    • reduce-nd: Set *workspace_size = 0 when num_output_elements == 0. In addition:
      • Guard SIMD zero padding buffer allocation and workspace size calculation against integer overflow using xnn_safe_mul and xnn_safe_add.
      • Guard scale calculation against division by zero when num_reduction_elements == 0.
  2. Subgraph Rank & Axis Reshape Validation:

    • static-constant-pad: Record num_padding_dims in xnn_operator_data and zero-initialize pre/post padding buffers. Validate during reshape that input_value->shape.num_dims >= opdata->num_padding_dims.
    • static-slice: Record num_slice_dims in xnn_operator_data and zero-initialize begins/ends buffers. Validate during reshape that input_value->shape.num_dims == opdata->num_slice_dims.
    • concatenate: Validate axis bounds (axis < 0 || (size_t) axis >= num_dims) early in reshape_concatenate_operator before accessing input shapes or strides. Also fix duplicate word typo in error log ("failed to reshape reshape operator operator").
  3. Tests:

    • Added unit tests for zero workspace sizing in batch-matrix-multiply-nc, dynamic-fully-connected-nc, reduce-nd, and resize-bilinear-nhwc.
    • Added subgraph validation tests for invalid axis reshape in concatenate, rank mismatch in static-slice, and input rank less than padding rank in static-constant-pad.

@anagnorisis2peripeteia
anagnorisis2peripeteia force-pushed the fix-operator-workspace-and-subgraph-validation branch from 16b9147 to 3efdbd7 Compare October 1, 2026 00:52
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