perf: relocated Wine code is shared between processes through a per-session cache file

This commit is contained in:
xXJsonDeruloXx committed 2026-10-11 10:08:10 -04:00
1 parent e079f9602f
commit a760aea276
3 files changed
+194

No files matched your search

+75
View File
@@ -11,6 +11,8 @@
#include <sys/mman.h>
#include <fcntl.h>
#include <stdint.h>
#include <sys/auxv.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <sys/statvfs.h>
#include <sys/syscall.h>
@@ -79,8 +81,81 @@ static int private_file_mapping(uintptr_t start, uintptr_t end) {
return found;
}
static const char rx_dir[] = "/tmp/.droiddeck-rx";
static uint64_t fnv1a(const unsigned char *p, size_t n) {
uint64_t h = 1469598103934665603ULL;
for (size_t i = 0; i < n; i++) {
h ^= p[i];
h *= 1099511628211ULL;
}
return h;
}
static char *put_hex(char *out, uint64_t v) {
for (int shift = 60; shift >= 0; shift -= 4) *out++ = "0123456789abcdef"[(v >> shift) & 15];
return out;
}
static int same_content(int fd, const unsigned char *p, size_t len) {
char buf[16384];
for (size_t off = 0; off < len;) {
size_t want = len - off < sizeof buf ? len - off : sizeof buf;
if (syscall(SYS_pread64, fd, buf, want, off) != (long)want || memcmp(buf, p + off, want)) return 0;
off += want;
}
return 1;
}
static int write_all(int fd, const unsigned char *p, size_t len) {
for (size_t off = 0; off < len;) {
long n = syscall(SYS_write, fd, p + off, len - off);
if (n <= 0) return -1;
off += (size_t)n;
}
return 0;
}
static int rebake_shared(void *addr, size_t len, int prot) {
char path[sizeof rx_dir + 40], staged[sizeof path + 20];
char *end = path + sizeof rx_dir - 1;
memcpy(path, rx_dir, sizeof rx_dir - 1);
*end++ = '/';
end = put_hex(end, fnv1a(addr, len));
*end++ = '-';
end = put_hex(end, len);
*end = 0;
int fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
if (fd >= 0 && !same_content(fd, addr, len)) {
syscall(SYS_close, fd);
return -1;
}
if (fd < 0) {
syscall(SYS_mkdirat, AT_FDCWD, rx_dir, 0700);
memcpy(staged, path, (size_t)(end - path));
char *tail = put_hex(staged + (end - path), (uint64_t)syscall(SYS_gettid));
*tail = 0;
int out = (int)syscall(SYS_openat, AT_FDCWD, staged, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
if (out < 0) return -1;
int bad = write_all(out, addr, len);
syscall(SYS_close, out);
if (bad || syscall(SYS_renameat, AT_FDCWD, staged, AT_FDCWD, path)) {
syscall(SYS_unlinkat, AT_FDCWD, staged, 0);
return -1;
}
fd = (int)syscall(SYS_openat, AT_FDCWD, path, O_RDONLY | O_CLOEXEC);
if (fd < 0) return -1;
}
long mapped = syscall(SYS_mmap, addr, len, prot, MAP_PRIVATE | MAP_FIXED, fd, 0);
syscall(SYS_close, fd);
return mapped == (long)addr ? 0 : -1;
}
static int rebake(void *addr, size_t len, int prot) {
size_t page = getauxval(AT_PAGESZ);
if (page) len = (len + page - 1) & ~(page - 1);
if (real_mprotect(addr, len, PROT_READ)) return -1;
if (!rebake_shared(addr, len, prot)) return 0;
void *copy = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (copy == MAP_FAILED) return -1;
memcpy(copy, addr, len);