Fix unaligned distance metadata loads [MOD-15303]#1001
Open
dor-forer wants to merge 1 commit into
Open
Conversation
dor-forer
marked this pull request as ready for review
July 22, 2026 13:10
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.
Summary
Root cause
Trailing FP32 metadata follows byte-width vector payloads. For dimensions not divisible by four, casting
payload + dimensiontofloat *creates a misaligned pointer and undefined behavior. The SQ8-FP32 benchmark also supplied its FP32 query and SQ8 storage in reverse order, producing an out-of-bounds read.Impact
Odd-dimensional SQ8/SQ8-FP32 and INT8/UINT8 cosine distance calculations no longer rely on misaligned FP32 loads, including dispatched SIMD implementations and strict-alignment architectures.
Validation
./check-format.shDeterministic alignment regression proof
Built the regression test with Clang 18.1.8 and fatal AddressSanitizer/alignment sanitization:
c724d32b: exited0;SpacesTest.SQ8_FP32_odd_dim_unaligned_metadata_testpassed.dd6879e8, with only the test and safe reference-helper changes applied: exited1.SQ8_FP32_InnerProduct_Impl:The forced address ends in
...691, proving it is not four-byte aligned. The sanitizer stack traces the failure fromSQ8_FP32_InnerProduct_Impldirectly to the odd-dimension regression test.Jira: MOD-15303
Note
Medium Risk
Touches hot-path distance math across many SIMD backends; behavior should be equivalent but incorrect offset math would skew all SQ8/integer cosine results for affected dimensions.
Overview
Fixes undefined behavior when VecSim reads trailing FP32 metadata (SQ8 min/delta/sum, INT8/UINT8 cosine norms) after byte-sized payloads: odd dimensions leave metadata at addresses that are not 4-byte aligned, so
reinterpret_cast<const float *>loads were unsafe.Scalar and SIMD inner product / L2 paths for SQ8↔FP32, SQ8↔SQ8, and INT8/UINT8 cosine now use
load_unalignedwith byte offsets (sq8::* * sizeof(float)). Integer normalization writes the stored norm viamemcpyinstead of a misaligned float store. Reference helpers, unit tests, and the SQ8-FP32 benchmark follow the same pattern; the benchmark also swaps storage vs query to match API argument order and fixesdelete[].Adds
SQ8_FP32_odd_dim_unaligned_metadata_testto exercise deliberately misaligned storage blobs against scalar and dispatched kernels.Reviewed by Cursor Bugbot for commit c724d32. Bugbot is set up for automated code reviews on this repo. Configure here.