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")));
+160
View File
@@ -0,0 +1,160 @@
import os
from pathlib import Path
import shutil
import subprocess
import tempfile
import unittest
PRELOAD = Path(__file__).resolve().parents[1] / "linuxfs/preload"
GIB = 1 << 30
PROGRAM = r"""
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
static void report(const char *call, int fd, int result) {
struct stat st;
if (fstat(fd, &st) != 0) { printf("%s fstat-failed\n", call); return; }
printf("%s %d %lld\n", call, result, (long long)st.st_size);
}
int main(int argc, char **argv) {
const char *dir = argv[1];
long long far = atoll(argv[2]);
char path[4096];
snprintf(path, sizeof(path), "%s/machine-id", dir);
int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
if (fd < 0 || write(fd, "0123456789abcdef0123456789abcdef\n", 33) != 33) return 2;
report("fstat", fd, 0);
report("ftruncate", fd, ftruncate(fd, far));
report("pwrite", fd, pwrite(fd, "x", 1, far) == 1 ? 0 : errno);
close(fd);
snprintf(path, sizeof(path), "%s/reserved", dir);
fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644);
int result = posix_fallocate(fd, 0, 4096);
report("posix_fallocate", fd, result == EOPNOTSUPP ? 0 : result);
result = fallocate(fd, 0, 0, 8192);
report("fallocate", fd, result == 0 || errno == EOPNOTSUPP ? 0 : errno);
int other = open(path, O_RDWR);
struct flock lock = {.l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = far, .l_len = 1};
result = fcntl(fd, F_OFD_SETLK, &lock);
struct flock query = {.l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = far, .l_len = 1};
int queried = fcntl(other, F_OFD_GETLK, &query);
printf("lock %d %d %d %lld\n", result, queried, query.l_type == F_WRLCK, (long long)query.l_start);
close(other);
close(fd);
return 0;
}
"""
LOCKS = r"""
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv) {
int fd = open(argv[1], O_RDWR | O_CREAT, 0644);
struct flock lock = {.l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 0, .l_len = 1};
int set = fcntl(fd, F_SETLK, &lock);
struct flock query = {.l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 0, .l_len = 1};
int get = fcntl(fd, F_GETLK, &query);
printf("%d %d %s\n", set, get, query.l_type == F_UNLCK ? "unlocked" : "locked");
return 0;
}
"""
NO_LOCKS = r"""
#include <errno.h>
int fcntl(int fd, int cmd, void *arg) { (void)fd; (void)cmd; (void)arg; errno = ENOSYS; return -1; }
int fcntl64(int fd, int cmd, void *arg) { (void)fd; (void)cmd; (void)arg; errno = ENOSYS; return -1; }
"""
def compiles(bits, workdir):
source = Path(workdir) / ("probe%s.c" % bits)
source.write_text("int main(void) { return 0; }\n")
out = Path(workdir) / ("probe%s" % bits)
if subprocess.run(["cc", bits, "-o", str(out), str(source)], capture_output=True).returncode != 0:
return False
return subprocess.run([str(out)], capture_output=True).returncode == 0
@unittest.skipUnless(shutil.which("cc"), "needs a C compiler")
class LargeFilePreloadTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.dir = Path(tempfile.mkdtemp())
(cls.dir / "program.c").write_text(PROGRAM)
(cls.dir / "locks.c").write_text(LOCKS)
(cls.dir / "nolocks.c").write_text(NO_LOCKS)
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.dir, True)
def build(self, bits):
if not compiles(bits, self.dir):
self.skipTest("no %s toolchain" % bits)
out = self.dir / bits.lstrip("-")
out.mkdir(exist_ok=True)
lib, program = out / "libblsession.so", out / "program"
if not lib.exists():
sources = sorted(str(p) for p in PRELOAD.glob("*.c"))
subprocess.run(["cc", bits, "-shared", "-fPIC", "-O2", "-pthread", "-o", str(lib)] + sources + ["-ldl"], check=True)
subprocess.run(["cc", bits, "-O2", "-D_FILE_OFFSET_BITS=64", "-o", str(program), str(self.dir / "program.c")], check=True)
subprocess.run(["cc", bits, "-O2", "-D_FILE_OFFSET_BITS=64", "-o", str(out / "locks"), str(self.dir / "locks.c")], check=True)
subprocess.run(["cc", bits, "-shared", "-fPIC", "-O2", "-o", str(out / "nolocks.so"), str(self.dir / "nolocks.c")], check=True)
return lib, program
def run_program(self, bits, preload):
lib, program = self.build(bits)
work = Path(tempfile.mkdtemp(dir=self.dir))
env = {k: v for k, v in os.environ.items() if not k.startswith("BL_")}
env.pop("LD_PRELOAD", None)
if preload:
env["LD_PRELOAD"] = str(lib)
result = subprocess.run([str(program), str(work), str(5 * GIB)], env=env, capture_output=True, text=True, timeout=60, check=True)
return result.stdout.splitlines()
def expected(self):
return [
"fstat 0 33",
"ftruncate 0 %d" % (5 * GIB),
"pwrite 0 %d" % (5 * GIB + 1),
"posix_fallocate 0 4096",
"fallocate 0 8192",
"lock 0 0 1 %d" % (5 * GIB),
]
def test_32_bit_large_file_calls_see_the_real_size_and_offsets(self):
self.assertEqual(self.expected(), self.run_program("-m32", preload=True))
def test_32_bit_programs_behave_as_without_the_preload(self):
self.assertEqual(self.run_program("-m32", preload=False), self.run_program("-m32", preload=True))
def locks_without_filesystem_support(self, bits):
lib, program = self.build(bits)
env = {k: v for k, v in os.environ.items() if not k.startswith("BL_")}
env["LD_PRELOAD"] = "%s:%s" % (lib, program.parent / "nolocks.so")
work = Path(tempfile.mkdtemp(dir=self.dir))
result = subprocess.run([str(program.parent / "locks"), str(work / "file")], env=env, capture_output=True, text=True, timeout=60, check=True)
return result.stdout.strip()
def test_locks_on_filesystems_without_lock_support_still_succeed(self):
self.assertEqual("0 0 unlocked", self.locks_without_filesystem_support("-m64"))
self.assertEqual("0 0 unlocked", self.locks_without_filesystem_support("-m32"))
def test_64_bit_programs_are_unchanged(self):
self.assertEqual(self.expected(), self.run_program("-m64", preload=True))
self.assertEqual(self.run_program("-m64", preload=False), self.run_program("-m64", preload=True))
if __name__ == "__main__":
unittest.main()