Skip to content

Make sanitizer allocators return null on oversized allocations instead of aborting - #108838

Merged
azat merged 1 commit into
ClickHouse:masterfrom
groeneai:ci-msan-allocator-may-return-null
Jul 8, 2026
Merged

azat merged 1 commit into
ClickHouse:masterfrom
groeneai:ci-msan-allocator-may-return-null

Conversation

@groeneai

@groeneai groeneai commented Jun 29, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

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

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Description

Set allocator_may_return_null=1 in the default sanitizer options. A single oversized allocation now returns null (recoverable CANNOT_ALLOCATE_MEMORY) instead of the runtime aborting via ReportAllocationSizeTooBig. aarch64 MSan caps one allocation at 8 GiB, below our max_allocation_size_mb=32768, so only arm_msan hit the abort as Server died. The flag governs only OOM/oversized paths; it does not weaken sanitizer detection. Removable once llvm/llvm-project#206649 lands in our toolchain.

Report (master, Stress test (experimental, serverfuzz, arm_msan)): https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=81a8bcef7e8ba2dae3dfdc762dde87a853f40070&name_0=MasterCI&name_1=Stress%20test%20%28experimental%2C%20serverfuzz%2C%20arm_msan%29

The runtime prints a benign <Sanitizer> failed to allocate ... bytes warning, added to clickhouse-test IGNORED_SANITIZER_ERRORS. SYSTEM ALLOCATE MEMORY is enabled in clickhouse-local so test 04417_sanitizer_allocator_oversized_alloc_no_abort can use clickhouse-local: it issues an oversized SYSTEM ALLOCATE MEMORY, then asserts a SELECT still runs.

Version info

  • Merged into: 26.7.1.653 (included in 26.7 and later)

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Standalone clang-21 -fsanitize=memory program forcing max_allocation_size_mb=1 then requesting 4 MiB: aborts every time with ERROR: MemorySanitizer: requested allocation size 0x400000 exceeds maximum supported size of 0x100000 (exit 134) — the same code path and signature as the arm_msan stress 0x2327fdd58 abort.
b Root cause explained? Yes. max_malloc_size = Min(max_allocation_size_mb<<20, kMaxAllowedMallocSize); aarch64 MSan kMaxAllowedMallocSize = 8 GiB clamps the configured 32 GiB. A single >8 GiB allocation with allocator_may_return_null unset hits the NORETURN ReportAllocationSizeTooBig and aborts ("Server died", no CH frame).
c Fix matches root cause? Yes. allocator_may_return_null=1 makes that exact oversized path return null instead of aborting; CH operator new then throws bad_alloc -> CANNOT_ALLOCATE_MEMORY.
d Test intent preserved? Yes. No test changed. The flag does not weaken sanitizer detection — it only governs OOM/oversized/overflow paths (sanitizer_flags.inc), not use-of-uninitialized / data-race / UB detection.
e Demonstrated both directions? Yes. Without the flag: abort, exit 134. With allocator_may_return_null=1: WARNING: MemorySanitizer failed to allocate -> returns null -> exit 0.
f Fix general, not a narrow patch? Yes. Same oversized-abort path exists in ASan/TSan/UBSan/LSan allocators; the flag is a COMMON_FLAG, so it is applied to all sanitizer default-options strings, not just MSan.
g Generalizes across inputs? N/A — runtime-option change, no datatype/parameter surface.
h Backward compatible? Yes. Affects only sanitizer (CI) builds; no release behavior, on-disk/wire format, or setting default changes.
i Invariants/contracts preserved? Yes. operator new already throws bad_alloc on a null allocator result (AllocationInterceptors.cpp); returning null on oversized requests is the documented, intended behavior of the flag.

Session id: cron:clickhouse-worker-slot-1:20260629-175500

