diff --git a/docs/boot.md b/docs/boot.md index 2b7186e30..f73847c41 100644 --- a/docs/boot.md +++ b/docs/boot.md @@ -99,7 +99,9 @@ Useful switches: * `LEONIDA_SKIP_INT3=1` (with `--trace`) steps over each int3 in guest code and logs it once per address as an `int3_skipped` event, so a boot can be followed past soft asserts (an int3 followed by code that carries on). Diagnostic only: a correct run reaches none, so each skipped address is a finding, not a fix. A failed guest file - open is always traced as `file_open_failed` (path, error, caller) when `--trace` is on. + open is traced as `file_open_failed` (path, error, caller) for the first 256 failures when `--trace` is on. + A separate budget records the first 600 successful opens in `CreateFileW` and `CreateFile2` as `file_open` + events with the requested path, access mask and caller. * `LEONIDA_STUB_PIPELINES=1` creates a pipeline whose translated shader is missing (no shader library, or no entry for its program) as a no-op stub instead of failing `CreateGraphicsPipelineState` / `CreateComputePipelineState`. The stub is never registered, so `SetPipelineState` binds no PSO and its draws and dispatches are no-ops (D3D-482). diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index 48c8af360..4f016203d 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -1491,6 +1491,7 @@ if(BUILD_TESTING) endif() if(WIN32 AND TARGET file_system_test) add_test(NAME file_system COMMAND file_system_test) + add_test(NAME file_system_open_trace COMMAND file_system_test --trace-opens) endif() if(WIN32 AND TARGET int3_skip_test) add_test(NAME int3_skip COMMAND int3_skip_test) diff --git a/runtime/README.md b/runtime/README.md index fadaa47ef..ae3f41c0e 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -561,6 +561,7 @@ from the ANSI code page. | `xmem_attributes` | the first `XMemAlloc`, `XMemFree`, `XMemAllocDefault`, `XMemFreeDefault` and `XMemVirtualAlloc` for each distinct raw attribute word (not every allocation) | `api`, `attributes`, `size` (not for the frees), `caller` (module+RVA) | | `xmem_physical_pages` | the first `XMemAllocatePhysicalPages` for each distinct attribute word | `api`, `attributes`, `pages`, `caller` | | `file_open_failed` | a guest `CreateFileW`/`CreateFile2` that fails (at most 256 events) | `api`, `path` (as the title passed it), `error` (Win32), `caller` (module+RVA) | +| `file_open` | a guest `CreateFileW`/`CreateFile2` that succeeds (the first 600, counted apart from the failures) | `api`, `path` (as the title passed it), `access` (the requested access mask), `caller` | | `int3_skipped` | with `LEONIDA_SKIP_INT3` set: first int3 at each address, which is then stepped over | `at` (module+RVA) | | `debug_string` | `OutputDebugStringA/W` with no debugger attached | `text` | | `exception` | any exception, before handlers run | `code`, `at`, `access`/`address` for access violations, `cpp` for C++ throws, `stack` (up to 16 frames) | diff --git a/runtime/shims/file_system.cpp b/runtime/shims/file_system.cpp index dad6f37a8..8458236b1 100644 --- a/runtime/shims/file_system.cpp +++ b/runtime/shims/file_system.cpp @@ -687,41 +687,47 @@ std::filesystem::path overlay_dir() { return g_overlay_dir; } +std::string trace_path(LPCWSTR name) { + std::string utf8; + if (const int n = WideCharToMultiByte(CP_UTF8, 0, name, -1, nullptr, 0, nullptr, nullptr); n > 1) { + utf8.resize(static_cast(n) - 1); + WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8.data(), n, nullptr, nullptr); + } + return rt::startup_trace::quote(utf8); +} + // gta6rt --trace FILE: a failed open of a guest path goes to the startup trace as a "file_open_failed" event (the // path the title passed, the Win32 error, the caller as module+RVA), so a boot that stops on a missing or refused -// file names it. Failures only, and at most 256 of them, so a title probing many optional files can't fill the file. +// file names it. At most 256 failures are recorded. void note_open_failure(const char* api, LPCWSTR name, DWORD error, const void* caller) { if (!rt::startup_trace::enabled()) return; static std::atomic left{256}; if (left.fetch_sub(1, std::memory_order_relaxed) <= 0) return; - std::string utf8; - if (const int n = WideCharToMultiByte(CP_UTF8, 0, name, -1, nullptr, 0, nullptr, nullptr); n > 1) { - utf8.resize(static_cast(n) - 1); - WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8.data(), n, nullptr, nullptr); - } rt::startup_trace::event( "file_open_failed", - std::string("\"api\":\"") + api + "\",\"path\":" + rt::startup_trace::quote(utf8) + + std::string("\"api\":\"") + api + "\",\"path\":" + trace_path(name) + ",\"error\":" + std::to_string(error) + ",\"caller\":" + rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast(caller)))); } -// LOCAL DIAGNOSTIC: gta6rt --trace FILE also records the first 600 successful opens ("file_open": path, access, -// caller), so the startup trace shows which file the title was working on when it stopped making progress. +// The first 600 successful opens go to the trace as "file_open" events (the path the title passed, the access mask, +// the caller), so a boot that stops making progress shows which files it had reached. Their budget is separate from +// the failures', so neither kind of event can use up the other's. void note_open(const char* api, LPCWSTR name, DWORD access, const void* caller) { if (!rt::startup_trace::enabled()) return; - static std::atomic left{600}; - if (left.fetch_sub(1, std::memory_order_relaxed) <= 0) return; - std::string utf8; - if (const int n = WideCharToMultiByte(CP_UTF8, 0, name, -1, nullptr, 0, nullptr, nullptr); n > 1) { - utf8.resize(static_cast(n) - 1); - WideCharToMultiByte(CP_UTF8, 0, name, -1, utf8.data(), n, nullptr, nullptr); - } + static std::atomic slots{600}; + unsigned left = slots.load(std::memory_order_relaxed); + while (left && !slots.compare_exchange_weak(left, left - 1, std::memory_order_relaxed)) {} + if (!left) return; + // OPEN_ALWAYS and CREATE_ALWAYS report ERROR_ALREADY_EXISTS through the last error of a successful open, and + // writing the event changes it: the title must see the value the open left. + const DWORD error = GetLastError(); rt::startup_trace::event( "file_open", - std::string("\"api\":\"") + api + "\",\"path\":" + rt::startup_trace::quote(utf8) + ",\"access\":" + + std::string("\"api\":\"") + api + "\",\"path\":" + trace_path(name) + ",\"access\":" + std::to_string(access) + ",\"caller\":" + rt::startup_trace::quote(rt::startup_trace::where(reinterpret_cast(caller)))); + SetLastError(error); } // Reads one byte of every 4 KiB page of [buffer, buffer + size) from user mode, so that pages committed on first diff --git a/runtime/test/file_system_test.cpp b/runtime/test/file_system_test.cpp index 493ffd6bf..1cf2af607 100644 --- a/runtime/test/file_system_test.cpp +++ b/runtime/test/file_system_test.cpp @@ -241,9 +241,65 @@ void check_read_file_first_touch(const std::filesystem::path& root) { g_first_touch = nullptr; } +// --trace-opens, in a process of its own because the budgets are per process: with the startup trace on, the first +// 600 successful opens are "file_open" events and the rest are not, a failed open keeps its own event and budget, an +// open with the trace off writes nothing, and the last error of a successful OPEN_ALWAYS is the one the open left. +void check_successful_open_trace(const std::filesystem::path& root) { + const auto package = root / "package"; + const auto overlay = root / "overlay"; + write_path(package / "off.txt", "off"); + write_path(overlay / "existing.txt", "on"); + rt::fs::configure(package, overlay); + const auto wide = resolve("kernel32.dll", "CreateFileW"); + const auto second = resolve("api-ms-win-core-file-l1-2-0.dll", "CreateFile2"); + check(wide && second, "both file-open imports resolve"); + if (!wide || !second) return; + + const auto open_read = [&](const wchar_t* path) { + HANDLE h = wide(path, GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, 0, nullptr); + return h != INVALID_HANDLE_VALUE && CloseHandle(h); + }; + check(open_read(L"GAME:\\off.txt"), "a file opens before tracing starts"); + const auto trace = root / "trace.jsonl"; + if (!rt::startup_trace::open(trace.c_str())) { + check(false, "open the trace file"); + return; + } + HANDLE h = second(L"GAME:\\existing.txt", GENERIC_READ, FILE_SHARE_READ, OPEN_ALWAYS, nullptr); + const DWORD error = GetLastError(); + check(h != INVALID_HANDLE_VALUE && error == ERROR_ALREADY_EXISTS, "OPEN_ALWAYS preserves its success last-error"); + if (h != INVALID_HANDLE_VALUE) CloseHandle(h); + bool all_opened = true; + for (unsigned i = 0; i < 601; ++i) { + if (!open_read(L"GAME:\\existing.txt")) all_opened = false; + } + check(all_opened, "file opens still succeed beyond the trace limit"); + check(!open_read(L"GAME:\\missing.txt") && GetLastError() == ERROR_FILE_NOT_FOUND, + "a missing file still returns its native error"); + rt::startup_trace::close(); + + std::ifstream in(trace); + unsigned wide_events = 0, second_events = 0, failed_events = 0; + bool fields_match = true, off_path_absent = true; + for (std::string line; std::getline(in, line);) { + if (line.find("off.txt") != std::string::npos) off_path_absent = false; + if (line.find("\"kind\":\"file_open_failed\"") != std::string::npos) ++failed_events; + if (line.find("\"kind\":\"file_open\"") == std::string::npos) continue; + if (line.find("\"api\":\"CreateFileW\"") != std::string::npos) ++wide_events; + if (line.find("\"api\":\"CreateFile2\"") != std::string::npos) ++second_events; + if (line.find("\"path\":\"GAME:\\\\existing.txt\"") == std::string::npos || + line.find("\"access\":" + std::to_string(GENERIC_READ) + ",") == std::string::npos || + line.find("\"caller\":\"host:") == std::string::npos) fields_match = false; + } + check(second_events == 1, "CreateFile2 emits a success event"); + check(wide_events == 599, "CreateFileW fills the remaining success budget"); + check(wide_events + second_events == 600 && fields_match, "600 events contain the guest path, access and caller"); + check(failed_events == 1 && off_path_absent, "failure reporting is independent and disabled tracing emits nothing"); +} + } // namespace -int main() { +int main(int argc, char** argv) { // A folder of this run's own: Wine hands each fresh prefix the same small process ids, so a folder named after // the process id alone can be one an earlier run left behind. LARGE_INTEGER now{}; @@ -256,8 +312,12 @@ int main() { // An exception from one check (std::filesystem throws on a host error) ends the checks, not the run: it is a // failure, the folder is still removed and the summary is still printed. try { - check_read_file_first_touch(root / "first_touch"); - run_checks(root); + if (argc == 2 && std::string(argv[1]) == "--trace-opens") { + check_successful_open_trace(root); + } else { + check_read_file_first_touch(root / "first_touch"); + run_checks(root); + } } catch (const std::exception& e) { check(false, (std::string("the checks run to the end (stopped by: ") + e.what() + ")").c_str()); }