import os from pathlib import Path import shutil import subprocess import tempfile import unittest STEAMSHM = Path(__file__).resolve().parents[1] / "bionic-runtime/steamshm.c" SOURCES = { "libsystemxml.so": ("int xml_copy(void) { return 1; }\n", []), "libvendor.so": ("int xml_copy(void);\nint vendor_xml_copy(void) { return xml_copy(); }\n", ["systemxml"]), "libframework.so": ("int framework(void) { return 0; }\n", ["vendor"]), "libsteam.so": ("int xml_copy(void) { return 2; }\nint CreateInterface(void) { return 0; }\n", ["framework"]), } LOADER_PRELUDE = r""" #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #define say(...) fprintf(stderr, __VA_ARGS__) """ def steamclient_loader(): source = STEAMSHM.read_text() return LOADER_PRELUDE + source[source.index("static bool read_at("):] PROGRAM = r""" #include #include int main(void) { if (!dlopen("libsteam.so", RTLD_NOW)) { puts(dlerror()); return 1; } void *vendor = dlopen("libvendor.so", RTLD_NOW | RTLD_NOLOAD); int (*copy)(void) = vendor ? (int (*)(void))dlsym(vendor, "vendor_xml_copy") : 0; if (!copy) return 1; printf("%d\n", copy()); return 0; } """ @unittest.skipUnless(shutil.which("cc"), "needs a C compiler") class SteamClientNeedsTest(unittest.TestCase): def setUp(self): self.dir = Path(tempfile.mkdtemp()) self.addCleanup(shutil.rmtree, self.dir, True) for name, (code, needs) in SOURCES.items(): src = self.dir / (name + ".c") src.write_text(code) subprocess.run(["cc", "-shared", "-fPIC", "-o", str(self.dir / name), str(src), "-L", str(self.dir), "-Wl,--no-as-needed", *("-l" + n for n in needs)], check=True) (self.dir / "main.c").write_text(PROGRAM) subprocess.run(["cc", "-o", str(self.dir / "main"), str(self.dir / "main.c"), "-ldl"], check=True) loader = self.dir / "loader.c" loader.write_text(steamclient_loader()) self.preload = self.dir / "libsteamshm.so" subprocess.run(["cc", "-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-fvisibility=hidden", str(loader), "-ldl", "-o", str(self.preload)], check=True) def vendor_binding(self, preload=True, steam=None): env = {k: v for k, v in os.environ.items() if k not in ("LD_PRELOAD", "WINESTEAMCLIENTPATH64")} env["LD_LIBRARY_PATH"] = str(self.dir) if steam: env["WINESTEAMCLIENTPATH64"] = steam if preload: env["LD_PRELOAD"] = str(self.preload) run = subprocess.run([str(self.dir / "main")], env=env, capture_output=True, text=True, check=True) return run.stdout.strip() def test_without_the_preload_a_framework_library_binds_to_steams_copy(self): self.assertEqual(self.vendor_binding(preload=False), "2") def test_the_libraries_steam_needs_link_to_their_own_copy_first(self): self.assertEqual(self.vendor_binding(steam=str(self.dir / "libsteam.so")), "1") def test_without_a_steam_library_path_nothing_is_loaded_early(self): self.assertEqual(self.vendor_binding(), "2") def test_a_missing_or_foreign_steam_library_is_ignored(self): self.assertEqual(self.vendor_binding(steam=str(self.dir / "missing.so")), "2") self.assertEqual(self.vendor_binding(steam=str(self.dir / "main.c")), "2") if __name__ == "__main__": unittest.main()