Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions .github/workflows/gamenative-fexcore.yml
Original file line number Diff line number Diff line change
Expand Up @@ -146,15 +146,19 @@ 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

- 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

Expand Down Expand Up @@ -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 <<JSON
{
Expand All @@ -214,6 +219,14 @@ jobs:
{
"source": "system32/libwow64fex.dll",
"target": "\${system32}/libwow64fex.dll"
},
{
"source": "system32/FEXOfflineCompiler64.exe",
"target": "\${system32}/FEXOfflineCompiler64.exe"
},
{
"source": "system32/FEXOfflineCompiler32.exe",
"target": "\${system32}/FEXOfflineCompiler32.exe"
}
]
}
Expand Down
7 changes: 7 additions & 0 deletions gamenative/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ behaviour that is not a plain correctness fix is behind an env var.
| 0004 arm64ec-export-btcpu64notifynonimageviews | GameNative | Exports `BTCpu64NotifyNonImageViews` so ntdll only reports non-image views to a FEX that can take them | pairs with GameNative/proton-wine `dlls_arm64ec_emulator_notifications.patch` |
| 0005 windows-make-volatile-code-regions-opt-in | GameNative | Volatile code region detection (from 0003) runs only with `FEX_EXP_VOLATILE=1`; any other value or unset is off. On by default it misfires on JIT runtimes: TMNT: Shredder's Revenge (FNA/.NET) recompiles on its main thread, runs about 10x slower and never creates a D3D device. Age of Empires IV needs it and sets it in its overlay. Env gate: `FEX_EXP_VOLATILE=1` opts in | GameNative only |
| 0006 fexcore-fence-plain-loads-before-volatile-metadata-tso-loads | GameNative | With PE volatile metadata, a listed (TSO) load gets a `dmb ishld` in front when a plain load may precede it in the same metadata block, restoring x86 load order. Counter-Strike 2's CTSQueue pop let node->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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
From a71151abdcc80340b3af777b420759c9a0ae4e42 Mon Sep 17 00:00:00 2001
From: Utkarsh Dalal <dalal.utkarsh@gmail.com>
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

Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
From 65bbc2cd0527fddccd8f55742f2d9edb2ce2c53b Mon Sep 17 00:00:00 2001
From: Utkarsh Dalal <dalal.utkarsh@gmail.com>
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<std::byte> Code, std::span<const CPU::Relocation> Relocations, bool ForStorage);
+ bool ApplyCodeRelocations(uint64_t GuestDelta, std::span<std::byte> Code, std::span<const CPU::Relocation> 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::set<ui
std::as_writable_bytes(std::span {NewCodeBuffer->GetBufferBase(), NewCodeBuffer->GetBufferBase() + NewCodeBuffer->UsableSize()})
.subspan(0, CachedCode.size_bytes());

+ fextl::vector<CPU::Relocation> 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<std::b
}

bool CodeCache::ApplyCodeRelocations(uint64_t GuestEntry, std::span<std::byte> Code,
- std::span<const FEXCore::CPU::Relocation> EntryRelocations, bool ForStorage) {
+ std::span<const FEXCore::CPU::Relocation> 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::span<std::byte
auto StagingSpan = std::span {Staging, Size};
for (size_t i = StartPage; i < EndPage; ++i) {
auto PageRelocations = SpanPageRelocations(Code, i);
- (void)ApplyCodeRelocations(Code.GuestBase, StagingSpan, PageRelocations, false);
+ (void)ApplyCodeRelocations(Code.GuestBase, StagingSpan, PageRelocations, false, StartOffset);
Code.LoadedPages[i] = true;
}

diff --git a/FEXCore/Source/Interface/Core/JIT/JIT.cpp b/FEXCore/Source/Interface/Core/JIT/JIT.cpp
index 58b2fe4..b7a7ebb 100644
--- a/FEXCore/Source/Interface/Core/JIT/JIT.cpp
+++ b/FEXCore/Source/Interface/Core/JIT/JIT.cpp
@@ -1167,6 +1167,13 @@ CPUBackend::CompiledCode Arm64JITCore::CompileCode(uint64_t Entry, uint64_t Size
memcpy(AllocatedInfo.BufferAllocationOffset, TempCodeBuffer, CodeData.Size);
}

+ // Relocations accumulate across compile units while generating a code cache, so make them relative to the CodeBuffer base
+ if (CTX->GetCodeCache().IsGeneratingCache) {
+ for (auto& Relocation : std::span {Relocations}.subspan(PrevNumAllocations)) {
+ Relocation.Header.Offset += CodeData.HostCodeOffset;
+ }
+ }
+
TempCodeBufferAllocator.DelayedDisownBuffer();

ClearICache(CodeBegin, CodeOnlySize);
--
2.43.0

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
From 1660b4b4236c3dcdb7e58537a9742cd979cb3a77 Mon Sep 17 00:00:00 2001
From: Utkarsh Dalal <dalal.utkarsh@gmail.com>
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<std::string> QuotedArgs;
+ QuotedArgs.reserve(GenerateArgs.size());
+ for (auto* Arg : GenerateArgs) {
+ QuotedArgs.push_back(QuoteCommandLineArgument(Arg));
+ }
+ std::vector<const char*> 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

Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
From 4706b2913961673300fa407410b41a89c196f1de Mon Sep 17 00:00:00 2001
From: Utkarsh Dalal <dalal.utkarsh@gmail.com>
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<std::byte> CacheFile, const ExecutableFileInfo& F
return nullptr;
}
auto CodeBuffer = std::span {static_cast<std::byte*>(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<std::byte*>(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<std::byte
Allocator::VirtualDontNeed(Code.CodeBufferInFile.data() + StartOffset, Size);
#else
// TODO: Implement lazy mapping on Windows
-#ifdef _M_ARM64EC
memcpy(Code.CodeBuffer.data() + StartOffset, Code.CodeBufferInFile.data() + StartOffset, Size);
-#endif
for (size_t i = StartPage; i < EndPage; ++i) {
auto PageRelocations = SpanPageRelocations(Code, i);
(void)ApplyCodeRelocations(Code.GuestBase, Code.CodeBuffer, PageRelocations, false);
--
2.43.0

Loading
Loading