From c85b84a9648e029cefa4ac898496d1abd6c75a07 Mon Sep 17 00:00:00 2001 From: Kurt Himebauch <136133082+xXJSONDeruloXx@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:41:26 -0500 Subject: [PATCH] fix(input): fakeinput no longer aborts programs whose allocator calls a hooked function at startup (#2361) * fix(input): fakeinput no longer aborts programs whose allocator calls a hooked function at startup * fix(ctl): screenshot finds display ids that the device name carries inline * fix(input): fakeinput registers its fork handlers before other threads see its config --- app/src/main/cpp/fakeinput_steam.cpp | 43 ++++++++++++-------- tools/droiddeckctl | 7 +++- tools/tests/test_droiddeckctl.py | 9 +++++ tools/tests/test_fakeinput_reentry.py | 58 +++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 tools/tests/test_fakeinput_reentry.py diff --git a/app/src/main/cpp/fakeinput_steam.cpp b/app/src/main/cpp/fakeinput_steam.cpp index 9558d780c4..498461992f 100644 --- a/app/src/main/cpp/fakeinput_steam.cpp +++ b/app/src/main/cpp/fakeinput_steam.cpp @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -208,23 +209,33 @@ struct FakeConfig { bool uinput_enabled; }; +// The first allocation here can start the host program's own allocator, which may call a hooked +// function (mimalloc probes NUMA nodes with access()) and land back in config(). A guarded static +// would throw a recursive-init error there, so the fields are filled without allocating, and a +// reentry on the initializing thread gets them while other threads wait for the fork handlers. static FakeConfig &config() { - static FakeConfig *cfg = [] { - auto *c = new FakeConfig(); - const char *dir = getenv("FAKE_EVDEV_DIR"); - c->hook_dir = dir ? dir : "/data/data/com.termux/files/home/fake-input"; - c->udev_data_dir = getenv("FAKE_UDEV_DATA_DIR"); - c->vibration_enabled = - getenv("FAKE_EVDEV_VIBRATION") && atoi(getenv("FAKE_EVDEV_VIBRATION")); - const char *identity = getenv("FAKE_EVDEV_IDENTITY"); - c->xbox360_identity = identity && !strcmp(identity, "xbox360"); - c->uinput_enabled = getenv("FAKE_EVDEV_UINPUT") && atoi(getenv("FAKE_EVDEV_UINPUT")); - pthread_atfork(controller_fork_prepare, controller_fork_parent, - controller_fork_child); - Logger::init(); - return c; - }(); - return *cfg; + static FakeConfig cfg; + static std::atomic state{0}; + static __thread bool initializing __attribute__((tls_model("initial-exec"))); + if (state.load(std::memory_order_acquire) == 2 || initializing) return cfg; + int expected = 0; + if (!state.compare_exchange_strong(expected, 1, std::memory_order_acq_rel)) { + while (state.load(std::memory_order_acquire) != 2) sched_yield(); + return cfg; + } + initializing = true; + const char *dir = getenv("FAKE_EVDEV_DIR"); + cfg.hook_dir = dir ? dir : "/data/data/com.termux/files/home/fake-input"; + cfg.udev_data_dir = getenv("FAKE_UDEV_DATA_DIR"); + cfg.vibration_enabled = getenv("FAKE_EVDEV_VIBRATION") && atoi(getenv("FAKE_EVDEV_VIBRATION")); + const char *identity = getenv("FAKE_EVDEV_IDENTITY"); + cfg.xbox360_identity = identity && !strcmp(identity, "xbox360"); + cfg.uinput_enabled = getenv("FAKE_EVDEV_UINPUT") && atoi(getenv("FAKE_EVDEV_UINPUT")); + pthread_atfork(controller_fork_prepare, controller_fork_parent, controller_fork_child); + Logger::init(); + state.store(2, std::memory_order_release); + initializing = false; + return cfg; } static std::recursive_mutex &controller_mutex() { diff --git a/tools/droiddeckctl b/tools/droiddeckctl index 8384d6d8e2..8d08288d01 100755 --- a/tools/droiddeckctl +++ b/tools/droiddeckctl @@ -263,10 +263,15 @@ def displays(adb: str, serial: str) -> list[dict]: device = re.search(r"mPrimaryDisplayDevice=(.+)", block) size = re.search(r"real (\d+) x (\d+)", block) name = device.group(1).strip() if device else None + physical = devices.get(name) if name else None + if name and not physical: + # Some ROMs (Retroid Pocket Duo) print the device as "Built-in Screen(local:)" + inline = re.search(r"\(local:(\d+)\)$", name) + physical = inline.group(1) if inline else None out.append({ "display": logical, "name": name, - "physicalId": devices.get(name) if name else None, + "physicalId": physical, "size": [int(size.group(1)), int(size.group(2))] if size else None, }) return out diff --git a/tools/tests/test_droiddeckctl.py b/tools/tests/test_droiddeckctl.py index a626c7868d..c0fb5742b8 100644 --- a/tools/tests/test_droiddeckctl.py +++ b/tools/tests/test_droiddeckctl.py @@ -328,6 +328,15 @@ class DroiddeckctlTest(FakeAdbTest): self.assertEqual("222", out["displays"][1]["physicalId"]) self.assertEqual([1080, 1240], out["displays"][1]["size"]) + def test_displays_named_with_their_unique_id(self): + text = DUMPSYS_DISPLAY.replace("mPrimaryDisplayDevice=Screen-2", + "mPrimaryDisplayDevice=Screen-2(local:222)") + self.configure({}, dumpsys_display=text) + code, out = self.run_ctl("screenshot", str(self.dir / "shot.png"), "--display", "4") + self.assertEqual(0, code) + shot = [c for c in self.calls() if c["args"][:2] == ["exec-out", "screencap"]][0] + self.assertEqual(["exec-out", "screencap", "-p", "-d", "222"], shot["args"]) + def test_unknown_display_is_an_error(self): self.configure({}, dumpsys_display=DUMPSYS_DISPLAY) code, out = self.run_ctl("screenshot", str(self.dir / "x.png"), "--display", "9") diff --git a/tools/tests/test_fakeinput_reentry.py b/tools/tests/test_fakeinput_reentry.py new file mode 100644 index 0000000000..7abd49caed --- /dev/null +++ b/tools/tests/test_fakeinput_reentry.py @@ -0,0 +1,58 @@ +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" + +# Stands in for an allocator like mimalloc that calls access() while it starts up (Claude Code +# probes /sys/devices/system/node/node1 on its first allocation, which comes from fakeinput's +# constructor when it is preloaded). +PROGRAM = r""" +#define _GNU_SOURCE +#include +#include +#include + +extern void *__libc_malloc(size_t); +static int started; + +void *malloc(size_t size) { + if (!started) { + started = 1; + access("/sys/devices/system/node/node1", R_OK); + } + return __libc_malloc(size); +} + +int main(void) { + puts("ok"); + return 0; +} +""" + + +@unittest.skipUnless(sys.platform == "linux" and shutil.which("gcc") and shutil.which("g++"), + "needs Linux, gcc and g++") +class FakeinputReentryTest(unittest.TestCase): + def test_allocator_calling_hooks_during_first_allocation_survives_preload(self): + with tempfile.TemporaryDirectory() as tmp: + path = Path(tmp) + (path / "program.c").write_text(PROGRAM) + subprocess.run(["gcc", "-rdynamic", "-o", str(path / "program"), str(path / "program.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) + result = subprocess.run([str(path / "program")], + env=dict(os.environ, LD_PRELOAD=str(path / "libfakeinput.so")), + capture_output=True, text=True, timeout=10) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout, "ok\n") + + +if __name__ == "__main__": + unittest.main()