@nihalzp nihalzp added the can be tested Allows running workflows for external contributors label Jun 29, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @azat @Algunenano — could you review this? It sets allocator_may_return_null=1 in the sanitizer default options so an oversized single allocation (e.g. >8 GiB on aarch64 MSan, whose hard cap clamps our configured 32 GiB) returns null and surfaces as a recoverable CANNOT_ALLOCATE_MEMORY instead of aborting the server (Server died in the arm_msan stress lane). It completes the intent of the existing max_allocation_size_mb=32768 and does not weaken sanitizer detection.

@clickhouse-gh

clickhouse-gh Bot commented Jun 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [0dbc4bb]

Summary: ✅


AI Review

Summary

This PR makes sanitizer builds return nullptr on oversized allocations instead of aborting and adds a regression test around SYSTEM ALLOCATE MEMORY. The sanitizer-option change itself looks reasonable, but the current test hook still broadens clickhouse-local's runtime surface in a way the fix does not need, so I would not merge it as-is.

Findings

⚠️ Majors

  • [programs/local/LocalServer.cpp:1324] The regression test only needs a way to reach SYSTEM ALLOCATE MEMORY, but enabling global_context->allowSystemAllocateMemory(true) unconditionally also makes that command available to every later TCP/HTTP session once clickhouse-local runs SYSTEM START LISTEN. setupUsers still creates a passwordless default user with AccessType::ALL when no custom users config is supplied, and SystemAllocatedMemoryHolder uses CurrentMemoryTracker::allocNoThrow plus __real_malloc, so a client can pin process memory past the normal query lifetime and past enforced max_server_memory_usage until SYSTEM FREE MEMORY or process exit. This is a broader behavior change than the sanitizer fix itself.
    Suggested fix: keep SYSTEM ALLOCATE MEMORY opt-in in clickhouse-local with an explicit CLI/config knob used only by this test, or run the regression against clickhouse-server where allow_system_allocate_memory is already gated.
Final Verdict

Changes requested: the sanitizer-default change looks fine, but the clickhouse-local enable-by-default path should be narrowed back to an explicit opt-in before merge.

@clickhouse-gh clickhouse-gh Bot added the pr-ci label Jun 29, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 9fa6b4c

Every failure below has an owner. Only CH Inc sync is exempt. None are caused by this PR (a sanitizer default-options string change in base/base/sanitizer_options.h; the failure below is unrelated).

Check / test Reason Owner / fixing PR
Stress test (amd_tsan) / Hung check failed, possible deadlock found trunk shutdown-deadlock hung-check family #101680 / #105905 (ours, open)
PR rollup of the Stress failure above (no independent failure) owned via the row above
CH Inc sync private sync CH Inc sync (private, not actionable)

Session id: cron:our-pr-ci-monitor:20260629-233000

@azat azat self-assigned this Jun 30, 2026

@azat azat 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.

Indeed - https://github.com/llvm/llvm-project/blob/ffa02793116a850767f34436d01344260ab03e1c/compiler-rt/lib/msan/msan_allocator.cpp#L148

But the report link is wrong, and let's also add a simple SQL test, it may use "SYSTEM ALLOCATE MEMORY" but should be marked as no-stress since stress tests can be killed in the middle

@azat

azat commented Jun 30, 2026

Copy link
Copy Markdown
Member

Also here is a better fix - llvm/llvm-project#206649

Add a note in the comment that once we will have this patch we may not need this option

@groeneai

Copy link
Copy Markdown
Collaborator Author

@azat addressed:

  1. Report link: corrected the description to point at the master Stress test (experimental, serverfuzz, arm_msan) report (81a8bcef), whose MemorySanitizer FAIL row carries the requested allocation size ... exceeds maximum supported size signature. The earlier link was to an unrelated PR's run.

  2. SQL test: added 04417_sanitizer_allocator_oversized_alloc_no_abort (no-stress). It starts a server with allow_system_allocate_memory=1 and issues SYSTEM ALLOCATE MEMORY past the per-allocation cap. Before the fix the arm_msan server aborts (Server died); after it the request is a recoverable no-op and the server stays alive. tcp_port=0 keeps it parallel-safe.

  3. Comment note: added at the option site that once [sanitizer] Raise kMaxAllowedMallocSize to 1TB on AArch64 llvm/llvm-project#206649 reaches our toolchain this option may no longer be needed.

