Repository navigation
Fix null pointer dereference in EmbeddedRocksDB truncate() and drop() - #106940
Conversation
StorageEmbeddedRocksDB::truncate() and drop() called rocksdb_ptr->Close() without checking for null, while every other accessor (write, optimize, the read iterator, getStatistics, multiGet, totalRows, totalBytes) guards the handle with a null check. The handle can legitimately be null: truncate() sets rocksdb_ptr to null, removes and recreates the data directory, and then calls initDB(). For a read_only table whose data was wiped this reopen fails (OpenForReadOnly on an empty directory throws), so the table object survives with rocksdb_ptr == nullptr. A subsequent TRUNCATE, or the DROP path (which goes through truncate() when the table directory is removed), then dereferenced the null handle and crashed the server with SIGSEGV. Guard both Close() calls with a null check so dropping or truncating such a table no longer crashes. Reproduced on master: a read_only EmbeddedRocksDB table over a populated directory, truncated twice, crashed at StorageEmbeddedRocksDB.cpp:299 (Received signal 11, NULL pointer read). This matches the serverfuzz crash STID 2954-31f0 whose innermost frame was StorageEmbeddedRocksDB::drop() reached via DatabaseCatalog::dropTableFinally. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session: cron:clickhouse-worker-slot-7:20260610-084800 |
|
cc @rschu1ze @CurtizJ could you review this? It guards the two unchecked |
|
Workflow [PR], commit [2f3d6a4] Summary: ✅
AI ReviewSummaryThis PR guards the two remaining null-sensitive Final VerdictStatus: ✅ Approve |
|
@groeneai where did you find this? Always attach the URL logs. |
|
@nikitamikhaylov found via CIDB monitoring of serverfuzz BuzzHouse runs. Two STIDs, same root cause (the unguarded STID 2954-406b - UBSan
STID 2954-31f0 - SIGSEGV (NULL pointer read), innermost frame
Reduced to a deterministic repro: a Session: cron:clickhouse-worker-slot-1:20260610-141200 |
CI summary for HEAD
|
| Check | Failure | Classification |
|---|---|---|
BuzzHouse (arm_asan_ubsan) |
Mutation of \Memory` table produced incomplete output` (STID 2380-3043) |
Chronic trunk bug, unrelated. 81 distinct PRs + 12 master hits / 30d. Active fix in flight (PR #106615). Not this PR. |
Stress test (arm_asan_ubsan) |
Hung check failed, possible deadlock found (Close WriteBufferFromAzureBlobStorage) |
Chronic Azure/stress flake. 40 PRs + 90 master hits / 7d. Not this PR. |
Mergeable Check / PR |
aggregator | Downstream of the two above. |
The RocksDB-only diff does not touch Memory-table mutation, Azure I/O, or any stress/deadlock path. Config Workflow re-ran green; all builds, Fast test, Stateless, the RocksDB regression test 04330_rocksdb_truncate_drop_null_ptr, AST fuzzer and Integration tests pass.
The regression test is isolated by CLICKHOUSE_DATABASE and the per-database RDB_DIR, and uses no failpoints, shared caches or SYSTEM DROP, so it does not need no-parallel and can run in the faster parallel pool. Replace "DETACH TABLE rdb_rw" with "DROP TABLE rdb_rw SYNC". DETACH left a detached table definition behind that the final cleanup did not remove, making the test non-idempotent in a fixed database (a restart could reattach rdb_rw after the directory was removed). DROP closes the writable RocksDB handle but leaves the explicit rocksdb_dir on disk, so the read_only table can still open it. The now-redundant trailing "DROP TABLE IF EXISTS rdb_rw" is removed. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate (test change addressing review feedback in 08d50b4)
Session id: cron:clickhouse-worker-slot-27:20260610-194400 |
|
CI finished on HEAD
Fast test, Style check, and Bugfix validation are green on this HEAD. |
|
@groeneai The segfaults are NEVER an infrastructure noise. When seeing this, try to fix or find the PR that fixes it and link. Write this down to your memory and apply all the time. |
|
@nikitamikhaylov Recorded, and I will apply it to all CI triage: for any segfault, reproduce and fix it, or find the PR that fixes it and link it. I will not file a segfault as "noise" again. This PR is itself an example of that: the EmbeddedRocksDB SIGSEGV from serverfuzz (STID 2954-31f0) was reproduced locally, root-caused to the null On my earlier comment about STID 0883-693e: calling it "infrastructure noise" without showing the analysis was wrong. Here is the evidence I should have posted. This specific failure is the ASan internal-allocator-under-host-OOM signature, not a ClickHouse bug:
Going forward: reproduce+fix or find+link for every segfault. The only thing I set aside is this exact ASan-allocator-OOM host signature, and only with this kind of evidence attached, never as a bare "noise" claim. |
CI status — all failures triaged (commit 08d50b4)CI has fully finished on the review-fix HEAD (DROP instead of DETACH, no-parallel tag removed). The two red checks are not caused by this RocksDB-only diff:
The fix guards |
CI summary, covered HEAD
|
| Check | Failure | Classification | Disposition |
|---|---|---|---|
| Stateless tests (arm_asan_ubsan, azure, sequential) | Segmentation fault (STID 0883-693e), then Server died |
Inherited trunk corruption (multistage distributed-queries thread-teardown UAF, STID 0883-* family) |
Not PR-caused. This HEAD predates the master revert #107122 (merged 01:37 UTC 06-11), so it inherited the now-fixed corruption. The arm_asan_ubsan, azure, sequential check is clean on master across 55 commits since 03:00 UTC 06-11 (0 failures); the same pre-revert window shows this family on 16+ unrelated PRs as singletons. A rebase past 01:37 UTC 06-11 clears it. |
| Integration tests (amd_llvm_coverage, 3/5) | job-level failure, no individual test row recorded in CIDB | Flaky/infra (coverage instrumentation batch) | Not PR-caused (this PR only touches StorageEmbeddedRocksDB::truncate/drop, unrelated to the integration batch). |
No PR-caused failures. The fix (null rocksdb_ptr dereference in truncate()/drop(); regression test 04330) is unaffected.
|
@hanfei1991 Like to review this one? |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered by tests: 10/10 (100.00%) | Lost baseline coverage: none · Uncovered code |
The regression test for the EmbeddedRocksDB truncate()/drop() null-handle fix (merged in ClickHouse#106940) created a persistent read_only table over a relative user_files directory and TRUNCATEd it. A stress-test server restart then re-attached that poisoned read_only table whose data dir had been wiped, so the server refused to start (ROCKSDB_ERROR, "Cannot start clickhouse-server"). Run the whole scenario inside a single one-shot clickhouse-local process so the poisoned read_only table never persists into server metadata, and place the data dir under CLICKHOUSE_TMP with a unique name so parallel runs never collide (drops the no-parallel tag). A SELECT count() over the read_only table runs before the first TRUNCATE and must print 1: it proves the table actually opened over the data the rw table persisted (the EmbeddedRocksDB read_only path was reached) rather than --ignore-error silently swallowing a broken setup, so the trailing 'ok' only appears when the null-handle path was genuinely exercised. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Description
StorageEmbeddedRocksDB::truncate()anddrop()callrocksdb_ptr->Close()without a null check, unlike every other accessor in the class (write,optimize, the read iterator,getStatistics,multiGet,totalRows,totalBytes), all of which guard the handle.The handle can legitimately be null.
truncate()nullsrocksdb_ptr, removes and recreates the data directory, then callsinitDB(). For aread_onlytable whose data was wiped, that reopen fails (OpenForReadOnlyon an empty directory throws), so the table object survives withrocksdb_ptr == nullptr. A laterTRUNCATE, or theDROPpath (which routes throughtruncate()when the table directory is removed), then dereferenced the null handle and crashed the server with SIGSEGV.This matches the serverfuzz crash STID 2954-31f0 whose innermost frame was
StorageEmbeddedRocksDB::drop()reached viaDatabaseCatalog::dropTableFinally.Fix
Guard both
Close()calls with a null check, so dropping or truncating such a table no longer crashes.Regression test
04330_rocksdb_truncate_drop_null_ptr: open aread_onlytable over a populated directory, truncate it twice and drop it. Without the fix the server crashes atStorageEmbeddedRocksDB.cpp:299(signal 11, NULL pointer read); with the fix the server survives.No related open issue found.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fix a server crash (null pointer dereference) when running
TRUNCATEorDROPon anEmbeddedRocksDBtable whose RocksDB handle was released, for example aread_onlytable whose data directory was emptied by a priorTRUNCATE.Version info
26.6.1.951