Skip to content

Fix unaligned distance metadata loads [MOD-15303]#1001

Open
dor-forer wants to merge 1 commit into
mainfrom
dor-forer-MOD-15303-fix-misaligned-metadata-loads
Open

Fix unaligned distance metadata loads [MOD-15303]#1001
dor-forer wants to merge 1 commit into
mainfrom
dor-forer-MOD-15303-fix-misaligned-metadata-loads

Conversation

@dor-forer

@dor-forer dor-forer commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • load SQ8, INT8, and UINT8 trailing FP32 metadata through alignment-safe helpers in scalar and SIMD kernels
  • make integer normalization and test/reference metadata access alignment-safe
  • repair the SQ8-FP32 benchmark's storage/query argument order and array teardown
  • add explicit odd-dimension SQ8-FP32 regression coverage

Root cause

Trailing FP32 metadata follows byte-width vector payloads. For dimensions not divisible by four, casting payload + dimension to float * 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.sh
  • alignment/undefined sanitizer: 520 SQ8/SQ8-FP32/INT8/UINT8 distance tests
  • alignment/undefined sanitizer: affected component, INT8, UINT8, and index utility tests
  • AddressSanitizer + undefined sanitizer: SQ8-FP32 dimension-100 benchmark matrix (available AVX2-FMA, AVX2, SSE4, and scalar paths)

Deterministic alignment regression proof

Built the regression test with Clang 18.1.8 and fatal AddressSanitizer/alignment sanitization:

  • PR c724d32b: exited 0; SpacesTest.SQ8_FP32_odd_dim_unaligned_metadata_test passed.
  • Main dd6879e8, with only the test and safe reference-helper changes applied: exited 1.
  • UBSan reported the old production load in SQ8_FP32_InnerProduct_Impl:
src/VecSim/spaces/IP/IP.cpp:61:27:
runtime error: load of misaligned address 0x503000025691
for type 'const float', which requires 4 byte alignment

The forced address ends in ...691, proving it is not four-byte aligned. The sanitizer stack traces the failure from SQ8_FP32_InnerProduct_Impl directly 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_unaligned with byte offsets (sq8::* * sizeof(float)). Integer normalization writes the stored norm via memcpy instead 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 fixes delete[].

Adds SQ8_FP32_odd_dim_unaligned_metadata_test to 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.

@dor-forer
dor-forer marked this pull request as ready for review July 22, 2026 13:10
@dor-forer
dor-forer requested a review from lerman25 July 22, 2026 13:11
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