diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index a71a8edd94..53c50a2bfb 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -1099,6 +1099,7 @@ def newest_foreign_backup(backups): def restore_compatible_system_dlls(prefix, proton, env=None): restored = [] + game = (env or {}).get("STEAM_COMPAT_INSTALL_PATH") for folder, name, path, source, machines, builtins, backups in prefix_system_dlls(prefix, proton, env): if not path.is_symlink() or not path.is_file(): continue @@ -1111,7 +1112,7 @@ def restore_compatible_system_dlls(prefix, proton, env=None): if backup is None or backup.is_symlink() or not backup.is_file(): continue machine = pe_machine(backup) - if machine is None or not PE["machine_compatible"](backup, machines, machine, builtins, name): + if machine is None or not PE["machine_compatible"](backup, machines, machine, builtins, name, game): continue try: os.replace(backup, path) @@ -1123,11 +1124,12 @@ def restore_compatible_system_dlls(prefix, proton, env=None): def repair_foreign_system_dlls(prefix, proton, env=None): repaired = [] + game = (env or {}).get("STEAM_COMPAT_INSTALL_PATH") for folder, name, path, source, machines, builtins, backups in prefix_system_dlls(prefix, proton, env): if path.is_symlink() or not path.is_file(): continue machine = pe_machine(path) - if machine is None or PE["machine_compatible"](path, machines, machine, builtins): + if machine is None or PE["machine_compatible"](path, machines, machine, builtins, game=game): continue link = path.parent / (name + ".dd-new") backup = path.parent / (name + FOREIGN_SUFFIX) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents index e196f6fa33..62b705effe 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents @@ -479,7 +479,7 @@ def save_state(compat, state): os.replace(temp, compat / STATE) -def foreign_component(rel, path, machines, dist=None): +def foreign_component(rel, path, machines, dist=None, game=None): parts = Path(rel).parts folder = parts[2] if len(parts) > 2 and parts[:2] == ("drive_c", "windows") else None if not machines or folder not in PE_MACHINES or not rel.lower().endswith(".dll"): @@ -487,7 +487,7 @@ def foreign_component(rel, path, machines, dist=None): machine = pe_machine(path) libdir, compatible = machines[folder] builtins = dist / libdir if dist else None - return machine is not None and not machine_compatible(path, compatible, machine, builtins) + return machine is not None and not machine_compatible(path, compatible, machine, builtins, game=game) def pending_for(app_id, pending=PENDING): @@ -549,6 +549,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING placed_stems = set() machines = PE["proton_pe_machines"](proton, env) if proton else None dist = Path(proton).parent / "files/lib/wine" if proton else None + game = (env or {}).get("STEAM_COMPAT_INSTALL_PATH") saved_overrides = state.get("overrides") if isinstance(state.get("overrides"), dict) else {} saved_requires = state.get("requires") if isinstance(state.get("requires"), dict) else {} recorded = [c for c in state.get("components", []) if isinstance(c, str) and valid_id(c)] @@ -588,7 +589,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING kept_requires[cid] = list(requires) values += [(cid, value) for value in registry] for rel, src in files: - foreign = foreign_component(rel, src, machines, dist) + foreign = foreign_component(rel, src, machines, dist, game) if foreign: skipped.add(Path(rel).stem.lower()) changed = False @@ -609,7 +610,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING path = existing(pfx / rel) saved = compat / BACKUP / rel if saved.is_file(): - if foreign_component(rel, saved, machines, dist): + if foreign_component(rel, saved, machines, dist, game): if path is not None and not path.is_symlink(): path.unlink() continue diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe index c4f4469b6c..46643dc42f 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe @@ -1,8 +1,12 @@ #!/usr/bin/python3 +import mmap import os +import re from pathlib import Path +VC_RUNTIME = re.compile(r"(msvc[pr]\d.*|vcruntime\d.*|vcomp\d*|vcamp\d.*|vccorlib\d.*|concrt\d.*|mfcm?\d.*)\.dll", + re.IGNORECASE) PE_MACHINES = { "system32": ("aarch64-windows", frozenset((0xAA64, 0xA641))), "syswow64": ("i386-windows", frozenset((0x14C,))), @@ -79,8 +83,8 @@ def pe_has_arm64ec_metadata(path): def pe_imports(path): try: with open(path, "rb") as source: - image = source.read() - except OSError: + image = mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ) + except (OSError, ValueError): return None try: if image[:2] != b"MZ": @@ -137,9 +141,13 @@ def pe_imports(path): return names except (IndexError, ValueError): return None + finally: + image.close() PROGRAM_IMPORTS = {} +GAME_IMPORTS = {} +GAME_DEPTH = 4 def program_imports(builtins): @@ -166,13 +174,33 @@ def program_imports(builtins): return PROGRAM_IMPORTS[key] -def machine_compatible(path, machines, machine=None, builtins=None, name=None): +def game_arm64ec_imports(folder): + key = str(folder) + if key not in GAME_IMPORTS: + names = set() + root = os.path.realpath(folder) + for top, dirs, files in os.walk(root): + if os.path.relpath(top, root).count(os.sep) >= GAME_DEPTH: + dirs[:] = [] + for entry in files: + path = os.path.join(top, entry) + if (entry.lower().endswith((".exe", ".dll")) and pe_machine(path) == 0x8664 + and pe_has_arm64ec_metadata(path)): + names |= pe_imports(path) or set() + GAME_IMPORTS[key] = frozenset(names) + return GAME_IMPORTS[key] + + +def machine_compatible(path, machines, machine=None, builtins=None, name=None, game=None): machine = pe_machine(path) if machine is None else machine if machine == 0x8664 and 0xA641 in machines: if pe_has_arm64ec_metadata(path): return True imported = program_imports(builtins) if builtins is not None else None - return imported is not None and (name or Path(path).name).lower() not in imported + name = (name or Path(path).name).lower() + if game and VC_RUNTIME.fullmatch(name) and name in game_arm64ec_imports(game): + return False + return imported is not None and name not in imported if machine == 0x8664 and pe_has_arm64ec_metadata(path): return False return machine in machines diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index 830b7f2e5d..b1566040fb 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -3499,6 +3499,16 @@ def program_image(machine, imports): return bytes(image) +def arm64ec_program_image(imports): + image = bytearray(program_image(0x8664, imports)) + directory = 68 + 20 + 112 + 10 * 8 + image[directory:directory + 4] = (0x1100).to_bytes(4, "little") + image[directory + 4:directory + 8] = (0xD0).to_bytes(4, "little") + image[0x300:0x304] = (0xD0).to_bytes(4, "little") + image[0x3C8:0x3D0] = (0x10000000).to_bytes(8, "little") + return bytes(image) + + def bytes_path(folder, data): path = folder / "image.exe" path.write_bytes(data) @@ -3573,6 +3583,63 @@ class ForeignSystemDllTest(unittest.TestCase): self.assertFalse((windows / "system32" / name).is_symlink()) self.assertEqual(0x8664, MODULE["pe_machine"](windows / "system32" / name)) + def arm64ec_game(self, tmp, shipping): + game = Path(tmp) / "common/Game" + binaries = game / "Game/Binaries/Win64" + binaries.mkdir(parents=True) + (game / "Game.exe").write_bytes(program_image(0x8664, ["kernel32.dll"])) + (binaries / "Game-Win64-Shipping.exe").write_bytes(shipping) + (binaries / "plugin.dll").write_bytes(program_image(0x8664, ["concrt140.dll"])) + return {"STEAM_COMPAT_INSTALL_PATH": str(game)} + + def visual_cpp_prefix(self, tmp): + proton, compat, windows = self.prefix(tmp) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + for name in ("vcruntime140.dll", "msvcp140.dll", "concrt140.dll", "vulkan-1.dll"): + (builtins / name).write_bytes(program_image(0xAA64, [])) + (windows / "system32" / name.upper()).write_bytes(pe_image(0x8664)) + return proton, compat, windows + + def test_x64_runtime_an_arm64ec_game_image_imports_is_replaced_by_the_builtin(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.visual_cpp_prefix(tmp) + env = self.arm64ec_game(tmp, arm64ec_program_image(["VCRUNTIME140.dll", "MSVCP140.dll", "vulkan-1.dll"])) + fixed = MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env) + self.assertEqual(["system32/MSVCP140.DLL (machine 0x8664)", "system32/VCRUNTIME140.DLL (machine 0x8664)"], fixed) + for name in ("CONCRT140.DLL", "VULKAN-1.DLL"): + self.assertFalse((windows / "system32" / name).is_symlink()) + self.assertEqual(0x8664, MODULE["pe_machine"](windows / "system32" / name)) + self.assertEqual([], MODULE["restore_compatible_system_dlls"](str(compat), str(proton), env)) + self.assertEqual([], MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env)) + restored = MODULE["restore_compatible_system_dlls"](str(compat), str(proton)) + self.assertEqual(2, len(restored)) + self.assertFalse((windows / "system32/VCRUNTIME140.DLL").is_symlink()) + + def test_x64_runtime_stays_for_x64_games_and_launches_without_a_game_folder(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.visual_cpp_prefix(tmp) + env = self.arm64ec_game(tmp, program_image(0x8664, ["VCRUNTIME140.dll", "MSVCP140.dll"])) + self.assertEqual([], MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env)) + self.assertEqual([], MODULE["repair_foreign_system_dlls"](str(compat), str(proton), {})) + self.assertEqual([], MODULE["repair_foreign_system_dlls"]( + str(compat), str(proton), {"STEAM_COMPAT_INSTALL_PATH": str(Path(tmp) / "missing")})) + for name in ("VCRUNTIME140.DLL", "MSVCP140.DLL"): + self.assertFalse((windows / "system32" / name).is_symlink()) + + def test_arm64ec_images_below_the_scanned_depth_are_ignored(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.visual_cpp_prefix(tmp) + game = Path(tmp) / "common/Deep" + deep = game.joinpath(*("d%d" % level for level in range(MODULE["PE"]["GAME_DEPTH"] + 2))) + deep.mkdir(parents=True) + (deep / "tool.exe").write_bytes(arm64ec_program_image(["VCRUNTIME140.dll"])) + env = {"STEAM_COMPAT_INSTALL_PATH": str(game)} + self.assertEqual([], MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env)) + (deep.parent / "tool.exe").write_bytes(arm64ec_program_image(["VCRUNTIME140.dll"])) + MODULE["PE"]["GAME_IMPORTS"].clear() + self.assertEqual(["system32/VCRUNTIME140.DLL (machine 0x8664)"], + MODULE["repair_foreign_system_dlls"](str(compat), str(proton), env)) + def test_repaired_dll_proton_programs_do_not_import_gets_its_native_copy_back(self): with tempfile.TemporaryDirectory() as tmp: proton, compat, windows = self.prefix(tmp) diff --git a/tools/tests/test_wincomponents.py b/tools/tests/test_wincomponents.py index 54e5fed490..1ed6bb0d2d 100644 --- a/tools/tests/test_wincomponents.py +++ b/tools/tests/test_wincomponents.py @@ -6,6 +6,8 @@ import unittest from pathlib import Path from unittest import mock +from tools.tests.test_game_environment import arm64ec_program_image + BIN = Path(__file__).resolve().parents[1] / "linuxfs/overlay/usr/local/bin" MODULE = runpy.run_path(str(BIN / "droiddeck-wincomponents")) GAME_ENV = runpy.run_path(str(BIN / "droiddeck-game-env")) @@ -169,6 +171,22 @@ class WinComponentsTest(unittest.TestCase): self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/msvcp140.dll"), 0x8664) self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/vcruntime140.dll"), 0x8664) + def test_x64_runtime_an_arm64ec_game_imports_is_not_placed(self): + proton = self.proton() + game = Path(self.tmp.name) / "common/Game/Game/Binaries/Win64" + game.mkdir(parents=True) + (game / "Game-Win64-Shippingarm64ec.exe").write_bytes(arm64ec_program_image(["MSVCP140.dll", "VCRUNTIME140.dll"])) + names = ["concrt140", "msvcp140", "vcruntime140"] + self.component("vc", names, {"system32/%s.dll" % name: self.pe_image(0x8664) for name in names}) + self.pick("vc") + env = {"STEAM_COMPAT_INSTALL_PATH": str(game.parents[2])} + for _ in range(2): + placed, _ = self.apply(proton, env) + self.assertEqual(placed, ["concrt140"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/concrt140.dll"), 0x8664) + self.assertFalse((self.pfx / "system32/msvcp140.dll").exists()) + self.assertFalse((self.pfx / "system32/vcruntime140.dll").exists()) + def test_deselected_component_restores_the_saved_x64_dll(self): proton = self.proton() self.pfx.joinpath("system32").mkdir(parents=True)