From 8b50f854e453e8fad8a8509a4452ab3e3b4ecee8 Mon Sep 17 00:00:00 2001 From: xacidr4inx Date: Thu, 26 Nov 2026 10:57:52 -0500 Subject: [PATCH] fix(libc): keep AVX state across resumed exceptions on affected Windows 11 kernels Windows 11 24H2/25H2 kernels before 26100.7920 (and Windows Server 2025) can build a user-mode exception's context from a stale XSAVE header left on the stack (JDK-8365196). The context then omits AVX state the thread uses, and resuming from it zeroes the upper halves of ymm0-15 and the AVX-512 state. A guest store that faults on a write-watched page and is resumed by ReportCrash then writes zeros where those halves belong. The vectored handler now takes an XSAVE of the vector components on entry, and before an EXCEPTION_CONTINUE_EXECUTION writes the components in use that the context omits back into its XSAVE area and fixes the header, so the resume restores them. Co-Authored-By: Claude Opus 5.5 --- .../src/specifics/windows/CrashReport.cpp | 102 +++++++++++++++++- docs/dev/TechnicalDebt.md | 1 + 2 files changed, 102 insertions(+), 1 deletion(-) diff --git a/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp b/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp index a4f388e..9d0a8a9 100644 --- a/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp +++ b/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include "Sse4aEmulation.hpp" @@ -234,6 +235,93 @@ bool HandleSse4a(EXCEPTION_POINTERS* info) { return true; } +constexpr std::uint64_t kVectorComponents = 0xe4; +constexpr std::size_t kXSaveAreaSize = 4096; + +struct XStateComponent { + std::uint32_t standardOffset = 0; + std::uint32_t size = 0; + bool align64 = false; +}; + +struct ContextChunk { + LONG offset; + DWORD length; +}; + +struct ContextEx { + ContextChunk all, legacy, xstate; +}; + +XStateComponent g_xstateComponents[64]; + +std::uint64_t ReadVectorComponents() { + unsigned eax = 0, ebx = 0, ecx = 0, edx = 0; + asm volatile("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(1), "c"(0)); + if ((ecx & (1u << 27)) == 0) return 0; + unsigned low = 0, high = 0; + asm volatile("xgetbv" : "=a"(low), "=d"(high) : "c"(0)); + const std::uint64_t enabled = ((static_cast(high) << 32) | low) & kVectorComponents; + for (unsigned i = 2; i < 64; ++i) { + if (((enabled >> i) & 1) == 0) continue; + asm volatile("cpuid" : "=a"(eax), "=b"(ebx), "=c"(ecx), "=d"(edx) : "a"(0xd), "c"(i)); + g_xstateComponents[i] = {ebx, eax, (ecx & 2) != 0}; + } + return enabled; +} + +const std::uint64_t g_vectorComponents = ReadVectorComponents(); + +void SaveXState(unsigned char* area, std::uint64_t mask) { + asm volatile("xsave64 %0" : "+m"(*reinterpret_cast(area)) : "a"(static_cast(mask)), "d"(static_cast(mask >> 32))); +} + +void RestoreXState(unsigned char* area, std::uint64_t mask) { + asm volatile("xrstor64 %0" : : "m"(*reinterpret_cast(area)), "a"(static_cast(mask)), "d"(static_cast(mask >> 32)) : "memory"); +} + +bool RepairContextXState(CONTEXT* context, const unsigned char* area, std::uint64_t mask) { + if ((context->ContextFlags & CONTEXT_XSTATE) != CONTEXT_XSTATE) return false; + std::uint64_t live = 0; + std::memcpy(&live, area + 512, sizeof(live)); + live &= mask; + auto* ex = reinterpret_cast(context + 1); + auto* chunk = reinterpret_cast(ex) + ex->xstate.offset; + const std::uint64_t length = ex->xstate.length; + if (length < 64) return false; + std::uint64_t header[2]; + std::memcpy(header, chunk, sizeof(header)); + if ((live & ~header[0]) == 0) return false; + constexpr std::uint64_t compacted = 1ull << 63; + std::uint64_t place[64] = {}; + std::uint64_t layout = 0; + if ((header[1] & compacted) != 0) { + if ((header[1] & ~compacted & ~3ull & ~mask) != 0) return false; + layout = (header[1] & ~compacted) | mask; + std::uint64_t offset = 576; + for (unsigned i = 2; i < 64; ++i) { + if (((layout >> i) & 1) == 0) continue; + if (g_xstateComponents[i].align64) offset = (offset + 63) & ~63ull; + place[i] = offset; + offset += g_xstateComponents[i].size; + } + if (offset - 512 > length) return false; + } else { + for (unsigned i = 2; i < 64; ++i) { + if (((mask >> i) & 1) == 0) continue; + place[i] = g_xstateComponents[i].standardOffset; + if (place[i] + g_xstateComponents[i].size - 512 > length) return false; + } + } + for (unsigned i = 2; i < 64; ++i) { + if (((live >> i) & 1) != 0) std::memcpy(chunk + (place[i] - 512), area + g_xstateComponents[i].standardOffset, g_xstateComponents[i].size); + } + header[0] = (header[0] & ~mask) | live; + if ((header[1] & compacted) != 0) header[1] = compacted | (header[1] & 3ull) | layout; + std::memcpy(chunk, header, sizeof(header)); + return true; +} + LONG WINAPI ReportCrash(EXCEPTION_POINTERS* info) { static std::atomic reported{false}; const auto* fault = info->ExceptionRecord; @@ -333,6 +421,18 @@ LONG WINAPI ReportCrash(EXCEPTION_POINTERS* info) { return EXCEPTION_CONTINUE_SEARCH; } +LONG WINAPI ReportCrashKeepingXState(EXCEPTION_POINTERS* info) { + const std::uint64_t mask = g_vectorComponents; + if (mask == 0) return ReportCrash(info); + alignas(64) unsigned char area[kXSaveAreaSize]; + std::memset(area + 512, 0, 64); + SaveXState(area, mask); + const LONG result = ReportCrash(info); + if (result == EXCEPTION_CONTINUE_EXECUTION) RepairContextXState(info->ContextRecord, area, mask); + RestoreXState(area, mask); + return result; +} + // abort() and the UCRT's invalid-parameter path end the process with a silent fast fail // (0xc0000409); both are reported with the calling thread's stack first. void ReportBacktrace(const char* what) { @@ -366,7 +466,7 @@ struct ExitReporter { const bool g_crashReportInstalled = [] { g_sse4aEmulation = !IsEnvironmentSet("APS5_NO_SSE4A_EMULATION"); g_sse4aTrace = IsEnvironmentSet("APS5_TRACE_SSE4A"); - AddVectoredExceptionHandler(1, ReportCrash); + AddVectoredExceptionHandler(1, ReportCrashKeepingXState); std::signal(SIGABRT, AbortSignalHandler); _set_invalid_parameter_handler(InvalidParameterHandler); InstallWatch(); diff --git a/docs/dev/TechnicalDebt.md b/docs/dev/TechnicalDebt.md index 2ef5d5e..dd8d2e5 100644 --- a/docs/dev/TechnicalDebt.md +++ b/docs/dev/TechnicalDebt.md @@ -50,3 +50,4 @@ Throughout the project, every function at every stage either **does exactly what - [RytyNet](../../core/libs/prx/libRytyNet/src/RytyNetBridge.cpp) (2.0.0, experimental) performs plain HTTP/1.1 over TCP only: no TLS (https requests fail with the network error and a log line), no keep-alive, no proxies, and the `libSceNpManager` signed-in presence is local (`RytyPlayer`) with nothing behind it. The non-blocking `sceHttp*Epoll` API stays offline even with the bridge on. - The macOS writer's guest `sce_module` PRXs are emitted as Linux ELF shared objects and loaded by the runtime's own loader; dyld load commands for them are deliberately not emitted (a `LC_LOAD_DYLIB` at an ELF file would abort launch), so a macOS port still needs its `libs/` populated like the Linux one. Mach-O guest modules would need a `GuestModuleWriter::WriteMachO` backend. - The macOS ad-hoc code signature covers the classic CodeDirectory only (no requirements, entitlements or CMS slot); `codesign --force --deep -s -` remains the fallback if a hardened-runtime host refuses it. +- [ReportCrashKeepingXState](../../core/libs/prx/libc/src/specifics/windows/CrashReport.cpp) works around a Windows kernel bug (Windows 11 24H2/25H2 kernels before 26100.7920, Windows Server 2025, JDK-8365196): an exception context built from a stale XSAVE header on the stack omits AVX state in use, and resuming from it zeroes the upper halves of ymm0-15 and the AVX-512 state. Before a resume, the handler writes the omitted components back into the context from an XSAVE taken on entry. Remove it once no supported Windows ships an affected kernel.