From 78f942313db3684c498ad766ad7d649ab715b361 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Mon, 23 Nov 2026 20:02:08 -0500 Subject: [PATCH] fix(fex): x86 programs no longer see the host's arm64 preload libraries (#3098) * fix(fex): x86 programs no longer see the host's arm64 preload libraries * test(fex): build the x86 preload fixtures with an x86-64 compiler --- ...4-guest-preload-drops-host-libraries.patch | 51 ++++++++ tools/tests/test_fex_guest_preload.py | 120 ++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 tools/fex/patches/0014-guest-preload-drops-host-libraries.patch create mode 100644 tools/tests/test_fex_guest_preload.py diff --git a/tools/fex/patches/0014-guest-preload-drops-host-libraries.patch b/tools/fex/patches/0014-guest-preload-drops-host-libraries.patch new file mode 100644 index 0000000000..204c5c5dc6 --- /dev/null +++ b/tools/fex/patches/0014-guest-preload-drops-host-libraries.patch @@ -0,0 +1,51 @@ +diff --git a/Source/Tools/FEXInterpreter/ELFCodeLoader.h b/Source/Tools/FEXInterpreter/ELFCodeLoader.h +index e1f601b..cebf1f5 100644 +--- a/Source/Tools/FEXInterpreter/ELFCodeLoader.h ++++ b/Source/Tools/FEXInterpreter/ELFCodeLoader.h +@@ -222,6 +222,32 @@ class ELFCodeLoader final : public FEX::CodeLoader { + + public: + ++ static fextl::string GuestPreload(const char* List, const fextl::string& RootFS) { ++ fextl::string Result; ++ for (const char* Start = List; *Start; Start += *Start ? 1 : 0) { ++ size_t Length = strcspn(Start, ": "); ++ fextl::string Entry(Start, Length); ++ Start += Length; ++ if (Entry.empty()) { ++ continue; ++ } ++ if (Entry[0] == '/') { ++ auto Type = ::ELFLoader::ELFContainer::GetELFType(ResolveRootfsFile(Entry, RootFS)); ++ if (Type == ::ELFLoader::ELFContainer::TYPE_NONE) { ++ Type = ::ELFLoader::ELFContainer::GetELFType(Entry); ++ } ++ if (Type == ::ELFLoader::ELFContainer::TYPE_OTHER_ELF) { ++ continue; ++ } ++ } ++ if (!Result.empty()) { ++ Result += ':'; ++ } ++ Result += Entry; ++ } ++ return Result; ++ } ++ + static fextl::string ResolveRootfsFile(const fextl::string& File, fextl::string RootFS) { + // If the path is relative then just run that + if (File[0] != '/') { +@@ -330,6 +356,13 @@ public: + if (envp[i] == nullptr) { + break; + } ++ if (strncmp(envp[i], "LD_PRELOAD=", 11) == 0) { ++ auto Preload = GuestPreload(envp[i] + 11, RootFS); ++ if (!Preload.empty()) { ++ EnvironmentVariables.emplace_back("LD_PRELOAD=" + Preload); ++ } ++ continue; ++ } + EnvironmentVariables.emplace_back(envp[i]); + } + } diff --git a/tools/tests/test_fex_guest_preload.py b/tools/tests/test_fex_guest_preload.py new file mode 100644 index 0000000000..4b2f200bc0 --- /dev/null +++ b/tools/tests/test_fex_guest_preload.py @@ -0,0 +1,120 @@ +from pathlib import Path +import re +import shutil +import subprocess +import tempfile +import unittest + +PATCH = Path(__file__).resolve().parents[1] / "fex/patches/0014-guest-preload-drops-host-libraries.patch" + +HARNESS = r""" +#include +#include +#include +#include +#include +#include +namespace fextl { using string = std::string; } +namespace ELFLoader { +struct ELFContainer { + enum ELFType { TYPE_NONE, TYPE_X86_64, TYPE_X86_32, TYPE_OTHER_ELF }; + static ELFType GetELFType(const std::string& Filename) { + int FD = open(Filename.c_str(), O_RDONLY | O_CLOEXEC); + if (FD == -1) return TYPE_NONE; + Elf64_Ehdr Header {}; + ssize_t Read = pread(FD, &Header, sizeof(Header), 0); + close(FD); + if (Read < (ssize_t)sizeof(Header) || memcmp(Header.e_ident, ELFMAG, SELFMAG) != 0) return TYPE_NONE; + if (Header.e_machine == EM_X86_64) return TYPE_X86_64; + if (Header.e_machine == EM_386) return TYPE_X86_32; + return TYPE_OTHER_ELF; + } +}; +} +struct Loader { + static fextl::string ResolveRootfsFile(const fextl::string& File, fextl::string RootFS) { return RootFS + File; } + GUEST_PRELOAD +}; +int main(int argc, char** argv) { + std::printf("%s", Loader::GuestPreload(argv[1], argv[2]).c_str()); +} +""" + + +def x86_compiler(): + for name in ("x86_64-linux-gnu-gcc", "gcc"): + path = shutil.which(name) + if path and subprocess.run([path, "-dumpmachine"], capture_output=True, text=True).stdout.startswith("x86_64"): + return path + return None + + +X86_CC = x86_compiler() + + +def patch_function(): + lines = [line[1:] for line in PATCH.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")] + body = "\n".join(lines) + first = body.index(" static fextl::string GuestPreload(") + last = body.index("\n }\n", first) + len("\n }\n") + return body[first:last] + + +@unittest.skipUnless(shutil.which("g++") and X86_CC and shutil.which("aarch64-linux-gnu-gcc"), "needs g++, an x86-64 gcc and aarch64-linux-gnu-gcc") +class FexGuestPreloadTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = tempfile.TemporaryDirectory() + cls.dir = Path(cls.tmp.name) + source = cls.dir / "harness.cpp" + source.write_text(HARNESS.replace("GUEST_PRELOAD", patch_function())) + cls.binary = cls.dir / "harness" + subprocess.run(["g++", "-std=c++20", "-O1", "-o", str(cls.binary), str(source)], check=True) + stub = cls.dir / "stub.c" + stub.write_text("int stub(void) { return 0; }\n") + cls.host = cls.dir / "host" + cls.rootfs = cls.dir / "rootfs" + cls.shadow = cls.host / "lib/libshadow.so" + cls.rootfs_shadow = Path(str(cls.rootfs) + str(cls.shadow)) + for folder in (cls.host / "lib", cls.rootfs_shadow.parent): + folder.mkdir(parents=True) + cls.compile("aarch64-linux-gnu-gcc", stub, cls.host / "lib/libhost.so") + cls.compile(X86_CC, stub, cls.host / "lib/libguest.so") + cls.compile("aarch64-linux-gnu-gcc", stub, cls.shadow) + cls.compile(X86_CC, stub, cls.rootfs_shadow) + + @classmethod + def tearDownClass(cls): + cls.tmp.cleanup() + + @staticmethod + def compile(compiler, source, output): + subprocess.run([compiler, "-shared", "-fPIC", "-o", str(output), str(source)], check=True) + + def preload(self, value, rootfs=None): + root = str(self.rootfs) if rootfs is None else rootfs + return subprocess.run([str(self.binary), value, root], check=True, capture_output=True, text=True).stdout + + def test_host_libraries_leave_the_guest_list(self): + host, guest = self.host / "lib/libhost.so", self.host / "lib/libguest.so" + self.assertEqual(str(guest), self.preload(f"{host}:{guest}")) + self.assertEqual("", self.preload(str(host))) + + def test_rootfs_copy_decides_before_the_host_path(self): + self.assertEqual(str(self.shadow), self.preload(str(self.shadow))) + self.assertEqual("", self.preload(str(self.shadow), "")) + + def test_names_missing_files_and_separators_are_kept(self): + missing = str(self.dir / "missing.so") + guest = str(self.host / "lib/libguest.so") + self.assertEqual(f"libgamemodeauto.so.0:{missing}:{guest}", + self.preload(f" libgamemodeauto.so.0::{missing} {guest}:")) + + def test_patch_adds_no_comments(self): + added = [line[1:].strip() for line in PATCH.read_text().splitlines() if line.startswith("+") and not line.startswith("+++")] + self.assertFalse([line for line in added if line.startswith(("//", "/*"))]) + self.assertEqual(["Source/Tools/FEXInterpreter/ELFCodeLoader.h"], re.findall(r"^diff --git a/(\S+) ", PATCH.read_text(), re.M)) + + +if __name__ == "__main__": + unittest.main()