diff --git a/docs/development/anti-cheat.md b/docs/development/anti-cheat.md new file mode 100644 index 0000000000..5abd8e0df1 --- /dev/null +++ b/docs/development/anti-cheat.md @@ -0,0 +1,33 @@ +# Easy Anti-Cheat and BattlEye + +On a Steam Deck, Proton loads Valve's anti-cheat runtimes (Proton EasyAntiCheat Runtime, app 1826330, and +Proton BattlEye Runtime, app 1161040). Their bridge libraries are x86 only, so the ARM64 DroidDeck-Proton +cannot load them. Games that need them run an x86-64 DroidDeck-Proton under FEX instead. + +## Routing + +The Proton launchers (`steam-compatibility`: the built-in DroidDeck-Proton launcher and the wrapper put around +every tool in `compatibilitytools.d`) check each `run` and `waitforexitandrun` before any ARM64 setup: + +- **Condition.** The game folder (`STEAM_COMPAT_INSTALL_PATH`, three levels deep) holds one of + `EasyAntiCheat/Settings.json`, `easyanticheat_x64.so`, `BEClient_x64.so` or `BEService_x64.exe`. No + title, app ID or device list is involved. +- **Tool.** The newest `compatibilitytools.d/DroidDeck-Proton*` whose layout is x86-64 + (`files/lib/wine/x86_64-unix`, no `files/bin-arm64`). +- **Runtime.** `PROTON_EAC_RUNTIME` or `PROTON_BATTLEYE_RUNTIME` when it already points at a folder with the + bridge libraries, otherwise the runtime's folder in any Steam library. +- **Launch.** `droiddeck-fex proton /proton `: FEX with the Steam Linux Runtime rootfs, + keeping the game's own `STEAM_COMPAT_DATA_PATH` prefix. + +Without the x86-64 tool or the runtime files the launch stays on ARM64 and the launcher logs why. +`DROIDDECK_ANTICHEAT=off` in the game's launch options keeps it on ARM64. Picking the x86-64 tool for a +game in Steam always runs it under FEX. + +## Why the x86-64 build needs its own Wine patches + +Android kernels use 39-bit user address spaces. Easy Anti-Cheat's launcher maps fixed memory at +`0x7001_0000_0000`, which FEX serves from emulated guest pages (`tools/fex/patches/0026`). The x86-64 Wine +in DroidDeck-Proton carries `ntdll-fixed-allocations-above-host-address-limit.patch` (fixed mappings above +the host limit go to the emulator) and `ntdll-keep-libc-in-native-syscall-dispatch-range.patch` (the +re-armed syscall user dispatch range keeps libc native). Stock x86-64 Proton stops after "Hack native +syscalls" on the same game. diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex b/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex index 0b432bb5d3..76b8a73b24 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-fex @@ -664,7 +664,7 @@ def data_dir(env): return DATA_DIR -def guest_environment(fex, env): +def guest_environment(fex, env, keep_compat=False): result = dict(env) data = data_dir(env) for directory in (CONFIG_DIR, data, CACHE_DIR): @@ -672,7 +672,7 @@ def guest_environment(fex, env): result["FEX_APP_CONFIG_LOCATION"] = CONFIG_DIR + "/" result["FEX_APP_DATA_LOCATION"] = data + "/" result["FEX_APP_CACHE_LOCATION"] = CACHE_DIR + "/" - if result.get("STEAM_COMPAT_DATA_PATH"): + if result.get("STEAM_COMPAT_DATA_PATH") and not keep_compat: short = data if data != DATA_DIR else "/tmp/fex" os.makedirs(short, exist_ok=True) result["STEAM_COMPAT_DATA_PATH"] = short @@ -761,7 +761,7 @@ def resolve(program, env): return shutil.which(program, path=env.get("PATH")) or program -def launch(command, env, mode, decide=None): +def launch(command, env, mode, decide=None, keep_compat=False): program = resolve(command[0], env) use = mode == "on" or (mode == "auto" and needs_fex(decide or program)) if not use: @@ -769,7 +769,7 @@ def launch(command, env, mode, decide=None): fex = prepare(required_runtime(program)) if fex is None: return 126 - guest = guest_environment(fex, env) + guest = guest_environment(fex, env, keep_compat) x86_64 = elf_arch(program) != "i386" thunks = "on" if "FEX_THUNKCONFIG" in guest else "off" folder = decide if decide and os.path.isdir(decide) else os.path.dirname(program) @@ -963,7 +963,7 @@ def parse_run(args): def main(argv): if not argv: - say("usage: droiddeck-fex steam VERB CMD... | run [--mode auto|on|off] [--for PATH] [--] CMD... | needs PATH | extract IMAGE | prepare | status") + say("usage: droiddeck-fex steam VERB CMD... | proton PROTON VERB CMD... | run [--mode auto|on|off] [--for PATH] [--] CMD... | needs PATH | extract IMAGE | prepare | status") return 2 action, args = argv[0], list(argv[1:]) if action == "steam": @@ -975,6 +975,10 @@ def main(argv): if not command: return 2 return launch(command, game_environment(dict(os.environ), command), os.environ.get("DROIDDECK_FEX", "auto")) + if action == "proton": + if len(args) < 2: + return 2 + return launch(args, game_environment(dict(os.environ), args[2:] or args), "on", keep_compat=True) if action == "run": try: mode, decide, command = parse_run(args) diff --git a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility index 4eb7ba26f7..6b1a8d8346 100755 --- a/tools/linuxfs/overlay/usr/local/bin/steam-compatibility +++ b/tools/linuxfs/overlay/usr/local/bin/steam-compatibility @@ -318,12 +318,59 @@ dd_vacdiag() { } """ +DD_ANTICHEAT_SETUP = """ +dd_anticheat_runtime() { + local current=$1 name=$2 library + if [ -n "$current" ] && [ -n "$(find "$current" -name '*.so' -print -quit 2>/dev/null)" ]; then + printf '%s' "$current"; return 0 + fi + while IFS= read -r library; do + [ -n "$(find "$library/steamapps/common/$name" -name '*.so' -print -quit 2>/dev/null)" ] || continue + printf '%s' "$library/steamapps/common/$name"; return 0 + done < <(printf '%s\\n' "$DD_AC_ROOT" /mnt/droiddeck-sd; sed -n 's/^[[:space:]]*"path"[[:space:]]*"\\(.*\\)"[[:space:]]*$/\\1/p' "$DD_AC_ROOT/steamapps/libraryfolders.vdf" 2>/dev/null) + return 1 +} + +dd_x86_64_proton() { + [ -d "$1/files/lib/wine/x86_64-unix" ] && [ ! -d "$1/files/bin-arm64" ] +} + +dd_anticheat() { + case ${verb:-} in waitforexitandrun | run) ;; *) return 0 ;; esac + case ${DROIDDECK_ANTICHEAT:-auto} in off | 0 | false) return 0 ;; esac + local game=${STEAM_COMPAT_INSTALL_PATH:-} found tool runtime + [ -d "$game" ] || return 0 + found=$(find "$game" -maxdepth 3 \\( -ipath '*/easyanticheat/settings.json' -o -iname 'easyanticheat_x64.so' \\ + -o -iname 'beclient_x64.so' -o -iname 'beservice_x64.exe' \\) -print -quit 2>/dev/null) + [ -n "$found" ] || return 0 + DD_AC_ROOT=${STEAM_COMPAT_CLIENT_INSTALL_PATH:-${XDG_DATA_HOME:-$HOME/.local/share}/Steam} + tool=$(cd "$DD_AC_ROOT/compatibilitytools.d" 2>/dev/null && for d in DroidDeck-Proton*; do + [ -x "$d/proton" ] && dd_x86_64_proton "$d" && printf '%s/%s\\n' "$PWD" "$d" + done | sort -V | tail -n 1) + if [ -z "$tool" ]; then + echo "droiddeck-proton: anti-cheat files in ${found#$game/}; no x86-64 DroidDeck-Proton is installed, so this launch stays on ARM64" >&2 + return 0 + fi + case ${found,,} in + *easyanticheat*) runtime=$(dd_anticheat_runtime "${PROTON_EAC_RUNTIME:-}" "Proton EasyAntiCheat Runtime") && export PROTON_EAC_RUNTIME="$runtime" ;; + *) runtime=$(dd_anticheat_runtime "${PROTON_BATTLEYE_RUNTIME:-}" "Proton BattlEye Runtime") && export PROTON_BATTLEYE_RUNTIME="$runtime" ;; + esac + if [ -z "$runtime" ]; then + echo "droiddeck-proton: anti-cheat files in ${found#$game/}; its Proton runtime is not installed, so this launch stays on ARM64" >&2 + return 0 + fi + dd_game_cpus + echo "droiddeck-proton: anti-cheat files in ${found#$game/}; running ${tool##*/} under FEX" >&2 + exec ${DD_TASKSET:-} /usr/local/bin/droiddeck-fex proton "$tool/proton" "$verb" "$@" +} +""" + # DroidDeck-Bionic runs only without proot. With proot on, a launch Steam sends to a DroidDeck-Bionic tool goes to the # newest DroidDeck-Proton's wrapper before anything else runs, as if that Proton had been picked; the picks stay as set. DD_BIONIC_PROOT_FALLBACK = """ if [ -f "$here/bionic/launch.sh" ] && [ -z "${DDNATV_ROOT:-}" ]; then dd_fallback=$(cd "$here/.." 2>/dev/null && for d in DroidDeck-Proton*; do - [ -x "$d/droiddeck-proton-wrap" ] && [ ! -e "$d/bionic/launch.sh" ] || continue + [ -x "$d/droiddeck-proton-wrap" ] && [ ! -e "$d/bionic/launch.sh" ] && ! dd_x86_64_proton "$d" || continue v=${d#DroidDeck-Proton}; printf '%s\\t%s/%s\\n' "${v#[ -]}" "$PWD" "$d" done | sort -V | tail -n 1 | cut -f 2-) if [ -n "$dd_fallback" ]; then @@ -461,6 +508,7 @@ for name in %s; do [ -n "$depot" ] && break done if [ -n "$depot" ]; then + dd_anticheat "$@" dd_components "$depot" dd_directaudio "$depot" "$@" dd_game_cpus @@ -481,7 +529,7 @@ LAUNCHER_TEMPLATE = LAUNCHER_SH def launcher_sh(sources=SOURCES): return (LAUNCHER_TEMPLATE % " ".join('"%s"' % n for n in sources)).replace( "verb=$1; shift", "verb=$1; shift\n" + DD_DRIVER_QUERY_SKIP + DD_DESKTOP_GPU_SETUP + DD_DIRECTAUDIO_SETUP + DD_GAME_CPUS_SETUP + DD_IDTECH3_SETUP - + DD_VACDIAG_SETUP + DD_COMPONENTS_SETUP + DD_LIBPATH_SETUP + DD_STORE_SETUP, 1) + + DD_VACDIAG_SETUP + DD_COMPONENTS_SETUP + DD_LIBPATH_SETUP + DD_STORE_SETUP + DD_ANTICHEAT_SETUP, 1) LAUNCHER_SH = launcher_sh() @@ -516,6 +564,11 @@ if [ -n "${PROOT_FP_KEY:-}" ] && [ -f "$audit" ]; then *) LD_AUDIT="$audit${LD_AUDIT:+:$LD_AUDIT}"; export LD_AUDIT ;; esac fi +if dd_x86_64_proton "$here"; then + dd_game_cpus + exec ${DD_TASKSET:-} /usr/local/bin/droiddeck-fex proton "$here/%s" "$verb" "$@" +fi +dd_anticheat "$@" dd_components "$here" dd_directaudio "$here" "$@" dd_game_cpus @@ -530,9 +583,9 @@ dd_run ${DD_TASKSET:-} /usr/local/bin/droiddeck-game-env "$DD_STORE_PROTON" "$ve def extra_wrapper_sh(entry_point): - return (EXTRA_WRAPPER_SH % entry_point).replace( + return (EXTRA_WRAPPER_SH % (entry_point, entry_point)).replace( 'here=$(dirname "$0")', - 'here=$(dirname "$0")\n' + DD_BIONIC_PROOT_FALLBACK + DD_DRIVER_QUERY_SKIP + DD_DESKTOP_GPU_SETUP + DD_DIRECTAUDIO_SETUP + DD_GAME_CPUS_SETUP + 'here=$(dirname "$0")\n' + DD_ANTICHEAT_SETUP + DD_BIONIC_PROOT_FALLBACK + DD_DRIVER_QUERY_SKIP + DD_DESKTOP_GPU_SETUP + DD_DIRECTAUDIO_SETUP + DD_GAME_CPUS_SETUP + DD_IDTECH3_SETUP + DD_VACDIAG_SETUP + DD_COMPONENTS_SETUP + DD_LIBPATH_SETUP + DD_STORE_SETUP, 1) diff --git a/tools/tests/test_anticheat_routing.py b/tools/tests/test_anticheat_routing.py new file mode 100644 index 0000000000..c5edbf098a --- /dev/null +++ b/tools/tests/test_anticheat_routing.py @@ -0,0 +1,147 @@ +import json +import os +from pathlib import Path +import runpy +import subprocess +import tempfile +import unittest + + +BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" +COMPAT = runpy.run_path(str(BIN / "steam-compatibility"), run_name="steam_compatibility") +SETUP = COMPAT["DD_ANTICHEAT_SETUP"] + + +class AntiCheatRoutingTest(unittest.TestCase): + def setUp(self): + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + self.root = Path(temp.name) + self.steam = self.root / "Steam" + self.common = self.steam / "steamapps" / "common" + self.tools = self.steam / "compatibilitytools.d" + self.game = self.common / "Some Game" + self.game.mkdir(parents=True) + self.tools.mkdir(parents=True) + self.record = self.root / "record.json" + self.fex = self.root / "droiddeck-fex" + self.fex.write_text("#!/bin/bash\npython3 -c 'import json,os,sys; json.dump({\"argv\": sys.argv[2:], " + "\"eac\": os.environ.get(\"PROTON_EAC_RUNTIME\"), \"be\": os.environ.get(\"PROTON_BATTLEYE_RUNTIME\")}, " + "open(sys.argv[1], \"w\"))' \"$DD_RECORD\" \"$@\"\n") + self.fex.chmod(0o755) + + def tool(self, name, x86_64=True): + tool = self.tools / name + (tool / "files" / "lib" / "wine" / ("x86_64-unix" if x86_64 else "aarch64-unix")).mkdir(parents=True) + if not x86_64: + (tool / "files" / "bin-arm64").mkdir() + (tool / "proton").write_text("#!/bin/sh\n") + (tool / "proton").chmod(0o755) + return tool + + def runtime(self, name, library): + folder = self.common / name / "v2" / "lib64" + folder.mkdir(parents=True) + (folder / library).write_text("") + return self.common / name + + def file(self, relative): + path = self.game / relative + path.parent.mkdir(parents=True, exist_ok=True) + path.write_text("{}") + + def launch(self, verb="waitforexitandrun", extra=None): + script = SETUP.replace("/usr/local/bin/droiddeck-fex", str(self.fex)) + ( + "dd_game_cpus() { :; }\n" + "verb=$1; shift\n" + "dd_anticheat \"$@\"\n" + "echo stayed\n") + env = {"PATH": os.environ["PATH"], "HOME": str(self.root), "DD_RECORD": str(self.record), + "STEAM_COMPAT_INSTALL_PATH": str(self.game), "STEAM_COMPAT_CLIENT_INSTALL_PATH": str(self.steam)} + env.update(extra or {}) + result = subprocess.run(["bash", "-c", script, "launcher", verb, str(self.game / "game.exe")], + env=env, capture_output=True, text=True, timeout=30) + routed = json.loads(self.record.read_text()) if self.record.exists() else None + return result, routed + + def test_eos_easy_anti_cheat_runs_the_newest_x86_64_build_under_fex(self): + self.file("EasyAntiCheat/Settings.json") + self.tool("DroidDeck-Proton-11.0-1-x86_64") + newest = self.tool("DroidDeck-Proton-11.0-10-x86_64") + self.tool("DroidDeck-Proton-11.0-20", x86_64=False) + runtime = self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so") + result, routed = self.launch() + self.assertNotIn("stayed", result.stdout) + self.assertEqual(routed["argv"], ["proton", str(newest / "proton"), "waitforexitandrun", str(self.game / "game.exe")]) + self.assertEqual(routed["eac"], str(runtime)) + self.assertIn("running DroidDeck-Proton-11.0-10-x86_64 under FEX", result.stderr) + + def test_battleye_sets_its_own_runtime(self): + self.file("BattlEye/BEClient_x64.so") + tool = self.tool("DroidDeck-Proton-11.0-1-x86_64") + runtime = self.runtime("Proton BattlEye Runtime", "beclient_x64.so") + _, routed = self.launch() + self.assertEqual((routed["argv"][1], routed["be"], routed["eac"]), (str(tool / "proton"), str(runtime), None)) + + def test_a_runtime_already_set_with_files_is_kept(self): + self.file("EasyAntiCheat/Settings.json") + self.tool("DroidDeck-Proton-11.0-1-x86_64") + own = self.root / "own-eac" + (own / "v2").mkdir(parents=True) + (own / "v2" / "easyanticheat_x64.so").write_text("") + _, routed = self.launch(extra={"PROTON_EAC_RUNTIME": str(own)}) + self.assertEqual(routed["eac"], str(own)) + + def test_games_without_anti_cheat_files_stay_on_arm64(self): + self.file("Data/Settings.json") + self.tool("DroidDeck-Proton-11.0-1-x86_64") + self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so") + result, routed = self.launch() + self.assertIsNone(routed) + self.assertIn("stayed", result.stdout) + self.assertEqual(result.stderr, "") + + def test_without_an_x86_64_build_the_launch_stays_on_arm64(self): + self.file("EasyAntiCheat/Settings.json") + self.tool("DroidDeck-Proton-11.0-20", x86_64=False) + self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so") + result, routed = self.launch() + self.assertIsNone(routed) + self.assertIn("stayed", result.stdout) + self.assertIn("no x86-64 DroidDeck-Proton is installed", result.stderr) + + def test_without_the_runtime_files_the_launch_stays_on_arm64(self): + self.file("EasyAntiCheat/Settings.json") + self.tool("DroidDeck-Proton-11.0-1-x86_64") + (self.common / "Proton EasyAntiCheat Runtime").mkdir(parents=True) + result, routed = self.launch() + self.assertIsNone(routed) + self.assertIn("its Proton runtime is not installed", result.stderr) + + def test_the_user_can_turn_routing_off(self): + self.file("EasyAntiCheat/Settings.json") + self.tool("DroidDeck-Proton-11.0-1-x86_64") + self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so") + result, routed = self.launch(extra={"DROIDDECK_ANTICHEAT": "off"}) + self.assertIsNone(routed) + self.assertIn("stayed", result.stdout) + + def test_other_verbs_are_left_alone(self): + self.file("EasyAntiCheat/Settings.json") + self.tool("DroidDeck-Proton-11.0-1-x86_64") + self.runtime("Proton EasyAntiCheat Runtime", "easyanticheat_x64.so") + result, routed = self.launch(verb="getcompatpath") + self.assertIsNone(routed) + self.assertIn("stayed", result.stdout) + + def test_launchers_check_before_arm64_setup_and_x86_64_tools_run_under_fex(self): + launcher = COMPAT["LAUNCHER_SH"] + self.assertLess(launcher.index('dd_anticheat "$@"'), launcher.index('dd_components "$depot"')) + wrapper = COMPAT["extra_wrapper_sh"]("proton") + self.assertLess(wrapper.index("dd_x86_64_proton() {"), wrapper.index("dd_fallback=")) + self.assertLess(wrapper.index('droiddeck-fex proton "$here/proton"'), wrapper.index('dd_components "$here"')) + self.assertIn('! dd_x86_64_proton "$d"', wrapper) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/tests/test_droiddeck_fex.py b/tools/tests/test_droiddeck_fex.py index 79f7043cbb..6dde936739 100644 --- a/tools/tests/test_droiddeck_fex.py +++ b/tools/tests/test_droiddeck_fex.py @@ -1235,6 +1235,20 @@ class LaunchTest(FexTestCase): self.assertEqual(emulated.call_args[0][2]["FEX_ROOTFS"], str(platform / "files")) self.assertEqual(FEX["required_runtime"](str(self.proton("1391110"))), "1628350") + def test_proton_action_keeps_the_prefix_and_runs_under_fex(self): + self.fex_tool() + self.runtime() + proton = self.proton("1628350") + prefix = str(self.tmp / "compatdata" / "730") + env = {"STEAM_COMPAT_DATA_PATH": prefix} + with mock.patch.dict(os.environ, env, clear=False), mock.patch("os.execve") as emulated: + self.quiet(FEX["main"], ["proton", str(proton), "waitforexitandrun", "game.exe"]) + command, guest = emulated.call_args[0][1], emulated.call_args[0][2] + self.assertEqual(command[1:], [str(proton), "waitforexitandrun", "game.exe"]) + self.assertEqual(guest["STEAM_COMPAT_DATA_PATH"], prefix) + self.assertNotEqual(FEX["guest_environment"]({"portable": True}, env)["STEAM_COMPAT_DATA_PATH"], prefix) + self.assertEqual(FEX["main"](["proton", str(proton)]), 2) + def test_native_programs_run_directly(self): program = self.write(self.tmp / "native", elf(183), 0o755) with mock.patch("os.execvpe") as native, mock.patch("os.execve") as emulated: