From 975d6e260adab5facc4c8d7d96f4a84feb1ca9c3 Mon Sep 17 00:00:00 2001 From: Delete228 <31237303+Delete228@users.noreply.github.com> Date: Wed, 21 Oct 2026 00:52:40 +0300 Subject: [PATCH] fix: restore Factorio ARM64 controller input through readv (#1195) --- app/src/main/cpp/fakeinput_steam.cpp | 84 ++++++++-- tools/linuxfs/check-preloads.sh | 2 +- tools/tests/test_fakeinput_ring.py | 235 +++++++++++++++++++++++++-- 3 files changed, 297 insertions(+), 24 deletions(-) diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index a8c94d2ff9..5c4fc4e3a5 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -3580,7 +3580,22 @@ EXPORT int close(int fd) { static constexpr int kMaxRingWaitMs = 2; static constexpr int kLowLatencyRingWaitUs = 250; -EXPORT ssize_t read(int fd, void *buf, size_t count) { +static ssize_t copy_input_events(void *buf, const struct input_event *events, size_t count) { + size_t bytes = count * sizeof(struct input_event); + struct iovec local = {const_cast(events), bytes}; + struct iovec remote = {buf, bytes}; + ssize_t copied = syscall(SYS_process_vm_writev, getpid(), &local, 1, &remote, 1, 0); + if (copied < 0) return -1; + if (copied != static_cast(bytes)) { + errno = EFAULT; + // evdev reads only report complete input_event records. + copied -= copied % sizeof(struct input_event); + if (!copied) return -1; + } + return copied; +} + +static ssize_t read_input(int fd, void *buf, size_t count) { if (!watched(fd)) return syscall(SYS_read, fd, buf, count); std::unique_lock guard(controller_mutex()); auto controller = controller_map().find(fd); @@ -3625,42 +3640,44 @@ EXPORT ssize_t read(int fd, void *buf, size_t count) { if (events == 0) break; const uint8_t *ring_events = reinterpret_cast(fake.ring) + FAKE_INPUT_RING_HEADER_SIZE; + std::vector input_events(events); for (size_t i = 0; i < events; ++i) { size_t index = (fake.read_seq + i) % FAKE_INPUT_RING_CAPACITY; RingEvent stored; memcpy(&stored, ring_events + index * FAKE_INPUT_EVENT_SIZE, FAKE_INPUT_EVENT_SIZE); - struct input_event ev = {}; + struct input_event &ev = input_events[i]; ev.time.tv_sec = stored.sec; ev.time.tv_usec = stored.usec; ev.type = stored.type; ev.code = stored.code; ev.value = stored.value; if (ev.type == EV_ABS) ev.code = presented_abs_code(fake, ev.code); - memcpy(static_cast(buf) + i * sizeof(ev), &ev, sizeof(ev)); } __atomic_thread_fence(__ATOMIC_ACQUIRE); if (snap.sequence != __atomic_load_n(&fake.ring->snapshot_seq, __ATOMIC_RELAXED)) continue; - fake.read_seq += events; - return static_cast(events * sizeof(struct input_event)); + ssize_t copied = copy_input_events(buf, input_events.data(), events); + if (copied > 0) fake.read_seq += copied / sizeof(struct input_event); + return copied; } } size_t events = std::min(requested, fake.keyframe_remaining); + struct input_event input_events[kNeutralEventCount] = {}; struct timeval now = {}; gettimeofday(&now, nullptr); for (size_t i = 0; i < events; ++i) { - size_t index = kNeutralEventCount - fake.keyframe_remaining; - struct input_event ev = {}; + size_t index = kNeutralEventCount - fake.keyframe_remaining + i; + struct input_event &ev = input_events[i]; ev.time = now; ev.type = kNeutralEvents[index].type; ev.code = kNeutralEvents[index].code; ev.value = keyframe_value(fake, ev.type, ev.code); if (ev.type == EV_ABS) ev.code = presented_abs_code(fake, ev.code); - memcpy(static_cast(buf) + i * sizeof(ev), &ev, sizeof(ev)); - --fake.keyframe_remaining; } - return static_cast(events * sizeof(struct input_event)); + ssize_t copied = copy_input_events(buf, input_events, events); + if (copied > 0) fake.keyframe_remaining -= copied / sizeof(struct input_event); + return copied; } if (nonblock) { errno = EAGAIN; @@ -3676,6 +3693,53 @@ EXPORT ssize_t read(int fd, void *buf, size_t count) { } } +EXPORT ssize_t read(int fd, void *buf, size_t count) { + return read_input(fd, buf, count); +} + +EXPORT ssize_t readv(int fd, const struct iovec *iov, int iovcnt) { + if (!is_fake_input_fd(fd)) return syscall(SYS_readv, fd, iov, iovcnt); + if (iovcnt < 0 || iovcnt > IOV_MAX) { + errno = EINVAL; + return -1; + } + if (iovcnt && !iov) { + errno = EFAULT; + return -1; + } + std::vector copied_iov(static_cast(iovcnt)); + if (iovcnt) { + size_t bytes = copied_iov.size() * sizeof(struct iovec); + struct iovec local = {copied_iov.data(), bytes}; + struct iovec remote = {const_cast(iov), bytes}; + ssize_t copied = syscall(SYS_process_vm_readv, getpid(), &local, 1, &remote, 1, 0); + if (copied != static_cast(bytes)) { + errno = EFAULT; + return -1; + } + } + iov = copied_iov.data(); + size_t size = 0; + for (int i = 0; i < iovcnt; ++i) { + if (iov[i].iov_len > static_cast(SSIZE_MAX) - size) { + errno = EINVAL; + return -1; + } + size += iov[i].iov_len; + } + + // evdev's readv calls its read handler per vector; Steam's ARM64 overlay uses this path too. + ssize_t total = 0; + for (int i = 0; i < iovcnt; ++i) { + if (!iov[i].iov_len) continue; + ssize_t got = read_input(fd, iov[i].iov_base, iov[i].iov_len); + if (got < 0) return total ? total : -1; + total += got; + if (static_cast(got) != iov[i].iov_len) break; + } + return total; +} + EXPORT ssize_t write(int fd, const void *buf, size_t count) { static auto my_write = reinterpret_cast(dlsym(RTLD_NEXT, "write")); diff --git a/tools/linuxfs/check-preloads.sh b/tools/linuxfs/check-preloads.sh index 78021c4404..30f94a96a6 100644 --- a/tools/linuxfs/check-preloads.sh +++ b/tools/linuxfs/check-preloads.sh @@ -9,7 +9,7 @@ for bad in libstdc++.so.6 libgcc_s.so.1; do done syms() { "${READELF:-readelf}" -Ws "$1" | awk '$4 == "FUNC" && $5 == "GLOBAL" {sub(/@.*/, "", $8); print $8}'; } FAKE=$(syms "$D/libfakeinput.so") -for sym in open openat ioctl read close poll ppoll select stat fstat access scandir; do +for sym in open openat ioctl read readv close poll ppoll select stat fstat access scandir; do grep -qx "$sym" <<<"$FAKE" || { echo "::error::libfakeinput.so does not export $sym"; exit 1; } done if grep -qxE '__gxx_personality_v0|__cxa_throw|_Unwind_Resume' <<<"$FAKE"; then diff --git a/tools/tests/test_fakeinput_ring.py b/tools/tests/test_fakeinput_ring.py index 3c87bd11e2..86afc9939a 100644 --- a/tools/tests/test_fakeinput_ring.py +++ b/tools/tests/test_fakeinput_ring.py @@ -18,24 +18,44 @@ READER = r""" #include #include #include +#include #include -int main(void) +static int vector_count; + +static ssize_t controller_read(int fd, void *buffer, size_t size) { + if (!vector_count) + return read(fd, buffer, size); + struct iovec vectors[3] = { + {buffer, sizeof(struct input_event)}, + {(char *)buffer + sizeof(struct input_event), 0}, + {(char *)buffer + sizeof(struct input_event), size - sizeof(struct input_event)}, + }; + if (vector_count == 1) { + vectors[0].iov_len = size; + return readv(fd, vectors, 1); + } + return readv(fd, vectors, 3); +} + +int main(int argc, char **argv) +{ + vector_count = argc > 1 ? (argv[1][0] == '1' ? 1 : 3) : 0; struct input_event events[64]; int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK); if (fd < 0) { perror("open"); return 1; } - while (read(fd, events, sizeof(events)) > 0) + while (controller_read(fd, events, sizeof(events)) > 0) ; puts("ready"); fflush(stdout); char go; if (read(0, &go, 1) != 1) return 1; - ssize_t got = read(fd, events, sizeof(events)); + ssize_t got = controller_read(fd, events, sizeof(events)); for (ssize_t i = 0; i < got / (ssize_t)sizeof(events[0]); i++) printf("%ld.%06ld %u %u %d\n", (long)events[i].time.tv_sec, (long)events[i].time.tv_usec, events[i].type, events[i].code, events[i].value); @@ -65,6 +85,146 @@ int main(void) return 0; } """ +READV_CHECKS = r""" +#include +#include +#include +#include +#include +#include +#include +#include + +int main(void) +{ + struct input_event events[64]; + int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK); + if (fd < 0) + return 1; + while (read(fd, events, sizeof(events)) > 0) + ; + struct iovec vector = {events, sizeof(events)}; + if (readv(fd, &vector, 1) != -1 || errno != EAGAIN) + return 2; + if (readv(fd, NULL, 0) != 0) + return 3; + vector.iov_len = 0; + if (readv(fd, &vector, 1) != 0) + return 4; + vector.iov_len = sizeof(struct input_event) - 1; + if (readv(fd, &vector, 1) != -1 || errno != EINVAL) + return 5; + ssize_t (*invoke)(int, const struct iovec *, int) = readv; + if (invoke(fd, NULL, -1) != -1 || errno != EINVAL) + return 6; + if (invoke(fd, NULL, 1025) != -1 || errno != EINVAL) + return 7; + if (invoke(fd, NULL, 1) != -1 || errno != EFAULT) + return 8; + long page = sysconf(_SC_PAGESIZE); + void *area = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (area == MAP_FAILED) + return 10; + struct iovec *boundary = (struct iovec *)((char *)area + page - sizeof(struct iovec)); + *boundary = vector; + if (mprotect((char *)area + page, page, PROT_NONE)) + return 11; + if (invoke(fd, boundary, 2) != -1 || errno != EFAULT) + return 12; + if (invoke(fd, boundary + 1, 1) != -1 || errno != EFAULT) + return 13; + if (munmap(area, 2 * page)) + return 14; + if (invoke(fd, boundary, 1) != -1 || errno != EFAULT) + return 15; + int pipefd[2]; + char text[5] = {0}; + struct iovec parts[2] = {{text, 1}, {text + 1, 3}}; + if (pipe(pipefd) || write(pipefd[1], "test", 4) != 4 || + readv(pipefd[0], parts, 2) != 4 || strcmp(text, "test")) + return 9; + puts("ok"); + return 0; +} +""" +READV_DESTINATION_CHECKS = r""" +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + struct input_event events[64]; + int fd = open("/dev/input/event16", O_RDONLY | O_NONBLOCK); + if (fd < 0) + return 1; + if (argc > 1) + while (read(fd, events, sizeof(events)) > 0) + ; + puts("ready"); + fflush(stdout); + char go; + if (read(0, &go, 1) != 1) + return 2; + long page = sysconf(_SC_PAGESIZE); + char *area = mmap(NULL, 2 * page, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (area == MAP_FAILED || mprotect(area + page, page, PROT_NONE)) + return 3; + struct iovec vector = {area + page, sizeof(struct input_event)}; + if (readv(fd, &vector, 1) != -1 || errno != EFAULT) + return 4; + if (read(fd, vector.iov_base, vector.iov_len) != -1 || errno != EFAULT) + return 5; + if (mprotect(area, page, PROT_READ)) + return 6; + vector.iov_base = area; + if (readv(fd, &vector, 1) != -1 || errno != EFAULT) + return 7; + if (mprotect(area, page, PROT_READ | PROT_WRITE) || munmap(area + page, page)) + return 8; + vector.iov_base = area + page; + if (readv(fd, &vector, 1) != -1 || errno != EFAULT) + return 9; + vector.iov_base = area + page - 1; + if (readv(fd, &vector, 1) != -1 || errno != EFAULT) + return 15; + vector.iov_base = area + page - sizeof(struct input_event); + vector.iov_len = 2 * sizeof(struct input_event); + if (readv(fd, &vector, 1) != sizeof(struct input_event)) + return 10; + struct input_event *first = vector.iov_base; + if (first->type != EV_KEY || first->code != BTN_A) + return 11; + struct iovec parts[2] = {{events, sizeof(struct input_event)}, {NULL, sizeof(struct input_event)}}; + if (readv(fd, parts, 2) != sizeof(struct input_event)) + return 12; + if (events[0].type != (argc > 1 ? EV_ABS : EV_KEY) || events[0].code != (argc > 1 ? ABS_Y : BTN_B)) + return 13; + vector.iov_base = events; + vector.iov_len = sizeof(events); + ssize_t got = readv(fd, &vector, 1); + if (got != (argc > 1 ? 1 : 18) * sizeof(struct input_event) || + events[0].type != (argc > 1 ? EV_SYN : EV_KEY) || events[0].code != (argc > 1 ? SYN_REPORT : BTN_X)) + return 14; + munmap(area, page); + puts("ok"); + return 0; +} +""" +OVERLAY_READ = r""" +#include +#include + +ssize_t read(int fd, void *buffer, size_t size) +{ + struct iovec vector = {buffer, size}; + return readv(fd, &vector, 1); +} +""" def can_build(bits): @@ -82,15 +242,16 @@ class FakeinputRingTest(unittest.TestCase): self.tmp = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.tmp, True) - def ring(self): + def ring(self, virtual=True): hooks = self.tmp / "hooks" hooks.mkdir() - (hooks / "event16").touch() - ring = bytearray(HEADER + CAPACITY * EVENT + DESCRIPTION) + (hooks / ("event16" if virtual else "event0")).touch() + ring = bytearray(HEADER + CAPACITY * EVENT + (DESCRIPTION if virtual else 0)) struct.pack_into("