diff --git a/.github/workflows/gamenative-fexcore.yml b/.github/workflows/gamenative-fexcore.yml index b4937e9a3c..2d537eec54 100644 --- a/.github/workflows/gamenative-fexcore.yml +++ b/.github/workflows/gamenative-fexcore.yml @@ -146,7 +146,9 @@ jobs: - name: Install working-directory: fex - run: DESTDIR="$PWD"/install cmake --build build -t install + run: | + DESTDIR="$PWD"/install cmake --build build -t install + cp build/Bin/FEXOfflineCompiler*.exe install/usr/lib/wine/aarch64-windows/ - name: ccache stats run: ccache -s @@ -154,7 +156,9 @@ jobs: - uses: actions/upload-artifact@v6 with: name: dll-${{ matrix.target }} - path: fex/install/usr/lib/wine/aarch64-windows/lib*.dll + path: | + fex/install/usr/lib/wine/aarch64-windows/lib*.dll + fex/install/usr/lib/wine/aarch64-windows/FEXOfflineCompiler*.exe if-no-files-found: error retention-days: 7 @@ -199,6 +203,7 @@ jobs: run: | mkdir -p pkg/system32 cp dlls/libarm64ecfex.dll dlls/libwow64fex.dll pkg/system32/ + cp dlls/FEXOfflineCompiler64.exe dlls/FEXOfflineCompiler32.exe pkg/system32/ patches=$(grep -c -v -e '^#' -e '^$' gamenative/series) cat > pkg/profile.json <next and the tail sequence drift past its sequence re-check and crashed reading NULL+8. Outside metadata blocks nothing changes | GameNative only for now | +| 0007 fexofflinecompiler-load-the-config-before-initializing-logging | GameNative | The Windows offline compiler read `SilentLog` before any config was loaded and crashed at startup | to be sent upstream | +| 0008 codecache-rebase-relocation-offsets-to-the-code-buffer | GameNative | Since upstream #5840 relocation offsets are per compiled unit; the cache writer still stored them as code-buffer offsets, so every offline cache carried stale host pointers and un-rebased guest addresses. Offsets are rebased when a cache is generated or validated, and sorted on save | to be sent upstream | +| 0009 fexofflinecompiler-quote-process-all-child-arguments | GameNative | `process-all` passed child arguments unquoted, so exe names with spaces broke | to be sent upstream | +| 0010 codecache-copy-cached-code-to-a-writable-buffer-on-wow64 | GameNative | The WoW64 loader patched relocations into the read-only cache file mapping and killed the guest at start-up; it now gets a writable copy like ARM64EC (upstream #5658 covered ARM64EC only) | to be sent upstream | +| 0011 wow64-handle-faults-before-the-first-thread-exists | GameNative | The WoW64 exception handler read the thread state without a null check; a fault during `BTCpuProcessInit` (cache validation) killed the process silently. The overcommit path now runs before any thread access | to be sent upstream | +| 0012 codecache-restore-the-signal-handler-config-after-validation | GameNative | The validation context's `InitCore` overwrote the shared signal handler config | to be sent upstream | +| 0013 codecache-log-the-load-phases | GameNative | INFO lines for each phase of an offline cache load (map, copy, relocate, register, finalize, enable), visible with `FEX_SILENTLOG=0` | GameNative only | Left out of 0003 on purpose: tarikbc 0002 (hides the emulator from the guest), 0007 (rewrites ntdll export bytes to pass one game's hook check), 0008 (debug tool), 0010 (obsolete on Proton diff --git a/gamenative/patches/0007-FEXOfflineCompiler-load-the-config-before-initializi.patch b/gamenative/patches/0007-FEXOfflineCompiler-load-the-config-before-initializi.patch new file mode 100644 index 0000000000..fa2b647127 --- /dev/null +++ b/gamenative/patches/0007-FEXOfflineCompiler-load-the-config-before-initializi.patch @@ -0,0 +1,32 @@ +From a71151abdcc80340b3af777b420759c9a0ae4e42 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:27:57 +0530 +Subject: [PATCH 07/10] FEXOfflineCompiler: load the config before initializing + logging on Windows + +FEX::Windows::Logging::Init reads SilentLog and OutputLog from the +config layers. The offline compiler initialized logging before any +config was loaded, so all log output was dropped regardless of the +user configuration. +--- + Source/Tools/FEXOfflineCompiler/Main.cpp | 4 ++++ + 1 file changed, 4 insertions(+) + +diff --git a/Source/Tools/FEXOfflineCompiler/Main.cpp b/Source/Tools/FEXOfflineCompiler/Main.cpp +index 484aeb8..df4e3cd 100644 +--- a/Source/Tools/FEXOfflineCompiler/Main.cpp ++++ b/Source/Tools/FEXOfflineCompiler/Main.cpp +@@ -844,6 +844,10 @@ int main(int argc, char** argv) { + LogMan::Throw::InstallHandler(AssertHandler); + LogMan::Msg::InstallHandler(MsgHandler); + #else ++ { ++ char* envp[] = {nullptr}; ++ FEX::Config::LoadConfig("", envp, FEX::ReadPortabilityInformation()); ++ } + FEX::Windows::Logging::Init(); + #endif + +-- +2.43.0 + diff --git a/gamenative/patches/0008-CodeCache-rebase-relocation-offsets-to-the-code-buff.patch b/gamenative/patches/0008-CodeCache-rebase-relocation-offsets-to-the-code-buff.patch new file mode 100644 index 0000000000..c17a69b285 --- /dev/null +++ b/gamenative/patches/0008-CodeCache-rebase-relocation-offsets-to-the-code-buff.patch @@ -0,0 +1,131 @@ +From 65bbc2cd0527fddccd8f55742f2d9edb2ce2c53b Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:28:42 +0530 +Subject: [PATCH 08/10] CodeCache: rebase relocation offsets to the code buffer + when serializing + +Since ad3939a44 ("Avoid double offset relocations") relocation offsets +are relative to the start of their compile unit. That works for the +disk cache, which stores one unit at a time, but code cache generation +and validation accumulate relocations across all units of a binary +and apply them to the whole code buffer. + +The result was that every unit's relocations were applied to the first +few pages of the buffer: generated caches kept the host address of the +ExitFunctionLinker and unrelocated guest addresses in all but the first +unit, the relocation list was no longer sorted (breaking the per-page +grouping in LoadCache), and loading patched garbage into the first +pages. + +Rebase the offsets by the unit's code buffer offset when the JIT +compiles for cache generation and when Validate compiles reference +code, sort the serialized list by offset, and let the lazy finalization +path on Linux pass the offset of its staging window. +--- + FEXCore/Source/Interface/Context/Context.h | 4 +++- + FEXCore/Source/Interface/Core/CodeCache.cpp | 19 ++++++++++++++----- + FEXCore/Source/Interface/Core/JIT/JIT.cpp | 7 +++++++ + 3 files changed, 24 insertions(+), 6 deletions(-) + +diff --git a/FEXCore/Source/Interface/Context/Context.h b/FEXCore/Source/Interface/Context/Context.h +index a889086..f124325 100644 +--- a/FEXCore/Source/Interface/Context/Context.h ++++ b/FEXCore/Source/Interface/Context/Context.h +@@ -122,11 +122,13 @@ public: + * + * @param GuestDelta Guest address offset to apply to RIP-relative data + * @param ForStorage True for serializing data (producing deterministic output); false for de-serializing it (resolving dynamic symbols) ++ * @param CodeOffset Offset of Code within the buffer that relocation offsets are relative to + * + * @return Returns true on success + */ + [[nodiscard]] +- bool ApplyCodeRelocations(uint64_t GuestDelta, std::span Code, std::span Relocations, bool ForStorage); ++ bool ApplyCodeRelocations(uint64_t GuestDelta, std::span Code, std::span Relocations, bool ForStorage, ++ uint64_t CodeOffset = 0); + + // Same but on disk cache packed relocations + [[nodiscard]] +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index 87a110c..7ddcf18 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -298,6 +298,8 @@ bool CodeCache::SaveData(Core::InternalThreadState& Thread, int fd, const Execut + auto CodeBuffer = CTX.GetLatest(); + auto& LookupCache = *Thread.LookupCache->Shared; + auto Relocations = Thread.CPUBackend->TakeRelocations(SourceBinary.FileStartVA); ++ // Loading groups relocations by code page, which requires them to be sorted by offset ++ std::ranges::stable_sort(Relocations, std::less {}, [](const CPU::Relocation& Reloc) { return Reloc.Header.Offset; }); + + // Write file header + CodeCacheHeader header {}; +@@ -425,15 +427,21 @@ void CodeCache::Validate(const ExecutableFileSectionInfo& Section, fextl::setGetBufferBase(), NewCodeBuffer->GetBufferBase() + NewCodeBuffer->UsableSize()}) + .subspan(0, CachedCode.size_bytes()); + ++ fextl::vector NewRelocations; + while (!GuestBlocks.empty()) { + auto [CompiledBlocks, _, _2, _3, _4] = ValidationCTX->CompileCode(ValidationThread.get(), *GuestBlocks.begin(), 0 /* TODO: Set MaxInst? */); + for (auto& Entry : CompiledBlocks.EntryPoints) { + GuestBlocks.erase(Entry.first); + } ++ ++ // Relocation offsets are relative to their compile unit; rebase them to the CodeBuffer start ++ for (auto& Reloc : ValidationThread->CPUBackend->TakeRelocations(Section.FileStartVA)) { ++ Reloc.Header.Offset += CompiledBlocks.HostCodeOffset; ++ NewRelocations.push_back(Reloc); ++ } + } + + // Patch FEX-internal function addresses with values from the main Context to ensure the code blocks are comparable +- auto NewRelocations = ValidationThread->CPUBackend->TakeRelocations(Section.FileStartVA); + NewRelocations.erase(std::remove_if(NewRelocations.begin(), NewRelocations.end(), [](const CPU::Relocation& Reloc) { + return Reloc.Header.Type != CPU::RelocationTypes::RELOC_NAMED_SYMBOL_LITERAL && Reloc.Header.Type != CPU::RelocationTypes::RELOC_NAMED_THUNK_MOVE; + })); +@@ -616,12 +624,13 @@ bool CodeCache::ApplyPackedCodeRelocations(uint64_t GuestEntry, std::span Code, +- std::span EntryRelocations, bool ForStorage) { ++ std::span EntryRelocations, bool ForStorage, uint64_t CodeOffset) { + CPU::Arm64Emitter Emitter(&CTX, Code.data(), Code.size_bytes()); + for (size_t j = 0; j < EntryRelocations.size(); ++j) { + const FEXCore::CPU::Relocation& Reloc = EntryRelocations[j]; +- LOGMAN_THROW_A_FMT(Reloc.Header.Offset < Code.size_bytes(), "Invalid relocation offset"); +- Emitter.SetCursorOffset(Reloc.Header.Offset); ++ LOGMAN_THROW_A_FMT(Reloc.Header.Offset >= CodeOffset, "Invalid relocation offset"); ++ LOGMAN_THROW_A_FMT(Reloc.Header.Offset - CodeOffset < Code.size_bytes(), "Invalid relocation offset"); ++ Emitter.SetCursorOffset(Reloc.Header.Offset - CodeOffset); + + switch (Reloc.Header.Type) { + case FEXCore::CPU::RelocationTypes::RELOC_NAMED_SYMBOL_LITERAL: { +@@ -970,7 +979,7 @@ void CodeCache::FinalizeCodePages(MappedCodeCacheFile& Code, std::spanGetCodeCache().IsGeneratingCache) { ++ for (auto& Relocation : std::span {Relocations}.subspan(PrevNumAllocations)) { ++ Relocation.Header.Offset += CodeData.HostCodeOffset; ++ } ++ } ++ + TempCodeBufferAllocator.DelayedDisownBuffer(); + + ClearICache(CodeBegin, CodeOnlySize); +-- +2.43.0 + diff --git a/gamenative/patches/0009-FEXOfflineCompiler-quote-process-all-child-arguments.patch b/gamenative/patches/0009-FEXOfflineCompiler-quote-process-all-child-arguments.patch new file mode 100644 index 0000000000..9628fb5bad --- /dev/null +++ b/gamenative/patches/0009-FEXOfflineCompiler-quote-process-all-child-arguments.patch @@ -0,0 +1,74 @@ +From 1660b4b4236c3dcdb7e58537a9742cd979cb3a77 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:29:24 +0530 +Subject: [PATCH 09/10] FEXOfflineCompiler: quote process-all child arguments + on Windows + +_spawnv joins argv with spaces and does not quote the elements, so the +generate subprocess split paths containing spaces (for example a code +map named after "Blazing Chrome.exe" or a cache directory under a +profile path with spaces) into several arguments and failed. + +Quote each argument following the CommandLineToArgvW rules before +spawning. +--- + Source/Tools/FEXOfflineCompiler/Main.cpp | 36 ++++++++++++++++++++++-- + 1 file changed, 34 insertions(+), 2 deletions(-) + +diff --git a/Source/Tools/FEXOfflineCompiler/Main.cpp b/Source/Tools/FEXOfflineCompiler/Main.cpp +index df4e3cd..25f7322 100644 +--- a/Source/Tools/FEXOfflineCompiler/Main.cpp ++++ b/Source/Tools/FEXOfflineCompiler/Main.cpp +@@ -387,6 +387,28 @@ static LONG ExceptionHandler(_EXCEPTION_POINTERS* ExceptionInfo) { + return EXCEPTION_CONTINUE_SEARCH; + } + ++// _spawnv joins its arguments with spaces without quoting them ++static std::string QuoteCommandLineArgument(std::string_view Arg) { ++ if (!Arg.empty() && Arg.find_first_of(" \t\n\v\"") == Arg.npos) { ++ return std::string {Arg}; ++ } ++ ++ std::string Quoted = "\""; ++ size_t NumBackslashes = 0; ++ for (char C : Arg) { ++ if (C == '\\') { ++ ++NumBackslashes; ++ continue; ++ } ++ Quoted.append(C == '"' ? NumBackslashes * 2 + 1 : NumBackslashes, '\\'); ++ Quoted += C; ++ NumBackslashes = 0; ++ } ++ Quoted.append(NumBackslashes * 2, '\\'); ++ Quoted += '"'; ++ return Quoted; ++} ++ + struct winsize { + int ws_col; + }; +@@ -827,8 +849,18 @@ static int ProcessAll() { + } + #else + GenerateArgs.insert(GenerateArgs.begin(), SelfPath.c_str()); +- GenerateArgs.push_back(nullptr); +- auto Status = _spawnv(_P_WAIT, SelfPath.c_str(), GenerateArgs.data()); ++ std::vector QuotedArgs; ++ QuotedArgs.reserve(GenerateArgs.size()); ++ for (auto* Arg : GenerateArgs) { ++ QuotedArgs.push_back(QuoteCommandLineArgument(Arg)); ++ } ++ std::vector SpawnArgs; ++ SpawnArgs.reserve(QuotedArgs.size() + 1); ++ for (auto& Arg : QuotedArgs) { ++ SpawnArgs.push_back(Arg.c_str()); ++ } ++ SpawnArgs.push_back(nullptr); ++ auto Status = _spawnv(_P_WAIT, SelfPath.c_str(), SpawnArgs.data()); + if (Status) { + fmt::println("ERROR: Cache generation failed for {}", BinaryName); + } +-- +2.43.0 + diff --git a/gamenative/patches/0010-CodeCache-copy-cached-code-to-a-writable-buffer-on-W.patch b/gamenative/patches/0010-CodeCache-copy-cached-code-to-a-writable-buffer-on-W.patch new file mode 100644 index 0000000000..9896f62349 --- /dev/null +++ b/gamenative/patches/0010-CodeCache-copy-cached-code-to-a-writable-buffer-on-W.patch @@ -0,0 +1,66 @@ +From 4706b2913961673300fa407410b41a89c196f1de Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 18:29:44 +0530 +Subject: [PATCH 10/10] CodeCache: copy cached code to a writable buffer on + WoW64 + +ImageTracker maps code cache files with PAGE_EXECUTE_READ. Since +0ac6b3e8f only ARM64EC copies the cached code into a dedicated +allocation before applying relocations; WoW64 kept using the file view +as the execution buffer and FinalizeCodePages wrote relocations into +it, which raises an access violation in BTCpuProcessInit before the +first guest instruction whenever a cache is loaded. + +Use the VirtualAlloc'd copy on all Windows targets. +--- + FEXCore/Source/Interface/Core/CodeCache.cpp | 12 ++++-------- + 1 file changed, 4 insertions(+), 8 deletions(-) + +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index 7ddcf18..67bdb89 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -51,7 +51,7 @@ MappedCodeCacheFile::~MappedCodeCacheFile() { + if (!CodeBuffer.empty()) { + FEXCore::Allocator::munmap(CodeBuffer.data(), CodeBuffer.size_bytes()); + } +-#elif defined(_M_ARM64EC) ++#else + if (!CodeBuffer.empty()) { + FEXCore::Allocator::VirtualFree(CodeBuffer.data(), CodeBuffer.size_bytes()); + } +@@ -718,18 +718,16 @@ CodeCache::LoadCache(std::span CacheFile, const ExecutableFileInfo& F + return nullptr; + } + auto CodeBuffer = std::span {static_cast(CodeBufferAllocation), header.CodeBufferSize}; +-#elif defined(_M_ARM64EC) ++#else + // TODO: Implement lazy mapping on Windows +- // NOTE: The executed code must have MEM_EXTENDED_PARAMETER_EC_CODE set, so we can't operate on the mapped cache file directly ++ // NOTE: The cache file is mapped read-only, so relocations must be applied to a separate buffer. ++ // On ARM64EC, the executed code must also have MEM_EXTENDED_PARAMETER_EC_CODE set. + void* CodeBufferAllocation = Allocator::VirtualAlloc(header.CodeBufferSize, true); + if (!CodeBufferAllocation) { + LogMan::Msg::EFmt("Failed to allocate code cache memory"); + return nullptr; + } + auto CodeBuffer = std::span {reinterpret_cast(CodeBufferAllocation), header.CodeBufferSize}; +-#else // WoW64 +- // TODO: Implement lazy mapping on Windows +- auto CodeBuffer = CodeDataInFile; + #endif + + // Group relocations by page +@@ -997,9 +995,7 @@ void CodeCache::FinalizeCodePages(MappedCodeCacheFile& Code, std::span +Date: Wed, 28 Oct 2026 19:14:50 +0530 +Subject: [PATCH 11/13] WOW64: handle exceptions raised before the first thread + is set up + +Code caches for the main executable are loaded from BTCpuProcessInit, +before BTCpuThreadInit has stored a thread state in the TEB. With +FEX_ENABLECODECACHEVALIDATION set, Validate compiles the cached blocks +on a separate context, and CompileCode's FindBlock reads that context's +LookupCache, which is reserved without being committed. The resulting +access violation is meant to be resolved by the OvercommitTracker. + +BTCpuResetToConsistentStateImpl dereferences the thread state in +FEXCORE_PROFILE_ACCUMULATION before checking it, so the handler itself +faults and the process terminates without any log output. Since the +dereference comes first, the compiler also drops the later null checks. + +Handle the case without a thread state before touching it: resolve +overcommit faults and leave everything else to the regular dispatch. +--- + Source/Windows/WOW64/Module.cpp | 10 ++++++++++ + 1 file changed, 10 insertions(+) + +diff --git a/Source/Windows/WOW64/Module.cpp b/Source/Windows/WOW64/Module.cpp +index 8c6b7f7..fc56048 100644 +--- a/Source/Windows/WOW64/Module.cpp ++++ b/Source/Windows/WOW64/Module.cpp +@@ -729,6 +729,16 @@ bool BTCpuResetToConsistentStateImpl(EXCEPTION_POINTERS* Ptrs) { + auto* Context = Ptrs->ContextRecord; + auto* Exception = Ptrs->ExceptionRecord; + auto Thread = GetTLS().ThreadState(); ++ ++ if (!Thread) { ++ // BTCpuProcessInit runs before any thread state exists, and loading code caches there can touch overcommitted memory ++ if (Exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && OvercommitTracker && ++ OvercommitTracker->HandleAccessViolation(static_cast(Exception->ExceptionInformation[1]))) { ++ return true; ++ } ++ return false; ++ } ++ + FEXCORE_PROFILE_ACCUMULATION(Thread, AccumulatedSignalTime); + + if (Exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) { +-- +2.43.0 + diff --git a/gamenative/patches/0012-CodeCache-keep-the-main-dispatcher-config-when-creat.patch b/gamenative/patches/0012-CodeCache-keep-the-main-dispatcher-config-when-creat.patch new file mode 100644 index 0000000000..ed8b22d0fa --- /dev/null +++ b/gamenative/patches/0012-CodeCache-keep-the-main-dispatcher-config-when-creat.patch @@ -0,0 +1,38 @@ +From 3f8e27dad05e35c2cb037a4af4701d46db09973d Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 19:10:47 +0530 +Subject: [PATCH 12/13] CodeCache: keep the main dispatcher config when + creating the validation context + +ValidationCTX shares the main context's SignalDelegator, and InitCore +replaces the delegator config with one describing the validation +context's dispatcher. Every later exception or suspend handled for the +main context then checks dispatcher ranges and resume addresses of a +dispatcher that never runs guest code. + +Save the config before initializing the validation context and restore +it afterwards. +--- + FEXCore/Source/Interface/Core/CodeCache.cpp | 3 +++ + 1 file changed, 3 insertions(+) + +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index 67bdb89..d9ee36b 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -395,9 +395,12 @@ void CodeCache::Validate(const ExecutableFileSectionInfo& Section, fextl::setSetSignalDelegator(CTX.SignalDelegation); + ValidationCTX->SetSyscallHandler(CTX.SyscallHandler); + ValidationCTX->SetThunkHandler(CTX.ThunkHandler); ++ // InitCore points the shared SignalDelegator at the validation context's dispatcher; keep the main one ++ const auto SignalConfig = CTX.SignalDelegation->GetConfig(); + if (!ValidationCTX->InitCore()) { + ERROR_AND_DIE_FMT("Failed to create cache load validation context"); + } ++ CTX.SignalDelegation->SetConfig(SignalConfig); + + ValidationThread.reset(ValidationCTX->CreateThread(nullptr)); + +-- +2.43.0 + diff --git a/gamenative/patches/0013-Windows-log-the-code-cache-load-phases-and-pre-threa.patch b/gamenative/patches/0013-Windows-log-the-code-cache-load-phases-and-pre-threa.patch new file mode 100644 index 0000000000..59904e1d78 --- /dev/null +++ b/gamenative/patches/0013-Windows-log-the-code-cache-load-phases-and-pre-threa.patch @@ -0,0 +1,165 @@ +From 8a3f34ddbedfbebc906ce5effe1b05de1ec33662 Mon Sep 17 00:00:00 2001 +From: Utkarsh Dalal +Date: Wed, 28 Oct 2026 19:15:01 +0530 +Subject: [PATCH 13/13] Windows: log the code cache load phases and pre-thread + exceptions + +A failure while loading a code cache from BTCpuProcessInit used to end +the process without output. Report each step of the load at info level +so a single run shows how far it got: + +- the mapped cache view, or the NTSTATUS when the section or view + cannot be created +- the writable copy of the cached code with its protection +- block and code page registration, finalization and validation +- completion of the load for each image + +On WOW64, also report exceptions taken before BTCpuThreadInit has set +up a thread state, and overcommit ranges committed in that window. +--- + FEXCore/Source/Interface/Core/CodeCache.cpp | 14 ++++++ + Source/Windows/Common/ImageTracker.cpp | 53 ++++++++++++--------- + Source/Windows/WOW64/Module.cpp | 4 ++ + 3 files changed, 48 insertions(+), 23 deletions(-) + +diff --git a/FEXCore/Source/Interface/Core/CodeCache.cpp b/FEXCore/Source/Interface/Core/CodeCache.cpp +index d9ee36b..1fbaa12 100644 +--- a/FEXCore/Source/Interface/Core/CodeCache.cpp ++++ b/FEXCore/Source/Interface/Core/CodeCache.cpp +@@ -401,6 +401,7 @@ void CodeCache::Validate(const ExecutableFileSectionInfo& Section, fextl::setSetConfig(SignalConfig); ++ LogMan::Msg::IFmt("Cache validation: context ready"); + + ValidationThread.reset(ValidationCTX->CreateThread(nullptr)); + +@@ -731,6 +732,13 @@ CodeCache::LoadCache(std::span CacheFile, const ExecutableFileInfo& F + return nullptr; + } + auto CodeBuffer = std::span {reinterpret_cast(CodeBufferAllocation), header.CodeBufferSize}; ++ { ++ MEMORY_BASIC_INFORMATION Info {}; ++ ::VirtualQuery(CodeBufferAllocation, &Info, sizeof(Info)); ++ LogMan::Msg::IFmt("Cache map: file {} ({:#x} bytes); code {:#x} bytes at file offset {:#x} copied to {} (protect {:#x}); {} relocations", ++ fmt::ptr(CacheFile.data()), CacheFile.size_bytes(), header.CodeBufferSize, CodeDataInFile.data() - CacheFile.data(), ++ CodeBufferAllocation, Info.Protect, header.NumRelocations); ++ } + #endif + + // Group relocations by page +@@ -846,6 +854,8 @@ bool CodeCache::EnableLoadedSection(Core::InternalThreadState* Thread, MappedCod + } + } + } ++ LogMan::Msg::IFmt("Cache load: registered {} blocks and {} code pages; host code {}-{}", BlockList.size(), Code.NumCodePages, ++ fmt::ptr(Code.CodeBuffer.data()), fmt::ptr(Code.CodeBuffer.data() + Code.CodeBuffer.size_bytes())); + + #ifndef _WIN32 + if (!EnableLazyCodeCaching || EnableCodeCacheValidation) { +@@ -854,7 +864,9 @@ bool CodeCache::EnableLoadedSection(Core::InternalThreadState* Thread, MappedCod + if (true) { + #endif + auto Range = SelectCodeRangeToFinalize(Code, 0, Code.CodeBuffer.size_bytes() / Utils::FEX_PAGE_SIZE); ++ LogMan::Msg::IFmt("Cache load: finalizing {} pages at {}", Range.size_bytes() / Utils::FEX_PAGE_SIZE, fmt::ptr(Range.data())); + FinalizeCodePages(Code, Range); ++ LogMan::Msg::IFmt("Cache load: finalized; guest base {:#x}", Code.GuestBase); + } + + if (EnableCodeCacheValidation) { +@@ -864,9 +876,11 @@ bool CodeCache::EnableLoadedSection(Core::InternalThreadState* Thread, MappedCod + HostBlocks.insert(Host.HostCode); + } + ++ LogMan::Msg::IFmt("Cache load: validating {} guest blocks", GuestBlocks.size()); + Validate(BinarySection, std::move(GuestBlocks), HostBlocks, Code.CodeBuffer); + } + ++ LogMan::Msg::IFmt("Cache load: enabled {}", BinarySection.FileInfo.Filename); + return true; + } + +diff --git a/Source/Windows/Common/ImageTracker.cpp b/Source/Windows/Common/ImageTracker.cpp +index c459c44..fbe74ee 100644 +--- a/Source/Windows/Common/ImageTracker.cpp ++++ b/Source/Windows/Common/ImageTracker.cpp +@@ -246,29 +246,36 @@ void ImageTracker::LoadAOTImages(MappedImageInfo& ImageInfo) { + ScopedHandle FileHandle; + if (NT_SUCCESS(NtOpenFile(&*FileHandle, GENERIC_READ | SYNCHRONIZE, &FileAttr, &IOSB, FILE_SHARE_READ, FILE_SYNCHRONOUS_IO_NONALERT))) { + ScopedHandle SectionHandle; +- if (NT_SUCCESS(NtCreateSection(&*SectionHandle, SECTION_MAP_EXECUTE | SECTION_MAP_READ, nullptr, nullptr, PAGE_EXECUTE_READ, SEC_COMMIT, +- *FileHandle))) { +- void* LoadAddress = nullptr; +- SIZE_T MappedSize = 0; +- // TODO: Consider NtMapViewOfSectionEx with MEM_EXTENDED_PARAMETER_EC_CODE; +- // instead, we're eagerly copying all cache data to a dedicated buffer currently +- // MEM_EXTENDED_PARAMETER ECParam {}; +- // ECParam.Type = MemExtendedParameterAttributeFlags; +- // ECParam.ULong64 = MEM_EXTENDED_PARAMETER_EC_CODE; +- if (NT_SUCCESS(NtMapViewOfSection(*SectionHandle, NtCurrentProcess(), &LoadAddress, 0, 0, nullptr, &MappedSize, ViewUnmap, +- MEM_RESERVE | MEM_TOP_DOWN, PAGE_EXECUTE_READ))) { +- +- auto CacheSpan = std::span {static_cast(LoadAddress), MappedSize}; +- auto MappedCache = CTX.GetCodeCache().LoadCache(CacheSpan, ImageInfo.Info, ImageInfo.SectionInfo.FileStartVA); +- if (MappedCache) { +- CTX.GetCodeCache().RegisterMappedCodeBuffer(*MappedCache); +- AOTImages[ID] = {.CacheFile = std::move(MappedCache)}; +- LogMan::Msg::IFmt("Loaded cache: {}", ID); +- } else { +- NtUnmapViewOfSection(NtCurrentProcess(), LoadAddress); +- LogMan::Msg::EFmt("Failed to load cache: {}", ID); +- } +- } ++ NTSTATUS Status = NtCreateSection(&*SectionHandle, SECTION_MAP_EXECUTE | SECTION_MAP_READ, nullptr, nullptr, PAGE_EXECUTE_READ, SEC_COMMIT, ++ *FileHandle); ++ if (!NT_SUCCESS(Status)) { ++ LogMan::Msg::EFmt("Failed to create cache section for {}: {:#x}", ID, static_cast(Status)); ++ return; ++ } ++ void* LoadAddress = nullptr; ++ SIZE_T MappedSize = 0; ++ // TODO: Consider NtMapViewOfSectionEx with MEM_EXTENDED_PARAMETER_EC_CODE; ++ // instead, we're eagerly copying all cache data to a dedicated buffer currently ++ // MEM_EXTENDED_PARAMETER ECParam {}; ++ // ECParam.Type = MemExtendedParameterAttributeFlags; ++ // ECParam.ULong64 = MEM_EXTENDED_PARAMETER_EC_CODE; ++ Status = NtMapViewOfSection(*SectionHandle, NtCurrentProcess(), &LoadAddress, 0, 0, nullptr, &MappedSize, ViewUnmap, ++ MEM_RESERVE | MEM_TOP_DOWN, PAGE_EXECUTE_READ); ++ if (!NT_SUCCESS(Status)) { ++ LogMan::Msg::EFmt("Failed to map cache view for {}: {:#x}", ID, static_cast(Status)); ++ return; ++ } ++ LogMan::Msg::IFmt("Mapped cache view: {} at {} ({:#x} bytes)", ID, LoadAddress, MappedSize); ++ ++ auto CacheSpan = std::span {static_cast(LoadAddress), MappedSize}; ++ auto MappedCache = CTX.GetCodeCache().LoadCache(CacheSpan, ImageInfo.Info, ImageInfo.SectionInfo.FileStartVA); ++ if (MappedCache) { ++ CTX.GetCodeCache().RegisterMappedCodeBuffer(*MappedCache); ++ AOTImages[ID] = {.CacheFile = std::move(MappedCache)}; ++ LogMan::Msg::IFmt("Loaded cache: {}", ID); ++ } else { ++ NtUnmapViewOfSection(NtCurrentProcess(), LoadAddress); ++ LogMan::Msg::EFmt("Failed to load cache: {}", ID); + } + } + } +diff --git a/Source/Windows/WOW64/Module.cpp b/Source/Windows/WOW64/Module.cpp +index fc56048..8bd2c8c 100644 +--- a/Source/Windows/WOW64/Module.cpp ++++ b/Source/Windows/WOW64/Module.cpp +@@ -734,8 +734,12 @@ bool BTCpuResetToConsistentStateImpl(EXCEPTION_POINTERS* Ptrs) { + // BTCpuProcessInit runs before any thread state exists, and loading code caches there can touch overcommitted memory + if (Exception->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && OvercommitTracker && + OvercommitTracker->HandleAccessViolation(static_cast(Exception->ExceptionInformation[1]))) { ++ LogMan::Msg::IFmt("Committed overcommit range at {:X} before thread setup", Exception->ExceptionInformation[1]); + return true; + } ++ LogMan::Msg::EFmt("Unhandled exception {:X} before thread setup: pc {:X}, info {:X} {:X}", Exception->ExceptionCode, Context->Pc, ++ Exception->NumberParameters > 0 ? Exception->ExceptionInformation[0] : 0, ++ Exception->NumberParameters > 1 ? Exception->ExceptionInformation[1] : 0); + return false; + } + +-- +2.43.0 + diff --git a/gamenative/series b/gamenative/series index 5f645822d4..d783c606c0 100644 --- a/gamenative/series +++ b/gamenative/series @@ -4,3 +4,10 @@ 0004-arm64ec-export-btcpu64notifynonimageviews-so-ntd.patch 0005-windows-make-volatile-code-regions-opt-in.patch 0006-fexcore-fence-plain-loads-before-volatile-metadata-t.patch +0007-FEXOfflineCompiler-load-the-config-before-initializi.patch +0008-CodeCache-rebase-relocation-offsets-to-the-code-buff.patch +0009-FEXOfflineCompiler-quote-process-all-child-arguments.patch +0010-CodeCache-copy-cached-code-to-a-writable-buffer-on-W.patch +0011-WOW64-handle-exceptions-raised-before-the-first-thre.patch +0012-CodeCache-keep-the-main-dispatcher-config-when-creat.patch +0013-Windows-log-the-code-cache-load-phases-and-pre-threa.patch