diff options
| author | IlyassRS <ilyassroussi2000@gmail.com> | 2026-11-25 13:08:19 +0100 |
|---|---|---|
| committer | IlyassRS <ilyassroussi2000@gmail.com> | 2026-11-25 13:09:51 +0100 |
| commit | eb4c4204e8c4523debb0b04d91e7e258d2a29a77 (patch) | |
| tree | 06a50fd66289e3879bc680fcb16ee90442940ea6 /runtime/src | |
| parent | 0d80bc5df7c873f40965f34f185cb96913786765 (diff) | |
runtime: harden title_window_plan and ArenaBlocks boundary and overflow checks
Fix edge-case arithmetic and boundary bugs in the title-memory window
planner and GPU address arena allocator:
1. runtime/src/title_window_plan.h:
- title_window_range(): reject va < lo (previously returned a range
starting at lo > va that excluded va, causing va - range.start to
underflow in resolve_title_memory), reject most == 0 (avoiding
std::clamp(want, 1, 0) UB), require power-of-two align, and avoid
UINT64_MAX - want - align unsigned underflow when computing end.
- title_pool_extent(): reject non-power-of-two align and unreadable
requests (readable_end <= va) before rounding end up to align, and
enforce the most cap after alignment rounding so out.size <= most
always holds and out contains va.
2. runtime/src/arena_blocks.h:
- Make align_up() overflow-checked so high alignments or keep_out_end
near UINT64_MAX cannot wrap to 0 and trigger an infinite loop in
ArenaBlocks::allocate().
- Guard against non-advancing keep_out_end <= start in allocate().
- Make consistent() safe against reserved_ > size_ underflow and
free_bytes_ + live_bytes overflow.
Extend runtime/test/arena_blocks_test.cpp and add portable
runtime/test/title_window_plan_bounds_test.cpp.
Item: RT-TITLE-WINDOW-AND-ARENA-BOUNDS
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/arena_blocks.h | 35 | ||||
| -rw-r--r-- | runtime/src/title_window_plan.h | 17 |
2 files changed, 37 insertions, 15 deletions
diff --git a/runtime/src/arena_blocks.h b/runtime/src/arena_blocks.h index f6b379e2f..c7ac093d8 100644 --- a/runtime/src/arena_blocks.h +++ b/runtime/src/arena_blocks.h | |||
| @@ -14,6 +14,7 @@ | |||
| 14 | #include <algorithm> | 14 | #include <algorithm> |
| 15 | #include <cstddef> | 15 | #include <cstddef> |
| 16 | #include <cstdint> | 16 | #include <cstdint> |
| 17 | #include <limits> | ||
| 17 | #include <map> | 18 | #include <map> |
| 18 | #include <utility> | 19 | #include <utility> |
| 19 | #include <vector> | 20 | #include <vector> |
| @@ -66,11 +67,18 @@ public: | |||
| 66 | const std::uint64_t block = next->first; | 67 | const std::uint64_t block = next->first; |
| 67 | const std::uint64_t block_size = next->second; | 68 | const std::uint64_t block_size = next->second; |
| 68 | const std::uint64_t end = block + block_size; | 69 | const std::uint64_t end = block + block_size; |
| 69 | std::uint64_t start = align_up(block, alignment); | 70 | std::uint64_t start = 0; |
| 70 | while (start < end && bytes <= end - start) { | 71 | if (align_up(block, alignment, start)) { |
| 71 | const std::uint64_t keep_out_end = keep_out(start, start + bytes); | 72 | while (start < end && bytes <= end - start) { |
| 72 | if (!keep_out_end) break; | 73 | const std::uint64_t keep_out_end = keep_out(start, start + bytes); |
| 73 | start = align_up(keep_out_end, alignment); | 74 | if (!keep_out_end) break; |
| 75 | if (keep_out_end <= start || !align_up(keep_out_end, alignment, start)) { | ||
| 76 | start = end; | ||
| 77 | break; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } else { | ||
| 81 | start = end; | ||
| 74 | } | 82 | } |
| 75 | if (cuts_ != cuts) break; // the map moved under the walk: search the new one | 83 | if (cuts_ != cuts) break; // the map moved under the walk: search the new one |
| 76 | if (start < end && bytes <= end - start) { | 84 | if (start < end && bytes <= end - start) { |
| @@ -189,7 +197,10 @@ public: | |||
| 189 | 197 | ||
| 190 | // Whether the map's free bytes and the device's live bytes add up to the arena: a byte released twice, or a | 198 | // Whether the map's free bytes and the device's live bytes add up to the arena: a byte released twice, or a |
| 191 | // released range that was never allocated, breaks this. O(1), so the device checks it on every call. | 199 | // released range that was never allocated, breaks this. O(1), so the device checks it on every call. |
| 192 | bool consistent(std::uint64_t live_bytes) const { return free_bytes_ + live_bytes == size_ - reserved_; } | 200 | bool consistent(std::uint64_t live_bytes) const { |
| 201 | const std::uint64_t total = size_ > reserved_ ? size_ - reserved_ : 0; | ||
| 202 | return live_bytes <= total && free_bytes_ == total - live_bytes; | ||
| 203 | } | ||
| 193 | 204 | ||
| 194 | // Faults reported so far, and the last one this call reported. | 205 | // Faults reported so far, and the last one this call reported. |
| 195 | std::uint64_t faults() const { return faults_; } | 206 | std::uint64_t faults() const { return faults_; } |
| @@ -202,8 +213,16 @@ public: | |||
| 202 | static constexpr int kAttempts = 8; | 213 | static constexpr int kAttempts = 8; |
| 203 | 214 | ||
| 204 | private: | 215 | private: |
| 205 | static std::uint64_t align_up(std::uint64_t value, std::uint64_t alignment) { | 216 | static bool align_up(std::uint64_t value, std::uint64_t alignment, std::uint64_t& out) { |
| 206 | return (value + alignment - 1) / alignment * alignment; | 217 | const std::uint64_t rem = value % alignment; |
| 218 | if (!rem) { | ||
| 219 | out = value; | ||
| 220 | return true; | ||
| 221 | } | ||
| 222 | const std::uint64_t add = alignment - rem; | ||
| 223 | if (value > std::numeric_limits<std::uint64_t>::max() - add) return false; | ||
| 224 | out = value + add; | ||
| 225 | return true; | ||
| 207 | } | 226 | } |
| 208 | 227 | ||
| 209 | Fault note_fault(Fault fault) { | 228 | Fault note_fault(Fault fault) { |
diff --git a/runtime/src/title_window_plan.h b/runtime/src/title_window_plan.h index 807641df1..cd17bc596 100644 --- a/runtime/src/title_window_plan.h +++ b/runtime/src/title_window_plan.h | |||
| @@ -31,12 +31,13 @@ inline bool title_window_fits(std::uint64_t cap, std::uint64_t used, std::uint64 | |||
| 31 | inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint64_t lo, std::uint64_t hi, | 31 | inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint64_t lo, std::uint64_t hi, |
| 32 | std::uint64_t align, std::uint64_t most, std::uint64_t cap, std::uint64_t used, | 32 | std::uint64_t align, std::uint64_t most, std::uint64_t cap, std::uint64_t used, |
| 33 | TitleWindowRange& out) { | 33 | TitleWindowRange& out) { |
| 34 | if (!align || hi <= va) return false; | 34 | if (!align || (align & (align - 1)) != 0 || !most || va < lo || hi <= va) return false; |
| 35 | const std::uint64_t want = std::clamp<std::uint64_t>(bytes ? bytes : 1, 1, most); | 35 | const std::uint64_t want = (std::min)(bytes ? bytes : std::uint64_t(1), most); |
| 36 | const std::uint64_t start = (std::max)(va & ~(align - 1), lo); | 36 | const std::uint64_t start = (std::max)(va & ~(align - 1), lo); |
| 37 | std::uint64_t end = va > UINT64_MAX - want - align ? UINT64_MAX : (va + want + align - 1) & ~(align - 1); | 37 | const std::uint64_t raw_end = want > UINT64_MAX - va ? UINT64_MAX : va + want; |
| 38 | std::uint64_t end = raw_end > UINT64_MAX - (align - 1) ? UINT64_MAX : (raw_end + align - 1) & ~(align - 1); | ||
| 38 | end = (std::min)(end, hi); | 39 | end = (std::min)(end, hi); |
| 39 | if (end <= start) return false; | 40 | if (end <= va || end <= start) return false; |
| 40 | const TitleWindowRange range{start, end - start}; | 41 | const TitleWindowRange range{start, end - start}; |
| 41 | if (!title_window_fits(cap, used, range.size)) return false; | 42 | if (!title_window_fits(cap, used, range.size)) return false; |
| 42 | out = range; | 43 | out = range; |
| @@ -49,13 +50,15 @@ inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint6 | |||
| 49 | inline bool title_pool_extent(std::uint64_t pool_lo, std::uint64_t pool_hi, std::uint64_t va, | 50 | inline bool title_pool_extent(std::uint64_t pool_lo, std::uint64_t pool_hi, std::uint64_t va, |
| 50 | std::uint64_t readable_end, std::uint64_t align, std::uint64_t most, | 51 | std::uint64_t readable_end, std::uint64_t align, std::uint64_t most, |
| 51 | TitleWindowRange& out) { | 52 | TitleWindowRange& out) { |
| 52 | if (!align || pool_hi <= pool_lo || va < pool_lo || va >= pool_hi) return false; | 53 | if (!align || (align & (align - 1)) != 0 || pool_hi <= pool_lo || va < pool_lo || va >= pool_hi || |
| 54 | readable_end <= va) | ||
| 55 | return false; | ||
| 53 | const std::uint64_t start = (std::max)(va & ~(align - 1), pool_lo); | 56 | const std::uint64_t start = (std::max)(va & ~(align - 1), pool_lo); |
| 54 | std::uint64_t end = (std::min)(readable_end, pool_hi); | 57 | std::uint64_t end = (std::min)(readable_end, pool_hi); |
| 55 | if (most) end = (std::min)(end, start > UINT64_MAX - most ? UINT64_MAX : start + most); | ||
| 56 | end = end > UINT64_MAX - (align - 1) ? UINT64_MAX : (end + align - 1) & ~(align - 1); | 58 | end = end > UINT64_MAX - (align - 1) ? UINT64_MAX : (end + align - 1) & ~(align - 1); |
| 59 | if (most) end = (std::min)(end, start > UINT64_MAX - most ? UINT64_MAX : start + most); | ||
| 57 | end = (std::min)(end, pool_hi); | 60 | end = (std::min)(end, pool_hi); |
| 58 | if (end <= va) return false; | 61 | if (end <= va || end <= start) return false; |
| 59 | out = {start, end - start}; | 62 | out = {start, end - start}; |
| 60 | return true; | 63 | return true; |
| 61 | } | 64 | } |