diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env index 3db9d1a649..c243a1afd2 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-game-env @@ -942,14 +942,11 @@ PE = runpy.run_path(str(Path(__file__).resolve().parent / "droiddeck-wine-pe")) pe_machine = PE["pe_machine"] -def repair_foreign_system_dlls(prefix, proton, env=None): +def prefix_system_dlls(prefix, proton, env=None): dist = Path(proton).parent / "files/lib/wine" windows = Path(prefix) / "pfx/drive_c/windows" machines_by_folder = PE["proton_pe_machines"](proton, env or {}) - if machines_by_folder is None: - return [] - repaired = [] - for folder, (libdir, machines) in machines_by_folder.items(): + for folder, (libdir, machines) in (machines_by_folder or {}).items(): builtins = dist / libdir try: builtin_names = {name.lower(): name for name in os.listdir(builtins)} @@ -959,35 +956,83 @@ def repair_foreign_system_dlls(prefix, proton, env=None): names = sorted(os.listdir(windows / folder)) except OSError: continue + # Both restoration and repair may examine many DLLs. Index the backup + # names from this directory listing instead of listing it for each DLL. + backups = {} + for entry in names: + stem, marker, suffix = entry.partition(FOREIGN_SUFFIX) + if not marker or not stem.lower().endswith(".dll"): + continue + if not suffix: + serial = 0 + elif suffix.startswith(".") and suffix[1:].isdigit(): + serial = int(suffix[1:]) + else: + continue + backups.setdefault(stem, []).append((serial, windows / folder / entry)) + for copies in backups.values(): + copies.sort() for name in names: - path = windows / folder / name builtin_name = builtin_names.get(name.lower()) source = builtins / builtin_name if builtin_name else builtins / name - if path.is_symlink() or not name.lower().endswith(".dll") or not source.is_file() or not path.is_file(): + if name.lower().endswith(".dll") and source.is_file(): + yield folder, name, windows / folder / name, source, machines, builtins, backups.get(name, ()) + + +def newest_foreign_backup(backups): + return backups[-1][1] if backups else None + + +def restore_compatible_system_dlls(prefix, proton, env=None): + restored = [] + 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 + try: + if not os.path.samefile(path, source): continue - machine = pe_machine(path) - if machine is None or PE["machine_compatible"](path, machines, machine): - continue - link = windows / folder / (name + ".bl-new") - backup = windows / folder / (name + FOREIGN_SUFFIX) - serial = 1 - while os.path.lexists(backup): - backup = windows / folder / (name + FOREIGN_SUFFIX + ".%d" % serial) - serial += 1 + except OSError: + continue + backup = newest_foreign_backup(backups) + 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): + continue + try: + os.replace(backup, path) + except OSError: + continue + restored.append("%s/%s (machine 0x%x)" % (folder, name, machine)) + return restored + + +def repair_foreign_system_dlls(prefix, proton, env=None): + repaired = [] + 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): + continue + link = path.parent / (name + ".bl-new") + backup = path.parent / (name + FOREIGN_SUFFIX) + if backups: + backup = path.parent / (name + FOREIGN_SUFFIX + ".%d" % (backups[-1][0] + 1)) + try: + os.symlink(os.path.relpath(source, path.parent), link) + os.replace(path, backup) try: - os.symlink(os.path.relpath(source, path.parent), link) - os.replace(path, backup) - try: - os.replace(link, path) - except OSError: - if not os.path.lexists(path) and os.path.lexists(backup): - os.replace(backup, path) - raise + os.replace(link, path) except OSError: - if link.is_symlink(): - link.unlink() - continue - repaired.append("%s/%s (machine 0x%x)" % (folder, name, machine)) + if not os.path.lexists(path) and os.path.lexists(backup): + os.replace(backup, path) + raise + except OSError: + if link.is_symlink(): + link.unlink() + continue + repaired.append("%s/%s (machine 0x%x)" % (folder, name, machine)) return repaired @@ -1317,10 +1362,14 @@ def main(): components = win_components(prefix_path, app_id, command[0], effective_env) if prefix_path and command: try: + restored = restore_compatible_system_dlls(prefix_path, command[0], effective_env) fixed = repair_foreign_system_dlls(prefix_path, command[0], effective_env) except OSError as error: - fixed = [] + restored = fixed = [] print("droiddeck-game-env: prefix system DLL check skipped (%s)" % error, file=sys.stderr) + if restored: + print("droiddeck-game-env: restored system DLLs this Proton's own programs do not load: %s" % ", ".join(restored), + file=sys.stderr) if fixed: print("droiddeck-game-env: replaced foreign-architecture system DLLs with this Proton's own: %s" % ", ".join(fixed), file=sys.stderr) diff --git a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents index dde2d6e6be..e196f6fa33 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wincomponents @@ -479,13 +479,15 @@ def save_state(compat, state): os.replace(temp, compat / STATE) -def foreign_component(rel, path, machines): +def foreign_component(rel, path, machines, dist=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"): return False machine = pe_machine(path) - return machine is not None and not machine_compatible(path, machines[folder][1], machine) + libdir, compatible = machines[folder] + builtins = dist / libdir if dist else None + return machine is not None and not machine_compatible(path, compatible, machine, builtins) def pending_for(app_id, pending=PENDING): @@ -546,6 +548,7 @@ def apply(compat_path, app_id, selection=SELECTION, store=STORE, pending=PENDING skipped = set() 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 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)] @@ -585,7 +588,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) + foreign = foreign_component(rel, src, machines, dist) if foreign: skipped.add(Path(rel).stem.lower()) changed = False @@ -606,7 +609,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): + if foreign_component(rel, saved, machines, dist): 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 65c3446a2a..c4f4469b6c 100755 --- a/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe +++ b/tools/linuxfs/overlay/usr/local/bin/droiddeck-wine-pe @@ -1,5 +1,5 @@ #!/usr/bin/python3 -import re +import os from pathlib import Path @@ -76,18 +76,105 @@ def pe_has_arm64ec_metadata(path): return False -# Only x64 code and members of the same runtime family import these, so an x64 copy loads in an -# ARM64EC prefix, and games check for a newer version than Wine's builtins report. -VC_RUNTIME = re.compile(r"(msvc[pr]\d.*|vcruntime\d.*|vcomp\d*|vcamp\d.*|vccorlib\d.*|concrt\d.*|mfcm?\d.*)\.dll", - re.IGNORECASE) +def pe_imports(path): + try: + with open(path, "rb") as source: + image = source.read() + except OSError: + return None + try: + if image[:2] != b"MZ": + return None + pe_offset = int.from_bytes(image[60:64], "little") + if image[pe_offset:pe_offset + 4] != b"PE\0\0": + return None + section_count = int.from_bytes(image[pe_offset + 6:pe_offset + 8], "little") + optional_size = int.from_bytes(image[pe_offset + 20:pe_offset + 22], "little") + optional = pe_offset + 24 + magic = int.from_bytes(image[optional:optional + 2], "little") + directories = optional + (112 if magic == 0x20B else 96) + if directories > len(image): + return None + directory_count = int.from_bytes(image[directories - 4:directories], "little") + sections = [] + for index in range(min(section_count, 96)): + entry = optional + optional_size + index * 40 + if entry + 40 > len(image): + return None + sections.append((int.from_bytes(image[entry + 12:entry + 16], "little"), + max(int.from_bytes(image[entry + 8:entry + 12], "little"), + int.from_bytes(image[entry + 16:entry + 20], "little")), + int.from_bytes(image[entry + 20:entry + 24], "little"))) + + def offset(rva): + for address, size, raw in sections: + if address <= rva < address + size: + return rva - address + raw + return None + + names = set() + for directory, step, field in ((1, 20, 12), (13, 32, 4)): + if directory >= directory_count: + continue + if directories + directory * 8 + 8 > len(image): + return None + rva = int.from_bytes(image[directories + directory * 8:directories + directory * 8 + 4], "little") + entry = offset(rva) if rva else None + if rva and entry is None: + return None + while entry is not None: + if entry + step > len(image): + return None + name_rva = int.from_bytes(image[entry + field:entry + field + 4], "little") + if not name_rva: + break + name = offset(name_rva) + end = image.find(b"\0", name) if name is not None else -1 + if end < 0: + return None + names.add(image[name:end].decode("ascii", "replace").lower()) + entry += step + return names + except (IndexError, ValueError): + return None -def machine_compatible(path, machines, machine=None): +PROGRAM_IMPORTS = {} + + +def program_imports(builtins): + key = str(builtins) + if key not in PROGRAM_IMPORTS: + try: + files = {name.lower(): Path(builtins) / name for name in os.listdir(builtins)} + except OSError: + files = None + pending = [name for name in files or () if name.endswith(".exe") and pe_machine(files[name]) in (None, 0xAA64)] + seen = set() + while pending and files is not None: + name = pending.pop() + if name in seen: + continue + seen.add(name) + if name in files: + imports = pe_imports(files[name]) + if imports is None: + files = None + else: + pending.extend(imports) + PROGRAM_IMPORTS[key] = None if files is None else frozenset(name for name in seen if name.endswith(".dll")) + return PROGRAM_IMPORTS[key] + + +def machine_compatible(path, machines, machine=None, builtins=None, name=None): machine = pe_machine(path) if machine is None else machine - if machine == 0x8664 and 0xA641 in machines and VC_RUNTIME.fullmatch(Path(path).name): - return True + 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 if machine == 0x8664 and pe_has_arm64ec_metadata(path): - return 0xA641 in machines + return False return machine in machines diff --git a/tools/tests/test_game_environment.py b/tools/tests/test_game_environment.py index 83f05b699a..30d859bc5a 100644 --- a/tools/tests/test_game_environment.py +++ b/tools/tests/test_game_environment.py @@ -811,6 +811,7 @@ class GameEnvironmentTest(unittest.TestCase): "install_scripts_done": lambda scripts, env: False, "scripts_digest": lambda scripts: None, "win_components": lambda *args: seen_components.append(args[3]) or "", + "restore_compatible_system_dlls": lambda *args: [], "repair_foreign_system_dlls": lambda *args: seen_repair.append(args[2]) or [], "gl_vsync": lambda env: False, "game_images": lambda env: None, @@ -2861,6 +2862,39 @@ def arm64ec_pe_image(): return bytes(image) +def program_image(machine, imports): + image = bytearray(0x400) + image[:2] = b"MZ" + image[60:64] = (64).to_bytes(4, "little") + image[64:68] = b"PE\0\0" + coff = 68 + image[coff:coff + 2] = machine.to_bytes(2, "little") + image[coff + 2:coff + 4] = (1).to_bytes(2, "little") + image[coff + 16:coff + 18] = (240).to_bytes(2, "little") + optional = coff + 20 + image[optional:optional + 2] = (0x20B).to_bytes(2, "little") + image[optional + 108:optional + 112] = (16).to_bytes(4, "little") + image[optional + 120:optional + 124] = (0x1000).to_bytes(4, "little") + image[optional + 124:optional + 128] = (20 * (len(imports) + 1)).to_bytes(4, "little") + section = optional + 240 + image[section + 8:section + 12] = (0x200).to_bytes(4, "little") + image[section + 12:section + 16] = (0x1000).to_bytes(4, "little") + image[section + 16:section + 20] = (0x200).to_bytes(4, "little") + image[section + 20:section + 24] = (0x200).to_bytes(4, "little") + name = 20 * (len(imports) + 1) + for index, dll in enumerate(imports): + image[0x200 + index * 20 + 12:0x200 + index * 20 + 16] = (0x1000 + name).to_bytes(4, "little") + image[0x200 + name:0x200 + name + len(dll)] = dll.encode() + name += len(dll) + 1 + return bytes(image) + + +def bytes_path(folder, data): + path = folder / "image.exe" + path.write_bytes(data) + return path + + class ForeignSystemDllTest(unittest.TestCase): def prefix(self, tmp, arm=True): proton = Path(tmp) / "Proton" @@ -2872,7 +2906,8 @@ class ForeignSystemDllTest(unittest.TestCase): for libdir, machine in (("aarch64-windows", 0xAA64), ("i386-windows", 0x14C)): (dist / libdir).mkdir(parents=True) for name in ("ucrtbase.dll", "rpcrt4.dll"): - (dist / libdir / name).write_bytes(pe_image(machine)) + (dist / libdir / name).write_bytes(program_image(machine, [])) + (dist / "aarch64-windows/wineboot.exe").write_bytes(program_image(0xAA64, ["UCRTBASE.dll", "rpcrt4.dll"])) windows = Path(tmp) / "compat/pfx/drive_c/windows" for folder in ("system32", "syswow64"): (windows / folder).mkdir(parents=True) @@ -2912,16 +2947,148 @@ class ForeignSystemDllTest(unittest.TestCase): self.assertFalse((system32 / "ucrtbase.dll").is_symlink()) self.assertEqual(0x8664, MODULE["pe_machine"](system32 / "userlib.dll")) - def test_x64_visual_cpp_runtime_in_arm64_system32_is_kept(self): + def test_x64_dlls_proton_programs_do_not_import_are_kept_in_arm64_system32(self): with tempfile.TemporaryDirectory() as tmp: proton, compat, windows = self.prefix(tmp) builtins = proton.parent / "files/lib/wine/aarch64-windows" - for name in ("msvcp140.dll", "VCRUNTIME140.dll", "msvcp_win.dll"): - (builtins / name.lower()).write_bytes(pe_image(0xAA64)) + (builtins / "explorer.exe").write_bytes(program_image(0xAA64, ["shell32.dll"])) + (builtins / "shell32.dll").write_bytes(program_image(0xAA64, ["msvcp_win.dll"])) + names = ("vulkan-1.dll", "msvcp140.dll", "VCRUNTIME140.dll", "concrt140.dll", "d3dcompiler_43.dll", + "winhttp.dll", "msvcp_win.dll") + for name in names: + (builtins / name.lower()).write_bytes(program_image(0xAA64, [])) (windows / "system32" / name).write_bytes(pe_image(0x8664)) self.assertEqual(["system32/msvcp_win.dll (machine 0x8664)"], self.run_repair(compat, proton)) - self.assertFalse((windows / "system32/msvcp140.dll").is_symlink()) - self.assertFalse((windows / "system32/VCRUNTIME140.dll").is_symlink()) + for name in names[:-1]: + self.assertFalse((windows / "system32" / name).is_symlink()) + self.assertEqual(0x8664, MODULE["pe_machine"](windows / "system32" / name)) + + 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) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + (builtins / "vulkan-1.dll").write_bytes(pe_image(0xAA64)) + system32 = windows / "system32" + for name in ("vulkan-1.dll", "ucrtbase.dll"): + (system32 / name).symlink_to(os.path.relpath(builtins / name, system32)) + (system32 / (name + ".foreign-arch")).write_bytes(pe_image(0x8664)) + (system32 / "vulkan-1.dll.foreign-arch.1").write_bytes(pe_image(0x8664) + b"newest") + restore = MODULE["restore_compatible_system_dlls"] + self.assertEqual(["system32/vulkan-1.dll (machine 0x8664)"], restore(str(compat), str(proton))) + self.assertFalse((system32 / "vulkan-1.dll").is_symlink()) + self.assertTrue((system32 / "vulkan-1.dll").read_bytes().endswith(b"newest")) + self.assertFalse((system32 / "vulkan-1.dll.foreign-arch.1").exists()) + self.assertTrue((system32 / "ucrtbase.dll").is_symlink()) + self.assertEqual([], restore(str(compat), str(proton))) + self.assertEqual([], self.run_repair(compat, proton)) + + def test_prefix_backup_directory_is_listed_once_per_pass(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + system32 = windows / "system32" + for index in range(12): + name = "native_%02d.dll" % index + (builtins / name).write_bytes(pe_image(0xAA64)) + (system32 / name).symlink_to(os.path.relpath(builtins / name, system32)) + (system32 / (name + ".foreign-arch")).write_bytes(pe_image(0x8664)) + (system32 / "ucrtbase.dll").write_bytes(pe_image(0x8664)) + original_listdir = os.listdir + visited = [] + + def counted_listdir(folder): + if Path(folder) == system32: + visited.append(folder) + return original_listdir(folder) + + with mock.patch("os.listdir", side_effect=counted_listdir): + restored = MODULE["restore_compatible_system_dlls"](str(compat), str(proton)) + fixed = self.run_repair(compat, proton) + self.assertEqual(12, len(restored)) + self.assertEqual(["system32/ucrtbase.dll (machine 0x8664)"], fixed) + # One system32 listing for restore and one for repair, regardless + # of the number of DLLs and backup files in the prefix. + self.assertEqual(2, len(visited)) + + def test_restore_takes_the_highest_backup_serial_across_gaps(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + (builtins / "vulkan-1.dll").write_bytes(pe_image(0xAA64)) + system32 = windows / "system32" + (system32 / "vulkan-1.dll").symlink_to(os.path.relpath(builtins / "vulkan-1.dll", system32)) + (system32 / "vulkan-1.dll.foreign-arch.2").write_bytes(pe_image(0x8664) + b"two") + (system32 / "vulkan-1.dll.foreign-arch.10").write_bytes(pe_image(0x8664) + b"ten") + self.assertEqual(["system32/vulkan-1.dll (machine 0x8664)"], + MODULE["restore_compatible_system_dlls"](str(compat), str(proton))) + self.assertTrue((system32 / "vulkan-1.dll").read_bytes().endswith(b"ten")) + + def test_unreadable_proton_program_keeps_the_conservative_rule(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + (builtins / "broken.exe").mkdir() + (builtins / "vulkan-1.dll").write_bytes(pe_image(0xAA64)) + system32 = windows / "system32" + (system32 / "concrt140.dll").write_bytes(pe_image(0x8664)) + (builtins / "concrt140.dll").write_bytes(pe_image(0xAA64)) + (system32 / "vulkan-1.dll").symlink_to(os.path.relpath(builtins / "vulkan-1.dll", system32)) + (system32 / "vulkan-1.dll.foreign-arch").write_bytes(pe_image(0x8664)) + self.assertEqual([], MODULE["restore_compatible_system_dlls"](str(compat), str(proton))) + self.assertTrue((system32 / "vulkan-1.dll").is_symlink()) + self.assertEqual(["system32/concrt140.dll (machine 0x8664)"], self.run_repair(compat, proton)) + + def test_new_backup_goes_above_the_highest_existing_serial(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + system32 = windows / "system32" + (system32 / "ucrtbase.dll").write_bytes(pe_image(0x8664) + b"newest") + (system32 / "ucrtbase.dll.foreign-arch").write_bytes(pe_image(0x8664)) + (system32 / "ucrtbase.dll.foreign-arch.2").write_bytes(pe_image(0x8664)) + self.assertEqual(["system32/ucrtbase.dll (machine 0x8664)"], self.run_repair(compat, proton)) + self.assertTrue((system32 / "ucrtbase.dll.foreign-arch.3").read_bytes().endswith(b"newest")) + self.assertFalse((system32 / "ucrtbase.dll.foreign-arch.1").exists()) + + def test_truncated_import_table_keeps_the_conservative_rule(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + (builtins / "cut.exe").write_bytes(program_image(0xAA64, ["concrt140.dll"])[:0x20A]) + self.assertIsNone(MODULE["PE"]["pe_imports"](builtins / "cut.exe")) + self.assertIsNone(MODULE["PE"]["pe_imports"](bytes_path(Path(tmp), program_image(0xAA64, ["a.dll"])[:200]))) + self.assertEqual(set(), MODULE["PE"]["pe_imports"](bytes_path(Path(tmp), program_image(0xAA64, [])))) + moved = bytearray(program_image(0xAA64, ["concrt140.dll"])) + moved[208:212] = (0x3000).to_bytes(4, "little") + self.assertIsNone(MODULE["PE"]["pe_imports"](bytes_path(Path(tmp), moved))) + (builtins / "concrt140.dll").write_bytes(pe_image(0xAA64)) + (windows / "system32/concrt140.dll").write_bytes(pe_image(0x8664)) + self.assertEqual(["system32/concrt140.dll (machine 0x8664)"], self.run_repair(compat, proton)) + + def test_restore_leaves_other_links_and_x64_proton_alone(self): + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp) + builtins = proton.parent / "files/lib/wine/aarch64-windows" + (builtins / "vulkan-1.dll").write_bytes(pe_image(0xAA64)) + other = Path(tmp) / "other-vulkan-1.dll" + other.write_bytes(pe_image(0xAA64)) + system32 = windows / "system32" + (system32 / "vulkan-1.dll").symlink_to(other) + (system32 / "vulkan-1.dll.foreign-arch").write_bytes(pe_image(0x8664)) + (system32 / "rpcrt4.dll").symlink_to(os.path.relpath(builtins / "rpcrt4.dll", system32)) + (system32 / "rpcrt4.dll.foreign-arch").write_bytes(pe_image(0x14C)) + self.assertEqual([], MODULE["restore_compatible_system_dlls"](str(compat), str(proton))) + self.assertTrue((system32 / "vulkan-1.dll").is_symlink()) + self.assertTrue((system32 / "rpcrt4.dll").is_symlink()) + with tempfile.TemporaryDirectory() as tmp: + proton, compat, windows = self.prefix(tmp, arm=False) + dist = proton.parent / "files/lib/wine/x86_64-windows" + dist.mkdir(parents=True) + (dist / "vulkan-1.dll").write_bytes(pe_image(0x8664)) + system32 = windows / "system32" + (system32 / "vulkan-1.dll").symlink_to(os.path.relpath(dist / "vulkan-1.dll", system32)) + (system32 / "vulkan-1.dll.foreign-arch").write_bytes(pe_image(0xAA64)) + self.assertEqual([], MODULE["restore_compatible_system_dlls"](str(compat), str(proton))) + self.assertTrue((system32 / "vulkan-1.dll").is_symlink()) def test_final_arm64ec_image_is_not_repaired_as_a_foreign_x64_dll(self): with tempfile.TemporaryDirectory() as tmp: diff --git a/tools/tests/test_wincomponents.py b/tools/tests/test_wincomponents.py index 7423fb2bbf..54e5fed490 100644 --- a/tools/tests/test_wincomponents.py +++ b/tools/tests/test_wincomponents.py @@ -41,8 +41,37 @@ class WinComponentsTest(unittest.TestCase): def proton(self, arm=True): root = Path(self.tmp.name) / ("arm-proton" if arm else "x64-proton") (root / "files/lib/wine" / ("aarch64-unix" if arm else "x86_64-unix")).mkdir(parents=True) + builtins = root / "files/lib/wine/aarch64-windows" + builtins.mkdir(parents=True) + (builtins / "wineboot.exe").write_bytes(self.program_image(0xAA64, ["ucrtbase.dll"])) + (builtins / "steam.exe").write_bytes(self.program_image(0x8664, ["vulkan-1.dll"])) + (builtins / "d3dx9_43.dll").write_bytes(self.program_image(0xAA64, ["d3dcompiler_43.dll"])) return root / "proton" + def program_image(self, machine, imports): + image = bytearray(0x400) + image[:2] = b"MZ" + image[60:64] = (64).to_bytes(4, "little") + image[64:68] = b"PE\0\0" + image[68:70] = machine.to_bytes(2, "little") + image[70:72] = (1).to_bytes(2, "little") + image[84:86] = (240).to_bytes(2, "little") + image[88:90] = (0x20B).to_bytes(2, "little") + image[196:200] = (16).to_bytes(4, "little") + image[208:212] = (0x1000).to_bytes(4, "little") + image[212:216] = (20 * (len(imports) + 1)).to_bytes(4, "little") + section = 88 + 240 + image[section + 8:section + 12] = (0x200).to_bytes(4, "little") + image[section + 12:section + 16] = (0x1000).to_bytes(4, "little") + image[section + 16:section + 20] = (0x200).to_bytes(4, "little") + image[section + 20:section + 24] = (0x200).to_bytes(4, "little") + name = 20 * (len(imports) + 1) + for index, dll in enumerate(imports): + image[0x200 + index * 20 + 12:0x200 + index * 20 + 16] = (0x1000 + name).to_bytes(4, "little") + image[0x200 + name:0x200 + name + len(dll)] = dll.encode() + name += len(dll) + 1 + return bytes(image) + def pe_image(self, machine): head = bytearray(64) head[:2] = b"MZ" @@ -92,17 +121,65 @@ class WinComponentsTest(unittest.TestCase): self.assertEqual(names, ["ucrtbase"]) self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/ucrtbase.dll"), 0x8664) - def test_x64_visual_cpp_runtime_is_placed_in_arm64_system32(self): + def test_x64_dlls_proton_programs_do_not_import_are_placed_in_arm64_system32(self): proton = self.proton() - self.component("vc", ["msvcp140", "vcruntime140_1"], { - "system32/msvcp140.dll": self.pe_image(0x8664), - "system32/vcruntime140_1.dll": self.pe_image(0x8664), - }) + names = ["concrt140", "d3dcompiler_43", "msvcp140", "vcruntime140_1", "vulkan-1"] + self.component("vc", names, {"system32/%s.dll" % name: self.pe_image(0x8664) for name in names}) self.pick("vc") - names, _ = self.apply(proton) - self.assertEqual(names, ["msvcp140", "vcruntime140_1"]) + placed, _ = self.apply(proton) + self.assertEqual(placed, names) + for name in names: + self.assertEqual(MODULE["pe_machine"](self.pfx / ("system32/%s.dll" % name)), 0x8664) + + def test_x64_d3dx9_and_its_compiler_stay_placed_on_every_launch(self): + proton = self.proton() + self.component("d3dx9", ["d3dx9_43"], {"system32/d3dx9_43.dll": self.pe_image(0x8664)}) + self.component("d3dcompiler", ["d3dcompiler_43"], {"system32/d3dcompiler_43.dll": self.pe_image(0x8664)}) + self.pick("d3dx9", "d3dcompiler") + for _ in range(2): + placed, _ = self.apply(proton) + self.assertEqual(placed, ["d3dx9_43", "d3dcompiler_43"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/d3dx9_43.dll"), 0x8664) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/d3dcompiler_43.dll"), 0x8664) + + def test_x64_compiler_alone_is_placed_although_a_builtin_dll_imports_it(self): + proton = self.proton() + self.component("d3dcompiler", ["d3dcompiler_43"], {"system32/d3dcompiler_43.dll": self.pe_image(0x8664)}) + self.pick("d3dcompiler") + placed, _ = self.apply(proton) + self.assertEqual(placed, ["d3dcompiler_43"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/d3dcompiler_43.dll"), 0x8664) + + def test_x64_proton_program_imports_do_not_make_a_dll_foreign(self): + proton = self.proton() + self.component("vk", ["vulkan-1"], {"system32/vulkan-1.dll": self.pe_image(0x8664)}) + self.pick("vk") + placed, _ = self.apply(proton) + self.assertEqual(placed, ["vulkan-1"]) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/vulkan-1.dll"), 0x8664) + + def test_partly_installed_x64_visual_cpp_runtime_stays_x64(self): + proton = self.proton() + self.pfx.joinpath("system32").mkdir(parents=True) + self.pfx.joinpath("system32/vcruntime140.dll").write_bytes(self.pe_image(0x8664)) + self.component("vc", ["msvcp140"], {"system32/msvcp140.dll": self.pe_image(0x8664)}) + self.pick("vc") + placed, _ = self.apply(proton) + self.assertEqual(placed, ["msvcp140"]) self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/msvcp140.dll"), 0x8664) - self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/vcruntime140_1.dll"), 0x8664) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/vcruntime140.dll"), 0x8664) + + def test_deselected_component_restores_the_saved_x64_dll(self): + proton = self.proton() + self.pfx.joinpath("system32").mkdir(parents=True) + self.pfx.joinpath("system32/d3dx9_43.dll").write_bytes(self.pe_image(0x8664)) + self.component("d3dx9", ["d3dx9_43"], {"system32/d3dx9_43.dll": self.program_image(0xAA64, [])}) + self.pick("d3dx9") + self.apply(proton) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/d3dx9_43.dll"), 0xAA64) + self.pick() + self.apply(proton) + self.assertEqual(MODULE["pe_machine"](self.pfx / "system32/d3dx9_43.dll"), 0x8664) def test_compatible_copy_keeps_override_when_other_architecture_copy_is_skipped(self): proton = self.proton()