mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-25 15:56:01 +00:00
* 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
81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
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()
|