mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-20 05:16:01 +00:00
FEX: preload the fault report only where x86 loaders look (#551)
Preload the FEX fault report only where x86 loaders look droiddeck-fex put the x86 libfaultreport.so in LD_PRELOAD, which FEX's own arm64 loader reads too - and FEXServer's, and every child FEX's - so each x86 start printed "ERROR: ld.so: object '.../libfaultreport.so' from LD_PRELOAD cannot be preloaded", and an i386 child added "wrong ELF class". It is now listed in the x86 runtime's own /etc/ld.so.preload, which only the loaders inside FEX read, as /usr/$LIB/droiddeck/libfaultreport.so. faultreport.c builds for i386 too, so 32-bit programs get the report as well instead of an error.
This commit is contained in:
1 parent
2a06204eb4
commit
d45933e559
7 files changed
+104
-23
No files matched your search
@@ -26,9 +26,14 @@ for arch in x86_64 i386; do
|
||||
fi
|
||||
done
|
||||
done
|
||||
for name in faultreport thunkaudit; do
|
||||
lib=$out/x86_64/lib$name.so
|
||||
gcc -m64 -shared -fPIC -O2 -Wall -Wextra -nostdlib -fno-stack-protector -fno-builtin -fvisibility=hidden \
|
||||
for target in x86_64/faultreport i386/faultreport x86_64/thunkaudit; do
|
||||
arch=${target%/*} name=${target#*/}
|
||||
case $arch in
|
||||
x86_64) bits=-m64 ;;
|
||||
i386) bits=-m32 ;;
|
||||
esac
|
||||
lib=$out/$arch/lib$name.so
|
||||
gcc $bits -shared -fPIC -O2 -Wall -Wextra -nostdlib -fno-stack-protector -fno-builtin -fvisibility=hidden \
|
||||
-o "$lib" tools/linuxfs/fex/$name.c
|
||||
strip --strip-unneeded "$lib"
|
||||
if objdump -p "$lib" | grep -q NEEDED || [ -n "$(nm -D --undefined-only "$lib" 2>/dev/null)" ]; then
|
||||
|
||||
@@ -1,15 +1,30 @@
|
||||
#if !defined(__x86_64__)
|
||||
#error faultreport is built for x86_64 guests only
|
||||
#endif
|
||||
|
||||
#if defined(__x86_64__)
|
||||
#define SYS_read 0
|
||||
#define SYS_write 1
|
||||
#define SYS_close 3
|
||||
#define SYS_rt_sigaction 13
|
||||
#define SYS_rt_sigreturn "15"
|
||||
#define SYS_getpid 39
|
||||
#define SYS_gettid 186
|
||||
#define SYS_tgkill 234
|
||||
#define SYS_openat 257
|
||||
#define SI_ADDR 16
|
||||
#define TRAP "syscall"
|
||||
#elif defined(__i386__)
|
||||
#define SYS_read 3
|
||||
#define SYS_write 4
|
||||
#define SYS_close 6
|
||||
#define SYS_rt_sigaction 174
|
||||
#define SYS_rt_sigreturn "173"
|
||||
#define SYS_getpid 20
|
||||
#define SYS_gettid 224
|
||||
#define SYS_tgkill 270
|
||||
#define SYS_openat 295
|
||||
#define SI_ADDR 12
|
||||
#define TRAP "int $0x80"
|
||||
#else
|
||||
#error faultreport is built for x86 guests only
|
||||
#endif
|
||||
#define AT_FDCWD (-100)
|
||||
#define SIGILL 4
|
||||
#define SA_SIGINFO 0x4UL
|
||||
@@ -21,14 +36,18 @@ struct kernel_sigaction {
|
||||
void (*handler)(int, void *, void *);
|
||||
unsigned long flags;
|
||||
void (*restorer)(void);
|
||||
unsigned long mask;
|
||||
unsigned long long mask;
|
||||
};
|
||||
|
||||
static long sys(long n, long a, long b, long c, long d)
|
||||
{
|
||||
long r;
|
||||
#if defined(__x86_64__)
|
||||
register long r10 __asm__("r10") = d;
|
||||
__asm__ volatile("syscall" : "=a"(r) : "a"(n), "D"(a), "S"(b), "d"(c), "r"(r10) : "rcx", "r11", "memory");
|
||||
#else
|
||||
__asm__ volatile("int $0x80" : "=a"(r) : "a"(n), "b"(a), "c"(b), "d"(c), "S"(d) : "memory");
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
@@ -100,7 +119,7 @@ static int find_mapping(unsigned long addr, const char **line, unsigned long *le
|
||||
static void report(int sig, void *info, void *context)
|
||||
{
|
||||
(void)context;
|
||||
unsigned long addr = *(unsigned long *)((char *)info + 16);
|
||||
unsigned long addr = *(unsigned long *)((char *)info + SI_ADDR);
|
||||
int code = *(int *)((char *)info + 8);
|
||||
used = 0;
|
||||
put("droiddeck-fex: SIGILL at 0x");
|
||||
@@ -135,7 +154,7 @@ static void report(int sig, void *info, void *context)
|
||||
}
|
||||
|
||||
void faultreport_restore(void);
|
||||
__asm__(".text\n.type faultreport_restore,@function\nfaultreport_restore:\nmov $15, %eax\nsyscall\nhlt\n");
|
||||
__asm__(".text\n.type faultreport_restore,@function\nfaultreport_restore:\nmov $" SYS_rt_sigreturn ", %eax\n" TRAP "\nhlt\n");
|
||||
|
||||
__attribute__((constructor)) static void install(void)
|
||||
{
|
||||
|
||||
@@ -34,7 +34,6 @@ LAYOUT = 1
|
||||
GUEST_LIBDIRS = (("x86_64", "lib/x86_64-linux-gnu"), ("i386", "lib/i386-linux-gnu"))
|
||||
PRELOAD_NAMES = ("libblsession.so", "libfakeinput.so")
|
||||
FAULT_REPORT = "libfaultreport.so"
|
||||
FAULT_REPORT_GUEST = "/usr/lib/x86_64-linux-gnu/droiddeck/" + FAULT_REPORT
|
||||
THUNK_AUDIT = "libthunkaudit.so"
|
||||
THUNK_AUDIT_GUEST = "/usr/lib/x86_64-linux-gnu/droiddeck/" + THUNK_AUDIT
|
||||
VULKAN_PIN = "libvulkan-thunk.so"
|
||||
@@ -241,18 +240,23 @@ def host_preloads():
|
||||
|
||||
|
||||
def install_preloads(root):
|
||||
shipped = set()
|
||||
shipped = {}
|
||||
for arch, libdir in GUEST_LIBDIRS:
|
||||
for name in PRELOAD_NAMES:
|
||||
for name in PRELOAD_NAMES + (FAULT_REPORT,):
|
||||
source = os.path.join(PRELOADS, arch, name)
|
||||
if os.path.isfile(source):
|
||||
copy_if_changed(source, os.path.join(root, libdir, "droiddeck", name))
|
||||
shipped.add(name)
|
||||
for name in (FAULT_REPORT, THUNK_AUDIT, VULKAN_PIN):
|
||||
shipped.setdefault(name, set()).add(arch)
|
||||
for name in (THUNK_AUDIT, VULKAN_PIN):
|
||||
source = os.path.join(PRELOADS, "x86_64", name)
|
||||
if os.path.isfile(source):
|
||||
copy_if_changed(source, os.path.join(root, "lib/x86_64-linux-gnu/droiddeck", name))
|
||||
wanted = [name for name in host_preloads() if name in shipped]
|
||||
# The fault report is preloaded from the runtime's own list, which only x86 loaders read: through
|
||||
# LD_PRELOAD it reached FEX's own loader too, and every FEX after it, which could not open it and
|
||||
# said so at each start. Listed only with both builds, or i386 programs would say so instead.
|
||||
if len(shipped.get(FAULT_REPORT, ())) == len(GUEST_LIBDIRS):
|
||||
wanted.append(FAULT_REPORT)
|
||||
write_if_changed(os.path.join(root, "etc", "ld.so.preload"), "".join("/usr/$LIB/droiddeck/%s\n" % name for name in wanted))
|
||||
|
||||
|
||||
@@ -502,8 +506,6 @@ def launch(command, env, mode, decide=None):
|
||||
return 126
|
||||
guest = guest_environment(fex, env)
|
||||
x86_64 = elf_arch(program) != "i386"
|
||||
if os.path.isfile(os.path.join(PRELOADS, "x86_64", FAULT_REPORT)) and x86_64:
|
||||
guest["LD_PRELOAD"] = ":".join([FAULT_REPORT_GUEST] + split_paths(guest.get("LD_PRELOAD")))
|
||||
thunks = "on" if "FEX_THUNKCONFIG" in guest else "off"
|
||||
folder = decide if decide and os.path.isdir(decide) else os.path.dirname(program)
|
||||
if thunks == "on" and x86_64 and all(os.path.isfile(os.path.join(PRELOADS, "x86_64", name)) for name in (THUNK_AUDIT, VULKAN_PIN)) \
|
||||
|
||||
@@ -59,6 +59,7 @@ def required():
|
||||
paths += ['assets/linuxfs/' + arch + '/' + lib for lib in ['libfakeinput.so', 'libblsession.so']]
|
||||
paths += ['assets/linuxfs/x86_64/' + lib for lib in
|
||||
['libfaultreport.so', 'libthunkaudit.so', 'libvulkan-thunk.so']]
|
||||
paths += ['assets/linuxfs/i386/libfaultreport.so']
|
||||
paths += ['assets/linuxfs/usr/local/lib/droiddeck-msitools/' + name for name in
|
||||
['msiinfo', 'cabextract', '7z', '7z.so', 'libmsi-1.0.so.0', 'libgsf-1.so.114', 'libgcab-1.0.so.0']]
|
||||
paths += ['assets/linuxfs/usr/local/lib/mangoapp/' + name for name in
|
||||
|
||||
@@ -198,12 +198,10 @@ class PrepareTest(FexTestCase):
|
||||
for arch in ("x86_64", "i386"):
|
||||
for name in ("libblsession.so", "libfakeinput.so"):
|
||||
self.write(self.tmp / "preloads" / arch / name, arch.encode() + name.encode())
|
||||
self.write(self.tmp / "preloads/x86_64/libfaultreport.so", b"report")
|
||||
self.write(self.tmp / "preloads/x86_64/libthunkaudit.so", b"audit")
|
||||
self.write(self.tmp / "preloads/x86_64/libvulkan-thunk.so", b"pin")
|
||||
self.write(self.tmp / "ld.so.preload", "/usr/local/lib/libblsession.so\n/usr/local/lib/libfakeinput.so\n")
|
||||
FEX["install_preloads"](str(root))
|
||||
self.assertEqual((root / "lib/x86_64-linux-gnu/droiddeck/libfaultreport.so").read_bytes(), b"report")
|
||||
self.assertEqual((root / "lib/x86_64-linux-gnu/droiddeck/libthunkaudit.so").read_bytes(), b"audit")
|
||||
self.assertEqual((root / "lib/x86_64-linux-gnu/droiddeck/libvulkan-thunk.so").read_bytes(), b"pin")
|
||||
self.assertEqual((root / "etc/ld.so.preload").read_text(),
|
||||
@@ -213,6 +211,17 @@ class PrepareTest(FexTestCase):
|
||||
FEX["install_preloads"](str(root))
|
||||
self.assertEqual((root / "etc/ld.so.preload").read_text(), "/usr/$LIB/droiddeck/libblsession.so\n")
|
||||
|
||||
def test_only_x86_loaders_preload_the_fault_report(self):
|
||||
root = self.tmp / "root"
|
||||
self.write(self.tmp / "preloads/x86_64/libfaultreport.so", b"report64")
|
||||
FEX["install_preloads"](str(root))
|
||||
self.assertNotIn("libfaultreport.so", (root / "etc/ld.so.preload").read_text())
|
||||
self.write(self.tmp / "preloads/i386/libfaultreport.so", b"report32")
|
||||
FEX["install_preloads"](str(root))
|
||||
self.assertEqual((root / "lib/x86_64-linux-gnu/droiddeck/libfaultreport.so").read_bytes(), b"report64")
|
||||
self.assertEqual((root / "lib/i386-linux-gnu/droiddeck/libfaultreport.so").read_bytes(), b"report32")
|
||||
self.assertEqual((root / "etc/ld.so.preload").read_text(), "/usr/$LIB/droiddeck/libfaultreport.so\n")
|
||||
|
||||
def test_config_turns_thunks_on_only_when_complete(self):
|
||||
FEX["write_config"]({"host_thunks": "/h", "guest_thunks": "/g", "thunks_db": "/db"}, "/root")
|
||||
config = json.loads((self.home / ".local/share/droiddeck-fex/config/Config.json").read_text())
|
||||
@@ -385,11 +394,12 @@ class LaunchTest(FexTestCase):
|
||||
self.assertEqual(env["FEX_PORTABLE"], "1")
|
||||
self.assertNotIn("LD_PRELOAD", env)
|
||||
self.assertEqual(env["FEX_ROOTFS"], str(self.steam / "steamapps/common/SteamLinuxRuntime_sniper/sniper_platform_3.0.20260805.254768/files"))
|
||||
self.write(self.tmp / "preloads/x86_64/libfaultreport.so", elf(62))
|
||||
for arch in ("x86_64", "i386"):
|
||||
self.write(self.tmp / "preloads" / arch / "libfaultreport.so", elf(62 if arch == "x86_64" else 3))
|
||||
x86 = self.write(self.tmp / "x86.so", elf(62))
|
||||
with mock.patch("os.execve") as emulated:
|
||||
self.quiet(FEX["launch"], [str(program)], {"LD_PRELOAD": str(x86)}, "auto")
|
||||
self.assertEqual(emulated.call_args[0][2]["LD_PRELOAD"], "/usr/lib/x86_64-linux-gnu/droiddeck/libfaultreport.so:%s" % x86)
|
||||
self.assertEqual(emulated.call_args[0][2]["LD_PRELOAD"], str(x86))
|
||||
old = self.write(self.tmp / "game.i386", elf(3), 0o755)
|
||||
with mock.patch("os.execve") as emulated:
|
||||
self.quiet(FEX["launch"], [str(old)], {}, "auto")
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
import unittest
|
||||
|
||||
SOURCE = Path(__file__).resolve().parents[1] / "linuxfs/fex/faultreport.c"
|
||||
PROGRAM = 'int main(void) { __asm__ volatile("ud2"); return 0; }\n'
|
||||
FLAGS = ["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror", "-nostdlib", "-fno-stack-protector", "-fno-builtin",
|
||||
"-fvisibility=hidden"]
|
||||
|
||||
|
||||
@unittest.skipUnless(shutil.which("gcc") and os.uname().machine == "x86_64", "needs gcc on an x86-64 host")
|
||||
class FaultReportTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmp = Path(tempfile.mkdtemp())
|
||||
self.addCleanup(shutil.rmtree, self.tmp, ignore_errors=True)
|
||||
(self.tmp / "program.c").write_text(PROGRAM)
|
||||
|
||||
def build(self, bits):
|
||||
library, program = self.tmp / ("libfaultreport%s.so" % bits), self.tmp / ("program%s" % bits)
|
||||
if subprocess.run(["gcc", "-m" + bits, "-o", str(program), str(self.tmp / "program.c")], capture_output=True).returncode:
|
||||
self.skipTest("gcc cannot build %s-bit programs here" % bits)
|
||||
made = subprocess.run(["gcc", "-m" + bits] + FLAGS + ["-o", str(library), str(SOURCE)], capture_output=True, text=True)
|
||||
self.assertEqual(made.returncode, 0, made.stderr)
|
||||
return library, program
|
||||
|
||||
def check(self, bits):
|
||||
library, program = self.build(bits)
|
||||
result = subprocess.run([str(program)], env=dict(os.environ, LD_PRELOAD=str(library)), capture_output=True, text=True)
|
||||
self.assertEqual(result.returncode, -4)
|
||||
self.assertRegex(result.stderr, r"droiddeck-fex: SIGILL at 0x[0-9a-f]+ \(si_code 02\), offset 0x[0-9a-f]+, bytes 0f 0b ")
|
||||
self.assertIn("program%s" % bits, result.stderr)
|
||||
|
||||
def test_an_x86_64_program_reports_its_illegal_instruction(self):
|
||||
self.check("64")
|
||||
|
||||
def test_an_i386_program_reports_its_illegal_instruction(self):
|
||||
self.check("32")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user