From a6fe10d8ffb289d1ee86928bf2bfc9cb495beb68 Mon Sep 17 00:00:00 2001 From: MaxsTechReview Date: Wed, 25 Nov 2026 04:24:58 -0500 Subject: [PATCH] fix(input): resolve fake input's real functions before any loader lock is held (#3193) * Resolve fake input's real functions before any loader lock is held Each hooked call looked up its real function through a function-local static, and the x86 dlsym hook opened libc the same way. A thread that entered a hook first held the static's guard while dlsym waited for the loader lock, and a thread inside dlopen held the loader lock while its constructor called the same hook and waited for the guard. Steam's update window hung that way under FEX, so x86 Steam stopped at Installing update. The real functions now live in lock-free slots filled by the library's constructor, and the dlsym hook resolves dlvsym and libc there too. * Use an atomic handshake in the loader lock test --- app/src/main/cpp/fakeinput_steam.cpp | 173 +++++++++++++++------- tools/tests/test_fakeinput_loader_lock.py | 80 ++++++++++ 2 files changed, 203 insertions(+), 50 deletions(-) create mode 100644 tools/tests/test_fakeinput_loader_lock.py diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index 904a7f6ed0..e6e4a2b6ac 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -58,6 +58,59 @@ #define EXPORT extern "C" __attribute__((visibility("default"))) +struct SymbolSlot { + std::atomic fn{nullptr}; + std::atomic resolved{false}; +}; + +template static Fn resolved_symbol(SymbolSlot &slot, void *handle, const char *name) { + if (!slot.resolved.load(std::memory_order_acquire)) { + slot.fn.store(dlsym(handle, name), std::memory_order_relaxed); + slot.resolved.store(true, std::memory_order_release); + } + return reinterpret_cast(slot.fn.load(std::memory_order_relaxed)); +} + +enum class Next : size_t { + open, + openat, + close, + write, + ioctl, + poll, + ppoll, + select, + stat, + stat64, + fstat, + fstat64, + xstat, + xstat64, + fxstat, + fxstat64, + statx, + statfs, + fstatfs, + access, + faccessat, + opendir, + scandir, + inotify_add_watch, + count +}; + +static const char *const next_names[] = {"open", "openat", "close", "write", "ioctl", "poll", "ppoll", "select", "stat", "stat64", "fstat", "fstat64", "__xstat", "__xstat64", "__fxstat", "__fxstat64", "statx", "statfs", "fstatfs", "access", "faccessat", "opendir", "scandir", "inotify_add_watch"}; +static_assert(sizeof(next_names) / sizeof(next_names[0]) == static_cast(Next::count), "next_names"); +static SymbolSlot next_slots[static_cast(Next::count)]; + +template static Fn next_symbol(Next which) { + return resolved_symbol(next_slots[static_cast(which)], RTLD_NEXT, next_names[static_cast(which)]); +} + +static void resolve_next_symbols() { + for (size_t i = 0; i < static_cast(Next::count); i++) next_symbol(static_cast(i)); +} + #ifdef __GLIBC__ typedef unsigned long ioctl_request_t; #else @@ -442,6 +495,7 @@ void log(const char *message, ...) { } __attribute__((constructor)) static void library_init() { + resolve_next_symbols(); config(); controller_mutex(); controller_map(); @@ -733,7 +787,7 @@ open_fake_input_ring(const char *event, int flags) { return -1; } - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); int fd = my_open(ring_path.c_str(), O_RDWR | (flags & (O_NONBLOCK | O_CLOEXEC))); if (fd < 0) @@ -1000,7 +1054,7 @@ __attribute__((visibility("hidden"))) static void unpublish_uinput(UinputWriter } __attribute__((visibility("hidden"))) static int claim_uinput_node(int *fd_out) { - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); for (int node = UINPUT_EVENT_BASE; node < UINPUT_EVENT_LIMIT; node++) { std::string path = uinput_ring_path(node); int fd = my_open(path.c_str(), O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0644); @@ -1027,7 +1081,7 @@ __attribute__((visibility("hidden"))) static int claim_uinput_node(int *fd_out) } __attribute__((visibility("hidden"))) static int publish_uinput(UinputWriter &w) { - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); int fd = -1; int node = claim_uinput_node(&fd); if (node < 0) return -1; @@ -1440,7 +1494,7 @@ __attribute__((visibility("hidden"))) static const uint8_t *ring_blocks() { std::lock_guard guard(lock); if ((mapped = block.load(std::memory_order_acquire))) return mapped; tries.fetch_add(1, std::memory_order_relaxed); - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); std::string path = get_ring_path_for_slot(0); int fd = path.empty() ? -1 : my_open(path.c_str(), O_RDONLY | O_CLOEXEC); if (fd < 0) return nullptr; @@ -1793,7 +1847,7 @@ __attribute__((visibility("hidden"))) static void *sony_report_thread(void *arg) __attribute__((visibility("hidden"))) static int open_deck_hidraw(int flags, uint8_t sony = 0) { const char *path = sony == 4 ? DS4_HIDRAW_PATH : sony == 5 ? DUALSENSE_HIDRAW_PATH : DECK_HIDRAW_PATH; - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); if (!process_reads_pads()) { Logger::log("deck: %s refused: not the Steam client\n", path); errno = ENOENT; @@ -2002,7 +2056,7 @@ __attribute__((visibility("hidden"))) static void *triton_pad_thread(void *arg) } __attribute__((visibility("hidden"))) static int open_triton_hidraw(int flags, bool emulated) { - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); if (!process_reads_pads()) { errno = ENOENT; return -1; @@ -2121,7 +2175,7 @@ __attribute__((visibility("hidden"))) static TouchRingFile *touch_ring() { std::call_once(once, [] { const char *path = getenv("FAKE_TOUCHCTL_RING"); if (!path || !*path || !fake_deck_enabled()) return; - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); int fd = my_open(path, O_RDWR | O_CLOEXEC); if (fd < 0) { Logger::log("touch: %s not openable (%s)\n", path, strerror(errno)); @@ -2338,7 +2392,8 @@ __attribute__((visibility("hidden"))) static int inject_uevent(const char *actio const char *subsystem, const char *devname, unsigned major, unsigned minor) { using inject_fn = int (*)(const char *, const char *, const char *, const char *, unsigned, unsigned); - static auto inject = reinterpret_cast(dlsym(RTLD_DEFAULT, "dd_udevmon_inject")); + static SymbolSlot inject_slot; + const auto inject = resolved_symbol(inject_slot, RTLD_DEFAULT, "dd_udevmon_inject"); return inject ? inject(action, devpath, subsystem, devname, major, minor) : -1; } @@ -2496,7 +2551,7 @@ __attribute__((visibility("hidden"))) static void note_deck_sysfs(const char *ho } EXPORT int fstatfs(int fd, struct statfs *buf) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "fstatfs")); + const auto real = next_symbol(Next::fstatfs); int result = real(fd, buf); if (fake_deck_enabled() && is_deck_sysfs_fd(fd)) { note_deck_sysfs("fstatfs", result); @@ -2506,7 +2561,7 @@ EXPORT int fstatfs(int fd, struct statfs *buf) { } EXPORT int statfs(const char *path, struct statfs *buf) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "statfs")); + const auto real = next_symbol(Next::statfs); int result = real(path, buf); if (fake_deck_enabled() && is_deck_sysfs_path(path)) { note_deck_sysfs(path, result); @@ -2555,7 +2610,7 @@ __attribute__((visibility("hidden"))) static int deck_imu_mode_change(const uint } __attribute__((visibility("hidden"))) static void publish_imu_mode(const DeckHidraw &deck, int mode) { - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); if (deck.mapping_size < FAKE_INPUT_RING_SIZE + DECK_IMU_BLOCK_SIZE) return; std::string path = get_ring_path_for_slot(0); int fd = path.empty() ? -1 : my_open(path.c_str(), O_WRONLY | O_CLOEXEC); @@ -3064,7 +3119,7 @@ static int real_fstat(int fd, struct stat *buf); __attribute__((visibility("hidden"))) static bool deck_mount_id_from_fdinfo( int dirfd, const char *path, int flags, struct statx *buf) { - static auto real_openat = reinterpret_cast(dlsym(RTLD_NEXT, "openat")); + const auto real_openat = next_symbol(Next::openat); int target; if (!path || !*path) { if (!(flags & AT_EMPTY_PATH)) return false; @@ -3102,7 +3157,7 @@ __attribute__((visibility("hidden"))) static bool deck_mount_id_from_fdinfo( } EXPORT int statx(int dirfd, const char *path, int flags, unsigned mask, struct statx *buf) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "statx")); + const auto real = next_symbol(Next::statx); int result = real(dirfd, path, flags, mask, buf); int saved_errno = errno; constexpr unsigned unique_mount_id = 0x4000; @@ -3123,7 +3178,7 @@ EXPORT int statx(int dirfd, const char *path, int flags, unsigned mask, struct s #endif EXPORT DIR *opendir(const char *name) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "opendir")); + const auto real = next_symbol(Next::opendir); DIR *dir = real(name); if (!dir) { note_sys_failure(AT_FDCWD, name); @@ -3153,7 +3208,7 @@ EXPORT int open(const char *pathname, int flags, ...) { va_end(va); - static auto my_open = reinterpret_cast(dlsym(RTLD_NEXT, "open")); + const auto my_open = next_symbol(Next::open); if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags); if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(flags); @@ -3231,7 +3286,7 @@ EXPORT int openat(int dirfd, const char *pathname, int flags, ...) { va_end(va); - static auto my_openat = reinterpret_cast(dlsym(RTLD_NEXT, "openat")); + const auto my_openat = next_symbol(Next::openat); if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags); if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(flags); @@ -3390,10 +3445,10 @@ static int fake_stat_fd(int fd, S *buf, Real real) { } static int real_stat(const char *pathname, struct stat *statbuf) { - static auto fn = reinterpret_cast(dlsym(RTLD_NEXT, "stat")); + const auto fn = next_symbol(Next::stat); if (fn) return fn(pathname, statbuf); #ifdef _STAT_VER - static auto xfn = reinterpret_cast(dlsym(RTLD_NEXT, "__xstat")); + const auto xfn = next_symbol(Next::xstat); if (xfn) return xfn(_STAT_VER, pathname, statbuf); #endif errno = ENOSYS; @@ -3401,10 +3456,10 @@ static int real_stat(const char *pathname, struct stat *statbuf) { } static int real_fstat(int fd, struct stat *buf) { - static auto fn = reinterpret_cast(dlsym(RTLD_NEXT, "fstat")); + const auto fn = next_symbol(Next::fstat); if (fn) return fn(fd, buf); #ifdef _STAT_VER - static auto xfn = reinterpret_cast(dlsym(RTLD_NEXT, "__fxstat")); + const auto xfn = next_symbol(Next::fxstat); if (xfn) return xfn(_STAT_VER, fd, buf); #endif errno = ENOSYS; @@ -3420,7 +3475,7 @@ EXPORT int fstat(int fd, struct stat *buf) { } EXPORT int access(const char *pathname, int mode) { - static auto my_access = reinterpret_cast(dlsym(RTLD_NEXT, "access")); + const auto my_access = next_symbol(Next::access); if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0; if (is_deck_hidraw_path(pathname) && process_reads_pads()) return 0; if ((is_triton_hidraw_path(pathname) || is_triton_pad_path(pathname) || sony_pad_path(pathname)) && @@ -3461,7 +3516,7 @@ EXPORT int access(const char *pathname, int mode) { } EXPORT int faccessat(int dirfd, const char *pathname, int mode, int flags) { - static auto my_faccessat = reinterpret_cast(dlsym(RTLD_NEXT, "faccessat")); + const auto my_faccessat = next_symbol(Next::faccessat); if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0; if (is_deck_hidraw_path(pathname) && process_reads_pads()) return 0; if ((is_triton_hidraw_path(pathname) || is_triton_pad_path(pathname) || sony_pad_path(pathname)) && @@ -3505,7 +3560,7 @@ EXPORT int scandir(const char *dirp, struct dirent ***namelist, int (*filter)(const struct dirent *), int (*compar)(const struct dirent **, const struct dirent **)) { - static auto my_scandir = reinterpret_cast(dlsym(RTLD_NEXT, "scandir")); + const auto my_scandir = next_symbol(Next::scandir); if (dirp) { if (!strcmp(dirp, "/dev/input")) { @@ -3519,7 +3574,7 @@ EXPORT int scandir(const char *dirp, struct dirent ***namelist, } EXPORT int inotify_add_watch(int fd, const char *pathname, uint32_t mask) { - static auto my_inotify_add_watch = reinterpret_cast(dlsym(RTLD_NEXT, "inotify_add_watch")); + const auto my_inotify_add_watch = next_symbol(Next::inotify_add_watch); char *fake_path = nullptr; if (pathname) { @@ -3658,7 +3713,7 @@ static bool wait_snapshot(std::shared_ptr fake, static int forward_ioctl(int fd, ioctl_request_t op, void *argp) { using ioctl_fn = int (*)(int, ioctl_request_t, ...); - static const auto next = reinterpret_cast(dlsym(RTLD_NEXT, "ioctl")); + const auto next = next_symbol(Next::ioctl); return next ? next(fd, op, argp) : static_cast(syscall(SYS_ioctl, fd, op, argp)); } @@ -3897,7 +3952,7 @@ EXPORT int ioctl(int fd, ioctl_request_t op, ...) { } EXPORT int close(int fd) { - static auto my_close = reinterpret_cast(dlsym(RTLD_NEXT, "close")); + const auto my_close = next_symbol(Next::close); if (!watched(fd)) return my_close(fd); close_uinput(fd); @@ -4085,7 +4140,7 @@ EXPORT ssize_t readv(int fd, const struct iovec *iov, int iovcnt) { } EXPORT ssize_t write(int fd, const void *buf, size_t count) { - static auto my_write = reinterpret_cast(dlsym(RTLD_NEXT, "write")); + const auto my_write = next_symbol(Next::write); if (!watched(fd)) return my_write(fd, buf, count); std::unique_lock guard(controller_mutex()); @@ -4160,8 +4215,8 @@ EXPORT ssize_t writev(int fd, const struct iovec *iov, int iovcnt) { static int poll_fake(struct pollfd *fds, nfds_t nfds, int timeout, const sigset_t *sigmask) { - static auto my_poll = reinterpret_cast(dlsym(RTLD_NEXT, "poll")); - static auto my_ppoll = reinterpret_cast(dlsym(RTLD_NEXT, "ppoll")); + const auto my_poll = next_symbol(Next::poll); + const auto my_ppoll = next_symbol(Next::ppoll); nfds_t first = 0; while (first < nfds && !watched(fds[first].fd)) first++; @@ -4265,7 +4320,7 @@ EXPORT int poll(struct pollfd *fds, nfds_t nfds, int timeout) { EXPORT int ppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *timeout, const sigset_t *sigmask) { - static auto my_ppoll = reinterpret_cast(dlsym(RTLD_NEXT, "ppoll")); + const auto my_ppoll = next_symbol(Next::ppoll); if (timeout && (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000L)) { errno = EINVAL; return -1; @@ -4292,7 +4347,7 @@ EXPORT int ppoll(struct pollfd *fds, nfds_t nfds, EXPORT int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout) { - static auto my_select = reinterpret_cast(dlsym(RTLD_NEXT, "select")); + const auto my_select = next_symbol(Next::select); fd_set original_readfds; fd_set original_writefds; @@ -4454,10 +4509,10 @@ EXPORT int openat64(int dirfd, const char *pathname, int flags, ...) { } static int real_stat64(const char *pathname, struct stat64 *statbuf) { - static auto fn = reinterpret_cast(dlsym(RTLD_NEXT, "stat64")); + const auto fn = next_symbol(Next::stat64); if (fn) return fn(pathname, statbuf); #ifdef _STAT_VER - static auto xfn = reinterpret_cast(dlsym(RTLD_NEXT, "__xstat64")); + const auto xfn = next_symbol(Next::xstat64); if (xfn) return xfn(_STAT_VER, pathname, statbuf); #endif errno = ENOSYS; @@ -4465,10 +4520,10 @@ static int real_stat64(const char *pathname, struct stat64 *statbuf) { } static int real_fstat64(int fd, struct stat64 *buf) { - static auto fn = reinterpret_cast(dlsym(RTLD_NEXT, "fstat64")); + const auto fn = next_symbol(Next::fstat64); if (fn) return fn(fd, buf); #ifdef _STAT_VER - static auto xfn = reinterpret_cast(dlsym(RTLD_NEXT, "__fxstat64")); + const auto xfn = next_symbol(Next::fxstat64); if (xfn) return xfn(_STAT_VER, fd, buf); #endif errno = ENOSYS; @@ -4484,27 +4539,27 @@ EXPORT int fstat64(int fd, struct stat64 *buf) { } EXPORT int __xstat(int version, const char *pathname, struct stat *statbuf) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "__xstat")); + const auto real = next_symbol(Next::xstat); if (!real) return stat(pathname, statbuf); - return fake_stat_path(pathname, statbuf, [version](const char *path, struct stat *buf) { return real(version, path, buf); }); + return fake_stat_path(pathname, statbuf, [version, real](const char *path, struct stat *buf) { return real(version, path, buf); }); } EXPORT int __xstat64(int version, const char *pathname, struct stat64 *statbuf) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "__xstat64")); + const auto real = next_symbol(Next::xstat64); if (!real) return stat64(pathname, statbuf); - return fake_stat_path(pathname, statbuf, [version](const char *path, struct stat64 *buf) { return real(version, path, buf); }); + return fake_stat_path(pathname, statbuf, [version, real](const char *path, struct stat64 *buf) { return real(version, path, buf); }); } EXPORT int __fxstat(int version, int fd, struct stat *buf) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "__fxstat")); + const auto real = next_symbol(Next::fxstat); if (!real) return fstat(fd, buf); - return fake_stat_fd(fd, buf, [version](int f, struct stat *b) { return real(version, f, b); }); + return fake_stat_fd(fd, buf, [version, real](int f, struct stat *b) { return real(version, f, b); }); } EXPORT int __fxstat64(int version, int fd, struct stat64 *buf) { - static auto real = reinterpret_cast(dlsym(RTLD_NEXT, "__fxstat64")); + const auto real = next_symbol(Next::fxstat64); if (!real) return fstat64(fd, buf); - return fake_stat_fd(fd, buf, [version](int f, struct stat64 *b) { return real(version, f, b); }); + return fake_stat_fd(fd, buf, [version, real](int f, struct stat64 *b) { return real(version, f, b); }); } #if defined(__x86_64__) || defined(__i386__) @@ -4518,18 +4573,37 @@ static const char *const interposed_names[] = { }; static dlsym_fn next_dlsym() { - static const dlsym_fn fn = [] { + static std::atomic fn{nullptr}; + static std::atomic resolved{false}; + if (!resolved.load(std::memory_order_acquire)) { #ifdef __x86_64__ const char *base = "GLIBC_2.2.5"; #else const char *base = "GLIBC_2.0"; #endif + dlsym_fn found{}; for (const char *version : {base, "GLIBC_2.34"}) { - if (auto found = reinterpret_cast(dlvsym(RTLD_NEXT, "dlsym", version))) return found; + if ((found = reinterpret_cast(dlvsym(RTLD_NEXT, "dlsym", version)))) break; } - return dlsym_fn{}; - }(); - return fn; + fn.store(found, std::memory_order_relaxed); + resolved.store(true, std::memory_order_release); + } + return fn.load(std::memory_order_relaxed); +} + +static void *libc_handle() { + static std::atomic handle{nullptr}; + static std::atomic resolved{false}; + if (!resolved.load(std::memory_order_acquire)) { + handle.store(dlopen(LIBC_SO, RTLD_LAZY | RTLD_NOLOAD), std::memory_order_relaxed); + resolved.store(true, std::memory_order_release); + } + return handle.load(std::memory_order_relaxed); +} + +__attribute__((constructor)) static void resolve_dlsym_targets() { + next_dlsym(); + libc_handle(); } static void *global_dlsym(void *, const char *name) { return next_dlsym()(RTLD_DEFAULT, name); } @@ -4547,8 +4621,7 @@ extern "C" __attribute__((visibility("hidden"), used)) void *fakeinput_dlsym_tar dlsym_fn next = next_dlsym(); if (!next) return reinterpret_cast(missing_dlsym); if (handle == RTLD_DEFAULT || handle == RTLD_NEXT || name == nullptr) return reinterpret_cast(next); - static void *const libc = dlopen(LIBC_SO, RTLD_LAZY | RTLD_NOLOAD); - if (handle != libc || !interposed(name)) return reinterpret_cast(next); + if (handle != libc_handle() || !interposed(name)) return reinterpret_cast(next); return reinterpret_cast(global_dlsym); } diff --git a/tools/tests/test_fakeinput_loader_lock.py b/tools/tests/test_fakeinput_loader_lock.py new file mode 100644 index 0000000000..f125173d7e --- /dev/null +++ b/tools/tests/test_fakeinput_loader_lock.py @@ -0,0 +1,80 @@ +from pathlib import Path +import os +import shutil +import subprocess +import sys +import tempfile +import unittest + +SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp" + +LIBRARY = r""" +#include +#include +#include + +extern _Atomic int loading; + +__attribute__((constructor)) static void start(void) { + struct statfs buf; + atomic_store_explicit(&loading, 1, memory_order_release); + usleep(300000); + statfs("/", &buf); +} +""" + +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +_Atomic int loading; + +static void *load(void *path) { + return dlopen(path, RTLD_NOW); +} + +int main(int argc, char **argv) { + pthread_t loader; + struct statfs buf; + pthread_create(&loader, NULL, load, argv[1]); + while (!atomic_load_explicit(&loading, memory_order_acquire)) sched_yield(); + statfs("/", &buf); + void *handle = NULL; + pthread_join(loader, &handle); + puts(handle ? "ok" : dlerror()); + return 0; +} +""" + + +@unittest.skipUnless(sys.platform == "linux" and shutil.which("gcc") and shutil.which("g++"), + "needs Linux, gcc and g++") +class FakeinputLoaderLockTest(unittest.TestCase): + def test_a_first_hooked_call_while_another_thread_loads_a_library_does_not_deadlock(self): + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) + (path / "program.c").write_text(PROGRAM) + (path / "library.c").write_text(LIBRARY) + subprocess.run(["gcc", "-rdynamic", "-pthread", "-o", str(path / "program"), str(path / "program.c"), + "-ldl"], check=True) + subprocess.run(["gcc", "-shared", "-fPIC", "-o", str(path / "library.so"), str(path / "library.c")], + check=True) + subprocess.run(["g++", "-shared", "-fPIC", "-O2", "-Wno-attributes", "-pthread", + "-std=c++17", "-static-libstdc++", "-static-libgcc", + "-Wl,--exclude-libs,ALL", "-o", str(path / "libfakeinput.so"), + str(SOURCE), "-ldl"], check=True) + for _ in range(5): + result = subprocess.run([str(path / "program"), str(path / "library.so")], + env=dict(os.environ, LD_PRELOAD=str(path / "libfakeinput.so")), + capture_output=True, text=True, timeout=20) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout, "ok\n") + + +if __name__ == "__main__": + unittest.main()