diff --git a/clean/installer/README.md b/clean/installer/README.md index e00533cf..7bd1e98f 100644 --- a/clean/installer/README.md +++ b/clean/installer/README.md @@ -932,6 +932,39 @@ Windows (the Wine run was not repeated for them): not a HIP runtime; `LLVM_PATH` removed for the child (the mock fails when it sees it); the newest ROCm folder (7.10 before 7.2) tried first. +**The tests never read the PC's own game libraries** (#243). Every read of a store's metadata goes through one place +(`util.h` `LibraryAccess` / `LibraryRegistryAccess`). That covers Steam's library list and app manifests (also Play's Steam +app id and the display name), Epic's manifests (also Play's Epic app name), the GOG registry, Steam's art cache, and every +walk of a library game's folder (`FindFilesNamed`). +- A `WRONGGPU_TESTING` build with `WRONGGPU_TEST_LIBRARIES` set takes its store libraries from that folder only (each + sub-folder a game; set but empty: none). It reads nothing outside the folder that holds it, the test run's own folder, and + no store registry key. The check is on the real path too: a path whose resolved form differs (a junction or link on the + way), a reparse point, a file with a second name (a hard link) and a test folder that itself resolves elsewhere are + refused. +- Such a build logs "library access: " (or "... refused: ()") to `WRONGGPU_TEST_ACCESS_LOG` before every + read: each walked folder and each file it opens. A read whose line cannot be written is refused (fail closed). A new log + starts with "run ", and every start of the launcher or the restorer writes a "process" line. + Release builds read none of these variables. +- `--noscan` (CLI, every verb; `apply --noscan` for the job too), `/NOSCAN` and the window's no-scan keep every library search + away from the store libraries, the AMD FSR 4 search and the other-model search included. An operation's own noscan (a + job's) holds for that operation only, never for the next job of a batch. +- `/S /NOSCAN /FSR4` and `/S /NOSCAN /LIST` search no library; the report says "libraries not searched (/NOSCAN)". + +Every runner (launcher, coexistence, Model L / K, convert-ignore, installer) sets these variables to its work folder and a +new run id; it never creates the access log itself (native installer runs need a `WRONGGPU_TESTING` build). Each +runs the guard's self-test (`tests/launcher_isolation.py` `self_test`) before any program call, and stops the run if the +guard is broken. Every launcher and installer call, raw ones and elevated jobs included, is checked by `check_isolation`. +The run stops, loudly, on: +- a read outside the work folder (also its `\\?\` and UNC forms); +- a store registry read; +- a missing or unreadable access log, one that does not start with this run's id, or a call that added no line to it; +- no access at all where a call was meant to scan (the guard's sensitivity); +- a search line of the main log that names a path outside the work folder. + +The `[isolation-verbs]` section runs install, repair, update, uninstall, the window's refresh, a batch (a noscan job, then a +normal one) and `/FSR4` / `/LIST`, with and without no-scan, against a non-empty test library. The 0.5.1 baseline installer +predates all this and runs only with `--run-old-baseline`, in a fully redirected sandbox. + On Windows: `python tests\run_installer_tests.py --prepare-only` writes the mock payload and the synthetic map; build the installer from them with CMake (`-DWRONGGPU_PAYLOAD_DIR=... -DWRONGGPU_MAP=...`) and run `python tests\run_installer_tests.py --setup `. Natively it points `LOCALAPPDATA` and diff --git a/clean/installer/src/app/cli.cpp b/clean/installer/src/app/cli.cpp index c7857fb0..c07b9215 100644 --- a/clean/installer/src/app/cli.cpp +++ b/clean/installer/src/app/cli.cpp @@ -570,6 +570,7 @@ static void IgnoreJson(Output& o, const IgnoreReport& r) { int Verb(const wstring& verb, const Args& a, Output& o) { if (IgnoreArgsMisplaced(verb, a, o)) return o.code; const bool noScan = a.has(L"noscan"); + if (noScan) { g.ctx.noScanAlways = true; g.ctx.noLibraryScan = true; } // every verb: no store library search (the AMD FSR 4 search too) GlobalSettings& gs = gl.global; GlobalSettings scan = gs; // --library=: scanned this time only, never saved for (auto& lib : a.libraries) scan.extraFolders.push_back(FullPath(lib)); @@ -758,6 +759,7 @@ int Verb(const wstring& verb, const Args& a, Output& o) { g.ctx.perGameEntry = perEntry >= 0 ? perEntry == 1 : !LauncherInstalled(); Ui ui; ui.step = [](int, int, const wstring&) {}; ui.log = [](const wstring& s) { LogLine(g.ctx, s); }; LogLine(g.ctx, wstring(L"elevated worker: ") + OpName(r.op) + L" " + (r.dir.empty() ? r.exe : r.dir) + (IsElevated() ? L" (elevated)" : L" (not elevated)")); + r.noScan = r.noScan || g.ctx.noScanAlways; // apply --noscan: the job's own searches too (#243) res = RunOp(g.ctx, r, ui); } if (!result.empty()) { const string rt = ResultText(res); try { WriteFileAtomic(result, rt.data(), rt.size()); } catch (const SetupError&) {} } diff --git a/clean/installer/src/app/main.cpp b/clean/installer/src/app/main.cpp index 4790b72e..86c228c6 100644 --- a/clean/installer/src/app/main.cpp +++ b/clean/installer/src/app/main.cpp @@ -131,21 +131,27 @@ int RunSilent() { LogLine(g.ctx, L"HIP check: " + (h.ok ? L"OK - " + h.device + L" (" + h.arch + L") with " + h.dll : h.text)); return h.ok ? EXIT_OK : EXIT_REFUSED; } + // /NOSCAN with /FSR4 or /LIST (#243): no store library is searched; the report says so + auto storeLibraries = [](const wchar_t* what) { + if (!g.ctx.noLibraryScan) return ScanLibraries(); + LogLine(g.ctx, wstring(what) + L": libraries not searched (/NOSCAN)"); + return std::vector{}; + }; if (g.fsr4) { // read-only: the AMD FSR 4 DLL an install would record for the comparison key - const CompareDll fc = FindCompareDll(g.ctx, ScanLibraries()); - LogLine(g.ctx, fc.path.empty() ? wstring(L"FSR 4 for the comparison key: none found (Home compares with the game's own AMD FSR)") + const CompareDll fc = FindCompareDll(g.ctx, storeLibraries(L"FSR 4 for the comparison key")); + if (!g.ctx.noLibraryScan) LogLine(g.ctx, fc.path.empty() ? wstring(L"FSR 4 for the comparison key: none found (Home compares with the game's own AMD FSR)") : L"FSR 4 for the comparison key: " + fc.path + L" (" + fc.version + L", " + fc.game + L")"); return EXIT_OK; } if (g.list) { // read-only: what the wizard would show, with the checks for each game - for (auto& f : DetectGames(ScanLibraries())) { + for (auto& f : DetectGames(storeLibraries(L"games"))) { GameTarget t = InspectGame(g.ctx, f.exe, f.name); Checks c = RunChecks(g.ctx, t, g.sys, nullptr, g.force); LogLine(g.ctx, L"game " + f.name + L" [" + f.source + L"] " + (f.validated ? L"VALIDATED" : L"experimental") + L" -> " + f.exe + L" | install folder " + t.dir); for (int i = 0; i < L_COUNT; ++i) LogLine(g.ctx, wstring(c.st[i] == CK_OK ? L" ok " : c.st[i] == CK_WARN ? L" warn " : c.st[i] == CK_NA ? L" n/a " : L" FAIL ") + c.text[i]); } - const CompareDll fc = FindCompareDll(g.ctx, ScanLibraries()); // what an install would record (nothing written here) - LogLine(g.ctx, fc.path.empty() ? wstring(L"FSR 4 for the comparison key: none found (Home compares with the game's own AMD FSR)") + const CompareDll fc = FindCompareDll(g.ctx, storeLibraries(L"FSR 4 for the comparison key")); // what an install would record (nothing written here) + if (!g.ctx.noLibraryScan) LogLine(g.ctx, fc.path.empty() ? wstring(L"FSR 4 for the comparison key: none found (Home compares with the game's own AMD FSR)") : L"FSR 4 for the comparison key: " + fc.path + L" (" + fc.version + L", " + fc.game + L")"); return EXIT_OK; } @@ -202,6 +208,7 @@ using namespace wg; using namespace wg::app; int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR, int) { + TestAccessStart(L"WrongGPU"); // (#243: a test build run by the tests leaves a line for every run) { // release 0.6.0: the Ray Reconstruction helper (rrprep.h): this program copied under a game's program name, started by // the launcher with --rr-prepare ; nothing else of the launcher runs in it int hc = 0; LPWSTR* hv = CommandLineToArgvW(GetCommandLineW(), &hc); @@ -243,7 +250,7 @@ int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE, PWSTR, int) { else if (la == L"/preset=m") {} #endif else if (kDirectRoute && la == L"/directfsr") g.route = wg_wizard::Route::DirectFsr; - else if (la == L"/noscan") g.noScan = g.ctx.noLibraryScan = true; + else if (la == L"/noscan") { g.noScan = g.ctx.noScanAlways = true; g.ctx.noLibraryScan = true; } else if (la == L"/force") g.force = true; else if (la == L"/experimental") g.experimentalOk = true; else if (la == L"/movemods") g.moveMods = true; diff --git a/clean/installer/src/app/restore_main.cpp b/clean/installer/src/app/restore_main.cpp index 6202096f..5cef1a1b 100644 --- a/clean/installer/src/app/restore_main.cpp +++ b/clean/installer/src/app/restore_main.cpp @@ -76,6 +76,7 @@ wstring PlainError(const SetupError& e) { } // namespace int WINAPI wWinMain(HINSTANCE, HINSTANCE, PWSTR, int) { + TestAccessStart(L"restorer"); // (#243: a test build run by the tests leaves a line for every run) SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32); static const EmbeddedData data{nullptr, 0, 0, L"", 0, L"", 0, WRONGGPU_VERSION_STR}; Context ctx; diff --git a/clean/installer/src/app/ui_art.cpp b/clean/installer/src/app/ui_art.cpp index 2e00c458..46485a94 100644 --- a/clean/installer/src/app/ui_art.cpp +++ b/clean/installer/src/app/ui_art.cpp @@ -54,9 +54,11 @@ struct ArtState { } st; wstring LibraryCache() { - wstring steam = RegString(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", L"SteamPath"); + // (util.h: the store access; a test build never reads the PC's own Steam) + wstring steam = LibraryRegistryAccess(L"Steam SteamPath (art cache)") ? RegString(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", L"SteamPath") : wstring(); for (auto& c : steam) if (c == L'/') c = L'\\'; - return steam.empty() ? wstring() : Join(steam, L"appcache\\librarycache"); + const wstring cache = steam.empty() ? wstring() : Join(steam, L"appcache\\librarycache"); + return cache.empty() || !LibraryAccess(cache) ? wstring() : cache; } // \\, or one folder deeper (Steam keeps hashed sub-folders); the newest when there are several. @@ -66,10 +68,13 @@ wstring FindArt(const wstring& dir, const wchar_t* name) { auto consider = [&](const wstring& p) { WIN32_FILE_ATTRIBUTE_DATA a{}; if (!GetFileAttributesExW(p.c_str(), GetFileExInfoStandard, &a) || (a.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) return; + if (!LibraryAccess(p)) return; // (util.h: each art file is asked before it can be opened) if (best.empty() || CompareFileTime(&a.ftLastWriteTime, &bestTime) > 0) { best = p; bestTime = a.ftLastWriteTime; } }; + if (!LibraryAccess(dir)) return best; // (util.h: every folder listed is asked first) consider(Join(dir, name)); - for (auto& sub : ListDirs(dir)) consider(Join(sub, name)); + for (auto& sub : ListDirs(dir)) + if (LibraryAccess(sub)) consider(Join(sub, name)); return best; } diff --git a/clean/installer/src/app/ui_dump.cpp b/clean/installer/src/app/ui_dump.cpp index 51d981ce..bb6c5434 100644 --- a/clean/installer/src/app/ui_dump.cpp +++ b/clean/installer/src/app/ui_dump.cpp @@ -111,6 +111,7 @@ using namespace win; // no network, no HIP probe or model conversion. Development and the launcher tests. int RunLauncherUiDump(const wstring& outFile, const wstring& shotDir, bool noScan, const wstring& apply) { g.noScan = noScan; + if (noScan) { g.ctx.noScanAlways = true; g.ctx.noLibraryScan = true; } // (the AMD FSR 4 search too) CreateLauncherWindow(false); RefreshModelNow(); SetGpu(DetectGpu()); diff --git a/clean/installer/src/wg_core/context.h b/clean/installer/src/wg_core/context.h index 47395fa0..c7700b6e 100644 --- a/clean/installer/src/wg_core/context.h +++ b/clean/installer/src/wg_core/context.h @@ -3,6 +3,7 @@ // g_presetK, g_map, g_opti, g_noLibraryScan and the embedded payload table), the Ui callbacks of a long operation, the // log file and the model data folder. Written for this project. #pragma once +#include #include #include #include @@ -84,7 +85,11 @@ struct Context { wgc::LoadedMap fMap; wstring fMapError; OptiPackage opti; // the player's OptiScaler package (/OPTISCALER=, the wizard's choice) - bool noLibraryScan = false; // /NOSCAN: the store libraries are not searched + // No search of the store libraries (#243). noScanAlways: this process was asked (--noscan, /NOSCAN, the window's); set once + // at start. noLibraryScan: what the searches read now (the AMD FSR 4 search, the other model); RunOp sets it per operation + // (noScanAlways or the operation's own noscan) and puts it back after, so a job's noscan never reaches the next job. + bool noScanAlways = false; + std::atomic noLibraryScan{false}; // The launcher (build/launcher-design.md): one Apps & features entry for all games instead of one per game; the // HIP runtime is optional (D3D12 is the default executor on RX 9000; a missing HIP runtime is a warning); the // uninstaller written into each game is the small restorer the launcher embeds (WrongGPU-Restore.exe). diff --git a/clean/installer/src/wg_core/detect.cpp b/clean/installer/src/wg_core/detect.cpp index a470a811..7e29c92f 100644 --- a/clean/installer/src/wg_core/detect.cpp +++ b/clean/installer/src/wg_core/detect.cpp @@ -76,9 +76,12 @@ bool LooksLikeHelperExe(const wstring& exe) { } // Unreal: \Game.exe is a small bootstrapper; the real game is \\Binaries\Win64\*-Shipping.exe. wstring UnrealShipping(const wstring& root) { + if (!LibraryAccess(root)) return L""; // (util.h: every folder listed is asked first) for (auto& project : ListDirs(root)) - for (const wchar_t* plat : {L"Binaries\\Win64", L"Binaries\\WinGDK"}) + for (const wchar_t* plat : {L"Binaries\\Win64", L"Binaries\\WinGDK"}) { + if (!IsDir(Join(project, plat)) || !LibraryAccess(Join(project, plat))) continue; for (auto& exe : ListFiles(Join(project, plat), L"*-Shipping.exe")) if (!LooksLikeHelperExe(exe)) return exe; + } return L""; } // rc6: a starter in the game's root whose game has the same name below it: \\Binaries\Win64 (or WinGDK)\ @@ -89,6 +92,7 @@ wstring SameNameGameExe(const wstring& exe) { const wstring root = Parent(exe), name = BaseName(exe); if (root.empty() || !IEq(GameRoot(root), root)) return L""; if (const wstring x = Join(root, L"bin\\x64\\" + name); IsFile(x)) return x; + if (!LibraryAccess(root)) return L""; for (auto& project : ListDirs(root)) for (const wchar_t* plat : {L"Binaries\\Win64", L"Binaries\\WinGDK"}) if (const wstring x = Join(Join(project, plat), name); IsFile(x)) return x; @@ -117,6 +121,7 @@ wstring FindGameExe(const wstring& root, const wstring& hint) { size_t visited = 0; while (!stack.empty() && visited < 20000) { auto [dir, depth] = stack.back(); stack.pop_back(); + if (!LibraryAccess(dir)) continue; // (util.h: every folder listed is asked first) WIN32_FIND_DATAW fd; HANDLE h = FindFirstFileW(Join(dir, L"*").c_str(), &fd); if (h == INVALID_HANDLE_VALUE) continue; @@ -174,9 +179,11 @@ wstring EpicAppName(const wstring& exe) { const wstring pd = EnvVar(L"ProgramData"); if (pd.empty() || exe.empty()) return L""; const wstring full = FullPath(exe); - for (auto& item : ListFiles(Join(pd, L"Epic\\EpicGamesLauncher\\Data\\Manifests"), L"*.item")) { + const wstring manifests = Join(pd, L"Epic\\EpicGamesLauncher\\Data\\Manifests"); + if (!LibraryAccess(manifests)) return L""; // (util.h: the store access; a test build never reads the PC's own) + for (auto& item : ListFiles(manifests, L"*.item")) { string raw; - if (!ReadAll(item, raw)) continue; + if (!LibraryAccess(item) || !ReadAll(item, raw)) continue; const wstring t = FromUtf8(raw); wstring loc = EpicField(t, L"InstallLocation"); for (auto& c : loc) if (c == L'/') c = L'\\'; @@ -204,6 +211,13 @@ wstring StoreExe(const wstring& root, const wstring& hint) { // Every game folder of the Steam, Epic and GOG libraries on this PC. Read-only. std::vector ScanLibraries() { + wstring testFolder; + if (TestLibraries(&testFolder)) { // test builds: the suite's libraries only, the PC's own stores are not read at all + std::vector test; + if (!testFolder.empty() && LibraryAccess(testFolder) && IsDir(testFolder)) + for (auto& d : ListDirs(testFolder)) test.push_back({BaseName(d), FullPath(d), L"", L"Test"}); + return test; + } std::vector roots; auto add = [&](const wstring& name, const wstring& root, const wstring& hint, const wchar_t* source) { if (name.empty() || root.empty() || !IsDir(root)) return; @@ -211,12 +225,13 @@ std::vector ScanLibraries() { roots.push_back({name, FullPath(root), hint, source}); }; // Steam: every library in libraryfolders.vdf, every app manifest in it. - wstring steam = RegString(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", L"SteamPath"); + wstring steam = LibraryRegistryAccess(L"Steam SteamPath") ? RegString(HKEY_CURRENT_USER, L"Software\\Valve\\Steam", L"SteamPath") : wstring(); if (!steam.empty()) { for (auto& c : steam) if (c == L'/') c = L'\\'; std::vector libs{steam}; string raw; - if (ReadAll(Join(steam, L"steamapps\\libraryfolders.vdf"), raw)) { + const wstring vdf = Join(steam, L"steamapps\\libraryfolders.vdf"); + if (LibraryAccess(vdf) && ReadAll(vdf, raw)) { wstring t = FromUtf8(raw); size_t pos = 0; while (pos < t.size()) { size_t nl = t.find(L'\n', pos); wstring line = t.substr(pos, nl == wstring::npos ? wstring::npos : nl - pos); pos = nl == wstring::npos ? t.size() : nl + 1; @@ -228,9 +243,10 @@ std::vector ScanLibraries() { } } for (auto& lib : libs) { + if (!LibraryAccess(Join(lib, L"steamapps"))) continue; for (auto& acf : ListFiles(Join(lib, L"steamapps"), L"appmanifest_*.acf")) { string a; - if (!ReadAll(acf, a)) continue; + if (!LibraryAccess(acf) || !ReadAll(acf, a)) continue; wstring t = FromUtf8(a), dir = KeyValue(t, L"installdir"); // benchmarks that ship upscaler test files but are not games (3DMark and its demo) static const wchar_t* const kNotGames[] = {L"223850", L"231350"}; @@ -243,17 +259,18 @@ std::vector ScanLibraries() { } // Epic Games Store. wstring pd = EnvVar(L"ProgramData"); - if (!pd.empty()) { + if (!pd.empty() && LibraryAccess(Join(pd, L"Epic\\EpicGamesLauncher\\Data\\Manifests"))) { for (auto& item : ListFiles(Join(pd, L"Epic\\EpicGamesLauncher\\Data\\Manifests"), L"*.item")) { string raw; - if (!ReadAll(item, raw)) continue; + if (!LibraryAccess(item) || !ReadAll(item, raw)) continue; const StoreGame e = EpicItemGame(FromUtf8(raw)); add(e.name, e.root, e.hint, L"Epic"); } } // GOG Galaxy and GOG offline installers. HKEY gog; - if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\GOG.com\\Games", 0, KEY_READ | KEY_WOW64_32KEY, &gog) == ERROR_SUCCESS) { + if (LibraryRegistryAccess(L"GOG.com Games") && + RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\GOG.com\\Games", 0, KEY_READ | KEY_WOW64_32KEY, &gog) == ERROR_SUCCESS) { wchar_t sub[256]; for (DWORD i = 0;; ++i) { DWORD n = (DWORD)std::size(sub); @@ -271,6 +288,7 @@ std::vector ScanLibraries() { std::vector DetectGames(const std::vector& roots) { std::vector found; for (auto& r : roots) { + if (!LibraryAccess(r.root)) continue; bool fsr = false; for (auto& sl : kSlots) fsr = fsr || !FindFilesNamed(r.root, sl.name, 30000, 8).empty(); // games with DLSS or XeSS only: installable with the player's OptiScaler @@ -389,10 +407,10 @@ std::vector InspectSlots(Context& ctx, const wstring& dir, const Reco // Display name: the Steam app name when the game lives in a Steam library, else the exe name without Unreal suffixes. wstring GameDisplayName(const wstring& exe) { const wstring root = GameRoot(Parent(exe)), common = Parent(root), steamapps = Parent(common); - if (IEq(BaseName(common), L"common") && IEq(BaseName(steamapps), L"steamapps")) { + if (IEq(BaseName(common), L"common") && IEq(BaseName(steamapps), L"steamapps") && LibraryAccess(steamapps)) { for (auto& acf : ListFiles(steamapps, L"appmanifest_*.acf")) { string a; - if (!ReadAll(acf, a)) continue; + if (!LibraryAccess(acf) || !ReadAll(acf, a)) continue; wstring t = FromUtf8(a); if (IEq(KeyValue(t, L"installdir"), BaseName(root))) { wstring n = KeyValue(t, L"name"); if (!n.empty()) return n; } } @@ -413,9 +431,10 @@ wstring SteamAppId(const wstring& exe) { while (!root.empty() && !(IEq(BaseName(Parent(root)), L"common") && IEq(BaseName(Parent(Parent(root))), L"steamapps"))) root = Parent(root); if (root.empty() || BaseName(root).empty()) return L""; const wstring steamapps = Parent(Parent(root)); + if (!LibraryAccess(steamapps)) return L""; for (auto& acf : ListFiles(steamapps, L"appmanifest_*.acf")) { string a; - if (!ReadAll(acf, a)) continue; + if (!LibraryAccess(acf) || !ReadAll(acf, a)) continue; const wstring t = FromUtf8(a); if (!IEq(KeyValue(t, L"installdir"), BaseName(root))) continue; wstring id = KeyValue(t, L"appid"); diff --git a/clean/installer/src/wg_core/detect.h b/clean/installer/src/wg_core/detect.h index 02f5a694..425d44c5 100644 --- a/clean/installer/src/wg_core/detect.h +++ b/clean/installer/src/wg_core/detect.h @@ -38,6 +38,9 @@ wstring UnrealShipping(const wstring& root); wstring SameNameGameExe(const wstring& exe); wstring FindGameExe(const wstring& root, const wstring& hint = L""); const KnownGame* Validated(const wstring& exe); +// The store libraries' game folders (Steam, Epic, GOG), every read through the store access (util.h LibraryAccess). In a +// WRONGGPU_TESTING build with WRONGGPU_TEST_LIBRARIES set: the sub-folders of that folder only (set but empty: none), and +// nothing of the PC's own stores is read (util.h TestLibraries). std::vector ScanLibraries(); StoreGame EpicItemGame(const wstring& manifestText); // one Epic manifest (.item): name, InstallLocation, LaunchExecutable hint // rc6 L7: the AppName of the Epic manifest (%ProgramData%\Epic\EpicGamesLauncher\Data\Manifests\*.item) whose InstallLocation diff --git a/clean/installer/src/wg_core/launcher.cpp b/clean/installer/src/wg_core/launcher.cpp index d362ab18..1f99aafa 100644 --- a/clean/installer/src/wg_core/launcher.cpp +++ b/clean/installer/src/wg_core/launcher.cpp @@ -1229,7 +1229,7 @@ static void InstallOp(Context& ctx, const OpRequest& r, Ui& ui, OpResult& res) { if (const GameRule* rule = GameRuleFor(target.exe); rule && rule->needsReFramework && !target.installed && !DetectReFramework(target.exeDir).found) Fail(NeedsReFrameworkText(*rule), EXIT_REFUSED); SystemInfo sys; sys.win = WindowsInfo(); sys.gpu = DetectGpu(); - const std::vector libraries = r.noScan ? std::vector{} : ScanLibraries(); + const std::vector libraries = ctx.noLibraryScan ? std::vector{} : ScanLibraries(); // (this operation's no-scan: RunOp) const ModelState model = FindModel(ctx, &target, r.modelDll.empty() ? wstring() : FullPath(r.modelDll), libraries); if (model.ok()) LogLine(ctx, L"model source: " + (model.reused ? wstring(L"model data already on this PC") : model.dll + L" (" + model.origin + L")")); const Checks c = RunChecks(ctx, target, sys, &model, r.force); @@ -1363,6 +1363,14 @@ wstring PartlyText(const wstring& done, const std::vector& warnings) { OpResult RunOp(Context& ctx, const OpRequest& r, Ui& ui) { OpResult res; + // this operation's no-scan (#243): the process's (--noscan, /NOSCAN, the window's) or its own (a job's noscan), for every + // search it makes (the AMD FSR 4 search and the other model read ctx.noLibraryScan); put back when it ends, so a job's + // noscan never reaches the next job of a batch + struct NoScan { + Context& c; + NoScan(Context& ctx, bool own) : c(ctx) { c.noLibraryScan = c.noScanAlways || own; } + ~NoScan() { c.noLibraryScan = c.noScanAlways; } + } noScan(ctx, r.noScan); try { switch (r.op) { case Op::Install: case Op::Repair: case Op::Update: InstallOp(ctx, r, ui, res); break; diff --git a/clean/installer/src/wg_core/util.cpp b/clean/installer/src/wg_core/util.cpp index 64268e52..4977f294 100644 --- a/clean/installer/src/wg_core/util.cpp +++ b/clean/installer/src/wg_core/util.cpp @@ -3,6 +3,8 @@ // this project. #include "util.h" +#include + namespace wg { [[noreturn]] void Fail(const wstring& m, int code) { throw SetupError{m + L" [WG-SETUP-" + std::to_wstring(code) + L"]", code}; } @@ -476,6 +478,154 @@ std::vector ListDirs(const wstring& dir) { std::sort(r.begin(), r.end(), [](const wstring& a, const wstring& b) { return Lower(a) < Lower(b); }); return r; } +// ---- store access (#243): the one way to the store libraries' metadata and folders -------------------------------------- +static std::atomic g_libraryAccesses{0}; + +bool TestLibraries(wstring* folder) { +#ifdef WRONGGPU_TESTING + wchar_t buf[MAX_PATH * 2] = {}; + SetLastError(ERROR_SUCCESS); + const DWORD n = GetEnvironmentVariableW(L"WRONGGPU_TEST_LIBRARIES", buf, (DWORD)(sizeof buf / sizeof buf[0])); + if (n == 0 && GetLastError() == ERROR_ENVVAR_NOT_FOUND) return false; + if (folder) *folder = n > 0 && n < sizeof buf / sizeof buf[0] ? wstring(buf) : wstring(); + return true; +#else + (void)folder; + return false; +#endif +} + +#ifdef WRONGGPU_TESTING +// A path as one form for the scope test: \\?\C:\x and \??\C:\x as C:\x, \\?\UNC\srv\share as \\srv\share, then full. +static wstring ScopeForm(const wstring& p) { + wstring s = p; + if (StartsWithI(s, L"\\\\?\\UNC\\")) s = L"\\\\" + s.substr(8); + else if (StartsWithI(s, L"\\\\?\\") || StartsWithI(s, L"\\??\\")) s = s.substr(4); + s = FullPath(s); + while (s.size() > 3 && s.back() == L'\\') s.pop_back(); + return s; +} + +// Test builds: the access log (WRONGGPU_TEST_ACCESS_LOG). A new log starts with "run " (the tests' guard +// checks it is this run's); every line is appended before the read it records. false: not written (the read is refused). +static bool TestAccessLine(const wstring& line) { + const wstring log = EnvVar(L"WRONGGPU_TEST_ACCESS_LOG"); + if (log.empty()) return false; + HANDLE h = CreateFileW(log.c_str(), FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, CREATE_NEW, + FILE_ATTRIBUTE_NORMAL, nullptr); + string text; + if (h != INVALID_HANDLE_VALUE) { + text = ToUtf8(L"run " + EnvVar(L"WRONGGPU_TEST_RUN_ID") + L"\r\n"); // (this process made the log) + } else { + if (GetLastError() != ERROR_FILE_EXISTS) return false; + h = CreateFileW(log.c_str(), FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_ATTRIBUTE_NORMAL, nullptr); + if (h == INVALID_HANDLE_VALUE) return false; + } + text += ToUtf8(line + L"\r\n"); + DWORD w = 0; + const bool ok = WriteFile(h, text.data(), (DWORD)text.size(), &w, nullptr) && w == text.size(); + CloseHandle(h); + return ok; +} + +// The final form of a path that exists: the name Windows resolves it to (every junction and link above it followed), as +// C:\... or \\server\...; empty when it cannot be opened. +static wstring FinalForm(const wstring& p) { + const HANDLE h = CreateFileW(p.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS, nullptr); + if (h == INVALID_HANDLE_VALUE) return L""; + wchar_t buf[MAX_PATH * 4] = {}; + const DWORD n = GetFinalPathNameByHandleW(h, buf, (DWORD)(sizeof buf / sizeof buf[0]), FILE_NAME_NORMALIZED | VOLUME_NAME_DOS); + CloseHandle(h); + return n > 0 && n < sizeof buf / sizeof buf[0] ? ScopeForm(buf) : L""; +} +// The long lexical form of a path that exists (8.3 names expanded), for comparing with its final form. +static wstring LongForm(const wstring& p) { + wchar_t buf[MAX_PATH * 4] = {}; + const DWORD n = GetLongPathNameW(p.c_str(), buf, (DWORD)(sizeof buf / sizeof buf[0])); + return n > 0 && n < sizeof buf / sizeof buf[0] ? ScopeForm(buf) : p; +} +// Test builds, the scope test on what a path really is (not only its spelling): the nearest part of it that exists must +// resolve to its own long name (no junction or link above it), the path itself must not be a reparse point, and a file +// must have no second name (a hard link inside could name an outside file's data). why: the refusal's reason. +static bool RealPathOk(const wstring& lexical, const wchar_t** why) { + wstring probe = lexical; + while (!probe.empty() && GetFileAttributesW(probe.c_str()) == INVALID_FILE_ATTRIBUTES) { + const wstring up = Parent(probe); + if (up == probe || up.empty()) return true; // nothing of it exists: nothing to read + probe = up; + } + if (probe.empty()) return true; + const wstring finalForm = FinalForm(probe); + if (finalForm.empty() || !IEq(finalForm, LongForm(probe))) { *why = L"resolves elsewhere (a junction or link)"; return false; } + if (IEq(probe, lexical)) { + const HANDLE h = CreateFileW(lexical.c_str(), FILE_READ_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, + FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr); + if (h == INVALID_HANDLE_VALUE) { *why = L"cannot be checked"; return false; } + BY_HANDLE_FILE_INFORMATION fi{}; + const bool got = GetFileInformationByHandle(h, &fi) != 0; + CloseHandle(h); + if (!got) { *why = L"cannot be checked"; return false; } + if (fi.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) { *why = L"is a reparse point (a link or junction)"; return false; } + if (!(fi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && fi.nNumberOfLinks > 1) { *why = L"has another name (a hard link)"; return false; } + } + return true; +} +#endif + +bool LibraryAccess(const wstring& path) { + ++g_libraryAccesses; +#ifdef WRONGGPU_TESTING + wstring folder; + if (!TestLibraries(&folder)) { + TestAccessLine(L"library access: " + path); // (a test build without the tests' list: logged where asked, never held back) + return true; + } + // the tests' scope: the folder that holds WRONGGPU_TEST_LIBRARIES (the test run's own folder), as it really is; nothing + // else is read, and nothing is read whose access line could not be written (fail closed) + const wstring root = folder.empty() ? wstring() : Parent(ScopeForm(folder)); + const wstring p = ScopeForm(path); + const wchar_t* why = nullptr; + bool inside = !root.empty() && (IEq(p, root) || (p.size() > root.size() && StartsWithI(p, root) && (p[root.size()] == L'\\' || root.back() == L'\\'))); + if (inside && GetFileAttributesW(root.c_str()) != INVALID_FILE_ATTRIBUTES && !IEq(FinalForm(root), LongForm(root))) { inside = false; why = L"the test folder resolves elsewhere"; } + if (inside && !RealPathOk(p, &why)) inside = false; + if (!inside) { + TestAccessLine(L"library access refused: " + path + (why ? wstring(L" (") + why + L")" : wstring())); + return false; + } + return TestAccessLine(L"library access: " + path); +#else + (void)path; + return true; +#endif +} + +bool LibraryRegistryAccess(const wchar_t* what) { + ++g_libraryAccesses; +#ifdef WRONGGPU_TESTING + if (!TestLibraries()) { + TestAccessLine(wstring(L"library access: registry ") + what); + return true; + } + TestAccessLine(wstring(L"library access refused: registry ") + what); + return false; +#else + (void)what; + return true; +#endif +} + +void TestAccessStart(const wchar_t* program) { +#ifdef WRONGGPU_TESTING + if (TestLibraries()) TestAccessLine(L"process " + std::to_wstring(GetCurrentProcessId()) + L" " + program + L" start"); +#else + (void)program; +#endif +} + +size_t LibraryAccessCount() { return g_libraryAccesses; } + // Every file named `name` below root (depth-limited, budgeted, no links), sorted by path. std::vector FindFilesNamed(const wstring& root, const wchar_t* name, size_t budget, int maxDepth) { std::vector out; @@ -483,6 +633,7 @@ std::vector FindFilesNamed(const wstring& root, const wchar_t* name, si size_t visited = 0; while (!stack.empty() && visited < budget) { auto [dir, depth] = stack.back(); stack.pop_back(); + if (!LibraryAccess(dir)) continue; // every folder listed is asked first (a test build: never one outside its own) WIN32_FIND_DATAW fd; HANDLE h = FindFirstFileW(Join(dir, L"*").c_str(), &fd); if (h == INVALID_HANDLE_VALUE) continue; @@ -493,7 +644,9 @@ std::vector FindFilesNamed(const wstring& root, const wchar_t* name, si if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { if (depth < maxDepth && !(fd.dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT) && !IEq(n, kStateDirName) && !IEq(n, kLegacyStateDir) && !IEq(n, kKeptDirName)) stack.push_back({Join(dir, n), depth + 1}); - } else if (IEq(n, name)) out.push_back(Join(dir, n)); + } else if (IEq(n, name) && LibraryAccess(Join(dir, n))) { + out.push_back(Join(dir, n)); // (each file found is asked too: what its caller opens next) + } } while (FindNextFileW(h, &fd)); FindClose(h); } diff --git a/clean/installer/src/wg_core/util.h b/clean/installer/src/wg_core/util.h index f1c7363f..8abf156c 100644 --- a/clean/installer/src/wg_core/util.h +++ b/clean/installer/src/wg_core/util.h @@ -140,7 +140,27 @@ std::vector RemoveOwned(const wstring& root, const std::function ListFiles(const wstring& dir, const wchar_t* pattern); std::vector ListDirs(const wstring& dir); +// Walks root (asked of LibraryAccess first: a test build never walks a folder outside the test run's own). std::vector FindFilesNamed(const wstring& root, const wchar_t* name, size_t budget = 60000, int maxDepth = 10); +// Store access (#243): every read of a store's metadata (Steam's library list and app manifests, Epic's manifests, the GOG +// registry, Steam's art cache) and every walk of a library game's folder asks here first. +// - LibraryAccess(path): true = may be read. In a WRONGGPU_TESTING build with WRONGGPU_TEST_LIBRARIES set, only paths below +// the folder that holds it (the test run's own folder; \\?\ and UNC forms compared as such); anything else is refused. +// - LibraryRegistryAccess(what): a store's registry keys; refused in such a test build (the PC's own stores). +// - Such a test build checks what a path really is: its final form (GetFinalPathNameByHandle) must be its own long name (no +// junction or link above it), it must not be a reparse point, a file must have no second name (hard link), and the test +// folder itself must resolve to itself; anything else is refused. +// - Test builds append "library access: " or "library access refused: ()" to WRONGGPU_TEST_ACCESS_LOG +// before the read (the tests' guard parses it). A new log starts with "run ". With the tests' list +// set, a read whose line cannot be written is refused (fail closed). Nothing of this in other builds. +// - TestAccessStart(program): a test build with the tests' list writes "process start" (every program run +// leaves a line, so the guard can tell a run that logged nothing). LibraryAccessCount counts the questions (every build). +// - TestLibraries(folder): such a test build (WRONGGPU_TEST_LIBRARIES set, its value in folder); false in other builds. +bool LibraryAccess(const wstring& path); +bool LibraryRegistryAccess(const wchar_t* what); +void TestAccessStart(const wchar_t* program); +size_t LibraryAccessCount(); +bool TestLibraries(wstring* folder = nullptr); wstring HexW(const wgc::Digest& d); wstring Sha256Bytes(const void* data, size_t size); wstring Sha256File(const wstring& path); diff --git a/clean/installer/tests/launcher_core_tests.cpp b/clean/installer/tests/launcher_core_tests.cpp index 40abb6bb..691047d5 100644 --- a/clean/installer/tests/launcher_core_tests.cpp +++ b/clean/installer/tests/launcher_core_tests.cpp @@ -8,6 +8,7 @@ // Written for this project. Run by ctest ("launcher-core"). #include #include +#include #include #include @@ -2165,6 +2166,165 @@ int main() { require(BuildTimeText(0) == L"unknown" && BuildTimeText(nov5).size() == 20 && BuildTimeText(nov5) == L"2026-11-05 23:06 UTC", "BuildTimeText: \"YYYY-MM-DD hh:mm UTC\""); } + // #243: the store access (util.h LibraryAccess) and the no-scan of each operation + { + const wstring nsDir = Join(dir, L"noscan"); + EnsureDir(nsDir); + Ui nui; + nui.step = [](int, int, const wstring&) {}; + nui.log = [](const wstring&) {}; + auto failingOp = [&](bool noScan) { // an operation that fails at once (no record): only what RunOp does around it matters + OpRequest r; + r.op = Op::Uninstall; + r.dir = Join(nsDir, L"no-such-game"); + r.noScan = noScan; + return r; + }; + // the no-scan is per operation: a job's noscan holds for that job only, the process's for all + Context pc; + pc.logFile = Join(nsDir, L"per-op.log"); + require(RunOp(pc, failingOp(true), nui).code != 0 && !pc.noLibraryScan, "a noscan operation: the flag goes back when it ends (never sticks into the next job)"); + RunOp(pc, failingOp(false), nui); + require(!pc.noLibraryScan, "an operation without noscan: no search is held back"); + Context ac; + ac.noScanAlways = true; + ac.noLibraryScan = true; + RunOp(ac, failingOp(false), nui); + require(ac.noLibraryScan, "--noscan for the process: every operation, also one whose own noscan is off, searches nothing"); + pc.noLibraryScan = true; // (as RunOp sets it for a noscan operation) + const CompareDll none = RecordCompareDll(pc, nui, L""); + string plog; + ReadAll(pc.logFile, plog); + require(none.path.empty() && plog.find("AMD FSR 4 search: libraries not searched") != string::npos && plog.find("candidate") == string::npos, + "noscan: the AMD FSR 4 search leaves the store libraries alone (said in the log)"); + pc.noLibraryScan = false; +#ifdef WRONGGPU_TESTING + // test builds: WRONGGPU_TEST_LIBRARIES (a NON-EMPTY test library here) is the only library list; every store read is + // asked of LibraryAccess first and logged to WRONGGPU_TEST_ACCESS_LOG; nothing outside the test folder is read + const wstring libs = Join(nsDir, L"libraries"), access = Join(nsDir, L"access.log"); + auto put = [](const wstring& p, const string& t) { EnsureDir(Parent(p)); WriteFileAtomic(p, t.data(), t.size()); }; + put(Join(libs, L"Library Game\\amd_fidelityfx_upscaler_dx12.dll"), "MZ not AMD's DLL"); + SetEnvironmentVariableW(L"WRONGGPU_TEST_LIBRARIES", libs.c_str()); + SetEnvironmentVariableW(L"WRONGGPU_TEST_ACCESS_LOG", access.c_str()); + auto accessLog = [&] { string t; ReadAll(access, t); DeleteFileW(access.c_str()); return t; }; + const string outsideRoot = "Q:\\NotTheTestFolder"; + const auto found = ScanLibraries(); + string al = accessLog(); + require(found.size() == 1 && found[0].source == L"Test" && TestLibraries() && al.find("library access: " + ToUtf8(libs)) != string::npos && + al.find("registry") == string::npos, + "ScanLibraries: the test library's games only, logged; no store registry read"); + require(!LibraryRegistryAccess(L"GOG.com Games") && accessLog().find("library access refused: registry GOG.com Games") != string::npos, + "GOG (and Steam's registry, the art cache's Steam path): a store's registry key is refused in a test build, logged"); + // Steam: an app manifest inside the test folder is read; one of the PC's is not + put(Join(libs, L"steamapps\\appmanifest_123.acf"), "\"AppState\"\r\n{\r\n\t\"appid\"\t\t\"123\"\r\n\t\"name\"\t\t\"Test Game\"\r\n\t\"installdir\"\t\t\"G\"\r\n}\r\n"); + put(Join(libs, L"steamapps\\common\\G\\Game.exe"), "MZ"); + require(SteamAppId(Join(libs, L"steamapps\\common\\G\\Game.exe")) == L"123" && GameDisplayName(Join(libs, L"steamapps\\common\\G\\Game.exe")) == L"Test Game" && + accessLog().find("library access: " + ToUtf8(Join(libs, L"steamapps"))) != string::npos, + "Steam (SteamAppId, GameDisplayName): an app manifest in the test folder is read, logged"); + require(SteamAppId(FromUtf8(outsideRoot) + L"\\steamapps\\common\\G\\Game.exe").empty() && + accessLog().find("library access refused: " + outsideRoot + "\\steamapps") != string::npos, + "Steam: an app manifest folder outside the test folder is refused, logged, not read"); + // Epic: the manifests of a ProgramData in the test folder are read; the PC's are not + wchar_t pdWas[MAX_PATH] = {}; + GetEnvironmentVariableW(L"ProgramData", pdWas, MAX_PATH); + const wstring pd = Join(nsDir, L"programdata"); + { + string loc; // (JSON: the path's backslashes escaped, as Epic writes them) + for (char c : ToUtf8(Join(libs, L"E"))) { loc += c; if (c == '\\') loc += '\\'; } + put(Join(pd, L"Epic\\EpicGamesLauncher\\Data\\Manifests\\t.item"), "{\"InstallLocation\": \"" + loc + "\", \"AppName\": \"TestApp\"}"); + } + SetEnvironmentVariableW(L"ProgramData", pd.c_str()); + const wstring epic = EpicAppName(Join(libs, L"E\\Game.exe")); + const string eal = accessLog(); + SetEnvironmentVariableW(L"ProgramData", FromUtf8(outsideRoot).c_str()); + const wstring epicOut = EpicAppName(Join(libs, L"E\\Game.exe")); + const string eal2 = accessLog(); + SetEnvironmentVariableW(L"ProgramData", pdWas[0] ? pdWas : nullptr); + require(epic == L"TestApp" && eal.find("library access: " + ToUtf8(pd)) != string::npos, "Epic (EpicAppName): the manifests of a ProgramData in the test folder are read, logged"); + require(epicOut.empty() && eal2.find("library access refused: " + outsideRoot) != string::npos, "Epic: the manifests outside the test folder are refused, logged, not read"); + // the walk of a library game's folder (FindFilesNamed): inside yes, outside no; \\?\ and UNC forms as such + require(FindFilesNamed(Join(libs, L"Library Game"), L"amd_fidelityfx_upscaler_dx12.dll").size() == 1 && + FindFilesNamed(FromUtf8(outsideRoot), L"amd_fidelityfx_upscaler_dx12.dll").empty() && !LibraryAccess(L"\\\\server\\share\\games") && + !LibraryAccess(L"\\\\?\\UNC\\server\\share\\games") && LibraryAccess(L"\\\\?\\" + Join(libs, L"Library Game")), + "FindFilesNamed and LibraryAccess: inside the test folder walked (also as \\\\?\\), outside and UNC refused"); + const string wal = accessLog(); + require(wal.find("library access refused: " + outsideRoot) != string::npos && wal.find("library access refused: \\\\server\\share\\games") != string::npos, + "the refused walks are logged"); + // what a path really is (recheck P1a): a junction above it, a file symlink, a hard link to an outside file, and a test + // folder that is itself a junction are all refused, however inside their spelling looks + { + const wstring outside = Join(dir, L"outside-target"); // (outside the test run's folder nsDir) + put(Join(outside, L"amd_fidelityfx_upscaler_dx12.dll"), "MZ outside"); + const wstring junction = Join(libs, L"Junction Game"); + _wsystem((L"cmd /d /c mklink /J \"" + junction + L"\" \"" + outside + L"\" >nul 2>nul").c_str()); + require(IsDir(junction) && !LibraryAccess(Join(junction, L"amd_fidelityfx_upscaler_dx12.dll")) && + FindFilesNamed(junction, L"amd_fidelityfx_upscaler_dx12.dll").empty() && + accessLog().find("library access refused: " + ToUtf8(junction)) != string::npos, + "a junction in the library (an ancestor of the file): refused, not walked, logged"); + const wstring symDir = Join(libs, L"Symlink Game"), sym = Join(symDir, L"amd_fidelityfx_upscaler_dx12.dll"); + EnsureDir(symDir); + if (CreateSymbolicLinkW(sym.c_str(), Join(outside, L"amd_fidelityfx_upscaler_dx12.dll").c_str(), SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE)) { + require(!LibraryAccess(sym) && FindFilesNamed(symDir, L"amd_fidelityfx_upscaler_dx12.dll").empty() && + accessLog().find("library access refused: " + ToUtf8(sym)) != string::npos, + "a file symlink in the library to an outside file: refused, not returned by the walk, logged"); + DeleteFileW(sym.c_str()); + } else { + std::printf(" skipped: the file symlink case (this account cannot create symbolic links: Windows developer mode off; the " + "junction and hard-link cases cover the same check)\n"); + } + const wstring hardDir = Join(libs, L"Hardlink Game"), hard = Join(hardDir, L"amd_fidelityfx_upscaler_dx12.dll"); + EnsureDir(hardDir); + require(CreateHardLinkW(hard.c_str(), Join(outside, L"amd_fidelityfx_upscaler_dx12.dll").c_str(), nullptr) && !LibraryAccess(hard) && + FindFilesNamed(hardDir, L"amd_fidelityfx_upscaler_dx12.dll").empty(), + "a hard link in the library to an outside file (a second name): refused, not returned by the walk"); + DeleteFileW(hard.c_str()); + RemoveDirectoryW(junction.c_str()); // (the junction only: never what it points to) + accessLog(); + // the test folder itself a junction: nothing is read + const wstring realRoot = Join(dir, L"real-root"), linkRoot = Join(nsDir, L"root-junction"); + EnsureDir(Join(realRoot, L"libraries\\G")); + _wsystem((L"cmd /d /c mklink /J \"" + linkRoot + L"\" \"" + realRoot + L"\" >nul 2>nul").c_str()); + SetEnvironmentVariableW(L"WRONGGPU_TEST_LIBRARIES", Join(linkRoot, L"libraries").c_str()); + require(IsDir(linkRoot) && !LibraryAccess(Join(linkRoot, L"libraries\\G")) && ScanLibraries().empty(), + "a test folder that resolves elsewhere (itself a junction): every read refused"); + SetEnvironmentVariableW(L"WRONGGPU_TEST_LIBRARIES", libs.c_str()); + RemoveDirectoryW(linkRoot.c_str()); + accessLog(); + } + // the access log (recheck P1b): a read whose line cannot be written is refused; a new log starts with this run's id + { + SetEnvironmentVariableW(L"WRONGGPU_TEST_ACCESS_LOG", libs.c_str()); // a folder: no line can be appended + const bool unwritable = LibraryAccess(Join(libs, L"Library Game")); + SetEnvironmentVariableW(L"WRONGGPU_TEST_ACCESS_LOG", nullptr); + const bool unset = LibraryAccess(Join(libs, L"Library Game")); + SetEnvironmentVariableW(L"WRONGGPU_TEST_ACCESS_LOG", access.c_str()); + require(!unwritable && !unset, "an access log that cannot be written (or none named): the read is refused (fail closed)"); + SetEnvironmentVariableW(L"WRONGGPU_TEST_RUN_ID", L"unit-run-7"); + DeleteFileW(access.c_str()); + TestAccessStart(L"unit"); + const string started = accessLog(); + require(started.rfind("run unit-run-7\r\nprocess ", 0) == 0 && started.find(" unit start") != string::npos, + "a new access log starts with \"run \", then the program's start line"); + SetEnvironmentVariableW(L"WRONGGPU_TEST_RUN_ID", nullptr); + } + // a normal operation after a noscan one finds its compare DLL in the test library (the search runs again) + RunOp(pc, failingOp(true), nui); + DeleteFileW(pc.logFile.c_str()); + RecordCompareDll(pc, nui, L""); + ReadAll(pc.logFile, plog); + require(plog.find("AMD FSR 4 search: " + ToUtf8(Join(libs, L"Library Game\\amd_fidelityfx_upscaler_dx12.dll"))) != string::npos && + accessLog().find("library access: " + ToUtf8(Join(libs, L"Library Game"))) != string::npos, + "after a noscan job, a normal one's AMD FSR 4 search finds the test library's DLL (here: not AMD's, so not used)"); + SetEnvironmentVariableW(L"WRONGGPU_TEST_LIBRARIES", L""); + require(ScanLibraries().empty() && TestLibraries(), "WRONGGPU_TEST_LIBRARIES set but empty: no library at all"); + SetEnvironmentVariableW(L"WRONGGPU_TEST_LIBRARIES", nullptr); + SetEnvironmentVariableW(L"WRONGGPU_TEST_ACCESS_LOG", nullptr); + require(!TestLibraries(), "WRONGGPU_TEST_LIBRARIES not set: TestLibraries false (the normal search, never run here)"); +#else + require(!TestLibraries(), "a build without WRONGGPU_TESTING never takes a test library list"); +#endif + } + RemoveTree(dir); std::printf("launcher-core: %d checks passed\n", checks); return 0; diff --git a/clean/installer/tests/launcher_isolation.py b/clean/installer/tests/launcher_isolation.py new file mode 100644 index 00000000..b1dfa3cb --- /dev/null +++ b/clean/installer/tests/launcher_isolation.py @@ -0,0 +1,275 @@ +# SPDX-License-Identifier: MIT +"""The launcher test runners' guard against the PC's own game folders (#243). A WRONGGPU_TESTING launcher logs every store +access (util.h LibraryAccess: "library access: ", "library access refused: ") to WRONGGPU_TEST_ACCESS_LOG +before the read; check_isolation parses that log after every launcher or installer call and stops the whole run, loudly, on +a read outside the run's work folder (also \\\\?\\ and UNC forms), a store registry read, a missing or unreadable access log, +a log not of this run (its first line "run "), a call that logged nothing (every start of ours logs a +"process" line), or no access at all where the call was expected to scan. The runners never create the log: the launcher +does, and refuses a read whose line it cannot write. self_test (no launcher) runs first in every runner's main and +aborts the run when the guard itself is broken. run_verbs: the launcher's verbs with and without --noscan against a +non-empty test library. Written for this project.""" +import os +import re +import uuid + +from run_installer_tests import write + +ACCESS_LOG = "library-access.log" # in the work folder (each runner sets WRONGGPU_TEST_ACCESS_LOG to it) +_RUN_IDS = {} # work folder -> this run's id (WRONGGPU_TEST_RUN_ID): the access log's first line must name it + + +def test_env(env, work): + """The environment every runner gives the launcher: its store libraries from \\libraries only (empty: none), every + store access logged to \\library-access.log by the launcher itself (never created here: a log the launcher could not + write stays missing, and the guard stops the run), this run's id for its first line.""" + libs = os.path.join(work, "libraries") + os.makedirs(libs, exist_ok=True) + env["WRONGGPU_TEST_LIBRARIES"] = libs + env["WRONGGPU_TEST_ACCESS_LOG"] = os.path.join(work, ACCESS_LOG) + env["WRONGGPU_TEST_RUN_ID"] = new_run(work) + return env + + +def new_run(work): + """This run's id (the access log's first line must name it); an earlier run's log removed, never counted as this run's.""" + p = os.path.join(work, ACCESS_LOG) + if os.path.exists(p): + os.remove(p) + rid = _RUN_IDS[os.path.normcase(os.path.abspath(work))] = uuid.uuid4().hex + return rid + + + +def access_size(work): + p = os.path.join(work, ACCESS_LOG) + return os.path.getsize(p) if os.path.isfile(p) else 0 + + +def _scope(p): + """A path in one form for the scope test (as util.cpp ScopeForm): \\\\?\\C:\\x as C:\\x, \\\\?\\UNC\\s\\x as \\\\s\\x.""" + p = p.strip() + if p[:8].upper() == "\\\\?\\UNC\\": + p = "\\\\" + p[8:] + elif p[:4] in ("\\\\?\\", "\\??\\"): + p = p[4:] + if p.startswith("\\\\"): + return os.path.normcase(p) # UNC: never inside a local work folder + return os.path.normcase(os.path.abspath(p)) + + +def _inside(path, root): + p = _scope(path) + return p == root or p.startswith(root.rstrip("\\") + "\\") + + +def _stop(args, what, line): + raise SystemExit("TEST ISOLATION FAILED: the launcher (%s) %s:\n %s\nA test build must take its store libraries from " + "WRONGGPU_TEST_LIBRARIES only (util.h LibraryAccess)." % (" ".join(str(a) for a in args[:2]), what, line.strip())) + + +def check_isolation(log, work, start, args, access_start=0, expect_scan=False, logged=True): + """After a launcher or installer call: its new access-log lines (from byte access_start) and its new main-log lines + (from byte start; log None: none) must name nothing outside the work folder. The access log must be there, written by + this run (its first line "run "), and (logged: a program of ours) have new lines from this call (every run + of ours writes its start line). expect_scan: the call was meant to search the test libraries, so at least one access + inside \\libraries must be there (the guard's sensitivity).""" + root = os.path.normcase(os.path.abspath(work)) + access = os.path.join(work, ACCESS_LOG) + try: + with open(access, "rb") as f: + first = f.readline().decode("utf-8", "replace").strip() + f.seek(access_start) + lines = f.read().decode("utf-8", "replace").splitlines() + except OSError as e: + _stop(args, "left no readable store access log (the launcher writes it; a missing one means it could not)", "%s (%s)" % (access, e)) + want = _RUN_IDS.get(root) + if want is None or first != "run " + want: + _stop(args, "left an access log that is not this run's", "first line %r, this run is %r" % (first, want)) + if logged and not lines: + _stop(args, "logged nothing (a program of ours writes its start line every run: its access lines are missing)", access) + scanned = False + libs = os.path.normcase(os.path.join(root, "libraries")) + for line in lines: + if line.startswith("library access refused: "): + continue # refused: nothing was read + if not line.startswith("library access: "): + continue + what = line[len("library access: "):] + if what.startswith("registry "): + _stop(args, "read a store's registry key (the PC's own stores)", line) + if not _inside(what, root): + _stop(args, "read outside the test folder", line) + scanned = scanned or _inside(what, libs) + if expect_scan and not scanned: + _stop(args, "was expected to search the test libraries but logged no access there (a guard that sees nothing proves nothing)", + "%d access line(s) in %s" % (len(lines), access)) + if log: # the main log: the searches' own lines (a second net) + try: + with open(log, "rb") as f: + f.seek(start) + text = f.read().decode("utf-8", "replace") + except OSError as e: + _stop(args, "left no readable log", "%s (%s)" % (log, e)) + searches = ("amd fsr 4", "converter: file:", "model: using", "model: rejected", "\\steamapps\\", "\\steamlibrary\\", + "\\epic games\\", "\\gog galaxy\\", "\\gog games\\") + for line in text.splitlines(): + if not any(s in line.lower() for s in searches): + continue + for m in re.finditer(r"(?i)(\\\\[?]\\[^\r\n]*?|\\\\[^\\\s][^\r\n]*?|\b[a-z]:\\[^\r\n]*?)(?=\s\(|\)|;|,\s|\s+not used|$)", line): + if not _inside(m.group(0), root): + _stop(args, "searched outside the test folder", line) + + +def self_test(work): + """The guard's own check, before any launcher call of a runner: every case must hold, else the run stops here.""" + print("\n[isolation] the guard against the PC's own game folders (before any launcher call)") + work = os.path.abspath(work) + os.makedirs(os.path.join(work, "libraries"), exist_ok=True) + access, log = os.path.join(work, ACCESS_LOG), os.path.join(work, "isolation-self-test.log") + inside = os.path.join(work, "libraries", "Game") + cases = [ + ("an access outside the work folder stops the run", "library access: Q:\\NotTheTestFolder\\Store\\Game", "", False, True), + ("an access as a UNC path stops the run", "library access: \\\\server\\share\\Game", "", False, True), + ("an access as \\\\?\\UNC stops the run", "library access: \\\\?\\UNC\\server\\share\\Game", "", False, True), + ("a store registry read stops the run", "library access: registry GOG.com Games", "", False, True), + ("an access inside the work folder passes", "library access: " + inside, "", True, False), + ("an access inside, as \\\\?\\, passes", "library access: \\\\?\\" + inside, "", True, False), + ("a refused access (nothing read) passes", "library access refused: Q:\\NotTheTestFolder\\Store", "", False, False), + ("no access where a scan was expected stops the run", "", "", True, True), + ("a model search line outside the work folder stops the run", "", "model: using Q:\\NotTheTestFolder\\x\\model-file.dll (another game (G))", False, True), + ("an AMD FSR 4 search line outside stops the run", "", "AMD FSR 4 search: candidate Q:\\NotTheTestFolder\\fsr4.dll (4.1.0.0, G)", False, True), + ("a converter line outside stops the run", "", "converter: file: Q:\\NotTheTestFolder\\model-file.dll", False, True), + ("a line that is no search passes", "", "model: reusing Q:\\NotTheTestFolder\\model.wgcm (no search)", False, False), + ] + key = os.path.normcase(os.path.abspath(work)) + saved = _RUN_IDS.get(key) + _RUN_IDS[key] = "self-test-run" + head = b"run self-test-run\r\nprocess 1 WrongGPU start\r\n" + cases += [("an access log of another run stops the run", None, "", False, True), + ("a call that logged nothing stops the run", "", "", False, True)] + bad = [] + for name, aline, mline, expect, stops in cases: + if aline is None: # another run's log + write(access, b"run another-run\r\nprocess 1 WrongGPU start\r\n") + start = 0 + elif name.startswith("a call that logged nothing"): + write(access, head) + start = len(head) # (nothing after this call's start) + else: + write(access, head + ((aline + "\r\n").encode("utf-8") if aline else b"")) + start = 0 + write(log, ("[00:00:00] " + mline + "\r\n").encode("utf-8") if mline else b"") + try: + check_isolation(log, work, 0, ("self-test",), start, expect) + stopped = False + except SystemExit as e: + stopped = "TEST ISOLATION FAILED" in str(e) + print(" %s guard: %s" % ("PASS " if stopped == stops else "FAIL ", name)) + if stopped != stops: + bad.append(name) + os.remove(access) + try: + check_isolation(None, work, 0, ("self-test",)) + missing = False + except SystemExit: + missing = True + print(" %s guard: a missing store access log stops the run" % ("PASS " if missing else "FAIL ")) + if not missing: + bad.append("missing access log") + for p in (log, access): + if os.path.exists(p): + os.remove(p) + if saved is None: + _RUN_IDS.pop(key, None) + else: + _RUN_IDS[key] = saved + if bad: + raise SystemExit("TEST ISOLATION SELF-TEST FAILED (%s): no launcher call is made with a broken guard." % "; ".join(bad)) + + +def run_verbs(t, opti, m): + """Every verb with --noscan searches nothing (no access in the test libraries), against a NON-EMPTY test library (a game + with an AMD FSR 4 file name and a model file name, synthetic bytes), and without --noscan it searches them (the guard's + sensitivity): install, repair, update, uninstall, the window's background refresh (ui-dump), the other model of a + Preset K build, an apply batch with a noscan job then a normal one, and /NOSCAN with /FSR4 and /LIST.""" + print("\n[isolation-verbs] --noscan for every verb, against a non-empty test library") + libgame = os.path.join(t.work, "libraries", "Library Game") + write(os.path.join(libgame, "amd_fidelityfx_upscaler_dx12.dll"), b"MZ synthetic, not AMD's") + write(os.path.join(libgame, "nvngx_dlss.dll"), t.model.file) + libs = os.path.normcase(os.path.join(os.path.abspath(t.work), "libraries")) + + def touched(start): + with open(os.path.join(t.work, ACCESS_LOG), "rb") as f: + f.seek(start) + return [l for l in f.read().decode("utf-8", "replace").splitlines() + if l.startswith("library access: ") and _inside(l[len("library access: "):], libs)] + + root, b, exe = m.game(t, "Isolation Game") + inst = ("--opti=" + opti, "--force", "--experimental") + for verb in ("install", "repair", "update", "uninstall"): + a0 = access_size(t.work) + args = (verb, exe, "--noscan") + (() if verb == "uninstall" else inst) + rc = t.run(*args) + t.check("%s --noscan: exit 0, nothing of the test libraries read (no access there)" % verb, rc == 0 and not touched(a0), (rc, touched(a0)[:3], t.j.get("message"))) + a0 = access_size(t.work) + l0 = os.path.getsize(t.log) if os.path.isfile(t.log) else 0 + t.expect_scan = True # the guard's sensitivity: this call must log accesses in the test libraries + rc = t.run("install", exe, *inst) + t.expect_scan = False + got = touched(a0) + t.check("install without --noscan: the test libraries searched (accesses logged there; the AMD FSR 4 search and the model step)", + rc == 0 and got and any("Library Game" in l for l in got), (rc, got[:3])) + logt = m.read(t.log)[l0:].decode("utf-8", "replace") if os.path.isfile(t.log) else "" # (this call's lines only) + t.check("...the AMD FSR 4 search saw the test library's file (not AMD's: not used); the PC's stores never (the guard)", + "AMD FSR 4 search:" in logt and "Library Game" in logt, t.logtail(800)) + if "(Preset K)" in logt or "Preset K model caches are prepared" in logt: # a Preset K build: the other model's search + t.check("...the other model's search (Preset K build) looked in the test library (its model file), nowhere else", + any(("converter: file:" in l or "model: " in l) and "Library Game" in l for l in logt.splitlines()), t.logtail(800)) + else: + print(" skipped: the other model's search (this launcher is built without Preset K)") + for verb in ("repair", "update"): # the guard's sensitivity for the other verbs that search + a0 = access_size(t.work) + t.expect_scan = True + rc = t.run(verb, exe, *inst) + t.expect_scan = False + t.check("%s without --noscan: the test libraries searched (accesses logged there)" % verb, rc == 0 and touched(a0), (rc, touched(a0)[:3], t.j.get("message"))) + dump = os.path.join(t.work, "ui-dump-isolation.json") + for noscan in (True, False): + a0 = access_size(t.work) + t.expect_scan = not noscan + rc = t.run("ui-dump", "--dump=" + dump, *(("--noscan",) if noscan else ())) + t.expect_scan = False + t.check("the window's background refresh (ui-dump) %s: %s" % ("--noscan" if noscan else "without --noscan", + "nothing of the test libraries read" if noscan else "the test libraries searched"), + rc == 0 and (not touched(a0)) == noscan, (rc, touched(a0)[:3])) + # an apply batch: a noscan job, then a normal one: the second one searches (the no-scan is per job) + jobs = [os.path.join(t.work, "iso-job-%d.txt" % i) for i in (1, 2)] + results = [os.path.join(t.work, "iso-result-%d.txt" % i) for i in (1, 2)] + lad = t.env["LOCALAPPDATA"] + for j, ns in zip(jobs, ("1", "0")): + write(j, "op=repair\r\nexe=%s\r\ndir=%s\r\nlocalappdata=%s\r\nopti=%s\r\nforce=1\r\nexperimental=1\r\nnoscan=%s\r\n" % (exe, b, lad, opti, ns)) + batch = os.path.join(t.work, "iso-batch.txt") + write(batch, "".join("job=%s\r\nresult=%s\r\n" % (j, r) for j, r in zip(jobs, results))) + a0 = access_size(t.work) + t.expect_scan = True + rc = t.run("apply", "--batch=" + batch) + t.expect_scan = False + t.check("apply batch: a noscan job then a normal one: the normal one searches the test libraries (the no-scan never sticks)", + rc == 0 and touched(a0), (rc, touched(a0)[:3], [m.read(r).decode("utf-8", "replace") if os.path.isfile(r) else "" for r in results])) + a0 = access_size(t.work) + write(jobs[0], "op=repair\r\nexe=%s\r\ndir=%s\r\nlocalappdata=%s\r\nopti=%s\r\nforce=1\r\nexperimental=1\r\nnoscan=0\r\n" % (exe, b, lad, opti)) + rc = t.run("apply", "--job=" + jobs[0], "--result=" + results[0], "--noscan") + t.check("apply --noscan: the job searches nothing (its noscan set by the command line)", rc == 0 and not touched(a0), (rc, touched(a0)[:3])) + # /NOSCAN with /FSR4 and /LIST: no search, the report says so + for flag in ("/FSR4", "/LIST"): + a0 = access_size(t.work) + l0 = os.path.getsize(t.log) if os.path.isfile(t.log) else 0 + rc = t.raw([t.exe, "/S", "/NOSCAN", flag, "/LOG=" + t.log]) + with open(t.log, "rb") as f: + f.seek(l0) + said = f.read().decode("utf-8", "replace") + t.check("/S /NOSCAN %s: no search (nothing of the test libraries read), the report says \"libraries not searched\"" % flag, + rc == 0 and not touched(a0) and "libraries not searched (/NOSCAN)" in said, (rc, touched(a0)[:3], said[-400:])) + rc = t.run("uninstall", exe, "--noscan") + m.shutil.rmtree(root, ignore_errors=True) + m.shutil.rmtree(libgame, ignore_errors=True) diff --git a/clean/installer/tests/run_coexist_tests.py b/clean/installer/tests/run_coexist_tests.py index 2e5fcaca..e9f6c70b 100644 --- a/clean/installer/tests/run_coexist_tests.py +++ b/clean/installer/tests/run_coexist_tests.py @@ -615,6 +615,7 @@ def main(): for d in ("games", "data", "localappdata"): shutil.rmtree(os.path.join(work, d), ignore_errors=True) os.makedirs(work, exist_ok=True) + __import__("launcher_isolation").self_test(work) # (#243) first: no launcher call with a broken guard model = suite.cases.f_mini_case(present=0) t = lt.T(os.path.abspath(a.launcher), work, model, None) t.env.pop("WRONGGPU_TEST_PROCESSES", None) diff --git a/clean/installer/tests/run_convert_ignore_tests.py b/clean/installer/tests/run_convert_ignore_tests.py index d3613e5a..f6807fc7 100644 --- a/clean/installer/tests/run_convert_ignore_tests.py +++ b/clean/installer/tests/run_convert_ignore_tests.py @@ -44,6 +44,7 @@ def __init__(self, exe, work): self.out = os.path.join(work, "out.json") self.lad = os.path.join(work, "localappdata") self.env = dict(os.environ, LOCALAPPDATA=self.lad, ProgramData=os.path.join(work, "programdata")) + __import__("launcher_isolation").test_env(self.env, work) # (#243) the store libraries: none of the PC's; every access logged for k in [k for k in self.env if k.upper().startswith("WG_") or k.upper() in ("HIP_PATH", "ROCM_PATH", "LLVM_PATH", "HIP_DEVICE_LIB_PATH")]: self.env.pop(k) os.makedirs(self.lad, exist_ok=True) @@ -68,6 +69,7 @@ def run(self, *args): if os.path.exists(self.log): os.remove(self.log) cmd = [self.exe] + list(args) + ["--noscan", "--log=" + self.log, "--out=" + self.out, "--data=" + self.data] + astart = __import__("launcher_isolation").access_size(self.work) p = subprocess.run(cmd, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600) self.stdout = p.stdout.decode("utf-8", "replace") try: @@ -75,6 +77,7 @@ def run(self, *args): self.j = json.load(f) except (OSError, ValueError): self.j = {} + __import__("launcher_isolation").check_isolation(self.log, self.work, 0, args, astart) # (#243) never the PC's own game folders return p.returncode def logtext(self): @@ -920,6 +923,7 @@ def main(): for d in ("libs", "data", "localappdata", "elsewhere", "programs"): shutil.rmtree(os.path.join(work, d), ignore_errors=True) os.makedirs(work, exist_ok=True) + __import__("launcher_isolation").self_test(work) # (#243) first: no launcher call with a broken guard t = T(os.path.abspath(a.launcher), work) t0 = time.time() test_empty_lists_same_winner(t) diff --git a/clean/installer/tests/run_installer_tests.py b/clean/installer/tests/run_installer_tests.py index 360c89a0..27bb74bd 100644 --- a/clean/installer/tests/run_installer_tests.py +++ b/clean/installer/tests/run_installer_tests.py @@ -161,6 +161,18 @@ def __init__(self, work): # Native Windows: never touch the tester's own WrongGPU data, store registry keys or libraries. self.env = dict(os.environ, LOCALAPPDATA=os.path.join(work, "localappdata"), ProgramData=os.path.join(work, "programdata")) os.makedirs(self.env["LOCALAPPDATA"], exist_ok=True) + # a test build takes its store libraries from this folder only (empty: none) and never reads the PC's own stores + # (detect.h TestLibraries) + libs = os.path.join(work, "libraries") + os.makedirs(libs, exist_ok=True) + self.env["WRONGGPU_TEST_LIBRARIES"] = self.win(libs) + self.access = os.path.join(work, "library-access.log") # every store access of the program, checked after each run (#243) + self.env["WRONGGPU_TEST_ACCESS_LOG"] = self.win(self.access) # (written by the program itself, never created here) + if self.native: + if os.environ.get("WRONGGPU_TESTING") != "1": + raise SystemExit("native Windows runs need an installer built with WRONGGPU_TESTING (and WRONGGPU_TESTING=1 set): " + "only a test build keeps to the test libraries and logs every store access (#243)") + self.env["WRONGGPU_TEST_RUN_ID"] = __import__("launcher_isolation").new_run(work) def close(self): if not self.native: @@ -172,7 +184,10 @@ def win(self, path): def run(self, args, timeout=300, extra_env=None): cmd = list(args) if self.native else [self.wine] + list(args) env = dict(self.env, **(extra_env or {})) + astart = os.path.getsize(self.access) if os.path.isfile(self.access) else 0 p = subprocess.run(cmd, env=env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout) + if self.native: # (#243) never the PC's own game folders (under Wine the prefix is the program's own world) + __import__("launcher_isolation").check_isolation(None, self.work, 0, list(args)[:2], astart) return p.returncode def localappdata(self): @@ -2231,6 +2246,9 @@ def main(): "installs that the installer under test is then run over") a = ap.parse_args() work = os.path.abspath(a.work) + os.makedirs(work, exist_ok=True) + if os.name == "nt": + __import__("launcher_isolation").self_test(work) # (#243) first: no installer run with a broken guard # The synthetic source and its map: version 4 with the optional source F (spec 08 draft 2), variant 1 in the file. model = cases.f_mini_case(present=0) prep = os.path.join(work, "prepared") diff --git a/clean/installer/tests/run_launcher_tests.py b/clean/installer/tests/run_launcher_tests.py index 92c7a88c..f4bbe0e2 100644 --- a/clean/installer/tests/run_launcher_tests.py +++ b/clean/installer/tests/run_launcher_tests.py @@ -25,6 +25,7 @@ import mockpe # noqa: E402 (synthetic PE files with a version resource) import run_installer_tests as suite # noqa: E402 (mock content, snapshots, the synthetic model case) from run_installer_tests import read, sha, snapshot, diff, write, kept_runtime, unrecorded_wronggpu_files # noqa: E402 +import launcher_isolation as iso # noqa: E402 (#243: no search of the PC's own game folders) OPTI_INI = ("[Upscalers]\nDx12Upscaler=auto\n[Libraries]\nFfxDx12SRPath=auto\n[FSR]\nFsr4Update=auto\nUpscalerIndex=auto\n" "[Menu]\nShortcutKey=auto\nShowFps=auto\n") @@ -43,6 +44,9 @@ def __init__(self, exe, work, model, hip): for k in [k for k in self.env if k.upper().startswith("WG_") or k.upper() in ("HIP_PATH", "ROCM_PATH", "LLVM_PATH", "HIP_DEVICE_LIB_PATH")]: self.env.pop(k) os.makedirs(self.env["LOCALAPPDATA"], exist_ok=True) + # the store libraries: \libraries only; every store access logged and checked after each call (launcher_isolation) + iso.test_env(self.env, work) + self.expect_scan = False # set by a test whose next call must search the test libraries (the guard's sensitivity) self.uninstall = suite.uninstall_root(self.env["LOCALAPPDATA"]) # this run's Apps & features root (the build's own) self.j = {} @@ -59,16 +63,24 @@ def run(self, *args, exe=None, data=True, timeout=600, cwd=None): if os.path.exists(self.out): os.remove(self.out) cmd = [exe or self.exe] + list(args) + ["--log=" + self.log, "--out=" + self.out] + (["--data=" + self.data] if data else []) + start, astart = os.path.getsize(self.log) if os.path.isfile(self.log) else 0, iso.access_size(self.work) p = subprocess.run(cmd, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout, cwd=cwd) try: with open(self.out, encoding="utf-8") as f: self.j = json.load(f) except (OSError, ValueError): self.j = {} + iso.check_isolation(self.log, self.work, start, args, astart, self.expect_scan) return p.returncode - def raw(self, cmd, timeout=600): - return subprocess.run(cmd, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout).returncode + def raw(self, cmd, timeout=600, guard=True): + """A program run as it is (no --log/--out added); its store accesses checked as for run() (guard=False only for the old + baseline installer, which predates the access log: --run-old-baseline).""" + start, astart = os.path.getsize(self.log) if os.path.isfile(self.log) else 0, iso.access_size(self.work) + rc = subprocess.run(cmd, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout).returncode + if guard: + iso.check_isolation(self.log, self.work, start, cmd[1:3], astart) + return rc def logtail(self, n=1500): try: @@ -94,6 +106,13 @@ def entries_for(self, folder): return {k: v for k, v in self.reg_keys().items() if v.get("InstallLocation", "").lower() == folder.lower()} +RUN_OLD_BASELINE = False + + +def test_isolation_verbs(t, opti): # #243, in launcher_isolation.py + iso.run_verbs(t, opti, sys.modules[__name__]) + + def wait_gone(path, seconds=20): for _ in range(seconds * 5): if not os.path.exists(path): @@ -733,9 +752,13 @@ def test_adopt_and_self_install(t, opti, baseline): if not baseline or not os.path.isfile(baseline): print(" (no 0.5.1 installer given: skipped)") return + if not RUN_OLD_BASELINE: # (#243) an old build ignores WRONGGPU_TEST_LIBRARIES: never here unless asked, in a sandbox + print(" skipped: the 0.5.1 installer predates the test isolation (it may read the PC's own libraries); --run-old-baseline " + "runs it, only in a fully redirected sandbox") + return root, b, exe = game(t, "Old Game", exe="Old.exe") before = snapshot(root) - rc = t.raw([baseline, "/S", "/LOG=" + t.log, "/NOSCAN", "/FORCE", "/EXPERIMENTAL", "/DIR=" + root, "/OPTISCALER=" + opti]) + rc = t.raw([baseline, "/S", "/LOG=" + t.log, "/NOSCAN", "/FORCE", "/EXPERIMENTAL", "/DIR=" + root, "/OPTISCALER=" + opti], guard=False) # the 0.5.1 test build wrote its entry under the shared test root of its time; released builds all use the one Apps & # features root, so the entry moves to this run's root (where this build looks), as if both had written the same one old_root = r"HKCU\Software\WrongGPU-CandidateTests\Uninstall" @@ -2986,9 +3009,11 @@ def link(): stdout=subprocess.PIPE, stderr=subprocess.PIPE).returncode == 0 def invoke_without_log(exe, verb): out = os.path.join(work, verb + "-result.json") + astart = iso.access_size(t.work) proc = subprocess.Popen([exe, verb, "--out=" + out], env=t.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE) proc.communicate(timeout=180) log = os.path.join(t.env.get("TEMP", ""), "WrongGPU-junction-%d.log" % proc.pid) + iso.check_isolation(log if os.path.isfile(log) else None, t.work, 0, (verb,), astart) # (#243: every launcher call) text = read(log).decode("utf-8", "replace") if os.path.isfile(log) else "" if os.path.isfile(log): os.remove(log) return proc.returncode, text @@ -3013,11 +3038,15 @@ def main(): ap.add_argument("--work", required=True) ap.add_argument("--baseline", help="the 0.5.1 installer (WRONGGPU_TESTING build, same test map) for the adoption cases") ap.add_argument("--hip", help="mock amdhip64_7.dll (WRONGGPU_HIP_RUNTIME)") + ap.add_argument("--run-old-baseline", action="store_true", help="also run --baseline (it predates the test isolation: only in a fully redirected sandbox)") ap.add_argument("--only", nargs="*", default=[], help="development: only these tests (names without test_) after the basics") ap.add_argument("--synth-f", default=os.environ.get("WGC_SYNTH_BINS", ""), help="folder with the spec team's synthetic preset-F file synth-common-v0.bin (the test build's F map is synth-r0)") a = ap.parse_args() + global RUN_OLD_BASELINE + RUN_OLD_BASELINE = a.run_old_baseline work = os.path.abspath(a.work) + iso.self_test(work) # (#243) first: no launcher call with a broken guard for d in ("games", "data", "localappdata", "programs", "startmenu"): shutil.rmtree(os.path.join(work, d), ignore_errors=True) os.makedirs(work, exist_ok=True) @@ -3038,6 +3067,7 @@ def main(): for f in t.failures: print(" FAILED:", f) return 1 if t.failures else 0 + test_isolation_verbs(t, opti) test_scan_and_refusals(t, opti) test_install_settings_uninstall(t, opti) test_slot(t) diff --git a/clean/installer/tests/run_model_k_tests.py b/clean/installer/tests/run_model_k_tests.py index 105f23ca..de469038 100644 --- a/clean/installer/tests/run_model_k_tests.py +++ b/clean/installer/tests/run_model_k_tests.py @@ -131,6 +131,7 @@ def main(): d = os.path.join(os.path.abspath(a.work), "choice") shutil.rmtree(d, ignore_errors=True) os.makedirs(d) + __import__("launcher_isolation").self_test(d) # (#243) first: no launcher call with a broken guard t = ml.T(os.path.abspath(a.launcher), d) t.model_path = t.model # (test_choice replaces t.model with the Model M case, as the Model L script does) test_choice(t) diff --git a/clean/installer/tests/run_model_l_tests.py b/clean/installer/tests/run_model_l_tests.py index 5871717e..0a51ca91 100644 --- a/clean/installer/tests/run_model_l_tests.py +++ b/clean/installer/tests/run_model_l_tests.py @@ -63,6 +63,7 @@ def __init__(self, exe, work): shutil.rmtree(self.lad, ignore_errors=True) os.makedirs(self.lad) self.env = dict(os.environ, LOCALAPPDATA=self.lad, ProgramData=os.path.join(work, "programdata")) + __import__("launcher_isolation").test_env(self.env, work) # (#243) the store libraries: none of the PC's; every access logged for k in [k for k in self.env if k.upper().startswith("WG_") or k.upper() in ("HIP_PATH", "ROCM_PATH", "LLVM_PATH", "HIP_DEVICE_LIB_PATH")]: self.env.pop(k) self.j, self.stdout = {}, "" @@ -81,6 +82,7 @@ def run(self, *args): if os.path.exists(p): os.remove(p) cmd = [self.exe] + list(args) + ["--noscan", "--log=" + self.log, "--out=" + self.out, "--data=" + os.path.join(self.work, "data")] + astart = __import__("launcher_isolation").access_size(self.work) p = subprocess.run(cmd, env=self.env, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=600) self.stdout = p.stdout.decode("utf-8", "replace") try: @@ -88,6 +90,7 @@ def run(self, *args): self.j = json.load(f) except (OSError, ValueError): self.j = {} + __import__("launcher_isolation").check_isolation(self.log, self.work, 0, args, astart) # (#243) never the PC's own game folders return p.returncode def model(self, *rel): @@ -326,6 +329,8 @@ def main(): if not a.launcher and not a.launcher_synth and not a.launcher_choice: ap.error("--launcher, --launcher-synth or --launcher-choice is needed") work = os.path.abspath(a.work) + os.makedirs(work, exist_ok=True) + __import__("launcher_isolation").self_test(work) # (#243) first: no launcher call with a broken guard passed, failures = 0, [] for exe, part, fn in ((a.launcher, "shipped", test_shipped_map), (a.launcher_synth, "synth", lambda t: test_conversion(t, a.reference_launcher)), (a.launcher_choice, "choice", test_choice), (a.launcher_synth, "no-choice", test_no_choice)):