Add an end-to-end probe for guest memory above the host address limit

This commit is contained in:
maxjivi05 committed 2026-12-03 21:28:02 -05:00
1 parent 15b5d46f70
commit 46e36d95a3
3 files changed
+117 -1

No files matched your search

+3 -1
View File
@@ -29,7 +29,9 @@ game in Steam always runs it under FEX.
## Why the x86-64 build needs its own Wine patches
Android kernels use 39-bit user address spaces. Easy Anti-Cheat's launcher maps fixed memory at
`0x7001_0000_0000`, which FEX serves from emulated guest pages (`tools/fex/patches/0027`). The x86-64 Wine
`0x7001_0000_0000`, which FEX serves from emulated guest pages (`tools/fex/patches/0027`). FEX claims such mappings only on
hosts with LSE atomics; elsewhere the map fails as in stock FEX. `tools/fex/probes/guest-high-memory.c`
checks loads, stores, atomics, iovec system calls, `madvise` and code written through a high alias. The x86-64 Wine
in DroidDeck-Proton carries `ntdll-fixed-allocations-above-host-address-limit.patch` (fixed mappings above
the host limit go to the emulator) and `ntdll-keep-libc-in-native-syscall-dispatch-range.patch` (the
re-armed syscall user dispatch range keeps libc native). Stock x86-64 Proton stops after "Hack native
+91
View File
@@ -0,0 +1,91 @@
#define _GNU_SOURCE
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/uio.h>
#include <unistd.h>
#define HIGH ((uintptr_t)0x7e0000000000ULL)
#define ALIAS ((uintptr_t)0x7e0000100000ULL)
#define THREADS 4
#define ITERS 200000
static volatile uint64_t *counter;
static volatile uint64_t *cas;
static int fails;
static void check(const char *name, int ok) {
printf("%s %s\n", ok ? "PASS" : "FAIL", name);
if (!ok) fails++;
}
static void *worker(void *arg) {
(void)arg;
for (int i = 0; i < ITERS; i++) {
__atomic_fetch_add(counter, 1, __ATOMIC_SEQ_CST);
uint64_t old = __atomic_load_n(cas, __ATOMIC_RELAXED);
while (!__atomic_compare_exchange_n(cas, &old, old + 1, 0, __ATOMIC_SEQ_CST, __ATOMIC_RELAXED)) {
}
}
return NULL;
}
int main(void) {
alarm(60);
uint8_t *high = mmap((void *)HIGH, 65536, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, -1, 0);
if (high == MAP_FAILED) {
printf("NOTCLAIMED errno=%d\n", errno);
return 0;
}
check("fixed-high-map", high == (uint8_t *)HIGH);
memset(high, 0x5a, 65536);
check("load-store", high[100] == 0x5a && high[65535] == 0x5a);
counter = (uint64_t *)(high + 64);
cas = (uint64_t *)(high + 128);
*counter = 0;
*cas = 0;
pthread_t t[THREADS];
for (int i = 0; i < THREADS; i++) pthread_create(&t[i], NULL, worker, NULL);
for (int i = 0; i < THREADS; i++) pthread_join(t[i], NULL);
check("atomic-add", *counter == (uint64_t)THREADS * ITERS);
check("atomic-cmpxchg", *cas == (uint64_t)THREADS * ITERS);
int p[2];
check("pipe", pipe(p) == 0);
memcpy(high + 4096, "hello ", 6);
memcpy(high + 8192, "world", 5);
struct iovec out[2] = {{high + 4096, 6}, {high + 8192, 5}};
ssize_t w = writev(p[1], out, 2);
memset(high + 12288, 0, 64);
struct iovec in[2] = {{high + 12288, 3}, {high + 12291, 8}};
ssize_t r = readv(p[0], in, 2);
check("writev-readv", w == 11 && r == 11 && memcmp(high + 12288, "hello world", 11) == 0);
check("write", write(p[1], "abcdef", 6) == 6);
check("read", read(p[0], high + 20000, 6) == 6 && memcmp(high + 20000, "abcdef", 6) == 0);
high[30000] = 7;
check("madvise-dontneed", madvise(high + 28672, 4096, MADV_DONTNEED) == 0 && high[30000] == 0);
int fd = memfd_create("code", 0);
check("memfd", fd >= 0 && ftruncate(fd, 4096) == 0);
static const uint8_t code[] = {0xb8, 0x01, 0x00, 0x00, 0x00, 0xc3};
check("pwrite", pwrite(fd, code, sizeof code, 0) == (ssize_t)sizeof code);
uint8_t *low = mmap(NULL, 4096, PROT_READ | PROT_EXEC, MAP_SHARED, fd, 0);
uint8_t *alias = mmap((void *)ALIAS, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd, 0);
check("alias-map", low != MAP_FAILED && alias == (uint8_t *)ALIAS);
int (*fn)(void) = (int (*)(void))low;
int before = fn();
alias[1] = 2;
__atomic_thread_fence(__ATOMIC_SEQ_CST);
int after = fn();
printf("alias before=%d after=%d\n", before, after);
check("smc-through-high-alias", before == 1 && after == 2);
printf("%s\n", fails ? "RESULT FAIL" : "RESULT PASS");
return fails != 0;
}
+23
View File
@@ -8,6 +8,7 @@ from pathlib import Path
REPO = Path(__file__).resolve().parents[2]
PATCH = REPO / "tools/fex/patches/0027-fixed-guest-memory-above-host-address-limit.patch"
PROBE = REPO / "tools/fex/probes/guest-high-memory.c"
SOURCE = "Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.cpp"
HEADER = "Source/Tools/LinuxEmulation/LinuxSyscalls/GuestHighMemory.h"
@@ -402,5 +403,27 @@ class GuestHighMemoryTest(unittest.TestCase):
self.assertNotIn("OverlapsLocked", text)
@unittest.skipUnless(platform.machine() == "x86_64", "the probe is x86-64 code")
class GuestHighMemoryProbeTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
compiler = shutil.which("cc") or shutil.which("gcc") or shutil.which("clang")
if not compiler:
raise unittest.SkipTest("no C compiler")
cls.tmp = Path(tempfile.mkdtemp(prefix="fex-guest-high-memory-"))
cls.binary = cls.tmp / "probe"
subprocess.run([compiler, "-O2", "-pthread", "-o", str(cls.binary), str(PROBE)], check=True, capture_output=True, text=True)
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.tmp, ignore_errors=True)
def test_native_kernel_passes_every_check(self):
result = subprocess.run([str(self.binary)], capture_output=True, text=True, timeout=120)
self.assertEqual(result.returncode, 0, result.stdout)
self.assertNotIn("FAIL", result.stdout)
self.assertEqual(result.stdout.splitlines()[-1], "RESULT PASS")
if __name__ == "__main__":
unittest.main()