Repository navigation
dnn: add QLinearConv models for onnx parseQConv bias fold tests - #1445
Merged
Merged
Conversation
4 of 6 tasks
asmorkalov
pushed a commit
to opencv/opencv
that referenced
this pull request
Oct 21, 2026
dnn: bound per-channel w_scale to bias length in onnx parseQConv 🤖🤖🤖 - #30395 OpenCV Extra: opencv/opencv_extra#1445 `QLinearConv` import folds a constant int32 bias to float in `parseQConv` (`modules/dnn/src/onnx/onnx_importer2.cpp`): each bias element is multiplied by its per-channel weight scale and written into `bias_float`, which is sized from the bias tensor. The fold loop runs over the length of `w_scale`, but `w_scale` and the bias are independent inputs of the model, so a 1-D `w_scale` longer than the bias writes past `bias_float`. It is reached from `readNetFromONNX` on any `.onnx` carrying such a `QLinearConv`, and the scalar `x_scale` read is likewise unguarded against an empty tensor. ASan on the default engine loading a model whose bias has one element and whose per-channel `w_scale` has 64: ``` ==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 4 #0 ONNXImporter2::parseQConv(...) #1 ONNXImporter2::parseNode(...) #2 ONNXImporter2::parseGraph(...) #5 readNetFromONNX2(...) #6 readNetFromONNX(...) 0x... is located 0 bytes after 4-byte region allocated by Mat::convertTo in parseQConv ``` Require the per-channel `w_scale` to hold one entry per bias element before the loop, the per-axis quantization invariant that pairs one scale and one bias with each output channel, and check the scalar `x_scale` is non-empty before the read. A well-formed model is unaffected; a mismatched one now throws from the importer and the net loads empty instead of overrunning the buffer. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [ ] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake
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.
Test data for opencv/opencv#30395.
Adds two load-only
QLinearConvmodels and their generator ingenerate_onnx_models.py. Both carry constantx_scale,w_scaleand int32B, so the importer takes the path that folds the bias to float. No reference input/output data.qconv_bias_scale_mismatch.onnx: per-channelw_scalewith 64 entries,Bwith 1. Before: the fold loop runs overw_scaleand writes past the bias buffer (ASan heap-buffer-overflow). After: the importer rejects it and the net loads empty.qconv_empty_x_scale.onnx:x_scalewith zero elements. Before:x_scale[0]is read from an empty tensor (segfault). After: rejected, net loads empty.Both pass
onnx.checker.check_model, since it does not check that the scale and bias lengths agree.