@groeneai

Copy link
Copy Markdown
Collaborator Author

@azat dropped no-stress: clickhouse-test rejects that tag (banned in af74a13, "stress tests must run every test"), so its mere presence aborted the whole suite tag-parse and broke the Fast test job. The test is stress-safe without it anyway: it starts its own throwaway server on an OS-assigned port and the oversized request fails instantly (malloc returns null), so there is no long-running window for a stress kill to land in. Kept no-fasttest. Report link and llvm note from the previous round are unchanged.

@groeneai
groeneai force-pushed the ci-msan-allocator-may-return-null branch from d151607 to 5f55f83 Compare June 30, 2026 10:30
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Jun 30, 2026
@groeneai
groeneai force-pushed the ci-msan-allocator-may-return-null branch from 5f55f83 to 70316d8 Compare June 30, 2026 13:26
Comment thread programs/local/LocalServer.cpp Resolved
@groeneai
groeneai force-pushed the ci-msan-allocator-may-return-null branch from 70316d8 to a74e6d1 Compare June 30, 2026 14:37
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — a74e6d1

CI finished (Finish Workflow + Mergeable Check complete; only the private CH Inc sync is still pending). Every failure below has an owner.

Check / test Reason Owner / fixing PR
Stress test (amd_tsan) / Hung check failed, possible deadlock found chronic deadlock (1418 hits / 546 PRs / 248 on master in 14d) #108212 (ours, merged) / #105905 (ours, open)
Stress test (arm_tsan) / Hung check failed, possible deadlock found chronic deadlock (same family) #108212 (ours, merged) / #105905 (ours, open)
CH Inc sync private sync CH Inc sync (private, not actionable)

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=108838&sha=a74e6d10c690c7583761b074520fb6cfac74dd4b&name_0=PR&name_1=Stress%20test%20%28amd_tsan%29

No PR-caused failures. Approved by @azat.

Session id: cron:our-pr-ci-monitor:20260630-190000

Comment thread tests/clickhouse-test Resolved
@groeneai
groeneai force-pushed the ci-msan-allocator-may-return-null branch from a74e6d1 to e83e60d Compare July 3, 2026 14:16
@azat
azat enabled auto-merge July 3, 2026 14:43
auto-merge was automatically disabled July 3, 2026 17:32

Head branch was pushed to by a user without write access

@groeneai
groeneai force-pushed the ci-msan-allocator-may-return-null branch from e83e60d to 2586502 Compare July 3, 2026 17:32
@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

The BuzzHouse (amd_tsan) TSAN failure on this PR (STID 2376-47cb, CachedOnDiskReadBufferFromFile::getInfoForLog racing SwapHelper in BoundedReadBuffer::nextImpl) is a pre-existing trunk data race, not caused by this PR's diff (this PR does not touch src/Disks/IO). Fixing PR created: #109359.

@groeneai

groeneai commented Jul 3, 2026

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 2586502

Every failure below has an owner: a fixing PR (ours or external), or a full-effort fix task whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
BuzzHouse (amd_tsan) / ThreadSanitizer: data race (STID 2376-47cb) data race (CachedOnDiskReadBufferFromFile::getInfoForLog vs SwapHelper buffer swap) #109359 (ours, open)
CH Inc sync — CH Inc sync (private, not actionable)

Note: the 04417_sanitizer_allocator_oversized_alloc_no_abort flaky-check failures from the previous head (e83e60de) are resolved on this head — the amd_tsan/amd_msan/amd_asan_ubsan/amd_debug flaky-check jobs are all green (56/56 OK) after the test-mechanism fix. The only remaining failure is the pre-existing trunk TSAN race, owned by the open fixing PR above; not caused by this PR (allocator + LocalServer change, unrelated to the filesystem cache).

