Skip to content

CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build - #109950

Merged
fm4v merged 2 commits into
masterfrom
fix-rust-asan-combined-sanitize
Jul 10, 2026
Merged

fm4v merged 2 commits into
masterfrom
fix-rust-asan-combined-sanitize

Conversation

@tiandiwonder

@tiandiwonder tiandiwonder commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Closes: #104692
Caused by: #99657
Related: https://github.com/ClickHouse/clickhouse-private/issues/63613
Related: bytecodealliance/wasmtime#13857

Problem

Sporadic SEGVs at CompressedReadBufferBase::readCompressedData on addresses "not mapped to object" keep appearing in ASan CI (#104692) — across unrelated configurations (s3, azure, db disk, with and without distributed cache), on random columns and queries, surviving multiple read-path fixes. Analyzed occurrence: CI report.

Additionally, since the switch to combined ASan+UBSan builds all Rust code in sanitizer CI has been silently compiled without any sanitizer.

Root cause

When #99657 switched CI to combined builds (-DSANITIZE=address,undefined), it added the new value to the C++ flag dispatch in cmake/sanitize.cmake:

+    elseif (SANITIZE STREQUAL "address,undefined")
+        set (ASAN_UBSAN_FLAGS "-fsanitize=address,undefined -fsanitize-address-use-after-scope ...")

but the Rust flag dispatch in contrib/corrosion-cmake/CMakeLists.txt was left matching the old value only:

if (SANITIZE STREQUAL "memory")
    ...
elseif (SANITIZE STREQUAL "thread")
    ...
elseif (SANITIZE STREQUAL "address")          # <-- "address,undefined" falls through
    set(RUST_CARGO_BUILD_STD "-Zbuild-std=std,panic_abort,core,alloc")
    list(APPEND RUSTFLAGS "-Zsanitizer=address")
endif()

so -Zsanitizer=address was silently lost for the asan_ubsan builds. Without it cargo does not expose CARGO_CFG_SANITIZE=address, wasmtime's build script does not emit --cfg asan, and wasmtime's own ASan guard in lazy_per_thread_init (crates/wasmtime/src/runtime/vm/sys/unix/signals.rs) is disarmed — so it installs a per-thread 256 KiB sigaltstack on every thread that executes a WASM UDF. That stack's TLS destructor munmaps the memory without sigaltstack(SS_DISABLE) (upstream bug: bytecodealliance/wasmtime#13857), leaving a dangling kernel registration, and ASan's UnsetAlternateSignalStack unmaps the same — by then reused — address range at thread exit:

pool thread T (ran a WASM UDF)                      query thread Q
──────────────────────────────                      ─────────────────────────────
wasmtime lazy_per_thread_init:
  mmap W (260 KiB) + sigaltstack(W)      kernel: T's altstack = W
... minutes later the thread pool reaps T ...
(1) Rust TLS dtor Stack::drop:
    munmap(W) — no SS_DISABLE            kernel: T's altstack = W (dangling)
                                                    ASan secondary allocator reuses the
                                                    freed range for a ~244 KiB prefetch
                                                    buffer; recv() fills it — succeeds,
                                                    pages are mapped at that moment
(2) ASan UnsetAlternateSignalStack:
    queries the stale W and
    UnmapOrDie(W) → munmap #2 destroys
    the pages of the live buffer
T dies                                              Q consumes the prefetch and reads
                                                    byte 0 of the buffer (block checksum
                                                    in readCompressedData zero-copy path)
                                                    → SIGSEGV, SEGV_MAPERR

The crash stack always belongs to the first reader of the destroyed buffer, never the culprit — for a MergeTree prefetch buffer that first read is CompressedReadBufferBase::readCompressedData, hence the fixed signature on random victims. Established from the decrypted core of the 2026-07-07 occurrence: the faulted buffer was still registered as allocated in ASan's LargeMmapAllocator (no free involved), exactly one of 2994 live large chunks was damaged, and the destroyed page range measured exactly one wasmtime sigaltstack allocation (guard page + 64×4096 bytes).

Fix

Match SANITIZE STREQUAL "address,undefined" in the same contrib/corrosion-cmake dispatch — the single place where the Rust workspace RUSTFLAGS are assembled (the MSan/TSan branches live there too). SANITIZE=address stays supported: the fuzzer build and local pure-ASan builds still pass it. This restores real ASan instrumentation of Rust code and re-arms wasmtime's cfg!(asan) guard, so the custom sigaltstack is not installed at all in ASan builds; the dangling-registration defect itself is reported upstream (bytecodealliance/wasmtime#13857).

Verified locally with SANITIZE=address,undefined: the wasmtime build script emits cargo:rustc-cfg=asan again, all Rust crates and the full server build and run, and under strace executing WASM UDFs no longer allocates the custom signal stack (the mmap(266240) fingerprint is gone) while the UDFs keep working.

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Version info

  • Merged into: 26.7.1.742 (included in 26.7 and later)
  • Backported to: 26.6.2.59, 26.5.6.33, 26.4.5.121, 26.3.17.38, 25.8.29.13

When the CI switched to combined ASan+UBSan builds (SANITIZE=address,undefined),
cmake/sanitize.cmake got an explicit branch for the combined value, but the Rust
side (contrib/corrosion-cmake) still matched SANITIZE STREQUAL "address" only.
Since then the sanitizer CI builds have compiled all Rust code without ASan.

Besides losing the instrumentation, this disarmed wasmtime's own ASan guard:
without CARGO_CFG_SANITIZE=address its build scripts do not emit --cfg asan, so
lazy_per_thread_init installs a per-thread 256 KiB sigaltstack. Its Rust TLS
destructor munmaps that stack without sigaltstack(SS_DISABLE), and ASan's
UnsetAlternateSignalStack at the same thread's exit then unmaps the same - by
that time reused - address range, destroying pages of live heap allocations.
This is the source of the sporadic CompressedReadBufferBase::readCompressedData
SEGVs on unmapped addresses in ASan CI: the victim is whatever large allocation
(usually a read buffer) reuses the freed range between the two munmaps. Root
cause established from the decrypted core of the 2026-07-07 occurrence: the
faulted buffer was still registered as allocated in the ASan secondary
allocator (never freed), exactly one of 2994 live large chunks was damaged, and
the destroyed page range measured exactly one wasmtime sigaltstack allocation
(guard page + 64*4096 bytes).

Verified locally: with SANITIZE=address,undefined the wasmtime build script now
emits cargo:rustc-cfg=asan, all Rust crates build with -Zsanitizer=address, the
full server links and runs, and executing WASM UDFs no longer allocates the
custom signal stack (checked with strace; the mmap(266240) fingerprint is gone
while the UDFs keep working).

Closes: #104692
Caused by: #99657
Related: ClickHouse/clickhouse-private#63613
@clickhouse-gh

clickhouse-gh Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [39dfbf8]

Summary: ✅


AI Review

Summary

This PR fixes the Rust-side sanitizer dispatch so SANITIZE=address,undefined now propagates -Zsanitizer=address the same way the existing address build does. I did not find any remaining contract gap in the current diff: the changed branch is the relevant Rust flag assembly point, there are no existing review threads to reconcile, and the PR discussion plus CI status provide enough evidence for this small CMake fix.

Final Verdict
  • Status: ✅ Approve

@tiandiwonder tiandiwonder changed the title Pass -Zsanitizer=address to Rust in the combined address,undefined build CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build Jul 10, 2026
@Algunenano Algunenano self-assigned this Jul 10, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is a great finding. I think this fixes #106370 but we can keep it open and monitor. I spent quite some time investigating the issue, even looking at rust and specifically wasm multiple times, and did not notice it.

Let's backport it too please

@Algunenano Algunenano added the pr-must-backport Pull request should be backported intentionally. Use this label with great care! label Jul 10, 2026
@fm4v
fm4v added this pull request to the merge queue Jul 10, 2026
Merged via the queue into master with commit 20894ec Jul 10, 2026
176 checks passed
@fm4v
fm4v deleted the fix-rust-asan-combined-sanitize branch July 10, 2026 11:35
@robot-ch-test-poll robot-ch-test-poll added the pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore label Jul 10, 2026
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added the pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR label Jul 10, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 10, 2026
clickhouse-gh Bot added a commit that referenced this pull request Jul 10, 2026
Backport #109950 to 26.6: CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build
Algunenano added a commit that referenced this pull request Jul 10, 2026
Backport #109950 to 25.8: CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build
Algunenano added a commit that referenced this pull request Jul 10, 2026
Backport #109950 to 26.3: CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build
Algunenano added a commit that referenced this pull request Jul 10, 2026
Backport #109950 to 26.4: CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build
Algunenano added a commit that referenced this pull request Jul 10, 2026
Backport #109950 to 26.5: CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build
joelynch pushed a commit to aiven/ClickHouse that referenced this pull request Aug 17, 2026
…ust in the combined address,undefined build
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…ust in the combined address,undefined build
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…ust in the combined address,undefined build
kewin-robetti pushed a commit to viasoftkorp/ClickHouse that referenced this pull request Aug 24, 2026
…ust in the combined address,undefined build
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…mbined-sanitize

CI: Pass -Zsanitizer=address to Rust in the combined address,undefined build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-backports-created Backport PRs are successfully created, it won't be processed by CI script anymore pr-ci pr-must-backport Pull request should be backported intentionally. Use this label with great care! pr-must-backport-synced The `*-must-backport` labels are synced into the cloud Sync PR pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flaky test: 03710_parallel_alter_comment_rename_selects

6 participants