Skip to content

ci: run lance-linalg's tests under AddressSanitizer nightly - #8878

Merged
Xuanwo merged 5 commits into
lance-format:mainfrom
LuciferYang:ci/linalg-asan
Oct 23, 2026
Merged

Xuanwo merged 5 commits into
lance-format:mainfrom
LuciferYang:ci/linalg-asan

Conversation

@LuciferYang

@LuciferYang LuciferYang commented Aug 30, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

A linalg-asan job in nightly_run.yml runs cargo test -p lance-linalg --tests under -Zsanitizer=address.

The public entry points already validate the lengths a caller passes: sum_4bit_dist_table asserts dists.len() >= n, and hamming_batch_u64 asserts the output length with a comment explaining why it is not a debug_assert. What nothing checks is the pointer arithmetic the kernels then do inside those bounds. _mm256_storeu_si256(dists.as_mut_ptr().add(16), ...) in the AVX2 dist-table kernels and *results_ptr.add(1) through .add(3) in hamming_batch_avx2 are raw stores at offsets the kernel computes itself, so an off-by-one in a chunk count writes past the buffer while every entry assert still passes.

It lands in nightly_run.yml rather than rust.yml because -Zsanitizer needs a nightly toolchain, and the only nightly this repo uses is in daily-code-coverage.yml, which is schedule plus workflow_dispatch. The pin is the same one that file uses, so there is one nightly date in the repo rather than two.

Three details in the job are load-bearing and each is there for a reason a reader would otherwise have to rediscover.

RUSTUP_TOOLCHAIN rather than rustup default. A rustup override file outranks the default toolchain, and rust-toolchain.toml pins a stable release, so rustup default nightly-... followed by cargo test runs the sanitizer flag into a stable compiler and stops at error: the option 'Z' is only accepted on the nightly compiler. daily-code-coverage.yml solves the same problem the other way, with cargo +nightly-....

RUSTFLAGS repeats the baseline from .cargo/config.toml. Setting the variable replaces that file's per-target rustflags wholesale rather than appending, so leaving it at just the sanitizer flag would instrument a build no release ships, and would compile out the arms gated on target_feature = "avx2". One of those is where the batch call that writes into a heap buffer lives (cosine_batch_avx512 into vec![0.0; batch.len() / dimension]), which is the case a sanitizer detects best.

--target x86_64-unknown-linux-gnu keeps the sanitizer off build scripts and proc macros, which run on the host.

There is no protoc step, unlike every other Rust job here. lance-linalg's dependency closure contains no prost-build or tonic-build, dev-dependencies included, and the job runs green without it.

Test plan

On aarch64-apple-darwin, with one deliberate 2-byte store planted past the 32-slot output of sum_dist_table_32bytes_batch_neon, ASan reports heap-buffer-overflow, WRITE of size 2 at dist_table.rs with the chain up through sum_4bit_dist_table_uninit to the test. The same binary without the sanitizer dies with a bare SIGTRAP and no location, so what this job adds on that mutation is the location rather than the detection.

It has run for real on Linux, four times, on a fork branch with the github.repository guard dropped since that guard is what stops it running anywhere but here.

Two clean runs: 33305364459 and, after dropping the protoc step, 33312991796. Both green, about a minute and a half to build and three to four minutes to run.

Then two runs with a deliberate out-of-bounds store planted in a kernel, to check the claim that this job names a file and a line. That is where the ASAN_SYMBOLIZER_PATH step comes from. Without it the report was detected but useless:

WRITE of size 4 at 0x7cb5cf675100 thread T428
    #0 0x5620f81ba20f  (/home/runner/work/lance/lance/target/.../lance_linalg-...+0x...)
    #1 0x5620f81b1619  (/home/runner/work/lance/lance/target/.../lance_linalg-...+0x...)

ubuntu-24.04 ships llvm-symbolizer only under a versioned name, and ASan looks for the unversioned one on PATH. With the step, the same overflow reports:

WRITE of size 4 at 0x7ce193dcf280 thread T428
    #0 ... sum_hacc_dist_table_32bytes_batch_avx2 rust/lance-linalg/src/simd/dist_table.rs:479:5
    #1 ... sum_4bit_hacc_dist_table_avx2          rust/lance-linalg/src/simd/dist_table.rs:361:21
    #2 ... sum_4bit_hacc_dist_table_uninit        rust/lance-linalg/src/simd/dist_table.rs:244:13
    #3 ... sum_4bit_hacc_dist_table               rust/lance-linalg/src/simd/dist_table.rs:214:14
    #4 ... tests::test_sum_4bit_hacc_dist_table_matches_u16_reference_multi_batch

