diff options
| author | MoonEyes <ducoinbenjamin@yahoo.fr> | 2026-11-28 18:53:57 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-28 18:53:57 +0100 |
| commit | 3107c8a2a8adbbfe588c6022085bc41c4fe09a81 (patch) | |
| tree | 4a4a8feb7b3b49d498b4f282cb0e0018601f89a8 /runtime/src | |
| parent | f139d7690a25f4efedb6a1ea1b2a607008400946 (diff) | |
| parent | 7f56e6a50b45ea8d760d93df855b6acc04594f0c (diff) | |
Merge pull request #2035 from IlyassRS3/fix/rt-cpu-emulation-clzero-region-and-bounds
runtime: step try_clzero_loop by VirtualQuery regions and guard wraparound in cpu_emulation
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/cpu_emulation.h | 47 |
1 files changed, 33 insertions, 14 deletions
diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h index 4f9565dcc..e72587434 100644 --- a/runtime/src/cpu_emulation.h +++ b/runtime/src/cpu_emulation.h | |||
| @@ -45,6 +45,8 @@ struct Decoded { | |||
| 45 | 45 | ||
| 46 | // One of the encodings above at `code` (`avail` readable bytes), or Op::none. | 46 | // One of the encodings above at `code` (`avail` readable bytes), or Op::none. |
| 47 | inline Decoded decode(const uint8_t* code, std::size_t avail) { | 47 | inline Decoded decode(const uint8_t* code, std::size_t avail) { |
| 48 | if (!code) return {}; | ||
| 49 | if (avail > 15) avail = 15; // architectural x86-64 maximum instruction length | ||
| 48 | Decoded d; | 50 | Decoded d; |
| 49 | std::size_t i = 0; | 51 | std::size_t i = 0; |
| 50 | bool p66 = false, pf2 = false, pf3 = false; | 52 | bool p66 = false, pf2 = false, pf3 = false; |
| @@ -186,10 +188,12 @@ inline std::atomic<uint64_t>& count(Op op) { | |||
| 186 | return counts[int(op)]; | 188 | return counts[int(op)]; |
| 187 | } | 189 | } |
| 188 | 190 | ||
| 189 | // 0 when the page holding `addr` can be written; otherwise the exception a write raises there. A guard page loses | 191 | // 0 when the page holding `addr` can be written (and sets `region_end` to the end of the contiguous committed |
| 192 | // writable region returned by VirtualQuery); otherwise the exception a write raises there. A guard page loses | ||
| 190 | // its guard on that first touch and reports STATUS_GUARD_PAGE_VIOLATION, as the system does for a real access. | 193 | // its guard on that first touch and reports STATUS_GUARD_PAGE_VIOLATION, as the system does for a real access. |
| 191 | inline DWORD writable(uint64_t addr) { | 194 | inline DWORD writable_region(uint64_t addr, uint64_t& region_end) { |
| 192 | MEMORY_BASIC_INFORMATION mbi; | 195 | region_end = addr; |
| 196 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 193 | if (!VirtualQuery(reinterpret_cast<void*>(addr), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT) | 197 | if (!VirtualQuery(reinterpret_cast<void*>(addr), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT) |
| 194 | return DWORD(STATUS_ACCESS_VIOLATION); | 198 | return DWORD(STATUS_ACCESS_VIOLATION); |
| 195 | if (mbi.Protect & PAGE_GUARD) { | 199 | if (mbi.Protect & PAGE_GUARD) { |
| @@ -200,19 +204,33 @@ inline DWORD writable(uint64_t addr) { | |||
| 200 | const DWORD p = mbi.Protect & 0xFF; | 204 | const DWORD p = mbi.Protect & 0xFF; |
| 201 | const bool ok = p == PAGE_READWRITE || p == PAGE_WRITECOPY || p == PAGE_EXECUTE_READWRITE || | 205 | const bool ok = p == PAGE_READWRITE || p == PAGE_WRITECOPY || p == PAGE_EXECUTE_READWRITE || |
| 202 | p == PAGE_EXECUTE_WRITECOPY; | 206 | p == PAGE_EXECUTE_WRITECOPY; |
| 203 | return ok ? 0 : DWORD(STATUS_ACCESS_VIOLATION); | 207 | if (!ok) return DWORD(STATUS_ACCESS_VIOLATION); |
| 208 | const uint64_t base = reinterpret_cast<uint64_t>(mbi.BaseAddress); | ||
| 209 | if (addr < base || addr - base >= mbi.RegionSize) return DWORD(STATUS_ACCESS_VIOLATION); | ||
| 210 | const uint64_t avail = mbi.RegionSize - (addr - base); | ||
| 211 | region_end = avail > UINT64_MAX - addr ? UINT64_MAX : addr + avail; | ||
| 212 | return 0; | ||
| 213 | } | ||
| 214 | |||
| 215 | inline DWORD writable(uint64_t addr) { | ||
| 216 | uint64_t region_end = 0; | ||
| 217 | return writable_region(addr, region_end); | ||
| 204 | } | 218 | } |
| 205 | 219 | ||
| 206 | // How many of the `want` bytes at `addr` can be read, by page protection (no fault is taken to find out). | 220 | // How many of the `want` bytes at `addr` can be read, by page protection (no fault is taken to find out). |
| 207 | inline std::size_t readable(uint64_t addr, std::size_t want) { | 221 | inline std::size_t readable(uint64_t addr, std::size_t want) { |
| 208 | std::size_t got = 0; | 222 | std::size_t got = 0; |
| 209 | while (got < want) { | 223 | while (got < want) { |
| 210 | MEMORY_BASIC_INFORMATION mbi; | 224 | if (got > UINT64_MAX - addr) break; |
| 211 | if (!VirtualQuery(reinterpret_cast<void*>(addr + got), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT || | 225 | const uint64_t cur = addr + got; |
| 226 | MEMORY_BASIC_INFORMATION mbi{}; | ||
| 227 | if (!VirtualQuery(reinterpret_cast<void*>(cur), &mbi, sizeof mbi) || mbi.State != MEM_COMMIT || | ||
| 212 | (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) || !mbi.Protect) | 228 | (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) || !mbi.Protect) |
| 213 | break; | 229 | break; |
| 214 | const uint64_t end = reinterpret_cast<uint64_t>(mbi.BaseAddress) + mbi.RegionSize; | 230 | const uint64_t base = reinterpret_cast<uint64_t>(mbi.BaseAddress); |
| 215 | got += std::size_t(end - (addr + got)) < want - got ? std::size_t(end - (addr + got)) : want - got; | 231 | if (cur < base || cur - base >= mbi.RegionSize) break; |
| 232 | const uint64_t avail = mbi.RegionSize - (cur - base); | ||
| 233 | got += avail < want - got ? static_cast<std::size_t>(avail) : want - got; | ||
| 216 | } | 234 | } |
| 217 | return got; | 235 | return got; |
| 218 | } | 236 | } |
| @@ -315,15 +333,17 @@ inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, | |||
| 315 | 333 | ||
| 316 | const uint64_t start = c->Rax; | 334 | const uint64_t start = c->Rax; |
| 317 | const uint64_t limit = gpr_read(c, limit_reg); | 335 | const uint64_t limit = gpr_read(c, limit_reg); |
| 318 | if ((start & 63) != 0 || limit <= start + 64) return ClzeroLoopResult::no_match; | 336 | if ((start & 63) != 0 || limit <= start || limit - start <= 64) return ClzeroLoopResult::no_match; |
| 319 | 337 | ||
| 320 | uint64_t total = (limit - start + 63) & ~uint64_t(63); | 338 | const uint64_t span = limit - start; |
| 321 | constexpr uint64_t kMaxBatch = uint64_t(64) << 20; | 339 | constexpr uint64_t kMaxBatch = uint64_t(64) << 20; |
| 322 | if (total > kMaxBatch) total = kMaxBatch; | 340 | const uint64_t total = span >= kMaxBatch ? kMaxBatch : ((span + 63) & ~uint64_t(63)); |
| 341 | if (total > UINT64_MAX - start) return ClzeroLoopResult::no_match; | ||
| 323 | const uint64_t end = start + total; | 342 | const uint64_t end = start + total; |
| 324 | 343 | ||
| 325 | for (uint64_t cur = start; cur < end;) { | 344 | for (uint64_t cur = start; cur < end;) { |
| 326 | const DWORD err = writable(cur); | 345 | uint64_t region_end = cur; |
| 346 | const DWORD err = writable_region(cur, region_end); | ||
| 327 | if (err) { | 347 | if (err) { |
| 328 | if (cur > start) { | 348 | if (cur > start) { |
| 329 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(cur - start)); | 349 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(cur - start)); |
| @@ -339,8 +359,7 @@ inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, | |||
| 339 | } | 359 | } |
| 340 | return ClzeroLoopResult::faulted; | 360 | return ClzeroLoopResult::faulted; |
| 341 | } | 361 | } |
| 342 | const uint64_t next_page = (cur & ~uint64_t(0xFFF)) + 0x1000; | 362 | cur = region_end < end ? region_end : end; |
| 343 | cur = next_page < end ? next_page : end; | ||
| 344 | } | 363 | } |
| 345 | 364 | ||
| 346 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(total)); | 365 | std::memset(reinterpret_cast<void*>(start), 0, std::size_t(total)); |