Skip to content

Fix UBSan null deref in ThreadStatus::flushUntrackedMemory - #104253

Merged
Algunenano merged 1 commit into
ClickHouse:masterfrom
Algunenano:fix-thread-status-ubsan
May 7, 2026
Merged

Algunenano merged 1 commit into
ClickHouse:masterfrom
Algunenano:fix-thread-status-ubsan

Conversation

@Algunenano

Copy link
Copy Markdown
Member

flushUntrackedMemory is a member function of ThreadStatus and the surrounding lines access the member untracked_memory directly. The new local on line 242 was reading current_thread->untracked_memory instead — in the typical case current_thread == this so the value is the same, but when current_thread is null (e.g. before TLS is set or inside paths where this != current_thread) UBSan flags a null pointer dereference. Use the member directly, matching the read/write of untracked_memory on the lines around it.

Reported by the function_prop_fuzzer unit test:

/ClickHouse/src/Common/ThreadStatus.cpp:242:54: runtime error: member access within null pointer of type 'ThreadStatus'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /ClickHouse/src/Common/ThreadStatus.cpp:242:54

CI report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104136&sha=latest&name_0=PR&name_1=Unit+tests+%28asan_ubsan%2C+function_prop_fuzzer%29

cc @azat

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):

...

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

`flushUntrackedMemory` is a member function of `ThreadStatus` and the
existing logic on the surrounding lines accesses the member
`untracked_memory` directly (line 238 reads it, line 243 zeroes it).
Line 242 was reading `current_thread->untracked_memory` instead; in the
typical case `current_thread == this` so the value is the same, but
when `current_thread` is null (e.g. before TLS is set or inside paths
where `this != current_thread`) UBSan flags a null pointer dereference.
Use the member directly, matching the read/write of `untracked_memory`
on the lines around it.

Found by `function_prop_fuzzer` unit test in CI:

  /ClickHouse/src/Common/ThreadStatus.cpp:242:54: runtime error: member access within null pointer of type 'ThreadStatus'
  SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /ClickHouse/src/Common/ThreadStatus.cpp:242:54

Report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104136&sha=latest&name_0=PR&name_1=Unit+tests+%28asan_ubsan%2C+function_prop_fuzzer%29

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [a4ae04d]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_debug, sequential) FAIL
03988_cached_read_big_at FAIL cidb IGNORED
Integration tests (arm_binary, distributed plan, 2/4) FAIL
test_storage_rabbitmq/test_shutdown.py::test_shutdown_rabbitmq_with_materialized_view FAIL cidb IGNORED
test_storage_rabbitmq/test_shutdown.py::test_idle_loop_shutdown_completes_promptly FAIL cidb IGNORED
test_storage_rabbitmq/test_shutdown.py::test_rabbitmq_virtual_column_table FAIL cidb IGNORED

AI Review

Summary

This PR fixes undefined behavior in ThreadStatus::flushUntrackedMemory by reading untracked_memory from this instead of dereferencing current_thread, which can be null in valid execution paths. The change is minimal, targeted, and consistent with surrounding logic.

ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ✅
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ✅
Compilation time ✅
No large/binary files ✅
Final Verdict
  • Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-ci label May 6, 2026
@alexey-milovidov alexey-milovidov self-assigned this May 6, 2026
@clickhouse-gh

clickhouse-gh Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.10% 91.10% +0.00%
Branches 76.60% 76.60% +0.00%

Changed lines: 100.00% (5/5) | lost baseline coverage: 1 line(s) · Uncovered code

Full report · Diff report

@alexey-milovidov

Copy link
Copy Markdown
Member

@groeneai, remove (or, optionally, fix) failed RabbitMQ tests and send a PR. Tag the author who tried to resurrect RabbitMQ tests recently, as it was a bad idea.

@groeneai

groeneai commented May 7, 2026

Copy link
Copy Markdown
Collaborator

@alexey-milovidov — fix PR up at #104273.

Removed tests/integration/test_storage_rabbitmq/test_shutdown.py entirely. The three failing tests on this PR's CI (test_shutdown_rabbitmq_with_materialized_view, test_idle_loop_shutdown_completes_promptly, test_rabbitmq_virtual_column_table) all live in that file. The only other test in the file was already @pytest.mark.skip'd for a known shutdown data race, so deleting the whole file doesn't disable any test that was actually running.

