Repository navigation
Add MSan support for fibers and fix dragonbox struct padding - #102158
Conversation
The boost fiber implementation (`fiber_ucontext.hpp`) has MSan annotations (`__msan_start_switch_fiber`/`__msan_finish_switch_fiber`) around every `swapcontext` call. These annotations tell MSan about fiber stack switches so it can properly save/restore shadow memory state. However, they are gated on `BOOST_USE_MSAN`, which was never defined in the build. Without these annotations, MSan loses track of shadow memory during fiber context switches, causing false "use-of-uninitialized-value" reports for values that were written inside fibers — such as `TryResult` fields set by `ConnectionEstablisher::run` in the async connection path. Fixes: #101649 CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102114&sha=5b68859e4e2e0023ace05c14f750a827f75ed7f5&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 See also: #102114 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…fines.h Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Value-initialize `ret_value` in `dragonbox::compute_nearest` to zero the 4 bytes of tail padding in `fp_t<double,false,false>`. Without this, the uninitialized padding shadow propagates through stack slot reuse on fiber stacks and can contaminate unrelated values — causing MSan to report false "use-of-uninitialized- value" errors (e.g. in `shared_ptr` refcount decrements). This was the root cause of STID 4148-3044, which triggered the revert of PR #101621. The test verifies both the fix (padding is clean after `to_decimal`) and the underlying mechanism (default-init leaves padding uninitialized at offset 12). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
With BOOST_USE_MSAN properly defined (fiber switch annotations) and the dragonbox padding fix in place, the blanket __msan_unpoison on the fiber stack is no longer needed. Removing it allows MSan to catch real uninitialized-value bugs on fiber stacks. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
|
Workflow [PR], commit [343615b] Summary: ❌
AI ReviewSummaryThis PR reintroduces MSan fiber support by defining Findings
ClickHouse Rules
Final Verdict
|
… values Reverting the dragonbox change and restoring __msan_unpoison. Investigation showed that value-initializing dragonbox's ret_value does NOT fix the issue: MSan tracks shadow per-field in LLVM IR, not per-byte of the padded struct representation. When a struct with padding is returned by value, the padding bytes have no shadow entry and appear uninitialized in the caller — regardless of how the callee initializes them. This is a fundamental MSan limitation, not a dragonbox bug. Any struct with padding returned by value will exhibit this. On the main thread stack it is harmless (OS zero-inits pages), but on heap-allocated fiber stacks the dirty padding shadow propagates via stack slot reuse, causing false positives. The __msan_unpoison in FiberStack::allocate is the correct workaround. Updated the comment to explain why. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
| #pragma once | ||
| /// defines.h should be included before fiber.hpp | ||
| /// BOOST_USE_ASAN, BOOST_USE_TSAN and BOOST_USE_UCONTEXT should be correctly defined for sanitizers. | ||
| /// BOOST_USE_ASAN, BOOST_USE_MSAN, BOOST_USE_TSAN and BOOST_USE_UCONTEXT are defined via CMake for sanitizer builds. |
There was a problem hiding this comment.
Minor wording issue: BOOST_USE_UCONTEXT is not only enabled for sanitizer builds. In contrib/boost-cmake/CMakeLists.txt, it is also defined when BOOST_USE_UCONTEXT=ON (even without sanitizers).
Could you rephrase this comment to avoid implying BOOST_USE_UCONTEXT is sanitizer-only?
| /// MSan doesn't track shadow for struct padding bytes through return values | ||
| /// (the LLVM IR shadow is per-field, not per-byte of the padded representation). | ||
| /// Any struct with padding returned by value will have uninitialized padding shadow | ||
| /// in the caller. On the main thread stack this is harmless (OS zero-inits pages), | ||
| /// but on heap-allocated fiber stacks the dirty shadow can propagate via stack slot | ||
| /// reuse and eventually trigger false positives in unrelated code. |
There was a problem hiding this comment.
How it is possible?
We allocate stack with zero initialized memory and should not reuse it.
There was a problem hiding this comment.
As explained, returning a small (<=16 bytes) struct by value from a function, with some bytes belonging to padding.
There was a problem hiding this comment.
Why it does not triggered everywhere? There should be tons of such places.
On the main thread stack this is harmless (OS zero-inits pages),
but on heap-allocated fiber stacks the dirty shadow can propagate via stack slot
reuse and eventually trigger false positives in unrelated code.
And this part I also don't get, we initialize stack with aligned_alloc, it should be zero-initialized.
There was a problem hiding this comment.
If the stack memory happens to be zero-initialized (just by a chance of, e.g., using mmap), shouldn't matter for MSan. I also found that this "On the main thread stack this is harmless (OS zero-inits pages)" is pointless.
| FP local{}; | ||
| local.significand = 1; | ||
| local.exponent = -1; | ||
| EXPECT_EQ(__msan_test_shadow(&local, sizeof(local)), static_cast<intptr_t>(-1)) |
There was a problem hiding this comment.
This test is not reliable, it explicitly uses __msan_test_shadow
There was a problem hiding this comment.
It serves the purpose of explicitly checking what MSan has in the shadow memory, and what it does not have.
|
|
||
|
|
||
| /// Demonstrates the MSan limitation: struct padding is uninitialized in return values. | ||
| TEST(DragonboxMSan, ReturnValuePaddingShadowIsLost) |
There was a problem hiding this comment.
Does this test actually crash with msan error without the fix? Or are we taking claude's word that these EXPECT_EQ are representative of what happens in the real code? I asked claude to investigate the same (similar?) msan+fiber issue a week ago, and it came up with a similar supposed root cause and "fix", but couldn't produce a working test (without poking into msan api directly).
There was a problem hiding this comment.
It uses a function to check the MSan internals (is something belongs to "shadow"), without touching that memory.
There was a problem hiding this comment.
+1, I was also trying to make a repro but failed to do so w/o using internal MSan API and I don't think that this is what happens here
There was a problem hiding this comment.
without poking into msan api directly
This is what it does. A strange test, but I'll take it :)
…e positives On AArch64, LLVM loses MSan shadow for struct-padding bytes in return values (llvm/llvm-project#54476). On heap-allocated fiber stacks the dirty shadow accumulates across context switches and propagates via stack-slot reuse, triggering false positives in unrelated code (e.g. STID 1478-2063 in `system.text_log`). The one-time `__msan_unpoison` at fiber allocation is insufficient because `BOOST_USE_MSAN` (added in #102158) now properly preserves shadow across context switches — including the dirty padding shadow. Fix by calling `__msan_unpoison` on the whole fiber stack after every `__msan_finish_switch_fiber` on AArch64. This clears accumulated dirty padding without affecting the main thread stack. Fixes: #102241 CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=97744&sha=aa7dccac1cbacd8009ec368bdbf6045ff7bc3c29&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…on fibers This test reproduces MemorySanitizer "use-of-uninitialized-value" errors that occur on AArch64 in `vector::erase` called from `PoolWithFailoverBase::getMany`, running on a fiber stack (`AsyncTaskExecutor` → `boost::context`). The root cause: on the fiber's heap-allocated stack, compiler-generated temporaries from inlined functions leave dirty MSan shadow from struct padding. This shadow persists across function calls on the fiber stack and contaminates later stack allocations. The test runs concurrent distributed queries via `remote()` which go through `RemoteQueryExecutor` → `ConnectionPoolWithFailover` on fibers. See also: #102158 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
…lickHouse#101994, ClickHouse#102158, ClickHouse#101952, ClickHouse#102148, ClickHouse#102008, ClickHouse#102010) # Conflicts: # src/Common/AsynchronousMetrics.cpp
…lickHouse#101994, ClickHouse#102158, ClickHouse#101952, ClickHouse#102148, ClickHouse#102008, ClickHouse#102010) # Conflicts: # src/Formats/FormatSettings.h
…e positives On AArch64, LLVM loses MSan shadow for struct-padding bytes in return values (llvm/llvm-project#54476). On heap-allocated fiber stacks the dirty shadow accumulates across context switches and propagates via stack-slot reuse, triggering false positives in unrelated code (e.g. STID 1478-2063 in `system.text_log`). The one-time `__msan_unpoison` at fiber allocation is insufficient because `BOOST_USE_MSAN` (added in ClickHouse#102158) now properly preserves shadow across context switches — including the dirty padding shadow. Fix by calling `__msan_unpoison` on the whole fiber stack after every `__msan_finish_switch_fiber` on AArch64. This clears accumulated dirty padding without affecting the main thread stack. Fixes: ClickHouse#102241 CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=97744&sha=aa7dccac1cbacd8009ec368bdbf6045ff7bc3c29&name_0=PR&name_1=Stress%20test%20%28arm_msan%29 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Re-applies #101621 (which was reverted in #102005) with an updated understanding of the root cause.
BOOST_USE_MSANfor fiber context tracking: The boost fiber implementation has MSan annotations (__msan_start_switch_fiber/__msan_finish_switch_fiber) around everyswapcontextcall, but they were gated onBOOST_USE_MSANwhich was never defined. Without them, MSan loses track of shadow memory across fiber context switches, causing false positives like STID 4179-5154.Keep
__msan_unpoisonwith a correct explanation: The original PR #101621 removed__msan_unpoisonfromFiberStack::allocate, which caused STID 4148-3044 and the revert. Investigation traced the origin to struct padding in return values (e.g. dragonbox'sfp_t). Value-initializing the struct does not help: MSan tracks shadow per-field in LLVM IR, not per-byte of the padded representation. When any struct with padding is returned by value, the padding shadow is lost. This is a known unfixed LLVM limitation:struct addrinfoOn the main thread stack this is harmless (OS zero-inits pages), but on heap-allocated fiber stacks the dirty padding shadow propagates via stack slot reuse, causing false positives. The
__msan_unpoisoninFiberStack::allocateis the correct workaround. Updated the comment to explain why.A test is included that documents this MSan limitation.
CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=102114&sha=5b68859e4e2e0023ace05c14f750a827f75ed7f5&name_0=PR&name_1=Stress%20test%20%28arm_msan%29
See also: #102114
Fixes: #101649
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
...
Documentation entry for user-facing changes
Version info
26.4.1.695