So the failing test names itself in the stack, which is why the run does not need --test-threads=1 to attribute a report.

That exercise also turned up a coverage limit worth stating. I first planted the store in sum_dist_table_32bytes_batch_avx2 and the job stayed green: on a runner with AVX-512BW, sum_4bit_dist_table_uninit selects the AVX-512 C kernel, so the Rust AVX2 one never executes. The hacc entry point has no AVX-512 kernel, which is why the planted store there did fire. So on an AVX-512 runner this job covers the hacc AVX2 kernel, the scalar paths and everything else in the crate, but not the AVX2 dist_table kernel that a smaller host would take.

One gap remains: the C is not instrumented. dist_table.c compiles without -fsanitize=address, so the AVX-512 dist_table kernel is not covered. I tried CFLAGS=-fsanitize=address and it broke a host link in lance-arrow's build script, so it is left out rather than half-applied.

Scope note

--tests selects the lib's unit tests and every integration target, so tests/panic_location.rs runs under the sanitizer too, and a target added later is included without editing this job.

@github-actions github-actions Bot added A-ci CI / build workflows ci Github Action or Test issues labels Aug 30, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 30, 2026
@LuciferYang

Copy link
Copy Markdown
Contributor Author

Revised after another review pass, which found the justification overstated and two steps dead.

"a write past the end is bounds-checked nowhere" was false, and the code says so: sum_4bit_dist_table asserts dists.len() >= n and hamming_batch_u64 asserts its output length with a comment about why it is not a debug_assert. The class this job actually catches is narrower and is what I measured: the kernels compute their own store offsets inside those bounds, so an off-by-one in a chunk count writes past the buffer with every entry assert still passing.

The CC: clang comment claimed build.rs needs it for dist_table.c and amx_fp16.c. Only the first is true. find_amx_fp16_compiler probes LANCE_AMX_FP16_CC, then cc, then hardcoded clang-18/17/16, so CC does not gate it, and its requirement is -mamx-fp16 -mamx-tile rather than -march=sapphirerapids. Comment now names one file. CXX is gone: build.rs compiles no C++.

The protoc step is gone too. cargo tree -p lance-linalg --edges normal,build,dev has no prost-build or tonic-build, and I re-ran the job on the fork without it to be sure rather than reasoning about it: run 33312991796, green, 457 passed. --lib dropped as redundant with --tests.

@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 30, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 30, 2026
@LuciferYang

Copy link
Copy Markdown
Contributor Author

A fourth pass asked a question I had not: the job claims it makes an overflow "fail with a file and a line", and nothing had ever exercised the failure path. So I planted a deliberate out-of-bounds store on a fork branch and looked at what the log actually says.

It was detected and unusable. Bare hex addresses and module offsets, no function names, no line numbers: ubuntu-24.04 ships llvm-symbolizer only under a versioned name and ASan looks for the unversioned one on PATH. There is now a step that locates the versioned binary and exports ASAN_SYMBOLIZER_PATH, and with it the same overflow reports sum_hacc_dist_table_32bytes_batch_avx2 rust/lance-linalg/src/simd/dist_table.rs:479:5 plus the whole chain down to the test that triggered it. Full before and after in the PR body. Since the test names itself in the stack, the run does not need --test-threads=1 for attribution.

The same exercise found a coverage limit I have added to the body rather than papered over. My first attempt planted the store in sum_dist_table_32bytes_batch_avx2 and the job stayed green, because on a runner with AVX-512BW that entry point selects the AVX-512 C kernel and the Rust AVX2 one never runs. The hacc entry point has no AVX-512 kernel, which is why the store there did fire.

Also from that pass: rustup toolchain install "$RUSTUP_TOOLCHAIN" so the nightly date appears once in the job instead of twice, plus RUST_BACKTRACE: 1 to match rust.yml and --locked to match daily-code-coverage.yml.

Two suggestions from it I am declining, with reasons. Notifications: no job in this workflow has any, and adding issue-filing on failure would need issues: write, which contradicts this job being contents: read. Moving the job into daily-code-coverage.yml to share the pin: that file has a different runner size and cron, and the pin duplication is now one line in each file rather than two here.

@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 30, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 30, 2026
@LuciferYang

