diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index ad5e03374..984650c59 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -3600,6 +3600,13 @@ 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, ...); + // Preserve later preload hooks, including GPU allocation adapters. + static const auto next = reinterpret_cast(dlsym(RTLD_NEXT, "ioctl")); + return next ? next(fd, op, argp) : static_cast(syscall(SYS_ioctl, fd, op, argp)); +} + EXPORT int ioctl(int fd, ioctl_request_t op, ...) { va_list va; void *argp; @@ -3611,7 +3618,7 @@ EXPORT int ioctl(int fd, ioctl_request_t op, ...) { // The lock must not be held across the passthrough: binder's transport is a // blocking ioctl(BINDER_WRITE_READ), so a parked binder pool thread would own // controller_mutex() for as long as it waits and deadlock every other ioctl. - if (!watched(fd)) return syscall(SYS_ioctl, fd, op, argp); + if (!watched(fd)) return forward_ioctl(fd, op, argp); std::unique_lock guard(controller_mutex()); auto maker = uinput_map().find(fd); if (maker != uinput_map().end()) return ioctl_uinput(*maker->second, op, argp); @@ -3629,7 +3636,7 @@ EXPORT int ioctl(int fd, ioctl_request_t op, ...) { auto controller = controller_map().find(fd); if (controller == controller_map().end()) { guard.unlock(); - return syscall(SYS_ioctl, fd, op, argp); + return forward_ioctl(fd, op, argp); } int type = (op >> 8 & 0xFF); @@ -3825,7 +3832,7 @@ EXPORT int ioctl(int fd, ioctl_request_t op, ...) { } else { Logger::log("Unhandled evdev ioctl, type %d number %d\n", type, number); guard.unlock(); - return syscall(SYS_ioctl, fd, op, argp); + return forward_ioctl(fd, op, argp); } } diff --git a/tools/tests/test_fakeinput_ioctl.py b/tools/tests/test_fakeinput_ioctl.py new file mode 100644 index 000000000..0eab19162 --- /dev/null +++ b/tools/tests/test_fakeinput_ioctl.py @@ -0,0 +1,84 @@ +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +SOURCE = Path(__file__).resolve().parents[2] / "app/src/main/cpp/fakeinput_steam.cpp" + +PROGRAM = r""" +#include +#include +#include +#include +#include +#include + +#ifdef BACKEND +int ioctl(int fd, unsigned long request, ...) { + va_list ap; + va_start(ap, request); + int *value = va_arg(ap, int *); + va_end(ap); + assert(fd == -1 && request == 0x12345678 && value); + *value = 0x51; + errno = EACCES; + return -1; +} +#else +int main(int argc, char **argv) { + if (argc > 1) { + int value = 0; + assert(ioctl(-1, 0x12345678, &value) == -1); + assert(value == 0x51 && errno == EACCES); + } else { + int fds[2]; + assert(pipe(fds) == 0); + struct termios term; + assert(ioctl(fds[0], TCGETS, &term) == -1 && errno == ENOTTY); + close(fds[0]); + close(fds[1]); + } + return 0; +} +#endif +""" + + +@unittest.skipUnless(sys.platform == "linux" and shutil.which("gcc") and shutil.which("g++"), + "needs Linux, gcc and g++") +class FakeinputIoctlTest(unittest.TestCase): + @classmethod + def setUpClass(cls): + cls.tmp = tempfile.TemporaryDirectory() + cls.addClassCleanup(cls.tmp.cleanup) + cls.path = Path(cls.tmp.name) + source = cls.path / "program.c" + source.write_text(PROGRAM) + subprocess.run(["gcc", "-o", str(cls.path / "program"), str(source)], check=True) + subprocess.run(["gcc", "-shared", "-fPIC", "-DBACKEND", "-o", + str(cls.path / "backend.so"), str(source)], check=True) + subprocess.run(["g++", "-shared", "-fPIC", "-O2", "-Wno-attributes", "-pthread", + "-std=c++17", "-static-libstdc++", "-static-libgcc", + "-Wl,--exclude-libs,ALL", "-o", str(cls.path / "libfakeinput.so"), + str(SOURCE), "-ldl"], check=True) + + def run_program(self, backend=False): + preload = str(self.path / "libfakeinput.so") + args = [str(self.path / "program")] + if backend: + preload += ":" + str(self.path / "backend.so") + args.append("backend") + subprocess.run(args, env=dict(os.environ, LD_PRELOAD=preload), check=True, timeout=10) + + def test_later_interposer_receives_arguments_and_returns_errno(self): + self.run_program(backend=True) + + def test_libc_passthrough_retains_kernel_error(self): + self.run_program() + + +if __name__ == "__main__": + unittest.main()