diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index e9cb8d4345..94f6e353c9 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -4352,6 +4352,11 @@ if(TARGET Vulkan::Headers) target_compile_options(arena_blocks_test PRIVATE /W4 /permissive- /utf-8) endif() add_test(NAME arena_blocks COMMAND arena_blocks_test) + add_executable(title_window_plan_bounds_test test/title_window_plan_bounds_test.cpp) + if(WIN32 AND MSVC) + target_compile_options(title_window_plan_bounds_test PRIVATE /W4 /permissive- /utf-8) + endif() + add_test(NAME title_window_plan_bounds COMMAND title_window_plan_bounds_test) # Queue construction cleans its Vulkan handles if holder registration runs out of host memory. add_executable(vk_host_texture_order_test test/vk_host_texture_order_test.cpp) target_link_libraries(vk_host_texture_order_test PRIVATE Vulkan::Headers ${CMAKE_DL_LIBS}) diff --git a/runtime/src/arena_blocks.h b/runtime/src/arena_blocks.h index f6b379e2f5..c7ac093d8e 100644 --- a/runtime/src/arena_blocks.h +++ b/runtime/src/arena_blocks.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -66,11 +67,18 @@ class ArenaBlocks { const std::uint64_t block = next->first; const std::uint64_t block_size = next->second; const std::uint64_t end = block + block_size; - std::uint64_t start = align_up(block, alignment); - while (start < end && bytes <= end - start) { - const std::uint64_t keep_out_end = keep_out(start, start + bytes); - if (!keep_out_end) break; - start = align_up(keep_out_end, alignment); + std::uint64_t start = 0; + if (align_up(block, alignment, start)) { + while (start < end && bytes <= end - start) { + const std::uint64_t keep_out_end = keep_out(start, start + bytes); + if (!keep_out_end) break; + if (keep_out_end <= start || !align_up(keep_out_end, alignment, start)) { + start = end; + break; + } + } + } else { + start = end; } if (cuts_ != cuts) break; // the map moved under the walk: search the new one if (start < end && bytes <= end - start) { @@ -189,7 +197,10 @@ class ArenaBlocks { // Whether the map's free bytes and the device's live bytes add up to the arena: a byte released twice, or a // released range that was never allocated, breaks this. O(1), so the device checks it on every call. - bool consistent(std::uint64_t live_bytes) const { return free_bytes_ + live_bytes == size_ - reserved_; } + bool consistent(std::uint64_t live_bytes) const { + const std::uint64_t total = size_ > reserved_ ? size_ - reserved_ : 0; + return live_bytes <= total && free_bytes_ == total - live_bytes; + } // Faults reported so far, and the last one this call reported. std::uint64_t faults() const { return faults_; } @@ -202,8 +213,16 @@ class ArenaBlocks { static constexpr int kAttempts = 8; private: - static std::uint64_t align_up(std::uint64_t value, std::uint64_t alignment) { - return (value + alignment - 1) / alignment * alignment; + static bool align_up(std::uint64_t value, std::uint64_t alignment, std::uint64_t& out) { + const std::uint64_t rem = value % alignment; + if (!rem) { + out = value; + return true; + } + const std::uint64_t add = alignment - rem; + if (value > std::numeric_limits::max() - add) return false; + out = value + add; + return true; } Fault note_fault(Fault fault) { diff --git a/runtime/src/title_window_plan.h b/runtime/src/title_window_plan.h index 807641df15..cd17bc5961 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 inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint64_t lo, std::uint64_t hi, std::uint64_t align, std::uint64_t most, std::uint64_t cap, std::uint64_t used, TitleWindowRange& out) { - if (!align || hi <= va) return false; - const std::uint64_t want = std::clamp(bytes ? bytes : 1, 1, most); + if (!align || (align & (align - 1)) != 0 || !most || va < lo || hi <= va) return false; + const std::uint64_t want = (std::min)(bytes ? bytes : std::uint64_t(1), most); const std::uint64_t start = (std::max)(va & ~(align - 1), lo); - std::uint64_t end = va > UINT64_MAX - want - align ? UINT64_MAX : (va + want + align - 1) & ~(align - 1); + const std::uint64_t raw_end = want > UINT64_MAX - va ? UINT64_MAX : va + want; + std::uint64_t end = raw_end > UINT64_MAX - (align - 1) ? UINT64_MAX : (raw_end + align - 1) & ~(align - 1); end = (std::min)(end, hi); - if (end <= start) return false; + if (end <= va || end <= start) return false; const TitleWindowRange range{start, end - start}; if (!title_window_fits(cap, used, range.size)) return false; out = range; @@ -49,13 +50,15 @@ inline bool title_window_range(std::uint64_t va, std::uint64_t bytes, std::uint6 inline bool title_pool_extent(std::uint64_t pool_lo, std::uint64_t pool_hi, std::uint64_t va, std::uint64_t readable_end, std::uint64_t align, std::uint64_t most, TitleWindowRange& out) { - if (!align || pool_hi <= pool_lo || va < pool_lo || va >= pool_hi) return false; + if (!align || (align & (align - 1)) != 0 || pool_hi <= pool_lo || va < pool_lo || va >= pool_hi || + readable_end <= va) + return false; const std::uint64_t start = (std::max)(va & ~(align - 1), pool_lo); std::uint64_t end = (std::min)(readable_end, pool_hi); - if (most) end = (std::min)(end, start > UINT64_MAX - most ? UINT64_MAX : start + most); end = end > UINT64_MAX - (align - 1) ? UINT64_MAX : (end + align - 1) & ~(align - 1); + if (most) end = (std::min)(end, start > UINT64_MAX - most ? UINT64_MAX : start + most); end = (std::min)(end, pool_hi); - if (end <= va) return false; + if (end <= va || end <= start) return false; out = {start, end - start}; return true; } diff --git a/runtime/test/arena_blocks_test.cpp b/runtime/test/arena_blocks_test.cpp index d1da077f1f..e875a9d25b 100644 --- a/runtime/test/arena_blocks_test.cpp +++ b/runtime/test/arena_blocks_test.cpp @@ -257,6 +257,43 @@ int threads() { return 0; } +// High alignment or keep-out ends near UINT64_MAX, non-advancing keep-out callbacks, and inverted reset bounds must +// never wrap around or spin forever. +int overflow_and_bounds() { + Blocks b; + b.reset(kReserved, kSize); + std::uint64_t out = 0; + + // Alignment that would overflow value + alignment - 1: refused cleanly without wrapping to 0. + EXPECT(!b.allocate(0x1000, UINT64_MAX - 0x100, no_keep_out, out)); + + // A keep_out returning an end near UINT64_MAX where (keep_out_end + alignment - 1) wraps to 0 must skip the block + // instead of looping forever. + int calls = 0; + const auto wrapping_keep_out = [&](std::uint64_t, std::uint64_t) -> std::uint64_t { + ++calls; + return UINT64_MAX - 0x100; + }; + EXPECT(!b.allocate(0x1000, kAlign, wrapping_keep_out, out) && calls == 1); + + // A non-advancing keep_out (returning <= start) must not spin forever. + calls = 0; + const auto stagnant_keep_out = [&](std::uint64_t lo, std::uint64_t) -> std::uint64_t { + ++calls; + return lo; + }; + EXPECT(!b.allocate(0x1000, kAlign, stagnant_keep_out, out) && calls == 1); + + // consistent() must not overflow on huge live_bytes or underflow when reserved > size. + EXPECT(!b.consistent(UINT64_MAX)); + b.reset(kSize, kReserved); + EXPECT(b.free_bytes() == 0 && b.count() == 0); + EXPECT(b.check() == Blocks::Fault::none); + EXPECT(b.consistent(0) && !b.consistent(1)); + EXPECT(!b.allocate(0x1000, kAlign, no_keep_out, out)); + return 0; +} + } // namespace int main() { @@ -268,6 +305,7 @@ int main() { if (int r = bad_releases()) return r; if (int r = reader()) return r; if (int r = threads()) return r; + if (int r = overflow_and_bounds()) return r; std::fprintf(stderr, "arena_blocks: ok\n"); return 0; } diff --git a/runtime/test/title_window_plan_bounds_test.cpp b/runtime/test/title_window_plan_bounds_test.cpp new file mode 100644 index 0000000000..93cf2bed46 --- /dev/null +++ b/runtime/test/title_window_plan_bounds_test.cpp @@ -0,0 +1,97 @@ +// Portable boundary, alignment, and overflow tests for src/title_window_plan.h. +// No Windows headers, no Vulkan, no game data. +#include "../src/title_window_plan.h" + +#include +#include + +#define EXPECT(condition) do { \ + if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \ +} while (0) + +namespace { + +constexpr std::uint64_t kAlign = 0x10000; // 64 KiB +constexpr std::uint64_t kMiB = 1ull << 20; + +int test_title_window_fits() { + EXPECT(leonida::title_window_fits(100, 0, 100)); + EXPECT(leonida::title_window_fits(100, 40, 60)); + EXPECT(!leonida::title_window_fits(100, 40, 61)); + EXPECT(!leonida::title_window_fits(100, 101, 0)); + EXPECT(!leonida::title_window_fits(UINT64_MAX, UINT64_MAX - 10, 11)); + EXPECT(leonida::title_window_fits(UINT64_MAX, UINT64_MAX - 10, 10)); + return 0; +} + +int test_title_window_range_bounds() { + leonida::TitleWindowRange range{}; + constexpr std::uint64_t va = 0x5800e0000ull; + + // va < lo must be rejected: returning [lo, end) when lo > va would not contain va and would underflow + // va - range.start in resolve_title_memory. + EXPECT(!leonida::title_window_range(va, 0x20000, va + 0x1000, va + 0x40000, kAlign, 16 * kMiB, 64 * kMiB, 0, + range)); + EXPECT(!leonida::title_window_range(va, 0x10000, va + 0x10000, va + 0x40000, kAlign, 16 * kMiB, 64 * kMiB, 0, + range)); + + // most == 0 must be rejected cleanly (not passed to std::clamp(want, 1, 0)). + EXPECT(!leonida::title_window_range(va, 0x10000, 0, UINT64_MAX, kAlign, 0, 64 * kMiB, 0, range)); + + // Zero or non-power-of-two align must be rejected. + EXPECT(!leonida::title_window_range(va, 0x10000, 0, UINT64_MAX, 0, 16 * kMiB, 64 * kMiB, 0, range)); + EXPECT(!leonida::title_window_range(va, 0x10000, 0, UINT64_MAX, 3, 16 * kMiB, 64 * kMiB, 0, range)); + EXPECT(!leonida::title_window_range(va, 0x10000, 0, UINT64_MAX, 0x30000, 16 * kMiB, 64 * kMiB, 0, range)); + + // Large want + align near UINT64_MAX must saturate end to UINT64_MAX without underflowing UINT64_MAX - want - align. + const std::uint64_t high_va = UINT64_MAX - 0x1000; + EXPECT(leonida::title_window_range(high_va, UINT64_MAX, 0, UINT64_MAX, kAlign, UINT64_MAX, UINT64_MAX, 0, range)); + EXPECT(range.start == (high_va & ~(kAlign - 1))); + EXPECT(range.start <= high_va && high_va < range.start + range.size); + EXPECT(range.start + range.size == UINT64_MAX); + + // Normal aligned and clipped spans still cover va and charge the budget accurately. + EXPECT(leonida::title_window_range(va + 0x1234, 0, va, va + 0x20000, kAlign, 16 * kMiB, 64 * kMiB, 0, range)); + EXPECT(range.start == va && range.size == kAlign); + EXPECT(!leonida::title_window_range(va, 0x10000, 0, UINT64_MAX, kAlign, 16 * kMiB, 64 * kMiB, 64 * kMiB, range)); + return 0; +} + +int test_title_pool_extent_bounds() { + leonida::TitleWindowRange extent{}; + constexpr std::uint64_t pool_lo = 0x100000000ull; + constexpr std::uint64_t pool_hi = pool_lo + kMiB; + + // Unreadable va (readable_end <= va) must be rejected even when rounding readable_end up to align would pass va. + EXPECT(!leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x28000, pool_lo + 0x21000, kAlign, kMiB, extent)); + EXPECT(!leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x20000, pool_lo + 0x20000, kAlign, kMiB, extent)); + + // Non-power-of-two align must be rejected. + EXPECT(!leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x20000, pool_hi, 0, kMiB, extent)); + EXPECT(!leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x20000, pool_hi, 0x30000, kMiB, extent)); + + // When `most` is smaller than `align` (or pool_lo is unaligned), the returned extent must still respect `most` + // and contain `va`. + EXPECT(leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x20000, pool_hi, kAlign, 0x4000, extent)); + EXPECT(extent.start == pool_lo + 0x20000 && extent.size == 0x4000); + // If start + most cannot even reach va, refuse instead of returning a range excluding va. + EXPECT(!leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x28000, pool_hi, kAlign, 0x4000, extent)); + + // Two requests in the same aligned page and readable run produce the exact same extent. + leonida::TitleWindowRange first{}, second{}; + EXPECT(leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x20000, pool_hi, kAlign, kMiB, first)); + EXPECT(leonida::title_pool_extent(pool_lo, pool_hi, pool_lo + 0x28000, pool_hi, kAlign, kMiB, second)); + EXPECT(first.start == pool_lo + 0x20000 && first.size == 0xE0000); + EXPECT(second.start == first.start && second.size == first.size); + return 0; +} + +} // namespace + +int main() { + if (int r = test_title_window_fits()) return r; + if (int r = test_title_window_range_bounds()) return r; + if (int r = test_title_pool_extent_bounds()) return r; + std::printf("title_window_plan_bounds_test: ok\n"); + return 0; +}