diff options
| author | OreoFR <noreply@users.noreply.github.com> | 2026-11-28 05:28:00 +0100 |
|---|---|---|
| committer | OreoFR <noreply@users.noreply.github.com> | 2026-11-28 05:28:00 +0100 |
| commit | 5d677b7ba2b2dd5b128455edf2b92bea6da4dcc2 (patch) | |
| tree | cca8090de22564ac93e738a50eedd77885732956 /runtime/src | |
| parent | 63a73515d1fc26c309ac0d9cd13ec9afd2fa5016 (diff) | |
cpu_emulation: a CLWB #UD on a site another thread rewrote continues at it (RT-CLWB-PATCH)
Threads that fault on the same CLWB site together lost the race: a #UD handled
after another thread rewrote the site decoded CLFLUSHOPT (or the NOP), did not
know it and returned EXCEPTION_CONTINUE_SEARCH, which ends the title.
- The handler continues at a site that already holds the form written on this
host (or the NOP's halfway state): the CPU runs it.
- One lock around unprotect, re-check, write and protect again, so two threads
never interleave on a page; a site found rewritten under it is not written.
- The NOP is written opcode first (66 0F 1F /6 in between is a NOP too).
- The sites counter counts each site once, by the thread that wrote it.
cpu_emulation_test calls the handler with a made-up #UD, so it runs on hosts
with CLWB: first #UD, a second one on the rewritten site, and 16 threads on
2048 fresh execute-read sites. Before the fix 27958 of 32768 concurrent #UDs
were passed on, 4810 rewrites were counted for 2048 sites and pages were left
writable; with it all checks pass.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/cpu_emulation.h | 79 |
1 files changed, 65 insertions, 14 deletions
diff --git a/runtime/src/cpu_emulation.h b/runtime/src/cpu_emulation.h index 777a536bd..4f9565dcc 100644 --- a/runtime/src/cpu_emulation.h +++ b/runtime/src/cpu_emulation.h | |||
| @@ -356,7 +356,11 @@ inline ClzeroLoopResult try_clzero_loop(const uint8_t* code, std::size_t avail, | |||
| 356 | // 66 0F AE /6 (CLWB) becomes 66 0F AE /7 (CLFLUSHOPT): the same operand and length, and it writes the line back too | 356 | // 66 0F AE /6 (CLWB) becomes 66 0F AE /7 (CLFLUSHOPT): the same operand and length, and it writes the line back too |
| 357 | // (then evicts it). A host without CLFLUSHOPT gets 66 0F 1F /0, a NOP of the same length that touches no memory. | 357 | // (then evicts it). A host without CLFLUSHOPT gets 66 0F 1F /0, a NOP of the same length that touches no memory. |
| 358 | // LEONIDA_CLWB_PATCH=0 keeps emulating every execution instead. | 358 | // LEONIDA_CLWB_PATCH=0 keeps emulating every execution instead. |
| 359 | inline bool patch_clwb(uint64_t rip, const uint8_t* code, std::size_t length) { | 359 | // Threads of that worker reach the same site together: a #UD handled after another thread rewrote the site finds |
| 360 | // the new instruction there and continues at it, and the rewrite itself is done once, under a lock. | ||
| 361 | |||
| 362 | // 1: rewrite as CLFLUSHOPT, 2: as the NOP (no CLFLUSHOPT on this host), 0: LEONIDA_CLWB_PATCH=0. | ||
| 363 | inline int clwb_patch_mode() { | ||
| 360 | static const int mode = [] { | 364 | static const int mode = [] { |
| 361 | char value[4]{}; | 365 | char value[4]{}; |
| 362 | if (GetEnvironmentVariableA("LEONIDA_CLWB_PATCH", value, sizeof(value)) > 0 && value[0] == '0') return 0; | 366 | if (GetEnvironmentVariableA("LEONIDA_CLWB_PATCH", value, sizeof(value)) > 0 && value[0] == '0') return 0; |
| @@ -364,24 +368,70 @@ inline bool patch_clwb(uint64_t rip, const uint8_t* code, std::size_t length) { | |||
| 364 | __cpuidex(r, 7, 0); | 368 | __cpuidex(r, 7, 0); |
| 365 | return ((r[1] >> 23) & 1) ? 1 : 2; // EBX bit 23: CLFLUSHOPT | 369 | return ((r[1] >> 23) & 1) ? 1 : 2; // EBX bit 23: CLFLUSHOPT |
| 366 | }(); | 370 | }(); |
| 371 | return mode; | ||
| 372 | } | ||
| 373 | |||
| 374 | // How many CLWB sites were rewritten so far; each counts once, by the thread that wrote it. | ||
| 375 | inline std::atomic<unsigned>& clwb_sites() { | ||
| 376 | static std::atomic<unsigned> sites{0}; | ||
| 377 | return sites; | ||
| 378 | } | ||
| 379 | |||
| 380 | // True when `code` (`avail` bytes at a #UD) holds the form patch_clwb writes on this host, or the NOP's halfway | ||
| 381 | // state (66 0F 1F /6): another thread rewrote the site after this thread's #UD. Neither form raises #UD here, so the | ||
| 382 | // thread continues at the site and the CPU runs it. | ||
| 383 | inline bool clwb_rewritten(const uint8_t* code, std::size_t avail) { | ||
| 384 | const int mode = clwb_patch_mode(); | ||
| 385 | if (!mode) return false; | ||
| 386 | std::size_t i = 0; | ||
| 387 | bool p66 = false; | ||
| 388 | for (; i < avail && i < 14; ++i) { | ||
| 389 | const uint8_t b = code[i]; | ||
| 390 | if (b == 0x66) p66 = true; | ||
| 391 | else if (!((b & 0xF0) == 0x40 || b == 0x67 || b == 0x2E || b == 0x3E || b == 0x26 || b == 0x36 || | ||
| 392 | b == 0x64 || b == 0x65)) break; | ||
| 393 | } | ||
| 394 | if (!p66 || i + 3 > avail || code[i] != 0x0F || (code[i + 2] >> 6) == 3) return false; | ||
| 395 | const int reg = (code[i + 2] >> 3) & 7; | ||
| 396 | return mode == 1 ? code[i + 1] == 0xAE && reg == 7 : code[i + 1] == 0x1F && (reg == 0 || reg == 6); | ||
| 397 | } | ||
| 398 | |||
| 399 | inline bool patch_clwb(uint64_t rip, const uint8_t* code, std::size_t length) { | ||
| 400 | const int mode = clwb_patch_mode(); | ||
| 367 | if (!mode) return false; | 401 | if (!mode) return false; |
| 368 | std::size_t at = 0; | 402 | std::size_t at = 0; |
| 369 | while (at + 2 < length && !(code[at] == 0x0F && code[at + 1] == 0xAE)) ++at; | 403 | while (at + 2 < length && !(code[at] == 0x0F && code[at + 1] == 0xAE)) ++at; |
| 370 | if (at + 2 >= length) return false; | 404 | if (at + 2 >= length) return false; |
| 371 | uint8_t patched[3] = {0x0F, 0xAE, uint8_t(code[at + 2] | 0x08)}; // reg field 6 -> 7 | 405 | const uint8_t opcode = mode == 1 ? 0xAE : 0x1F; |
| 372 | if (mode == 2) patched[1] = 0x1F, patched[2] = uint8_t(code[at + 2] & ~0x38); // reg field 0 | 406 | const uint8_t modrm = mode == 1 ? uint8_t(code[at + 2] | 0x08) : uint8_t(code[at + 2] & ~0x38); // reg 7 or 0 |
| 373 | void* const where = reinterpret_cast<void*>(rip + at); | 407 | void* const where = reinterpret_cast<void*>(rip + at); |
| 408 | volatile uint8_t* const live = static_cast<volatile uint8_t*>(where); | ||
| 409 | // One thread at a time unprotects, checks, writes and protects again: without the lock a thread could write into | ||
| 410 | // a page another one has just set back to read-execute, or set it back to the read-write-execute it found. | ||
| 411 | static SRWLOCK lock = SRWLOCK_INIT; | ||
| 412 | AcquireSRWLockExclusive(&lock); | ||
| 413 | bool ok = live[1] == opcode && live[2] == modrm; // another thread rewrote it since this thread's #UD | ||
| 414 | bool wrote = false; | ||
| 374 | DWORD old = 0; | 415 | DWORD old = 0; |
| 375 | if (!VirtualProtect(where, sizeof patched, PAGE_EXECUTE_READWRITE, &old)) return false; | 416 | if (!ok && live[0] == 0x0F && live[1] == 0xAE && live[2] == code[at + 2] && |
| 376 | std::memcpy(where, patched, sizeof patched); | 417 | VirtualProtect(where, 3, PAGE_EXECUTE_READWRITE, &old)) { |
| 377 | VirtualProtect(where, sizeof patched, old, &old); | 418 | // Opcode first: between the two stores a thread running the site sees 66 0F 1F /6, a NOP as well. ModRM first |
| 378 | FlushInstructionCache(GetCurrentProcess(), where, sizeof patched); | 419 | // would show it 66 0F AE /0, FXSAVE's encoding with a 66 the SDM does not allow (a 512-byte store at the |
| 379 | static std::atomic<unsigned> sites{0}; | 420 | // operand, or a #UD that is no longer CLWB). CLFLUSHOPT only changes ModRM, one store. |
| 380 | const unsigned n = ++sites; | 421 | if (live[1] != opcode) live[1] = opcode; |
| 381 | if (n <= 4 || !(n & (n - 1))) | 422 | live[2] = modrm; |
| 382 | std::fprintf(stderr, "[rt] cpu: CLWB at %p rewritten as %s (%u sites so far)\n", reinterpret_cast<void*>(rip), | 423 | VirtualProtect(where, 3, old, &old); |
| 383 | mode == 1 ? "CLFLUSHOPT" : "a NOP", n); | 424 | FlushInstructionCache(GetCurrentProcess(), where, 3); |
| 384 | return true; | 425 | ok = wrote = true; |
| 426 | } | ||
| 427 | ReleaseSRWLockExclusive(&lock); | ||
| 428 | if (wrote) { | ||
| 429 | const unsigned n = ++clwb_sites(); | ||
| 430 | if (n <= 4 || !(n & (n - 1))) | ||
| 431 | std::fprintf(stderr, "[rt] cpu: CLWB at %p rewritten as %s (%u sites so far)\n", | ||
| 432 | reinterpret_cast<void*>(rip), mode == 1 ? "CLFLUSHOPT" : "a NOP", n); | ||
| 433 | } | ||
| 434 | return ok; | ||
| 385 | } | 435 | } |
| 386 | 436 | ||
| 387 | inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { | 437 | inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { |
| @@ -391,7 +441,8 @@ inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { | |||
| 391 | const std::size_t avail = readable(c->Rip, sizeof code); // the instruction may end at a page boundary | 441 | const std::size_t avail = readable(c->Rip, sizeof code); // the instruction may end at a page boundary |
| 392 | std::memcpy(code, reinterpret_cast<const void*>(c->Rip), avail); | 442 | std::memcpy(code, reinterpret_cast<const void*>(c->Rip), avail); |
| 393 | const Decoded d = decode(code, avail); | 443 | const Decoded d = decode(code, avail); |
| 394 | if (d.op == Op::none) return EXCEPTION_CONTINUE_SEARCH; | 444 | if (d.op == Op::none) |
| 445 | return clwb_rewritten(code, avail) ? EXCEPTION_CONTINUE_EXECUTION : EXCEPTION_CONTINUE_SEARCH; | ||
| 395 | if (d.op == Op::clwb && patch_clwb(c->Rip, code, d.length)) return EXCEPTION_CONTINUE_EXECUTION; | 446 | if (d.op == Op::clwb && patch_clwb(c->Rip, code, d.length)) return EXCEPTION_CONTINUE_EXECUTION; |
| 396 | EXCEPTION_RECORD fault; | 447 | EXCEPTION_RECORD fault; |
| 397 | if (d.op == Op::clzero && !d.addr32 && d.length == 3) { | 448 | if (d.op == Op::clzero && !d.addr32 && d.length == 3) { |