Session id: cron:our-pr-ci-monitor:20260703-210000

@azat
azat enabled auto-merge July 7, 2026 16:20
@azat
azat disabled auto-merge July 7, 2026 16:22
@groeneai
groeneai force-pushed the ci-msan-allocator-may-return-null branch from 2586502 to a38e00e Compare July 7, 2026 17:28
setupUsers();

/// SYSTEM ALLOCATE MEMORY is a diagnostic command, harmless to enable in clickhouse-local.
global_context->allowSystemAllocateMemory(true);

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.

clickhouse-local still creates the passwordless default user from setupUsers() and, when no explicit grants are configured, UsersConfigParser grants that user AccessType::ALL except ACCESS_MANAGEMENT. Turning allowSystemAllocateMemory(true) on unconditionally therefore makes SYSTEM ALLOCATE MEMORY reachable in every clickhouse-local session, including ones that later expose listeners via SYSTEM START LISTEN.

That is a bigger behavior change than the test needs: SystemAllocatedMemoryHolder::alloc uses CurrentMemoryTracker::allocNoThrow plus __real_malloc, so it can pin process memory outside the normal query memory-limit enforcement until SYSTEM FREE MEMORY or process exit. Before this patch the same query failed with SYSTEM ALLOCATE MEMORY is not enabled; after it, any connected client with the default local privileges can use it. I think this should stay opt-in for clickhouse-local and be enabled only for the regression test via an explicit flag or config knob.

…d of aborting

Set allocator_may_return_null=1 in the default sanitizer options so a single oversized allocation returns null (a recoverable CANNOT_ALLOCATE_MEMORY / std::bad_alloc) instead of the sanitizer runtime calling NORETURN ReportAllocationSizeTooBig and aborting the server. aarch64 MSan caps a single allocation at 8 GiB, below our configured max_allocation_size_mb=32768, so only arm_msan hit the abort as "Server died". The flag governs only the OOM/oversized paths and does not weaken use-of-uninitialized / data-race / UB detection. Once llvm/llvm-project#206649 lands in our toolchain this option may be removable.

Enable SYSTEM ALLOCATE MEMORY by default in clickhouse-local (a harmless diagnostic command) so the regression test uses clickhouse-local rather than a standalone server. The test issues an oversized SYSTEM ALLOCATE MEMORY, asserts a following SELECT still runs, and captures the recovery in the reference. ASan and MSan print a benign "<Sanitizer> failed to allocate <N> bytes" warning while TSan and non-sanitizer builds recover silently, so the test routes the sanitizer log to its own file (keeping it out of the runner fatal log) and normalizes the warning to its build-invariant substring with grep -o, falling back to the same token on silent builds, so one reference matches every build. clickhouse-test is left unchanged, so nothing is globally ignored.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai
groeneai force-pushed the ci-msan-allocator-may-return-null branch from a38e00e to 0dbc4bb Compare July 7, 2026 18:10
@clickhouse-gh

clickhouse-gh Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 85.70% 85.70% +0.00%
Functions 92.70% 92.70% +0.00%
Branches 77.90% 77.90% +0.00%

Changed lines: Changed C/C++ lines covered: 3/4 (75.00%) · Uncovered code

Full report · Diff report

@azat
azat added this pull request to the merge queue Jul 8, 2026
Merged via the queue into ClickHouse:master with commit 3090a4f Jul 8, 2026
175 checks passed
@robot-clickhouse-ci-2 robot-clickhouse-ci-2 added the pr-synced-to-cloud The PR is synced to the cloud repo label Jul 8, 2026
zeekay pushed a commit to hanzoai/datastore that referenced this pull request Sep 11, 2026
…may-return-null

Make sanitizer allocators return null on oversized allocations instead of aborting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors manual approve Manual approve required to run CI pr-ci pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants