fix(index): reject RabitQ indices whose dimension is not a multiple of 8 - #8869
Open
LuciferYang wants to merge 1 commit into
Open
fix(index): reject RabitQ indices whose dimension is not a multiple of 8#8869LuciferYang wants to merge 1 commit into
LuciferYang wants to merge 1 commit into
Conversation
LuciferYang
force-pushed
the
fix/rq-dist-table-bounds
branch
from
August 29, 2026 16:12
13a991f to
4b62154
Compare
LuciferYang
force-pushed
the
fix/rq-dist-table-bounds
branch
from
August 29, 2026 16:18
4b62154 to
d4dfdaf
Compare
Contributor
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The loader rejects byte-misaligned dimensions before any FastScan path can violate its LUT-size safety invariant, while preserving the format emitted by current writers. The regression test also verifies the public InvalidInput failure.
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.
What this changes
RabitQuantizationStorage::try_from_batchnow rejects an index whoserotated_dimis not a multiple of 8, andsum_4bit_dist_table_uninitgains thedebug_assert!its own# Safetysection already promises.Why
The FastScan LUT is allocated
4 * code_dimbytes, while the kernels index it asBATCH_SIZE * code_dim.div_ceil(8)withBATCH_SIZE = 32. Those agree only whencode_dim % 8 == 0, and then exactly, with no slack. Forcode_dim = 8q + rwith1 <= r <= 7the table is32 - 4rbytes short: 28 atr = 1, 4 atr = 7.RabitQuantizer::buildhas rejected a non-multiple since #6024, so no current writer can produce one. But IVF_RQ shipped in #4344 and that gate landed five months later, andtry_from_batchvalidatesnum_bitsand the code byte width without ever checking the dimension.binary_code_bytes()is itselfdiv_ceil(8), so acode_dim = 100index with 13-byte codes passes the existing width check.Loading one is not a clean failure.
sum_4bit_dist_table_uninitreads the LUT through_mm256_loadu_si256(dist_table.as_ptr().add(i))on AVX2, the equivalent on AVX-512, andvld1q_u8on NEON, none of which is length-checked, so all three read past the allocation and return distances computed from whatever follows. Only the scalar fallback panics. This isApproxMode::Normal, the default query path.What changes for callers
An index in that state stops loading and returns
invalid_inputnaming the dimension. It used to load and give wrong distances on any host with SIMD, so failing is the better of the two, but it is a behaviour change for anyone holding such an index rather than a pure hardening.The
debug_assert!sum_4bit_dist_table_uninit's# Safetysection lists four obligations and the body checked three. The missing one is the table bound, which is the only one no slice re-checks. Its siblingsum_4bit_hacc_dist_table_uninithas had the equivalent all along.Test plan
test_try_from_batch_rejects_dim_not_multiple_of_eightbuilds metadata atcode_dim = 12with a matching 2-byte code column, so it clears the existing width check, and asserts the load fails with a message naming the dimension.Measured: replacing the new condition with
if falsetakes that test to failed. With the fix,cargo test --profile ci -p lance-index --lib vector::bqis 223 passed and-p lance-linalg --lib simd::dist_tableis 9 passed.cargo fmt --all -- --checkandcargo clippy -p lance-linalg -p lance-index --all-targets -- -D warningsare clean.Not covered: the over-read itself. Reproducing it needs a host with SIMD and an index that current code cannot write, so the test pins the rejection rather than the behaviour it prevents.
Provenance
Found while re-checking an old note of mine that claimed the shortfall was a fixed 16 bytes and that the fix belonged at index creation. Both were wrong: the shortfall varies with the dimension, and the creation-time gate already exists. The read path was the part still open.