Repository navigation
Fix signed integer overflow in RefreshTask attempt log_comment - #108005
alexey-milovidov merged 4 commits into
Conversation
When a refreshable materialized view retries a failed refresh, executeRefresh formats an '(attempt N/total)' string where total = refresh_retries + 1. refresh_retries is an Int64 setting that accepts any value (including Int64 max), so refresh_retries + 1 overflows for large values, which is undefined behavior (UBSan: signed integer overflow at RefreshTask.cpp:938). Compute the total in UInt64 to avoid the overflow, and omit the meaningless denominator when retries is negative (the documented infinite-retries case, which previously printed '/0'). Co-Authored-By: Claude Opus 4.8 <[email protected]>
Pre-PR validation gate
Session id: cron:clickhouse-worker-slot-2:20260619-233900 |
|
cc @al13n321 @tuanpach — could you review this? It fixes a signed-integer overflow in |
|
Workflow [PR], commit [07b96e4] Summary: ✅
AI ReviewSummaryThis PR fixes the Final Verdict
|
|
CI finished on
("CH Inc sync" is fork-sync infra noise.) The fix itself is green: the new |
|
@groeneai, provide a fix for StatisticsCountMinSketch. |
|
@alexey-milovidov I investigated the Crash: The count_min code is sound (so a guard there would only mask the corruption):
Evidence it is external corruption, likely a residual of the merge UAF you fixed in #107451:
I can keep hunting the residual corruptor, but I cannot reproduce it and the core is unsymbolizable, so I would need it to recur with a usable core to pin the writer. Do you want me to instrument/stress the (cron:clickhouse-worker-slot-2:20260620-193900) |
…htask-refresh-retries-int64-overflow
…htask-refresh-retries-int64-overflow
|
Merged the latest Pushed Sole CI red was an unrelated master-wide breakage, now fixed by the mergeThe previous run's only failures were Root cause: it was introduced by #105591 and reverted by
That revert is included in the The PR remains approved (@alexey-milovidov), AI-Review is ✅ Approve, and there are no unresolved review threads — blocked only on the fresh CI run. |
|
Re-checked after the Sole completed CI failure is an unrelated flake
The rest of CI is still in progress on the fresh merge. The PR remains approved (@alexey-milovidov), AI-Review is ✅ Approve, and there are no unresolved review threads — blocked only on the fresh CI run completing. @groeneai, there is no tracking issue for the |
|
@alexey-milovidov This is the #93018 recurrence, already being fixed in your #108854. Its |
|
Re-checked the fresh CI on All three current reds are known, unrelated flakes:
The two Hung-check reds cannot be caused by this PR: the change is a pure Blocked only on the fresh CI run completing and a maintainer rerun/merge of the flaky reds. |
…nt64-overflow Pull in the fix for the flaky '03164_parallel_replicas_range_filter_min_max' distributed-query test (PR ClickHouse#108854, 'Unexpected packet from server') to refresh CI.
|
Merged the latest The key reason for this refresh: #108854 ("Fix flaky distributed query 'Unexpected packet from server (expected TablesStatusResponse, got ProfileInfo)'") merged into Approval still stands and there are no unresolved threads. |
|
Re-checked the fresh CI on The reds so far are not caused by this PR:
The net 3-dot diff is unchanged — exactly the |
|
The fresh CI run on
Re-running the failed jobs now; |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 6/7 (85.71%) · Uncovered code |
2700103
|
Closing out the Since my 2026-06-20 analysis, the That is consistent with the incidental-victim reading: the corruptor lived in the merge COW path and #107451 closed it. The single 2026-06-20 hit landed on a commit that already contained #107451, so it was the tail of that family rather than a count_min defect.
|
…shtask-refresh-retries-int64-overflow Fix signed integer overflow in RefreshTask attempt log_comment
…ckoff
backoff() bounds only the product refresh_retry_initial_backoff_ms *
multiplier, never the value it returns, so both branches can hand back an
arbitrary setting value: the else branch returns refresh_retry_max_backoff_ms
verbatim, and the if branch reaches the same magnitude at retry_idx = 0 via
initial * 1. Both settings are plain RefreshSettingsUInt64 with no upper bound
at parse or DDL time.
determineNextRefreshTime then computed
when = znode.last_attempt_time + backoff(...)
whose common type is milliseconds, so the sys_seconds operand was promoted by
1000 and the addition overflowed signed int64. last_attempt_time is itself read
from Keeper without validation, so it is a second untrusted operand.
Reported by BuzzHouse (arm_asan_ubsan), STID 3978-3a6d:
duration.h:403: signed integer overflow:
1785593651000 + 9223372036854775807 cannot be represented in type
'long long'
In a release build the wrapped instant lands about a millisecond before
start_time, so the "scheduled time not arrived yet" branch is skipped and the
view refreshes on every scheduling pass, ignoring the configured backoff.
Bound both operands in their own units before the conversions that widen them,
and do the addition with common::addOverflow. The retry instant is widened by
1000 three times over: seconds to milliseconds here, milliseconds to
system_clock's microseconds when constructing the time_point, and microseconds
to nanoseconds inside condition_variable::wait_for, which
BackgroundSchedulePool reaches from the delay this instant produces. Three
static_asserts pin those three requirements separately; none follows from the
others, so retuning one constant cannot silently break the argument.
A fourth requirement is not expressible as a static_assert and is stated as a
comment: last_attempt_time flows into CalendarTimeInterval::floor, which builds
a std::chrono::year_month_day, and std::chrono::year holds a short. A year
outside [year::min(), year::max()] wraps silently with both year().ok() and
ymd.ok() still true, so the bound has to clear every consumer's type limits and
not only the arithmetic domains.
Neither bound is reachable by any clock: 9e11 seconds is year ~30489 and the
delay bound is 95 years, against a largest in-tree fake clock of 2070. That
matters because last_attempt_time is written from a real timestamp, so a
wall-clock-reachable bound would clamp genuine retry timestamps into the past,
make when <= now, and recreate the very immediate-refresh loop this fixes. The
saturated instant also stays strictly below time_point::max(), which the
scheduler uses as the "no refresh scheduled" sentinel. No REFRESH AFTER/EVERY
period and no RANDOMIZE FOR window is bounded.
Verified with a deterministic reproducer on an ASAN+UBSAN build: the abort at
duration.h:403 is gone, and so is the further nanosecond-domain abort at
duration.h:93 that a milliseconds-only or microseconds-only bound would leave
behind. An exhaustive corner sweep over the bounds against two start times
finds no overflow in any domain.
Note the retry path is not the only caller of the scheduler's delay
arithmetic. src/Core/BackgroundSchedulePool.cpp remains able to overflow for a
corrupt last_attempt_time at its bound, and is independently reachable from a
plain REFRESH AFTER with no settings at all; that is a pre-existing bug in
another layer, tracked separately and deliberately not touched here.
ClickHouse#108005 fixed the sibling refresh_settings[refresh_retries] + 1 overflow in
this file and its closing note asked for this sweep.
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Fixed a signed integer overflow when a refreshable materialized view retries a failed refresh with
refresh_retriesset to a very large value (nearInt64max). The overflow could abort the server in builds with the undefined-behavior sanitizer.Description
RefreshTask::executeRefreshformats an(attempt N/total)string for the refresh log comment when a refresh retries, wheretotal = refresh_settings[refresh_retries] + 1.refresh_retriesis anInt64setting that accepts any value, sorefresh_retries + 1overflows when it is set toInt64max. Signed integer overflow is undefined behavior, which UBSan reports and (with-fno-sanitize-recover) turns into a server abort.The fix computes the total in
UInt64(range[1, 2^63], no overflow) for non-negativerefresh_retries, and omits the denominator entirely whenrefresh_retriesis negative (the documented infinite-retries case, which previously printed a meaningless/0).Found by the UBSan stress test:
Stress test (arm_ubsan)This is a pre-existing trunk bug surfaced during PR #88234's stress run (that PR does not touch RefreshTask).
Added regression test
03981_refreshable_mv_retries_int64_maxthat creates a refreshable MV withrefresh_retries = 9223372036854775807and a failing refresh, forcing a second attempt through the formatting branch. The test reproduces the abort without the fix and passes with it.Version info
26.7.1.418(included in26.7and later)