mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 15:56:01 +00:00
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
This commit is contained in:
1 parent
953549704a
commit
a6fe10d8ff
2 files changed
+203
-50
No files matched your search
@@ -58,6 +58,59 @@
|
|||||||
|
|
||||||
#define EXPORT extern "C" __attribute__((visibility("default")))
|
#define EXPORT extern "C" __attribute__((visibility("default")))
|
||||||
|
|
||||||
|
struct SymbolSlot {
|
||||||
|
std::atomic<void *> fn{nullptr};
|
||||||
|
std::atomic<bool> resolved{false};
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename Fn> 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<Fn>(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<size_t>(Next::count), "next_names");
|
||||||
|
static SymbolSlot next_slots[static_cast<size_t>(Next::count)];
|
||||||
|
|
||||||
|
template <typename Fn> static Fn next_symbol(Next which) {
|
||||||
|
return resolved_symbol<Fn>(next_slots[static_cast<size_t>(which)], RTLD_NEXT, next_names[static_cast<size_t>(which)]);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void resolve_next_symbols() {
|
||||||
|
for (size_t i = 0; i < static_cast<size_t>(Next::count); i++) next_symbol<void *>(static_cast<Next>(i));
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef __GLIBC__
|
#ifdef __GLIBC__
|
||||||
typedef unsigned long ioctl_request_t;
|
typedef unsigned long ioctl_request_t;
|
||||||
#else
|
#else
|
||||||
@@ -442,6 +495,7 @@ void log(const char *message, ...) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
__attribute__((constructor)) static void library_init() {
|
__attribute__((constructor)) static void library_init() {
|
||||||
|
resolve_next_symbols();
|
||||||
config();
|
config();
|
||||||
controller_mutex();
|
controller_mutex();
|
||||||
controller_map();
|
controller_map();
|
||||||
@@ -733,7 +787,7 @@ open_fake_input_ring(const char *event, int flags) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
|
|
||||||
int fd = my_open(ring_path.c_str(), O_RDWR | (flags & (O_NONBLOCK | O_CLOEXEC)));
|
int fd = my_open(ring_path.c_str(), O_RDWR | (flags & (O_NONBLOCK | O_CLOEXEC)));
|
||||||
if (fd < 0)
|
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) {
|
__attribute__((visibility("hidden"))) static int claim_uinput_node(int *fd_out) {
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
for (int node = UINPUT_EVENT_BASE; node < UINPUT_EVENT_LIMIT; node++) {
|
for (int node = UINPUT_EVENT_BASE; node < UINPUT_EVENT_LIMIT; node++) {
|
||||||
std::string path = uinput_ring_path(node);
|
std::string path = uinput_ring_path(node);
|
||||||
int fd = my_open(path.c_str(), O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0644);
|
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) {
|
__attribute__((visibility("hidden"))) static int publish_uinput(UinputWriter &w) {
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
int fd = -1;
|
int fd = -1;
|
||||||
int node = claim_uinput_node(&fd);
|
int node = claim_uinput_node(&fd);
|
||||||
if (node < 0) return -1;
|
if (node < 0) return -1;
|
||||||
@@ -1440,7 +1494,7 @@ __attribute__((visibility("hidden"))) static const uint8_t *ring_blocks() {
|
|||||||
std::lock_guard<std::mutex> guard(lock);
|
std::lock_guard<std::mutex> guard(lock);
|
||||||
if ((mapped = block.load(std::memory_order_acquire))) return mapped;
|
if ((mapped = block.load(std::memory_order_acquire))) return mapped;
|
||||||
tries.fetch_add(1, std::memory_order_relaxed);
|
tries.fetch_add(1, std::memory_order_relaxed);
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
std::string path = get_ring_path_for_slot(0);
|
std::string path = get_ring_path_for_slot(0);
|
||||||
int fd = path.empty() ? -1 : my_open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
int fd = path.empty() ? -1 : my_open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||||
if (fd < 0) return nullptr;
|
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) {
|
__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;
|
const char *path = sony == 4 ? DS4_HIDRAW_PATH : sony == 5 ? DUALSENSE_HIDRAW_PATH : DECK_HIDRAW_PATH;
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
if (!process_reads_pads()) {
|
if (!process_reads_pads()) {
|
||||||
Logger::log("deck: %s refused: not the Steam client\n", path);
|
Logger::log("deck: %s refused: not the Steam client\n", path);
|
||||||
errno = ENOENT;
|
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) {
|
__attribute__((visibility("hidden"))) static int open_triton_hidraw(int flags, bool emulated) {
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
if (!process_reads_pads()) {
|
if (!process_reads_pads()) {
|
||||||
errno = ENOENT;
|
errno = ENOENT;
|
||||||
return -1;
|
return -1;
|
||||||
@@ -2121,7 +2175,7 @@ __attribute__((visibility("hidden"))) static TouchRingFile *touch_ring() {
|
|||||||
std::call_once(once, [] {
|
std::call_once(once, [] {
|
||||||
const char *path = getenv("FAKE_TOUCHCTL_RING");
|
const char *path = getenv("FAKE_TOUCHCTL_RING");
|
||||||
if (!path || !*path || !fake_deck_enabled()) return;
|
if (!path || !*path || !fake_deck_enabled()) return;
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
int fd = my_open(path, O_RDWR | O_CLOEXEC);
|
int fd = my_open(path, O_RDWR | O_CLOEXEC);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
Logger::log("touch: %s not openable (%s)\n", path, strerror(errno));
|
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,
|
const char *subsystem, const char *devname,
|
||||||
unsigned major, unsigned minor) {
|
unsigned major, unsigned minor) {
|
||||||
using inject_fn = int (*)(const char *, const char *, const char *, const char *, unsigned, unsigned);
|
using inject_fn = int (*)(const char *, const char *, const char *, const char *, unsigned, unsigned);
|
||||||
static auto inject = reinterpret_cast<inject_fn>(dlsym(RTLD_DEFAULT, "dd_udevmon_inject"));
|
static SymbolSlot inject_slot;
|
||||||
|
const auto inject = resolved_symbol<inject_fn>(inject_slot, RTLD_DEFAULT, "dd_udevmon_inject");
|
||||||
return inject ? inject(action, devpath, subsystem, devname, major, minor) : -1;
|
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) {
|
EXPORT int fstatfs(int fd, struct statfs *buf) {
|
||||||
static auto real = reinterpret_cast<int (*)(int, struct statfs *)>(dlsym(RTLD_NEXT, "fstatfs"));
|
const auto real = next_symbol<int (*)(int, struct statfs *)>(Next::fstatfs);
|
||||||
int result = real(fd, buf);
|
int result = real(fd, buf);
|
||||||
if (fake_deck_enabled() && is_deck_sysfs_fd(fd)) {
|
if (fake_deck_enabled() && is_deck_sysfs_fd(fd)) {
|
||||||
note_deck_sysfs("fstatfs", result);
|
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) {
|
EXPORT int statfs(const char *path, struct statfs *buf) {
|
||||||
static auto real = reinterpret_cast<int (*)(const char *, struct statfs *)>(dlsym(RTLD_NEXT, "statfs"));
|
const auto real = next_symbol<int (*)(const char *, struct statfs *)>(Next::statfs);
|
||||||
int result = real(path, buf);
|
int result = real(path, buf);
|
||||||
if (fake_deck_enabled() && is_deck_sysfs_path(path)) {
|
if (fake_deck_enabled() && is_deck_sysfs_path(path)) {
|
||||||
note_deck_sysfs(path, result);
|
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) {
|
__attribute__((visibility("hidden"))) static void publish_imu_mode(const DeckHidraw &deck, int mode) {
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
if (deck.mapping_size < FAKE_INPUT_RING_SIZE + DECK_IMU_BLOCK_SIZE) return;
|
if (deck.mapping_size < FAKE_INPUT_RING_SIZE + DECK_IMU_BLOCK_SIZE) return;
|
||||||
std::string path = get_ring_path_for_slot(0);
|
std::string path = get_ring_path_for_slot(0);
|
||||||
int fd = path.empty() ? -1 : my_open(path.c_str(), O_WRONLY | O_CLOEXEC);
|
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(
|
__attribute__((visibility("hidden"))) static bool deck_mount_id_from_fdinfo(
|
||||||
int dirfd, const char *path, int flags, struct statx *buf) {
|
int dirfd, const char *path, int flags, struct statx *buf) {
|
||||||
static auto real_openat = reinterpret_cast<decltype(&::openat)>(dlsym(RTLD_NEXT, "openat"));
|
const auto real_openat = next_symbol<decltype(&::openat)>(Next::openat);
|
||||||
int target;
|
int target;
|
||||||
if (!path || !*path) {
|
if (!path || !*path) {
|
||||||
if (!(flags & AT_EMPTY_PATH)) return false;
|
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) {
|
EXPORT int statx(int dirfd, const char *path, int flags, unsigned mask, struct statx *buf) {
|
||||||
static auto real = reinterpret_cast<decltype(&::statx)>(dlsym(RTLD_NEXT, "statx"));
|
const auto real = next_symbol<decltype(&::statx)>(Next::statx);
|
||||||
int result = real(dirfd, path, flags, mask, buf);
|
int result = real(dirfd, path, flags, mask, buf);
|
||||||
int saved_errno = errno;
|
int saved_errno = errno;
|
||||||
constexpr unsigned unique_mount_id = 0x4000;
|
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
|
#endif
|
||||||
|
|
||||||
EXPORT DIR *opendir(const char *name) {
|
EXPORT DIR *opendir(const char *name) {
|
||||||
static auto real = reinterpret_cast<DIR *(*)(const char *)>(dlsym(RTLD_NEXT, "opendir"));
|
const auto real = next_symbol<DIR *(*)(const char *)>(Next::opendir);
|
||||||
DIR *dir = real(name);
|
DIR *dir = real(name);
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
note_sys_failure(AT_FDCWD, name);
|
note_sys_failure(AT_FDCWD, name);
|
||||||
@@ -3153,7 +3208,7 @@ EXPORT int open(const char *pathname, int flags, ...) {
|
|||||||
|
|
||||||
va_end(va);
|
va_end(va);
|
||||||
|
|
||||||
static auto my_open = reinterpret_cast<int (*)(const char *, int, ...)>(dlsym(RTLD_NEXT, "open"));
|
const auto my_open = next_symbol<int (*)(const char *, int, ...)>(Next::open);
|
||||||
|
|
||||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags);
|
if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags);
|
||||||
if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(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);
|
va_end(va);
|
||||||
|
|
||||||
static auto my_openat = reinterpret_cast<int (*)(int, const char *, int, ...)>(dlsym(RTLD_NEXT, "openat"));
|
const auto my_openat = next_symbol<int (*)(int, const char *, int, ...)>(Next::openat);
|
||||||
|
|
||||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags);
|
if (fake_uinput_enabled() && is_uinput_path(pathname)) return open_uinput(flags);
|
||||||
if (is_deck_hidraw_path(pathname)) return open_deck_hidraw(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 int real_stat(const char *pathname, struct stat *statbuf) {
|
||||||
static auto fn = reinterpret_cast<int (*)(const char *, struct stat *)>(dlsym(RTLD_NEXT, "stat"));
|
const auto fn = next_symbol<int (*)(const char *, struct stat *)>(Next::stat);
|
||||||
if (fn) return fn(pathname, statbuf);
|
if (fn) return fn(pathname, statbuf);
|
||||||
#ifdef _STAT_VER
|
#ifdef _STAT_VER
|
||||||
static auto xfn = reinterpret_cast<int (*)(int, const char *, struct stat *)>(dlsym(RTLD_NEXT, "__xstat"));
|
const auto xfn = next_symbol<int (*)(int, const char *, struct stat *)>(Next::xstat);
|
||||||
if (xfn) return xfn(_STAT_VER, pathname, statbuf);
|
if (xfn) return xfn(_STAT_VER, pathname, statbuf);
|
||||||
#endif
|
#endif
|
||||||
errno = ENOSYS;
|
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 int real_fstat(int fd, struct stat *buf) {
|
||||||
static auto fn = reinterpret_cast<int (*)(int, struct stat *)>(dlsym(RTLD_NEXT, "fstat"));
|
const auto fn = next_symbol<int (*)(int, struct stat *)>(Next::fstat);
|
||||||
if (fn) return fn(fd, buf);
|
if (fn) return fn(fd, buf);
|
||||||
#ifdef _STAT_VER
|
#ifdef _STAT_VER
|
||||||
static auto xfn = reinterpret_cast<int (*)(int, int, struct stat *)>(dlsym(RTLD_NEXT, "__fxstat"));
|
const auto xfn = next_symbol<int (*)(int, int, struct stat *)>(Next::fxstat);
|
||||||
if (xfn) return xfn(_STAT_VER, fd, buf);
|
if (xfn) return xfn(_STAT_VER, fd, buf);
|
||||||
#endif
|
#endif
|
||||||
errno = ENOSYS;
|
errno = ENOSYS;
|
||||||
@@ -3420,7 +3475,7 @@ EXPORT int fstat(int fd, struct stat *buf) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
EXPORT int access(const char *pathname, int mode) {
|
EXPORT int access(const char *pathname, int mode) {
|
||||||
static auto my_access = reinterpret_cast<decltype(&::access)>(dlsym(RTLD_NEXT, "access"));
|
const auto my_access = next_symbol<decltype(&::access)>(Next::access);
|
||||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
||||||
if (is_deck_hidraw_path(pathname) && process_reads_pads()) 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)) &&
|
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) {
|
EXPORT int faccessat(int dirfd, const char *pathname, int mode, int flags) {
|
||||||
static auto my_faccessat = reinterpret_cast<decltype(&::faccessat)>(dlsym(RTLD_NEXT, "faccessat"));
|
const auto my_faccessat = next_symbol<decltype(&::faccessat)>(Next::faccessat);
|
||||||
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
if (fake_uinput_enabled() && is_uinput_path(pathname)) return 0;
|
||||||
if (is_deck_hidraw_path(pathname) && process_reads_pads()) 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)) &&
|
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 (*filter)(const struct dirent *),
|
||||||
int (*compar)(const struct dirent **,
|
int (*compar)(const struct dirent **,
|
||||||
const struct dirent **)) {
|
const struct dirent **)) {
|
||||||
static auto my_scandir = reinterpret_cast<decltype(&::scandir)>(dlsym(RTLD_NEXT, "scandir"));
|
const auto my_scandir = next_symbol<decltype(&::scandir)>(Next::scandir);
|
||||||
|
|
||||||
if (dirp) {
|
if (dirp) {
|
||||||
if (!strcmp(dirp, "/dev/input")) {
|
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) {
|
EXPORT int inotify_add_watch(int fd, const char *pathname, uint32_t mask) {
|
||||||
static auto my_inotify_add_watch = reinterpret_cast<decltype(&::inotify_add_watch)>(dlsym(RTLD_NEXT, "inotify_add_watch"));
|
const auto my_inotify_add_watch = next_symbol<decltype(&::inotify_add_watch)>(Next::inotify_add_watch);
|
||||||
|
|
||||||
char *fake_path = nullptr;
|
char *fake_path = nullptr;
|
||||||
if (pathname) {
|
if (pathname) {
|
||||||
@@ -3658,7 +3713,7 @@ static bool wait_snapshot(std::shared_ptr<FakeController> fake,
|
|||||||
|
|
||||||
static int forward_ioctl(int fd, ioctl_request_t op, void *argp) {
|
static int forward_ioctl(int fd, ioctl_request_t op, void *argp) {
|
||||||
using ioctl_fn = int (*)(int, ioctl_request_t, ...);
|
using ioctl_fn = int (*)(int, ioctl_request_t, ...);
|
||||||
static const auto next = reinterpret_cast<ioctl_fn>(dlsym(RTLD_NEXT, "ioctl"));
|
const auto next = next_symbol<ioctl_fn>(Next::ioctl);
|
||||||
return next ? next(fd, op, argp) : static_cast<int>(syscall(SYS_ioctl, fd, op, argp));
|
return next ? next(fd, op, argp) : static_cast<int>(syscall(SYS_ioctl, fd, op, argp));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3897,7 +3952,7 @@ EXPORT int ioctl(int fd, ioctl_request_t op, ...) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
EXPORT int close(int fd) {
|
EXPORT int close(int fd) {
|
||||||
static auto my_close = reinterpret_cast<decltype(&::close)>(dlsym(RTLD_NEXT, "close"));
|
const auto my_close = next_symbol<decltype(&::close)>(Next::close);
|
||||||
|
|
||||||
if (!watched(fd)) return my_close(fd);
|
if (!watched(fd)) return my_close(fd);
|
||||||
close_uinput(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) {
|
EXPORT ssize_t write(int fd, const void *buf, size_t count) {
|
||||||
static auto my_write = reinterpret_cast<decltype(&::write)>(dlsym(RTLD_NEXT, "write"));
|
const auto my_write = next_symbol<decltype(&::write)>(Next::write);
|
||||||
|
|
||||||
if (!watched(fd)) return my_write(fd, buf, count);
|
if (!watched(fd)) return my_write(fd, buf, count);
|
||||||
std::unique_lock<std::recursive_mutex> guard(controller_mutex());
|
std::unique_lock<std::recursive_mutex> 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,
|
static int poll_fake(struct pollfd *fds, nfds_t nfds, int timeout,
|
||||||
const sigset_t *sigmask) {
|
const sigset_t *sigmask) {
|
||||||
static auto my_poll = reinterpret_cast<decltype(&::poll)>(dlsym(RTLD_NEXT, "poll"));
|
const auto my_poll = next_symbol<decltype(&::poll)>(Next::poll);
|
||||||
static auto my_ppoll = reinterpret_cast<decltype(&::ppoll)>(dlsym(RTLD_NEXT, "ppoll"));
|
const auto my_ppoll = next_symbol<decltype(&::ppoll)>(Next::ppoll);
|
||||||
|
|
||||||
nfds_t first = 0;
|
nfds_t first = 0;
|
||||||
while (first < nfds && !watched(fds[first].fd)) first++;
|
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,
|
EXPORT int ppoll(struct pollfd *fds, nfds_t nfds,
|
||||||
const struct timespec *timeout, const sigset_t *sigmask) {
|
const struct timespec *timeout, const sigset_t *sigmask) {
|
||||||
static auto my_ppoll = reinterpret_cast<decltype(&::ppoll)>(dlsym(RTLD_NEXT, "ppoll"));
|
const auto my_ppoll = next_symbol<decltype(&::ppoll)>(Next::ppoll);
|
||||||
if (timeout && (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000L)) {
|
if (timeout && (timeout->tv_sec < 0 || timeout->tv_nsec < 0 || timeout->tv_nsec >= 1000000000L)) {
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
return -1;
|
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,
|
EXPORT int select(int nfds, fd_set *readfds, fd_set *writefds,
|
||||||
fd_set *exceptfds, struct timeval *timeout) {
|
fd_set *exceptfds, struct timeval *timeout) {
|
||||||
static auto my_select = reinterpret_cast<decltype(&::select)>(dlsym(RTLD_NEXT, "select"));
|
const auto my_select = next_symbol<decltype(&::select)>(Next::select);
|
||||||
|
|
||||||
fd_set original_readfds;
|
fd_set original_readfds;
|
||||||
fd_set original_writefds;
|
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 int real_stat64(const char *pathname, struct stat64 *statbuf) {
|
||||||
static auto fn = reinterpret_cast<int (*)(const char *, struct stat64 *)>(dlsym(RTLD_NEXT, "stat64"));
|
const auto fn = next_symbol<int (*)(const char *, struct stat64 *)>(Next::stat64);
|
||||||
if (fn) return fn(pathname, statbuf);
|
if (fn) return fn(pathname, statbuf);
|
||||||
#ifdef _STAT_VER
|
#ifdef _STAT_VER
|
||||||
static auto xfn = reinterpret_cast<int (*)(int, const char *, struct stat64 *)>(dlsym(RTLD_NEXT, "__xstat64"));
|
const auto xfn = next_symbol<int (*)(int, const char *, struct stat64 *)>(Next::xstat64);
|
||||||
if (xfn) return xfn(_STAT_VER, pathname, statbuf);
|
if (xfn) return xfn(_STAT_VER, pathname, statbuf);
|
||||||
#endif
|
#endif
|
||||||
errno = ENOSYS;
|
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 int real_fstat64(int fd, struct stat64 *buf) {
|
||||||
static auto fn = reinterpret_cast<int (*)(int, struct stat64 *)>(dlsym(RTLD_NEXT, "fstat64"));
|
const auto fn = next_symbol<int (*)(int, struct stat64 *)>(Next::fstat64);
|
||||||
if (fn) return fn(fd, buf);
|
if (fn) return fn(fd, buf);
|
||||||
#ifdef _STAT_VER
|
#ifdef _STAT_VER
|
||||||
static auto xfn = reinterpret_cast<int (*)(int, int, struct stat64 *)>(dlsym(RTLD_NEXT, "__fxstat64"));
|
const auto xfn = next_symbol<int (*)(int, int, struct stat64 *)>(Next::fxstat64);
|
||||||
if (xfn) return xfn(_STAT_VER, fd, buf);
|
if (xfn) return xfn(_STAT_VER, fd, buf);
|
||||||
#endif
|
#endif
|
||||||
errno = ENOSYS;
|
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) {
|
EXPORT int __xstat(int version, const char *pathname, struct stat *statbuf) {
|
||||||
static auto real = reinterpret_cast<int (*)(int, const char *, struct stat *)>(dlsym(RTLD_NEXT, "__xstat"));
|
const auto real = next_symbol<int (*)(int, const char *, struct stat *)>(Next::xstat);
|
||||||
if (!real) return stat(pathname, statbuf);
|
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) {
|
EXPORT int __xstat64(int version, const char *pathname, struct stat64 *statbuf) {
|
||||||
static auto real = reinterpret_cast<int (*)(int, const char *, struct stat64 *)>(dlsym(RTLD_NEXT, "__xstat64"));
|
const auto real = next_symbol<int (*)(int, const char *, struct stat64 *)>(Next::xstat64);
|
||||||
if (!real) return stat64(pathname, statbuf);
|
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) {
|
EXPORT int __fxstat(int version, int fd, struct stat *buf) {
|
||||||
static auto real = reinterpret_cast<int (*)(int, int, struct stat *)>(dlsym(RTLD_NEXT, "__fxstat"));
|
const auto real = next_symbol<int (*)(int, int, struct stat *)>(Next::fxstat);
|
||||||
if (!real) return fstat(fd, buf);
|
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) {
|
EXPORT int __fxstat64(int version, int fd, struct stat64 *buf) {
|
||||||
static auto real = reinterpret_cast<int (*)(int, int, struct stat64 *)>(dlsym(RTLD_NEXT, "__fxstat64"));
|
const auto real = next_symbol<int (*)(int, int, struct stat64 *)>(Next::fxstat64);
|
||||||
if (!real) return fstat64(fd, buf);
|
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__)
|
#if defined(__x86_64__) || defined(__i386__)
|
||||||
@@ -4518,18 +4573,37 @@ static const char *const interposed_names[] = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
static dlsym_fn next_dlsym() {
|
static dlsym_fn next_dlsym() {
|
||||||
static const dlsym_fn fn = [] {
|
static std::atomic<dlsym_fn> fn{nullptr};
|
||||||
|
static std::atomic<bool> resolved{false};
|
||||||
|
if (!resolved.load(std::memory_order_acquire)) {
|
||||||
#ifdef __x86_64__
|
#ifdef __x86_64__
|
||||||
const char *base = "GLIBC_2.2.5";
|
const char *base = "GLIBC_2.2.5";
|
||||||
#else
|
#else
|
||||||
const char *base = "GLIBC_2.0";
|
const char *base = "GLIBC_2.0";
|
||||||
#endif
|
#endif
|
||||||
|
dlsym_fn found{};
|
||||||
for (const char *version : {base, "GLIBC_2.34"}) {
|
for (const char *version : {base, "GLIBC_2.34"}) {
|
||||||
if (auto found = reinterpret_cast<dlsym_fn>(dlvsym(RTLD_NEXT, "dlsym", version))) return found;
|
if ((found = reinterpret_cast<dlsym_fn>(dlvsym(RTLD_NEXT, "dlsym", version)))) break;
|
||||||
}
|
}
|
||||||
return dlsym_fn{};
|
fn.store(found, std::memory_order_relaxed);
|
||||||
}();
|
resolved.store(true, std::memory_order_release);
|
||||||
return fn;
|
}
|
||||||
|
return fn.load(std::memory_order_relaxed);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void *libc_handle() {
|
||||||
|
static std::atomic<void *> handle{nullptr};
|
||||||
|
static std::atomic<bool> 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); }
|
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();
|
dlsym_fn next = next_dlsym();
|
||||||
if (!next) return reinterpret_cast<void *>(missing_dlsym);
|
if (!next) return reinterpret_cast<void *>(missing_dlsym);
|
||||||
if (handle == RTLD_DEFAULT || handle == RTLD_NEXT || name == nullptr) return reinterpret_cast<void *>(next);
|
if (handle == RTLD_DEFAULT || handle == RTLD_NEXT || name == nullptr) return reinterpret_cast<void *>(next);
|
||||||
static void *const libc = dlopen(LIBC_SO, RTLD_LAZY | RTLD_NOLOAD);
|
if (handle != libc_handle() || !interposed(name)) return reinterpret_cast<void *>(next);
|
||||||
if (handle != libc || !interposed(name)) return reinterpret_cast<void *>(next);
|
|
||||||
return reinterpret_cast<void *>(global_dlsym);
|
return reinterpret_cast<void *>(global_dlsym);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 <stdatomic.h>
|
||||||
|
#include <sys/statfs.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
|
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 <dlfcn.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <sched.h>
|
||||||
|
#include <stdatomic.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/statfs.h>
|
||||||
|
|
||||||
|
_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()
|
||||||
Reference in new issue
Block a user