mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-22 14:26:00 +00:00
Fix runtime tool output isolation, mmap64 chaining and DST ambiguity
This commit is contained in:
1 parent
1c8f78a082
commit
8d9b7084f8
10 files changed
+341
-20
No files matched your search
@@ -13,7 +13,8 @@ address space with `MAP_FIXED_NOREPLACE`. It never truncates a pointer, copies
|
||||
into shadow storage, replaces existing mappings, or disables Wine's guard.
|
||||
Caller-specified/fixed mappings, regular files, private/anonymous memory and
|
||||
other devices retain their original behavior. Failure to find a suitable low
|
||||
hole fails with ENOMEM. Existing preload hooks are called through RTLD_NEXT;
|
||||
hole fails with ENOMEM. The mmap and mmap64 preload chains are independently
|
||||
resolved and called through their respective RTLD_NEXT symbols;
|
||||
only resolver bootstrap uses the raw syscall. A compact atomic cursor avoids
|
||||
wasting large address slots on thousands of small driver control allocations.
|
||||
If the compact probe collides, a bounded address-range walk of
|
||||
@@ -32,7 +33,9 @@ tools/hybris/low32/build.sh /absolute/build-dir /path/to/Vulkan-Headers/include
|
||||
```
|
||||
|
||||
The native library and unit test are always built. GPU probes are optional when
|
||||
headers are provided; the Windows x86 probe also needs i686-w64-mingw32-gcc.
|
||||
headers are provided; `gpu-copy-native` uses the selected native host compiler
|
||||
and has the host's architecture, while the Windows x86 probe also needs
|
||||
i686-w64-mingw32-gcc. Copy a native probe to a device only when their architectures match.
|
||||
The unit test uses /dev/zero and proves CPU data integrity, collision handling,
|
||||
concurrency, 2000 simultaneous small mappings, invalid offsets, forwarding and
|
||||
opt-out. A fragmented-address-space regression reserves the low region, opens
|
||||
@@ -42,6 +45,9 @@ additional density regression fills 64 existing page holes while preserving a
|
||||
separate contiguous 16 MiB interval for a later complete data-readback map; the
|
||||
prior gap-after-coarse implementation fails and the gap-first strategy passes. These
|
||||
CPU tests do not by themselves prove GPU correctness.
|
||||
The build also tests both preload orders with a second library that hooks mmap
|
||||
and mmap64 independently. Opt-out, tracing and adaptation must each preserve
|
||||
the selected symbol's chain, invalid-offset errno and file offsets above 4 GiB.
|
||||
|
||||
`gpu-copy.c` tests actual Vulkan transfer operations on vendor 0x1010. It maps
|
||||
two 32 MiB device allocations, runs four copies of full deterministic patterns,
|
||||
@@ -49,7 +55,7 @@ including offset/unmap/remap, uses flush/invalidate and barriers/fences, and
|
||||
compares every word. It can require each full CPU range to fit below 4 GiB:
|
||||
|
||||
```sh
|
||||
./gpu-copy-arm64 32 --require-low32
|
||||
./gpu-copy-native 32 --require-low32
|
||||
# Through a separate Proton ARM64/WoW64 test prefix:
|
||||
proton run gpu-copy-x86.exe 32 --require-low32 'Z:\path\probe-output.txt'
|
||||
```
|
||||
|
||||
@@ -9,8 +9,17 @@ cc=${CC:-cc}
|
||||
"$base/low32.c" -ldl -o "$output/libdroiddeck-low32.so"
|
||||
"$cc" -std=gnu11 -Wall -Wextra -Werror -O2 "$base/test-low32.c" -pthread -o "$output/test-low32"
|
||||
LD_PRELOAD="$output/libdroiddeck-low32.so" "$output/test-low32"
|
||||
"$cc" -std=gnu11 -Wall -Wextra -Werror -O2 -fPIC -shared \
|
||||
"$base/test-chain-hook.c" -ldl -o "$output/test-chain-hook.so"
|
||||
"$cc" -std=gnu11 -Wall -Wextra -Werror -O2 "$base/test-preload-chain.c" -ldl -o "$output/test-preload-chain"
|
||||
for preloads in "$output/libdroiddeck-low32.so:$output/test-chain-hook.so" \
|
||||
"$output/test-chain-hook.so:$output/libdroiddeck-low32.so"; do
|
||||
for mode in off trace low32; do
|
||||
LD_PRELOAD="$preloads" "$output/test-preload-chain" "$mode"
|
||||
done
|
||||
done
|
||||
if [ "$#" -ge 2 ]; then
|
||||
"$cc" -std=gnu11 -Wall -Wextra -Werror -O2 -s -I"$2" "$base/gpu-copy.c" -ldl -o "$output/gpu-copy-arm64"
|
||||
"$cc" -std=gnu11 -Wall -Wextra -Werror -O2 -s -I"$2" "$base/gpu-copy.c" -ldl -o "$output/gpu-copy-native"
|
||||
if command -v i686-w64-mingw32-gcc >/dev/null 2>&1; then
|
||||
i686-w64-mingw32-gcc -std=gnu11 -Wall -Wextra -Werror -O2 -s -I"$2" \
|
||||
"$base/gpu-copy.c" -o "$output/gpu-copy-x86.exe"
|
||||
|
||||
+35
-12
@@ -25,7 +25,11 @@
|
||||
static _Atomic uint64_t cursor;
|
||||
static _Atomic unsigned logged;
|
||||
typedef void *(*map_function)(void *,size_t,int,int,int,off_t);
|
||||
typedef void *(*map64_function)(void *,size_t,int,int,int,off64_t);
|
||||
static _Atomic(map_function) next_map;
|
||||
static _Atomic(map64_function) next_map64;
|
||||
_Static_assert(sizeof(off_t) == sizeof(off64_t) && sizeof(off_t) == 8,
|
||||
"64-bit host required");
|
||||
static __thread int resolving __attribute__((tls_model("initial-exec")));
|
||||
static __thread int active __attribute__((tls_model("initial-exec")));
|
||||
static void *native_map(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
@@ -42,6 +46,19 @@ static void *native_map(void *addr, size_t length, int prot, int flags, int fd,
|
||||
return (void *)syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
|
||||
}
|
||||
|
||||
static void *native_map64(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
{
|
||||
map64_function next=atomic_load(&next_map64);
|
||||
if (!next && !resolving) {
|
||||
resolving=1;
|
||||
next=(map64_function)dlsym(RTLD_NEXT,"mmap64");
|
||||
atomic_store(&next_map64,next);
|
||||
resolving=0;
|
||||
}
|
||||
if (next) return next(addr,length,prot,flags,fd,(off64_t)offset);
|
||||
return (void *)syscall(SYS_mmap, addr, length, prot, flags, fd, offset);
|
||||
}
|
||||
|
||||
static void trace_map(const char *kind, size_t n, int prot, int flags, int fd,
|
||||
off_t off, const struct stat *st, void *p, int error)
|
||||
{
|
||||
@@ -77,12 +94,13 @@ static int target_device(int fd, struct stat *st)
|
||||
return valid;
|
||||
}
|
||||
|
||||
static void *adapted_map(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
static void *adapted_map(map_function forward, void *addr, size_t length, int prot,
|
||||
int flags, int fd, off_t offset)
|
||||
{
|
||||
const char *mode = getenv("DROIDDECK_LOW32_MODE");
|
||||
struct stat st = {0};
|
||||
if (mode && !strcmp(mode, "trace")) {
|
||||
void *p = native_map(addr, length, prot, flags, fd, offset);
|
||||
void *p = forward(addr, length, prot, flags, fd, offset);
|
||||
if (fd >= 0 && (prot & PROT_WRITE) && !(prot & PROT_EXEC) &&
|
||||
(flags & MAP_TYPE) == MAP_SHARED) {
|
||||
int saved=errno;
|
||||
@@ -97,7 +115,7 @@ static void *adapted_map(void *addr, size_t length, int prot, int flags, int fd,
|
||||
(flags & (MAP_ANONYMOUS | MAP_FIXED | MAP_FIXED_NOREPLACE)) ||
|
||||
(prot & PROT_EXEC) || (flags & MAP_TYPE) != MAP_SHARED ||
|
||||
!target_device(fd, &st))
|
||||
return native_map(addr, length, prot, flags, fd, offset);
|
||||
return forward(addr, length, prot, flags, fd, offset);
|
||||
|
||||
const uintptr_t lower = 0x10000000u, upper = 0xffff0000u;
|
||||
const uintptr_t step = 0x00400000u;
|
||||
@@ -119,7 +137,7 @@ static void *adapted_map(void *addr, size_t length, int prot, int flags, int fd,
|
||||
/* Compact first: hundreds of small driver mappings must not waste a
|
||||
* 4MiB address slot each. Coarse search is only for existing Wine holes. */
|
||||
uintptr_t hint = lower + (i==0?compact:(uintptr_t)((start+i-1)%slots)*step);
|
||||
void *p = native_map((void *)hint, length, prot,
|
||||
void *p = forward((void *)hint, length, prot,
|
||||
flags|MAP_FIXED_NOREPLACE, fd, offset);
|
||||
if (p != MAP_FAILED) {
|
||||
uintptr_t first = (uintptr_t)p;
|
||||
@@ -153,7 +171,7 @@ static void *adapted_map(void *addr, size_t length, int prot, int flags, int fd,
|
||||
if (last <= gap_start) continue;
|
||||
uintptr_t gap_end = first < upper ? first : upper;
|
||||
if (gap_end > gap_start && rounded <= gap_end - gap_start) {
|
||||
void *p = native_map((void *)gap_start, length, prot,
|
||||
void *p = forward((void *)gap_start, length, prot,
|
||||
flags | MAP_FIXED_NOREPLACE, fd, offset);
|
||||
if (p != MAP_FAILED) {
|
||||
uintptr_t address = (uintptr_t)p;
|
||||
@@ -176,7 +194,7 @@ static void *adapted_map(void *addr, size_t length, int prot, int flags, int fd,
|
||||
if (gap_start >= upper) break;
|
||||
}
|
||||
if (gap_start < upper && rounded <= upper - gap_start) {
|
||||
void *p = native_map((void *)gap_start, length, prot,
|
||||
void *p = forward((void *)gap_start, length, prot,
|
||||
flags | MAP_FIXED_NOREPLACE, fd, offset);
|
||||
if (p != MAP_FAILED) {
|
||||
uintptr_t address = (uintptr_t)p;
|
||||
@@ -194,7 +212,7 @@ static void *adapted_map(void *addr, size_t length, int prot, int flags, int fd,
|
||||
for (unsigned i = 1; i <= slots; i++) {
|
||||
/* Last resort if address walking was unavailable or raced. */
|
||||
uintptr_t hint = lower + (i==0?compact:(uintptr_t)((start+i-1)%slots)*step);
|
||||
void *p = native_map((void *)hint, length, prot,
|
||||
void *p = forward((void *)hint, length, prot,
|
||||
flags|MAP_FIXED_NOREPLACE, fd, offset);
|
||||
if (p != MAP_FAILED) {
|
||||
uintptr_t first = (uintptr_t)p;
|
||||
@@ -217,17 +235,22 @@ static void *adapted_map(void *addr, size_t length, int prot, int flags, int fd,
|
||||
return MAP_FAILED;
|
||||
}
|
||||
|
||||
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
static void *intercept_map(map_function forward, void *addr, size_t length,
|
||||
int prot, int flags, int fd, off_t offset)
|
||||
{
|
||||
if (active) return native_map(addr,length,prot,flags,fd,offset);
|
||||
if (active) return forward(addr,length,prot,flags,fd,offset);
|
||||
active=1;
|
||||
void *p=adapted_map(addr,length,prot,flags,fd,offset);
|
||||
void *p=adapted_map(forward,addr,length,prot,flags,fd,offset);
|
||||
active=0;
|
||||
return p;
|
||||
}
|
||||
|
||||
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset)
|
||||
{
|
||||
return intercept_map(native_map,addr,length,prot,flags,fd,offset);
|
||||
}
|
||||
|
||||
void *mmap64(void *addr, size_t length, int prot, int flags, int fd, off64_t offset)
|
||||
{
|
||||
_Static_assert(sizeof(off_t) == sizeof(off64_t), "64-bit host required");
|
||||
return mmap(addr, length, prot, flags, fd, (off_t)offset);
|
||||
return intercept_map(native_map64,addr,length,prot,flags,fd,(off_t)offset);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
/* Controlled second preload: distinct mmap/mmap64 counters and 64-bit offsets. */
|
||||
#define _GNU_SOURCE
|
||||
#define _LARGEFILE64_SOURCE
|
||||
#include <dlfcn.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
static int selected_fd = -2;
|
||||
static unsigned calls[2];
|
||||
static int64_t offsets[2];
|
||||
|
||||
void chain_clear(int fd)
|
||||
{
|
||||
selected_fd = fd;
|
||||
calls[0] = calls[1] = 0;
|
||||
offsets[0] = offsets[1] = -1;
|
||||
}
|
||||
|
||||
unsigned chain_calls(unsigned channel) { return calls[channel]; }
|
||||
int64_t chain_offset(unsigned channel) { return offsets[channel]; }
|
||||
|
||||
void *mmap(void *addr, size_t len, int prot, int flags, int fd, off_t off)
|
||||
{
|
||||
static void *(*next)(void *, size_t, int, int, int, off_t);
|
||||
if (!next) next = dlsym(RTLD_NEXT, "mmap");
|
||||
if (fd == selected_fd) { ++calls[0]; offsets[0] = off; }
|
||||
return next(addr, len, prot, flags, fd, off);
|
||||
}
|
||||
|
||||
void *mmap64(void *addr, size_t len, int prot, int flags, int fd, off64_t off)
|
||||
{
|
||||
static void *(*next)(void *, size_t, int, int, int, off64_t);
|
||||
if (!next) next = dlsym(RTLD_NEXT, "mmap64");
|
||||
if (fd == selected_fd) { ++calls[1]; offsets[1] = off; }
|
||||
return next(addr, len, prot, flags, fd, off);
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
#define _GNU_SOURCE
|
||||
#define _LARGEFILE64_SOURCE
|
||||
#include <assert.h>
|
||||
#include <dlfcn.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
assert(argc == 2);
|
||||
void (*clear)(int) = dlsym(RTLD_DEFAULT, "chain_clear");
|
||||
unsigned (*calls)(unsigned) = dlsym(RTLD_DEFAULT, "chain_calls");
|
||||
int64_t (*offset)(unsigned) = dlsym(RTLD_DEFAULT, "chain_offset");
|
||||
assert(clear && calls && offset);
|
||||
if (strcmp(argv[1], "off")) assert(!setenv("DROIDDECK_LOW32_MODE", argv[1], 1));
|
||||
else assert(!unsetenv("DROIDDECK_LOW32_MODE"));
|
||||
int fd = open("/dev/zero", O_RDWR | O_CLOEXEC);
|
||||
assert(fd >= 0);
|
||||
struct stat st;
|
||||
assert(!fstat(fd, &st));
|
||||
char rdev[48];
|
||||
snprintf(rdev, sizeof(rdev), "%ju", (uintmax_t)st.st_rdev);
|
||||
assert(!setenv("DROIDDECK_LOW32_RDEV", rdev, 1));
|
||||
for (unsigned channel = 0; channel < 2; ++channel) {
|
||||
clear(fd);
|
||||
void *p = channel ? mmap64(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)
|
||||
: mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
assert(p != MAP_FAILED);
|
||||
assert(calls(channel) == 1 && calls(1 - channel) == 0 && offset(channel) == 0);
|
||||
if (!strcmp(argv[1], "low32")) assert((uintptr_t)p + 4096 <= UINT64_C(0x100000000));
|
||||
*(unsigned *)p = 0xcafef00d;
|
||||
assert(*(unsigned *)p == 0xcafef00d && !munmap(p, 4096));
|
||||
clear(fd);
|
||||
errno = 0;
|
||||
p = channel ? mmap64(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 1)
|
||||
: mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 1);
|
||||
assert(p == MAP_FAILED && errno == EINVAL);
|
||||
assert(calls(channel) == 1 && calls(1 - channel) == 0 && offset(channel) == 1);
|
||||
}
|
||||
close(fd);
|
||||
/* Regular-file forwarding must preserve an mmap64 offset above 4 GiB. */
|
||||
fd = memfd_create("preload-chain-test", 0);
|
||||
const off64_t large_offset = INT64_C(0x200001000);
|
||||
assert(fd >= 0 && !ftruncate64(fd, large_offset + 4096));
|
||||
unsigned value = 0x1234abcd;
|
||||
assert(pwrite64(fd, &value, sizeof(value), large_offset) == (ssize_t)sizeof(value));
|
||||
clear(fd);
|
||||
unsigned *p = mmap64(NULL, 4096, PROT_READ, MAP_SHARED, fd, large_offset);
|
||||
assert(p != MAP_FAILED && *p == value);
|
||||
assert(calls(1) == 1 && calls(0) == 0 && offset(1) == large_offset);
|
||||
assert(!munmap(p, 4096));
|
||||
close(fd);
|
||||
printf("PASS separate mmap/mmap64 chains (%s), EINVAL and >4GiB offset readback\n", argv[1]);
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user