From 435d7541fed13805ad3d487a454cce4e23b6ffbc Mon Sep 17 00:00:00 2001 From: IlyassRS Date: Wed, 25 Nov 2026 19:11:10 +0100 Subject: [PATCH] dstorage: harden staging_bytes overflow and StagingPool::Lease reuse --- runtime/shims/dstorage_staging.h | 75 ++++++++++++++++++++++---------- runtime/test/dstorage_test.cpp | 23 ++++++++++ 2 files changed, 75 insertions(+), 23 deletions(-) diff --git a/runtime/shims/dstorage_staging.h b/runtime/shims/dstorage_staging.h index a989f69ac..afa1d9605 100644 --- a/runtime/shims/dstorage_staging.h +++ b/runtime/shims/dstorage_staging.h @@ -17,12 +17,19 @@ inline uint64_t staging_bytes(const dsx::DSTORAGE_REQUEST& r) { const auto o = r.Options.AsUINT64; const bool bc = options::bcpack(o) != 0; uint64_t input = r.SourceSize, peak = 0; - const auto pages = [](uint64_t size, unsigned offset) { - return size ? size + sizeof(uint64_t) * (1 + (size - 1 + offset) / 65536) : 0; + const auto add_sat = [](uint64_t a, uint64_t b) { + return a > UINT64_MAX - b ? UINT64_MAX : a + b; + }; + const auto pages = [add_sat](uint64_t size, unsigned offset) -> uint64_t { + if (!size) return 0; + const uint64_t span = add_sat(size - 1, offset); + const uint64_t count = 1 + span / 65536; + const uint64_t table = count > UINT64_MAX / sizeof(uint64_t) ? UINT64_MAX : sizeof(uint64_t) * count; + return add_sat(size, table); }; if (o & options::kSourcePages) peak = pages(input, r.SourcePageOffset); const auto stage = [&](uint64_t output) { - peak = (std::max)(peak, input + output); + peak = (std::max)(peak, add_sat(input, output)); input = output; }; if (options::zlib(o)) stage(bc ? r.IntermediateSize : r.DestinationSize); @@ -44,49 +51,71 @@ public: Lease& operator=(const Lease&) = delete; bool try_pair(uint64_t bytes) { + if (held_) return false; std::lock_guard lock(pool_.mutex_); if (pool_.helpers_ >= 2 || pool_.next_ != pool_.serving_ || bytes > limit || pool_.used_ > limit - bytes) return false; ++pool_.helpers_; pair_ = true; + held_ = true; take(bytes); return true; } void acquire(uint64_t bytes) { + if (held_) release(); if (!bytes) return; - std::unique_lock lock(pool_.mutex_); - const uint64_t ticket = pool_.next_++; - pool_.changed_.wait(lock, [&] { - return ticket == pool_.serving_ && (!pool_.used_ || - (bytes <= limit && pool_.used_ <= limit - bytes)); - }); - take(bytes); - ++pool_.serving_; - if (pool_.next_ != pool_.serving_) pool_.changed_.notify_all(); - } - ~Lease() { - if (!bytes_ && !pair_) return; - std::lock_guard lock(pool_.mutex_); - pool_.used_ -= bytes_; - if (pair_) --pool_.helpers_; - if (!pool_.used_ && pool_.drained_ < pool_.peak_) { - pool_.drained_ = pool_.peak_; - if (trace_enabled()) std::fprintf(stderr, "[rt] ds-staging idle_bytes=0 peak_bytes=%llu\n", pool_.peak_); + bool notify = false; + { + std::unique_lock lock(pool_.mutex_); + const uint64_t ticket = pool_.next_++; + pool_.changed_.wait(lock, [&] { + return ticket == pool_.serving_ && (!pool_.used_ || + (bytes <= limit && pool_.used_ <= limit - bytes)); + }); + held_ = true; + take(bytes); + ++pool_.serving_; + notify = pool_.next_ != pool_.serving_; } - if (pool_.next_ != pool_.serving_) pool_.changed_.notify_all(); + if (notify) pool_.changed_.notify_all(); } + void release() { + if (!held_) return; + bool notify = false; + { + std::lock_guard lock(pool_.mutex_); + pool_.used_ -= bytes_; + if (pair_) --pool_.helpers_; + bytes_ = 0; + pair_ = false; + held_ = false; + if (!pool_.used_ && pool_.drained_ < pool_.peak_) { + pool_.drained_ = pool_.peak_; + if (trace_enabled()) + std::fprintf(stderr, "[rt] ds-staging idle_bytes=0 peak_bytes=%llu\n", + static_cast(pool_.peak_)); + } + notify = pool_.next_ != pool_.serving_; + } + if (notify) pool_.changed_.notify_all(); + } + ~Lease() { release(); } private: void take(uint64_t bytes) { bytes_ = bytes; pool_.used_ += bytes; if (pool_.used_ > pool_.peak_) { pool_.peak_ = pool_.used_; - if (trace_enabled()) std::fprintf(stderr, "[rt] ds-staging peak_bytes=%llu limit_bytes=%llu\n", pool_.peak_, limit); + if (trace_enabled()) + std::fprintf(stderr, "[rt] ds-staging peak_bytes=%llu limit_bytes=%llu\n", + static_cast(pool_.peak_), + static_cast(limit)); } } StagingPool& pool_; uint64_t bytes_{}; bool pair_{}; + bool held_{}; }; private: diff --git a/runtime/test/dstorage_test.cpp b/runtime/test/dstorage_test.cpp index c24bcdb65..83ad3fc28 100644 --- a/runtime/test/dstorage_test.cpp +++ b/runtime/test/dstorage_test.cpp @@ -234,6 +234,29 @@ void test_staging_budget() { check(entered, "waiting publisher wakes when exclusive buffers are released"); StagingPool::Lease reclaimed(pool); check(reclaimed.try_pair(limit), "pool is entirely reusable after the last publisher"); + check(!reclaimed.try_pair(1), "a held lease refuses duplicate try_pair without leaking helper count"); + reclaimed.acquire(0); // releases prior try_pair cleanly + check(reclaimed.try_pair(limit), "acquire(0) released prior reservation back to pool"); + reclaimed.acquire(limit / 2); // re-acquire replaces prior reservation without leaking used_ + { + StagingPool::Lease companion(pool); + check(companion.try_pair(limit / 2), "re-acquired lease kept exact remaining pool budget"); + } + reclaimed.release(); + check(reclaimed.try_pair(limit), "explicit release returns full pool budget"); + + // Overflow-safe staging_bytes saturation for huge page-backed or combined transform sizes. + DSTORAGE_REQUEST big_pages{}; + big_pages.Options.AsUINT64 = options::kSourceMemory | options::kSourcePages; + big_pages.SourceSize = UINT32_MAX; + big_pages.SourcePageOffset = 65535; + check(staging_bytes(big_pages) > big_pages.SourceSize, "large page-backed source does not wrap"); + + DSTORAGE_REQUEST big_pair{}; + big_pair.Options.AsUINT64 = options::kZlib; + big_pair.SourceSize = UINT32_MAX; + big_pair.DestinationSize = UINT32_MAX; + check(staging_bytes(big_pair) == uint64_t(UINT32_MAX) * 2, "large transform sum stays in 64-bit range"); } void test_layout_and_options() {