cc @fm4v (restored the test_storage_rabbitmq suite in #101967) and @jaapieaapie1 (added test_shutdown.py in #102711).

I'll rebase #104253 once #104273 lands so its CI clears.

@Algunenano

Copy link
Copy Markdown
Member Author

03988_cached_read_big_at -> #104291
test_storage_rabbitmq -> #104273.

@Algunenano
Algunenano added this pull request to the merge queue May 7, 2026
groeneai added a commit to groeneai/ClickHouse that referenced this pull request May 7, 2026
Per the bot review on PR ClickHouse#104273, replaces the wholesale deletion of
`tests/integration/test_storage_rabbitmq/test_shutdown.py` with
`@pytest.mark.skip` decorators on the three flaky tests, preserving the
test bodies for future stabilization or re-enable:

- `test_shutdown_rabbitmq_with_materialized_view`
- `test_idle_loop_shutdown_completes_promptly`
- `test_rabbitmq_virtual_column_table`

The fourth test in the file (`test_attach_detach_rabbitmq_with_materialized_view`)
remains marked as `@pytest.mark.skip` for the existing data-race reason.

This complies with the ClickHouse review rule that test coverage should
not be removed; flaky tests should be quarantined with an explicit
tracking link rather than deleted wholesale. Each skip reason links to
PR ClickHouse#104253 (the source of @alexey-milovidov's directive) and PR ClickHouse#104273
(this fix) for context and traceability.

Per directive on ClickHouse#104253
and bot review on ClickHouse#104273
Merged via the queue into ClickHouse:master with commit 16806ee May 7, 2026
162 of 165 checks passed
@Algunenano
Algunenano deleted the fix-thread-status-ubsan branch May 7, 2026 11:23
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label May 7, 2026
huldarchen pushed a commit to huldarchen/ClickHouse that referenced this pull request May 7, 2026
The three tests in `tests/integration/test_storage_rabbitmq/test_shutdown.py`
have been failing in CI:

- `test_shutdown_rabbitmq_with_materialized_view`
- `test_idle_loop_shutdown_completes_promptly`
- `test_rabbitmq_virtual_column_table`

The fourth test in the file (`test_attach_detach_rabbitmq_with_materialized_view`)
was already marked as `pytest.mark.skip` due to a known data race in
`StorageRabbitMQ` shutdown.

Per @alexey-milovidov's directive on PR ClickHouse#104253:
> "remove (or, optionally, fix) failed RabbitMQ tests and send a PR.
>  Tag the author who tried to resurrect RabbitMQ tests recently,
>  as it was a bad idea."

Removing the entire `test_shutdown.py` file rather than skipping its
contents, since the only non-skipped tests in it are flaky and the
remaining test was already non-runnable.

Context:
- The `test_storage_rabbitmq` suite was originally removed in PR ClickHouse#93107
  due to chronic flakiness, then restored in PR ClickHouse#101967 (2026-04-07).
- `test_shutdown.py` was added shortly after in PR ClickHouse#102711 (2026-04-21).

cc @alexey-milovidov @fm4v @jaapieaapie1
github-merge-queue Bot pushed a commit that referenced this pull request May 7, 2026
…bucket

Tests reading from https://clickhouse-public-datasets.s3.*.amazonaws.com/
can flake when AWS returns a transient retryable 5xx mid-stream: the
request is retried successfully, but the <Error> line emitted by
PocoHTTPClient is captured into *.stderr-fatal via --client_logs_file,
and the runner fails the test with "having stderror". Worst-affected is
03988_cached_read_big_at, which streams a 14 GiB parquet with
max_download_threads = 1.

Raise send_logs_level to fatal for the four tests that read this bucket
and have no other guard. Mirrors the precedent in
03271_s3_table_function_asterisk_glob.sql and ~130 other stateless tests.

Sample failure:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=104253&sha=a4ae04d39170c4d2d821aedb448667867bea5497&name_0=PR&name_1=Stateless%20tests%20%28amd_debug%2C%20sequential%29
PR: #104253

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

5 participants