fix(preload): 32-bit x86 programs get correct large-file stat, open, write and lock results (#2592)

The session preload is also built for i386 and preloaded into every 32-bit
x86 program under FEX. There it aliased fstat64, ftruncate64, pwrite64,
fallocate64, posix_fallocate64, open64, openat64 and fcntl64 to their 32-bit
hooks, which on i386 take a different struct layout or argument size. fstat64
returned a size with garbage high bits, so libdbus rejected a valid 33-byte
/etc/machine-id as too large and aborted the program. The aliases now exist
only where off_t is off64_t; on i386 the 64-bit calls reach their own glibc
functions, keeping the open and lock hooks.
This commit is contained in:
MaxsTechReview authored and GitHub committed 2026-11-15 17:20:59 -05:00
1 parent 9b5d7870a3
commit a20528c62b
4 files changed
+243 -26

No files matched your search

+35 -11
View File
@@ -30,6 +30,10 @@ static int is_lock_command(int cmd) {
switch (cmd) {
case F_SETLK:
case F_SETLKW:
#if defined(F_SETLK64) && F_SETLK64 != F_SETLK
case F_SETLK64:
case F_SETLKW64:
#endif
#ifdef F_OFD_SETLK
case F_OFD_SETLK:
#endif
@@ -45,6 +49,9 @@ static int is_lock_command(int cmd) {
static int is_lock_query(int cmd) {
switch (cmd) {
case F_GETLK:
#if defined(F_GETLK64) && F_GETLK64 != F_GETLK
case F_GETLK64:
#endif
#ifdef F_OFD_GETLK
case F_OFD_GETLK:
#endif
@@ -57,25 +64,18 @@ static int is_lock_query(int cmd) {
int bl_nl_before_dup(int fd, void **state) __attribute__((visibility("hidden")));
void bl_nl_after_dup(void *state, int fd) __attribute__((visibility("hidden")));
int fcntl(int fd, int cmd, ...) {
static int (*real_fcntl)(int, int, ...);
va_list ap;
void *arg;
static int fcntl_with(int (**real_fcntl)(int, int, ...), const char *name, int fd, int cmd, void *arg) {
int result;
va_start(ap, cmd);
arg = va_arg(ap, void *);
va_end(ap);
if (real_fcntl == NULL) real_fcntl = dlsym(RTLD_NEXT, "fcntl");
if (real_fcntl == NULL) {
if (*real_fcntl == NULL) *real_fcntl = dlsym(RTLD_NEXT, name);
if (*real_fcntl == NULL) {
errno = ENOSYS;
return -1;
}
void *nl_state = NULL;
int duplicate = cmd == F_DUPFD || cmd == F_DUPFD_CLOEXEC;
if (duplicate && bl_nl_before_dup(fd, &nl_state) != 0) return -1;
result = real_fcntl(fd, cmd, arg);
result = (*real_fcntl)(fd, cmd, arg);
if (duplicate) bl_nl_after_dup(nl_state, result);
if (result >= 0 || !unsupported(errno)) return result;
if (is_lock_command(cmd)) return 0;
@@ -86,6 +86,30 @@ int fcntl(int fd, int cmd, ...) {
return result;
}
int fcntl(int fd, int cmd, ...) {
static int (*real_fcntl)(int, int, ...);
va_list ap;
void *arg;
va_start(ap, cmd);
arg = va_arg(ap, void *);
va_end(ap);
return fcntl_with(&real_fcntl, "fcntl", fd, cmd, arg);
}
#if defined(__GLIBC__) && defined(F_SETLK64)
#ifdef __OFF_T_MATCHES_OFF64_T
int fcntl64(int fd, int cmd, ...) __attribute__((alias("fcntl")));
#else
int fcntl64(int fd, int cmd, ...) {
static int (*real_fcntl64)(int, int, ...);
va_list ap;
void *arg;
va_start(ap, cmd);
arg = va_arg(ap, void *);
va_end(ap);
return fcntl_with(&real_fcntl64, "fcntl64", fd, cmd, arg);
}
#endif
#endif
+46 -14
View File
@@ -42,20 +42,15 @@ static int is_trace_marker(const char *path) {
|| strcmp(path, "/sys/kernel/debug/tracing/trace_marker") == 0);
}
int open(const char *path, int flags, ...) {
va_list ap;
mode_t mode;
static int open_from(const char *path, int flags, mode_t mode, const void *caller) {
int fd;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
if (is_trace_marker(path)) {
errno = EACCES;
return -1;
}
path = bl_oom_adj_redirect(path, (flags & O_ACCMODE) != O_RDONLY);
if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd;
if ((fd = bl_status_without_tracer(path, flags, caller)) >= 0) return fd;
if ((fd = bl_ntsync_open(path, flags)) != -2) return fd;
if ((fd = bl_venc_open(path, flags)) != -2) return fd;
if ((fd = bl_vdec_open(path, flags)) != -2) return fd;
@@ -70,20 +65,15 @@ int open(const char *path, int flags, ...) {
return fd;
}
int openat(int dirfd, const char *path, int flags, ...) {
va_list ap;
mode_t mode;
static int openat_from(int dirfd, const char *path, int flags, mode_t mode, const void *caller) {
int fd;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
if (is_trace_marker(path)) {
errno = EACCES;
return -1;
}
path = bl_oom_adj_redirect(path, (flags & O_ACCMODE) != O_RDONLY);
if ((fd = bl_status_without_tracer(path, flags, __builtin_return_address(0))) >= 0) return fd;
if ((fd = bl_status_without_tracer(path, flags, caller)) >= 0) return fd;
if ((fd = bl_ntsync_open(path, flags)) != -2) return fd;
if ((fd = bl_venc_open(path, flags)) != -2) return fd;
if ((fd = bl_vdec_open(path, flags)) != -2) return fd;
@@ -98,5 +88,47 @@ int openat(int dirfd, const char *path, int flags, ...) {
return fd;
}
int open(const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return open_from(path, flags, mode, __builtin_return_address(0));
}
int openat(int dirfd, const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return openat_from(dirfd, path, flags, mode, __builtin_return_address(0));
}
#ifdef __OFF_T_MATCHES_OFF64_T
int open64(const char *path, int flags, ...) __attribute__((alias("open")));
int openat64(int dirfd, const char *path, int flags, ...) __attribute__((alias("openat")));
#else
int open64(const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return open_from(path, flags | O_LARGEFILE, mode, __builtin_return_address(0));
}
int openat64(int dirfd, const char *path, int flags, ...) {
va_list ap;
mode_t mode;
va_start(ap, flags);
mode = va_arg(ap, mode_t);
va_end(ap);
return openat_from(dirfd, path, flags | O_LARGEFILE, mode, __builtin_return_address(0));
}
#endif
+2 -1
View File
@@ -356,7 +356,8 @@ static void flush_summary(void) {
if (n > 0 && (size_t)n < sizeof(line)) append_line(line, (size_t)n);
}
#if defined(__USE_LARGEFILE64)
#if defined(__USE_LARGEFILE64) && defined(__OFF_T_MATCHES_OFF64_T)
_Static_assert(sizeof(struct stat) == sizeof(struct stat64), "stat64 alias needs one layout");
int fallocate64(int fd, int mode, off64_t offset, off64_t length) __attribute__((alias("fallocate")));
int posix_fallocate64(int fd, off64_t offset, off64_t length) __attribute__((alias("posix_fallocate")));
ssize_t pwrite64(int fd, const void *buffer, size_t count, off64_t offset) __attribute__((alias("pwrite")));