Skip to content

Commit f451d0c

Browse files
Clamp speculative RSS reservation against Int64 overflow
`MemoryWorker::updateResidentMemoryThread` was multiplying `delta` by `rss_speculative_reserve_ratio` and adding the result to `resident` without any bounds check. A very large configured ratio could overflow the `double -> Int64` conversion (UB) or the signed `Int64` addition, producing a negative or wrap-around `rss` value and weakening the global hard-limit check inside `MemoryTracker::allocImpl`. Compute the maximum admissible reservation as `std::numeric_limits<Int64>::max() - resident`, compare against the unrounded `double` product before casting, and clamp. Spotted by AI review on #104976. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 88104b9 commit f451d0c

1 file changed

Lines changed: 16 additions & 5 deletions

File tree

‎src/Common/MemoryWorker.cpp‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <fmt/ranges.h>
1717

1818
#include <filesystem>
19+
#include <limits>
1920
#include <optional>
2021

2122
namespace fs = std::filesystem;
@@ -434,17 +435,27 @@ void MemoryWorker::updateResidentMemoryThread()
434435
/// Speculatively reserve `(resident - tracked) * ratio` on top of the observed RSS.
435436
/// `resident - tracked` is the amount of memory we observed during the last tick that
436437
/// had not yet flowed into the tracker; treat it as a lower bound on what may grow in
437-
/// the next tick as well. With the default ratio = 1.0 we reserve one full delta of
438-
/// headroom, so `MemoryTracker::allocImpl` will throw `MEMORY_LIMIT_EXCEEDED` (via the
439-
/// global `will_be_rss > current_hard_limit` branch) before the kernel OOM-killer
440-
/// closes the gap. Setting the ratio to 0 disables the speculation entirely.
438+
/// the next tick as well. With `ratio = 1.0` we reserve one full delta of headroom,
439+
/// so `MemoryTracker::allocImpl` will throw `MEMORY_LIMIT_EXCEEDED` (via the global
440+
/// `will_be_rss > current_hard_limit` branch) before the kernel OOM-killer closes the
441+
/// gap. The default `ratio = 0` keeps the previous behaviour (`rss = resident`).
441442
Int64 speculative_rss = resident;
442443
if (rss_speculative_reserve_ratio > 0.0)
443444
{
444445
Int64 tracked = total_memory_tracker.get();
445446
Int64 delta = resident - tracked;
446447
if (delta > 0)
447-
speculative_rss += static_cast<Int64>(static_cast<double>(delta) * rss_speculative_reserve_ratio);
448+
{
449+
/// Clamp the speculative reservation so that the float multiplication
450+
/// and the subsequent `Int64` addition cannot overflow even if the
451+
/// configured ratio is very large.
452+
double reserve_double = static_cast<double>(delta) * rss_speculative_reserve_ratio;
453+
Int64 max_reserve = std::numeric_limits<Int64>::max() - resident;
454+
Int64 reserve = (reserve_double >= static_cast<double>(max_reserve))
455+
? max_reserve
456+
: static_cast<Int64>(reserve_double);
457+
speculative_rss += reserve;
458+
}
448459
}
449460
MemoryTracker::updateRSS(speculative_rss);
450461

0 commit comments

Comments
 (0)