Skip to content

bounds-check the padding tensor shape in hannk PadOp - #9716

Open
Nashit-h wants to merge 1 commit into
halide:mainfrom
Nashit-h:hannk-pad-shape-check
Open

Nashit-h wants to merge 1 commit into
halide:mainfrom
Nashit-h:hannk-pad-shape-check

Conversation

@Nashit-h

Copy link
Copy Markdown
Contributor

PadOp reads the padding tensor's buffer using the operand rank: padding(0, d) for d in [0, output rank) in map_bounds, and padding_buf(0/1, idx) for idx in [0, input rank) in execute. Both are only guarded by assert(), which is compiled out of release builds. The padding tensor is model data, and when it is constant its buffer points straight at the model's flatbuffer bytes, so a .tflite whose PAD padding tensor is not shaped [2, rank] reads past that buffer. On a -DNDEBUG -fsanitize=address build, a [2, 2] padding tensor with a rank-4 operand gives a heap-buffer-overflow READ 0 bytes past the 16-byte region.

This op was not among the ones covered by #9704 (Concatenation/Split/Gather/Transpose). Those check an axis attribute against a tensor's own rank; here a separate parameter tensor's shape must match the operand rank. The check goes in the PadOp constructor so both the tflite parser and the delegate are covered, matching the constructor-side checks #9704 added. Well-formed models construct and run exactly as before.

hannk has no unit-test harness for individual ops, so I verified this standalone against HalideBuffer.h: the padding(0, d) read trips ASan before the change, and after it the constructor rejects a [2, 2] padding tensor for a rank-4 operand while a valid [2, 4] tensor still constructs.

Checklist

  • Tests added or updated (not required for docs, CI config, or typo fixes)
  • Documentation updated (if public API changed)
  • Python bindings updated (if public API changed)
  • Benchmarks are included here if the change is intended to affect performance.
  • Commits include AI attribution where applicable (see Code of Conduct)

PadOp indexes the padding tensor's buffer using the operand rank, in
both map_bounds (padding(0, d)) and execute (padding_buf(0/1, idx)),
guarded only by assert(). In a release build a .tflite model whose PAD
padding tensor is not shaped [2, rank] reads past the padding buffer,
which points directly at the model's flatbuffer bytes.

Check the padding shape and the input/output ranks in the PadOp
constructor, so both the tflite parser and the delegate are covered.
Well-formed models are unaffected.
@codecov

codecov Bot commented Nov 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.27%. Comparing base (d7e0db3) to head (f0b7c8d).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #9716      +/-   ##
==========================================
+ Coverage   73.25%   73.27%   +0.02%     
==========================================
  Files         266      266              
  Lines       83749    83749              
  Branches    20572    20572              
==========================================
+ Hits        61352    61371      +19     
+ Misses      15845    15822      -23     
- Partials     6552     6556       +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch has not been deployed

No deployments
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