diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session index edbb3d5652..8efdbe4813 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-session +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-session @@ -62,6 +62,20 @@ steam_update_stuck() { [ "$steam_updates" -ge 3 ] } +run_gamescope() ( + local status_file child_status compositor_status + status_file=$(mktemp "${XDG_RUNTIME_DIR:-/tmp}/droiddeck-steam-exit.XXXXXX") || exit 73 + trap 'rm -f "$status_file"' EXIT + # Gamescope can return success when its primary child crashed. + BL_STEAM_EXIT_FILE="$status_file" gamescope "$@" + compositor_status=$? + if read -r child_status < "$status_file" && [[ $child_status =~ ^[0-9]{1,3}$ ]] \ + && [ "$child_status" -gt 0 ] && [ "$child_status" -le 255 ]; then + exit "$child_status" + fi + exit "$compositor_status" +) + collect_steam_logs() { [ -n "${BL_DEBUG_DIR:-}" ] || return 0 local steam_root=${XDG_DATA_HOME:-$HOME/.local/share}/Steam @@ -229,6 +243,10 @@ if [ -z "${BL_INSIDE:-}" ]; then echo "== gamescope: mangoapp on ($(command -v mangoapp), config $MANGOHUD_CONFIGFILE)" fi export BL_INSIDE=1 + if [ "$mode" = steam ]; then + run_gamescope "${args[@]}" -- "$0" "$mode" "$@" + exit $? + fi exec gamescope "${args[@]}" -- "$0" "$mode" "$@" fi @@ -917,6 +935,9 @@ BUSCONF [ -n "$_bl_jobs" ] && kill $_bl_jobs 2>/dev/null reap_orphans fi + if [ -n "${BL_STEAM_EXIT_FILE:-}" ]; then + printf '%s\n' "$rc" > "$BL_STEAM_EXIT_FILE" + fi exit "$rc" ;; *) diff --git a/tools/linuxfs/preload/robust.c b/tools/linuxfs/preload/robust.c index 6bbb6f91ea..1be12ba9e5 100644 --- a/tools/linuxfs/preload/robust.c +++ b/tools/linuxfs/preload/robust.c @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -25,8 +26,22 @@ struct bl_open_how { }; static long (*real_syscall)(long, ...); +static void *(*real_dlopen)(const char *, int); static int openat2_missing; +void *dlopen(const char *filename, int flags) { + if (!real_dlopen) { + real_dlopen = (void *(*)(const char *, int))dlsym(RTLD_NEXT, "dlopen"); + } + const char *basename = filename ? strrchr(filename, '/') : NULL; + basename = basename ? basename + 1 : filename; + // Steam's deep binding bypasses the robust-list shim under PRoot. + if (basename && strcmp(basename, "vrclient.so") == 0) { + flags &= ~RTLD_DEEPBIND; + } + return real_dlopen(filename, flags); +} + int bl_fsync_syscall(long number, const long *args, long *ret) __attribute__((visibility("hidden"))); static struct robust_list_head *thread_robust_head(void) { diff --git a/tools/tests/test_steam_exit_status.py b/tools/tests/test_steam_exit_status.py new file mode 100644 index 0000000000..e66341febe --- /dev/null +++ b/tools/tests/test_steam_exit_status.py @@ -0,0 +1,69 @@ +import os +from pathlib import Path +import re +import subprocess +import tempfile +import unittest + + +SESSION = Path(__file__).resolve().parents[1] / 'linuxfs/overlay/usr/local/bin/droiddeck-session' + + +class SteamExitStatusTest(unittest.TestCase): + def run_session(self, child_status, compositor_status=0, report=True, report_value=None, + stale_status=None): + source = SESSION.read_text() + function = re.search(r'^run_gamescope\(\) \(\n.*?^\)\n', source, re.M | re.S) + self.assertIsNotNone(function) + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + stale_file = root / 'droiddeck-steam-exit.stale' + if stale_status is not None: + stale_file.write_text(str(stale_status) + '\n') + compositor = root / 'gamescope' + compositor.write_text('#!/bin/bash\n"$@"\nexit ' + str(compositor_status) + '\n') + compositor.chmod(0o755) + child = root / 'steam' + report_arg = '"$1"' if report_value is None else '"$2"' + report_line = ('printf "%s\\n" ' + report_arg + ' > "$BL_STEAM_EXIT_FILE"\n' + if report else '') + child.write_text('#!/bin/bash\n' + report_line + 'exit "$1"\n') + child.chmod(0o755) + env = dict(os.environ, PATH=str(root) + os.pathsep + os.environ['PATH'], + XDG_RUNTIME_DIR=str(root)) + result = subprocess.run(['bash', '-c', function.group() + '\nrun_gamescope "$@"', + 'session', str(child), str(child_status), + str(report_value) if report_value is not None else ''], env=env, + capture_output=True, text=True, timeout=10) + self.assertEqual(list(root.glob('droiddeck-steam-exit.??????')), []) + if stale_status is not None: + self.assertEqual(stale_file.read_text(), str(stale_status) + '\n') + return result.returncode + + def test_steam_crash_survives_successful_compositor_shutdown(self): + self.assertEqual(self.run_session(139), 139) + + def test_clean_steam_shutdown_remains_success(self): + self.assertEqual(self.run_session(0), 0) + + def test_compositor_failure_remains_visible(self): + self.assertEqual(self.run_session(0, compositor_status=1), 1) + + def test_early_child_failure_preserves_compositor_result(self): + self.assertEqual(self.run_session(127, compositor_status=127, report=False), 127) + + def test_no_report_does_not_invent_a_failure(self): + self.assertEqual(self.run_session(0, report=False), 0) + + def test_invalid_report_cannot_become_a_shell_exit_status(self): + self.assertEqual(self.run_session(999), 0) + + def test_malformed_report_is_ignored(self): + self.assertEqual(self.run_session(0, report_value='not-an-exit-code'), 0) + + def test_stale_marker_does_not_affect_a_new_launch(self): + self.assertEqual(self.run_session(0, report=False, stale_status=139), 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/tools/tests/test_vrclient_loader.py b/tools/tests/test_vrclient_loader.py new file mode 100644 index 0000000000..f468269925 --- /dev/null +++ b/tools/tests/test_vrclient_loader.py @@ -0,0 +1,57 @@ +from pathlib import Path +import shutil +import subprocess +import tempfile +import unittest + + +@unittest.skipUnless(shutil.which("gcc"), "needs gcc on a Linux host") +class VrclientLoaderTest(unittest.TestCase): + def test_vrclient_keeps_syscall_interposition_without_changing_other_dsos(self): + with tempfile.TemporaryDirectory() as directory: + root = Path(directory) + module = root / "module.c" + module.write_text(r""" +#include +#include +#include +#include +long syscall(long number, ...) { (void)number; errno = ENOSYS; return -1; } +int probe(void) { + void *head = NULL; size_t length = 0; + return syscall(SYS_get_robust_list, 0L, &head, &length) == 0 && head && length > 0; +} +""") + subprocess.run(["gcc", "-shared", "-fPIC", "-O2", str(module), "-o", + str(root / "vrclient.so")], check=True) + shutil.copy(root / "vrclient.so", root / "ordinary.so") + robust = Path(__file__).resolve().parents[1] / "linuxfs/preload/robust.c" + harness = root / "loader.c" + harness.write_text(r""" +#define _GNU_SOURCE +#include +#include +int bl_fsync_syscall(long n, const long *a, long *r) { (void)n; (void)a; (void)r; return 0; } +#include """ + '"' + robust.as_posix() + '"' + r""" +int main(int argc, char **argv) { + assert(argc == 3); + void *ordinary = dlopen(argv[1], RTLD_NOW | RTLD_DEEPBIND); + assert(ordinary); + int (*probe)(void) = dlsym(ordinary, "probe"); + assert(probe && probe() == 0); + dlclose(ordinary); + void *vr = dlopen(argv[2], RTLD_NOW | RTLD_DEEPBIND); + assert(vr); + probe = dlsym(vr, "probe"); + assert(probe && probe() == 1); + dlclose(vr); +} +""") + subprocess.run(["gcc", "-O2", "-rdynamic", "-pthread", str(harness), + "-ldl", "-o", str(root / "loader")], check=True) + subprocess.run([str(root / "loader"), str(root / "ordinary.so"), + str(root / "vrclient.so")], check=True, timeout=10) + + +if __name__ == "__main__": + unittest.main()