Copy link
Copy Markdown
Contributor Author

One correction from another pass, on a claim that was backwards rather than merely loose.

The CC comment said a lesser compiler "would quietly shrink what this job covers". On a runner with AVX-512 the opposite happens: when dist_table.c compiles, kernel_support = "avx512_dist_table" is set and the AVX-512 C kernel runs, which the sanitizer does not instrument. Without a capable compiler that arm is cfg-ed out and the Rust path runs, which it does. So CC decides which of the two an AVX-512 host takes, and the comment now says that instead of ranking them.

Also folded two copies of the nightly-toolchain explanation into one; the header pointed at daily-code-coverage.yml and so did the RUSTUP_TOOLCHAIN comment twenty lines down.

@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 31, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 31, 2026
@LuciferYang

Copy link
Copy Markdown
Contributor Author

One false claim removed. The CC comment said this "instruments the same objects CI builds elsewhere", which is wrong on feature selection: linux-build passes --features "${ALL_FEATURES}", so it compiles f16.c and bf16.c with fp16kernels on, while this job runs default features and does not. The causal half of the sentence was right and is kept.

The difference is deliberate and now says so: enabling fp16kernels would route the f16 and bf16 dispatch into C that RUSTFLAGS cannot instrument, so default features leave the sanitizer more Rust to check, not less. dist_table.c and amx_fp16.c have no feature gate, so the kernel family the planted out-of-bounds store lived in is covered either way.

@lance-gatekeeper lance-gatekeeper Bot removed the K-approved Latest Gatekeeper recommendation permits acceptance. label Aug 31, 2026
@lance-gatekeeper lance-gatekeeper Bot removed the K-risk Latest Gatekeeper recommendation includes a non-blocking risk. label Aug 31, 2026
lance-gatekeeper[bot]

This comment was marked as outdated.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Aug 31, 2026
@LuciferYang

Copy link
Copy Markdown
Contributor Author

Three review passes on this job have now come back with nothing, and the last two went at it by measuring rather than reading, so the evidence is worth leaving here.

Round 8 compiled a probe against the job's exact flags to confirm the all(target_feature = "avx2", target_feature = "fma") arms really are selected, rather than trusting the comment that says so: rustc --target x86_64-unknown-linux-gnu -C target-cpu=haswell -C target-feature=+avx2,+fma,+f16c --print cfg emits both, and the same command without the flags emits neither. It also derived the 458-test count independently of the fork run, 448 on darwin plus the 10 #[test] items in dot_f16.rs gated on all(kernel_support = "amx_fp16", target_os = "linux"), and noted the count becomes 448 if a future clang ever fails the -mamx-fp16 probe.

Round 9 attacked the case that would matter most and is easiest to miss: whether the job can go green without testing anything. A compile error inside a #[cfg(test)] block gives cargo test exit 101, so the step fails. A vacuous --tests is possible in cargo, with [lib] test = false and no tests/ directory it exits 0 with a no-op warning, but it cannot happen here: this manifest has no [lib] section, src/ carries 259 test attributes, and --tests does not select the seven harness = false criterion benches. And a sanitizer report does fail the step rather than printing while the process exits clean: ASan's default exitcode survives detect_leaks=0, libtest runs tests as threads in one process, so the runtime kills the harness and cargo reports the target failed.

One incidental finding, not worth a change: build.rs emits cargo:rustc-cfg=feature="nightly" whenever RUSTUP_TOOLCHAIN starts with nightly, which is an undeclared feature. Nothing under rust/ reads that cfg and unexpected_cfgs is warn in the workspace lints, so with no -D warnings here it cannot fail the build. daily-code-coverage.yml already takes the same path.

@lance-gatekeeper lance-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 17, 2026

@lance-gatekeeper lance-gatekeeper Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Gate recommendation: approve with a non-blocking risk.

After the base refresh and version-independent toolchain comment, the Rust-side sanitizer job remains a useful, scoped check for internal pointer-arithmetic errors.

The author-retained limit is unchanged: native C kernels are not instrumented, including the AVX-512 dist-table path selected on capable hosts. Default features leave the f16/bf16 paths in Rust. No further change is requested for this pull request.

@lance-gatekeeper lance-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 17, 2026
Xuanwo added a commit that referenced this pull request Oct 23, 2026
## What this changes

