diff options
| author | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
|---|---|---|
| committer | Jan Trencansky <jan.trencansky@protonmail.com> | 2026-11-26 16:55:04 -0500 |
| commit | df92abca8ee2a8f9093efea7fcaa4dadc3ddaebc (patch) | |
| tree | 97ef8f95544fe263049d3f7360823f16100de209 /runtime/test/shadow_keep_test.cpp | |
| parent | 49a0b69fcdb80db1f2a44ce7ca0ab8dfdec1e403 (diff) | |
| parent | e5a7c8942678b323f655939c81f5d8855d20c248 (diff) | |
Merge main e5a7c8942 into shadow-aliased-guard (#1867)
One conflict, runtime/README.md: this branch rewrote the LEONIDA_SHADOW_ALIAS_MS
row, main (#2163) added the LEONIDA_SHADOW_KEEP row after it. Both rows kept;
the keep row's "aliased" now reads "aliased while they can't be guarded at every
address", since aliased pages are no longer copied without a sign of a write.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/shadow_keep_test.cpp')
| -rw-r--r-- | runtime/test/shadow_keep_test.cpp | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/runtime/test/shadow_keep_test.cpp b/runtime/test/shadow_keep_test.cpp new file mode 100644 index 000000000..87960c0a2 --- /dev/null +++ b/runtime/test/shadow_keep_test.cpp | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | // src/shadow_keep.h: a shadow copy keeps a GPU write until the title's bytes under it change. No Windows headers, no | ||
| 2 | // GPU, no game data: the "title" and its "copy" are arrays here, and the values are made up. | ||
| 3 | #include "../src/shadow_keep.h" | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <cstdio> | ||
| 7 | #include <cstring> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | int failures = 0; | ||
| 14 | #define EXPECT(condition) \ | ||
| 15 | do { \ | ||
| 16 | if (!(condition)) { \ | ||
| 17 | std::fprintf(stderr, "FAIL line %d: %s\n", __LINE__, #condition); \ | ||
| 18 | ++failures; \ | ||
| 19 | } \ | ||
| 20 | } while (0) | ||
| 21 | |||
| 22 | using leonida::shadow_keep::kPiece; | ||
| 23 | |||
| 24 | // A title range and its copy, as ShadowPages has them: the copy of the title byte at address a is copy[a - base]. | ||
| 25 | struct Pages { | ||
| 26 | static constexpr std::uintptr_t base = 0x10000; // kPiece-aligned, like a tracker's first page | ||
| 27 | std::vector<std::uint8_t> title, copy; | ||
| 28 | std::vector<std::uint64_t> sums; | ||
| 29 | std::vector<std::pair<std::uintptr_t, std::uintptr_t>> copied; // the ranges the last refresh copied | ||
| 30 | |||
| 31 | explicit Pages(std::size_t bytes) : title(bytes), copy(bytes), sums(bytes / kPiece) { | ||
| 32 | for (std::size_t i = 0; i < bytes; ++i) title[i] = std::uint8_t(i * 7 + 3); | ||
| 33 | } | ||
| 34 | // A refresh of [va, va + n): what ShadowPages::copy_changed does for one page. | ||
| 35 | std::size_t refresh(std::uintptr_t va, std::size_t n) { | ||
| 36 | copied.clear(); | ||
| 37 | std::size_t bytes = 0; | ||
| 38 | leonida::shadow_keep::copy_changed( | ||
| 39 | title.data() + (va - base), va, n, sums.data(), base, | ||
| 40 | [&](std::uintptr_t from, std::uintptr_t to, const std::uint8_t* source) { | ||
| 41 | std::memcpy(copy.data() + (from - base), source, to - from); | ||
| 42 | copied.emplace_back(from, to); | ||
| 43 | bytes += to - from; | ||
| 44 | }); | ||
| 45 | return bytes; | ||
| 46 | } | ||
| 47 | std::size_t refresh() { return refresh(base, title.size()); } | ||
| 48 | bool same(std::size_t from, std::size_t to) const { | ||
| 49 | return !std::memcmp(title.data() + from, copy.data() + from, to - from); | ||
| 50 | } | ||
| 51 | }; | ||
| 52 | |||
| 53 | void unchanged_title_keeps_gpu_write() { | ||
| 54 | Pages p(4096); | ||
| 55 | EXPECT(p.refresh() == 4096); // nothing known yet: all of it | ||
| 56 | EXPECT(p.same(0, 4096)); | ||
| 57 | EXPECT(p.refresh() == 0); // nothing changed | ||
| 58 | // A shader stores 16 bytes in the copy; the title's bytes there stay as they were. | ||
| 59 | const std::uint8_t gpu[16] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; | ||
| 60 | std::memcpy(p.copy.data() + 0x340, gpu, sizeof(gpu)); | ||
| 61 | EXPECT(p.refresh() == 0); | ||
| 62 | EXPECT(!std::memcmp(p.copy.data() + 0x340, gpu, sizeof(gpu))); // kept: the old code overwrote it here | ||
| 63 | EXPECT(p.same(0, 0x340) && p.same(0x350, 4096)); | ||
| 64 | } | ||
| 65 | |||
| 66 | void changed_title_piece_is_copied() { | ||
| 67 | Pages p(4096); | ||
| 68 | p.refresh(); | ||
| 69 | const std::uint8_t gpu[16] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, | ||
| 70 | 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}; | ||
| 71 | std::memcpy(p.copy.data() + 0x340, gpu, sizeof(gpu)); // GPU write in piece 3 | ||
| 72 | std::memcpy(p.copy.data() + 0x800, gpu, sizeof(gpu)); // and in piece 8 | ||
| 73 | p.title[0x810] ^= 0x5A; // the title writes piece 8 | ||
| 74 | EXPECT(p.refresh() == kPiece); | ||
| 75 | EXPECT(p.copied.size() == 1 && p.copied[0].first == Pages::base + 0x800 && | ||
| 76 | p.copied[0].second == Pages::base + 0x900); | ||
| 77 | EXPECT(p.same(0x800, 0x900)); // the title's new bytes win there | ||
| 78 | EXPECT(!std::memcmp(p.copy.data() + 0x340, gpu, sizeof(gpu))); // the other GPU write stays | ||
| 79 | EXPECT(p.refresh() == 0); | ||
| 80 | } | ||
| 81 | |||
| 82 | void partial_first_and_last_pieces() { | ||
| 83 | Pages p(4096); | ||
| 84 | // A target that starts and ends inside pieces: [base + 100, base + 1000). | ||
| 85 | const std::uintptr_t va = Pages::base + 100; | ||
| 86 | const std::size_t n = 900; | ||
| 87 | EXPECT(p.refresh(va, n) == n); | ||
| 88 | EXPECT(p.copied.size() == 4); | ||
| 89 | EXPECT(p.copied.front().first == va && p.copied.front().second == Pages::base + kPiece); | ||
| 90 | EXPECT(p.copied.back().first == Pages::base + 3 * kPiece && p.copied.back().second == va + n); | ||
| 91 | EXPECT(p.same(100, 1000)); | ||
| 92 | EXPECT(p.copy[99] == 0 && p.copy[1000] == 0); // nothing outside the range | ||
| 93 | EXPECT(p.refresh(va, n) == 0); | ||
| 94 | p.title[999] ^= 1; // last byte of the last, partial piece | ||
| 95 | EXPECT(p.refresh(va, n) == 1000 - 3 * kPiece); | ||
| 96 | p.title[100] ^= 1; // first byte of the first, partial piece | ||
| 97 | EXPECT(p.refresh(va, n) == kPiece - 100); | ||
| 98 | EXPECT(p.same(100, 1000)); | ||
| 99 | } | ||
| 100 | |||
| 101 | void forgotten_sums_copy_once() { | ||
| 102 | Pages p(4096); | ||
| 103 | p.refresh(); | ||
| 104 | // The pages were not committed for a refresh: the copies were zeroed and their sums forgotten. | ||
| 105 | std::memset(p.copy.data() + 0x400, 0, 0x200); | ||
| 106 | leonida::shadow_keep::forget(p.sums.data(), Pages::base, Pages::base + 0x400, Pages::base + 0x600); | ||
| 107 | EXPECT(p.refresh() == 0x200); // the same title bytes back: copied once all the same | ||
| 108 | EXPECT(p.same(0, 4096)); | ||
| 109 | EXPECT(p.refresh() == 0); | ||
| 110 | // A partial range forgets every piece it touches. | ||
| 111 | leonida::shadow_keep::forget(p.sums.data(), Pages::base, Pages::base + 0x410, Pages::base + 0x501); | ||
| 112 | EXPECT(p.refresh() == 0x200); | ||
| 113 | } | ||
| 114 | |||
| 115 | void sums_tell_bytes_apart() { | ||
| 116 | std::uint8_t a[kPiece] = {}, b[kPiece] = {}; | ||
| 117 | EXPECT(leonida::shadow_keep::sum_of(a, kPiece) != 0); | ||
| 118 | EXPECT(leonida::shadow_keep::sum_of(a, kPiece) == leonida::shadow_keep::sum_of(b, kPiece)); | ||
| 119 | for (std::size_t i = 0; i < kPiece; ++i) { | ||
| 120 | b[i] = 1; | ||
| 121 | EXPECT(leonida::shadow_keep::sum_of(a, kPiece) != leonida::shadow_keep::sum_of(b, kPiece)); | ||
| 122 | b[i] = 0; | ||
| 123 | } | ||
| 124 | EXPECT(leonida::shadow_keep::sum_of(a, 16) != leonida::shadow_keep::sum_of(a, 17)); // the length counts | ||
| 125 | } | ||
| 126 | |||
| 127 | } // namespace | ||
| 128 | |||
| 129 | int main() { | ||
| 130 | unchanged_title_keeps_gpu_write(); | ||
| 131 | changed_title_piece_is_copied(); | ||
| 132 | partial_first_and_last_pieces(); | ||
| 133 | forgotten_sums_copy_once(); | ||
| 134 | sums_tell_bytes_apart(); | ||
| 135 | if (failures) { | ||
| 136 | std::fprintf(stderr, "shadow_keep: %d check(s) failed\n", failures); | ||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | std::puts("shadow_keep: OK"); | ||
| 140 | return 0; | ||
| 141 | } | ||