`qemu-pre-haswell` already builds `lance-linalg`'s lib tests in release
mode. This adds one step that runs that same binary a second time, on
the runner's own CPU instead of under qemu, by overriding the cargo
runner to `env` for that step only. The build step and the new run move
above the QEMU setup steps, and the job's `name:` changes because it now
does two things.

## Why: no PR job runs the dispatched SIMD kernels in an optimized build

The job emulates a Nehalem, and every runtime feature gate in
`lance-linalg` tests for `avx`, `avx2`, `fma`, or an `avx512*` feature.
A Nehalem has none of them, so `SIMD_SUPPORT` resolves to `None` and
every dispatch takes its fallback arm. That is correct for a SIGILL
check, but no job a pull request runs executes the dispatched kernels in
an optimized build: the jobs that run tests use the `ci` profile, which
inherits `dev`. The nightly `compat-sequence` job builds a release wheel
and builds and searches an IVF_PQ index over float32 vectors, but it
runs once a night, gates no pull request, and checks recall rather than
comparing kernels against a reference.

Running the binary that is already on disk closes that gap, and it
closes it for free. What it catches is a kernel that computes a wrong
result only under optimized code generation, which `lance-linalg`'s
comparisons against the scalar reference flag. A memory error inside a
kernel is not guaranteed to crash in either profile; that is what the
sanitizer job in #8878 is for.

## Cost

Nothing beyond the test run itself. Measured locally: after `cargo test
--release -p lance-linalg --lib --no-run`, changing only the runner env
var and running again reports every crate `Fresh` and recompiles
nothing. The runner override is not an input to cargo's build
fingerprint. The job's existing 60 minute budget already covers a
from-scratch release build.

## What the native run does not cover

Worth stating so the next reader does not assume more than is there.

The binary is built with the job's `RUSTFLAGS: "-C
target-cpu=x86-64-v2"`, and env `RUSTFLAGS` replaces the per-target
`rustflags` in `.cargo/config.toml` wholesale. So `#[cfg(target_feature
= "avx2")]` code is compiled out of it, and only the runtime-dispatched
kernels are reachable, since `#[target_feature(enable = "avx2")]` is
additive per function. Both runs use default features, so `fp16kernels`
is off.

Which dispatch tier the native run takes depends on the runner's CPU,
and GitHub does not contract a CPU model.
`test_x86_runtime_feature_report` writes the detected features to
stderr, so the job log records what actually happened rather than
leaving it to be inferred.

## Why the new step sits before the QEMU build

A step runs only if the ones before it succeeded, and the QEMU steps
include building QEMU 8.2.10 from source on a cache miss. With the build
and the native run first, a QEMU failure cannot take the native signal
down with it. Nothing in the QEMU setup is a prerequisite for the cargo
build: `--no-run` never invokes the runner, and `lance-linalg`'s
`build.rs` needs only a C compiler.

The move does not disturb the two ordering constraints in the job.
`Swatinem/rust-cache` still precedes the first cargo invocation, and
`Restore QEMU 8.2.10` still precedes both steps that read its
`cache-hit` output.

## The rename, and the one line of Rust

The job id `qemu-pre-haswell` is unchanged, so the `rust-cache` key,
which is derived from `github.job`, is unaffected. Only the display name
changes.

`rust/lance-linalg/src/distance/dot_f16.rs` referred to the job by its
display name, which the rename would have left pointing at a string that
exists nowhere in the repository. It now names the job id, matching what
`rust/lance-linalg/src/simd/i32.rs` already does.

## Test plan

An x86_64 run with `RUSTFLAGS="-C target-cpu=x86-64-v2"` and the
pass-through runner, the closest local equivalent of the native step,
confirms three things at once: the v2 baseline builds, `env` works as a
cargo runner, and `test_x86_runtime_feature_report` is visible without
`--nocapture`. The resulting step order was checked against both
ordering constraints above. The AVX2 and AVX-512 arms were compiled but
not executed locally, since the host is aarch64 and an x86_64 build
under Rosetta reports neither feature; they first execute when this job
runs on a real x86 runner.

## A gap this leaves open

No job runs `lance-linalg` in an optimized profile with `--features
fp16kernels`. Turning it on compiles C, which would cost a rebuild and
undo the zero-cost property above, so it belongs in its own change.

Co-authored-by: Xuanwo <[email protected]>
@Xuanwo
Xuanwo merged commit 7b6d8ad into lance-format:main Oct 23, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ci CI / build workflows ci Github Action or Test issues K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants