diff --git a/docs/boot.md b/docs/boot.md index 2b7186e302..0b6a88907b 100644 --- a/docs/boot.md +++ b/docs/boot.md @@ -100,6 +100,10 @@ Useful switches: `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. +* `LEONIDA_SKIP_ASSERTS=N` steps over up to N int3 in guest code, with or without `--trace`, and prints each one + as `[rt] LEONIDA_SKIP_ASSERTS: int3 at +0x... stepped over (k of N)`; the next one after N ends the run as + before. An int3 in a host module is never stepped over. The same caveat: each skipped address is a finding + ([runtime/README.md](../runtime/README.md), "Guest asserts"). * `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 e9cb8d4345..d52d072d04 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -449,6 +449,16 @@ if(WIN32 AND LEONIDA_BUILD_WINDOWS_RUNTIME) target_compile_options(int3_skip_test PRIVATE /W4 /permissive- /utf-8) target_link_options(int3_skip_test PRIVATE /OPT:NOREF) + # LEONIDA_SKIP_ASSERTS (src/assert_skip.h): int3 in a page no host module owns, stepped over up to a limit. + add_executable(assert_skip_test + test/assert_skip_test.cpp + shims/registry.cpp + ) + target_include_directories(assert_skip_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src) + target_compile_definitions(assert_skip_test PRIVATE WIN32_LEAN_AND_MEAN NOMINMAX) + target_compile_options(assert_skip_test PRIVATE /W4 /permissive- /utf-8) + target_link_options(assert_skip_test PRIVATE /OPT:NOREF) + # Offline WinHTTP for mapped images (the package's XCurl.dll and libHttpClient.GDK.dll); no network is touched. add_executable(winhttp_offline_test test/winhttp_offline_test.cpp @@ -1548,6 +1558,9 @@ if(BUILD_TESTING) if(WIN32 AND TARGET int3_skip_test) add_test(NAME int3_skip COMMAND int3_skip_test) endif() + if(WIN32 AND TARGET assert_skip_test) + add_test(NAME assert_skip COMMAND assert_skip_test) + endif() if(WIN32 AND TARGET winhttp_offline_test) add_test(NAME winhttp_offline COMMAND winhttp_offline_test) endif() @@ -1734,6 +1747,12 @@ if(BUILD_TESTING) "-DTOOL=$" "-DWANT=${GTA6RT_STACK}" -P "${CMAKE_CURRENT_SOURCE_DIR}/test/check_stack_reserve.cmake") add_test(NAME run_tiny_game_stack COMMAND gta6rt --run $ -- hello 42 stack) + # LEONIDA_SKIP_ASSERTS (src/assert_skip.h): two int3 in the mapped image, in no host module, are stepped over and + # the game carries on to its normal exit. Without the variable the first one would end the run. + add_test(NAME run_tiny_game_skip_asserts COMMAND gta6rt --run $ -- hello 42 int3) + set_tests_properties(run_tiny_game_skip_asserts PROPERTIES + ENVIRONMENT "LEONIDA_SKIP_ASSERTS=2" + FAIL_REGULAR_EXPRESSION "tiny_game: FAIL|LEONIDA_SKIP_ASSERTS: [^\n]* is not") # A /GA executable under --run and --init reads its TLS from slot 0, and the trace names gta6rt.exe as the image # whose index 0 moved. add_test(NAME run_tiny_tls_ga COMMAND ${CMAKE_COMMAND} diff --git a/runtime/README.md b/runtime/README.md index 1573b8883c..595d20b30b 100644 --- a/runtime/README.md +++ b/runtime/README.md @@ -101,6 +101,25 @@ The thunk keeps the Win64 argument registers and the return address intact (it i reaches a shared stub that saves them, so a floating-point caller still reads 0.0 from xmm0 and a debugger can unwind through it. +### Guest asserts: `LEONIDA_SKIP_ASSERTS` + +A title's assert ends in an `int3`. Without a debugger that breakpoint is unhandled and the run ends there. +`LEONIDA_SKIP_ASSERTS=N` (a decimal number, 1 to 1000000; diagnostic, off by default) makes `--init` and `--run` +install a vectored exception handler ([src/assert_skip.h](src/assert_skip.h)) before any image code runs. It steps +over up to N `int3` (the byte 0xCC) at addresses in no host module, the mapped images and code a title wrote +itself, and prints one line for each: + +``` +[rt] LEONIDA_SKIP_ASSERTS: int3 at game.exe+0x1234 stepped over (1 of 3) +``` + +After N it says so once (`3 int3 stepped over; the one at ... is not`) and the next `int3` ends the run as before. An +`int3` in a host module (gta6rt, its shims, a system DLL) is never stepped over. Unset, empty or `0` installs +nothing; any other value is reported and treated as `0`. No `--trace` is needed; with one, each skip is also an +`int3_skipped` event and the trace's own handler records the breakpoint and its stack first. `LEONIDA_SKIP_INT3` +(with `--trace`) is the older switch: it steps over every guest `int3` without a limit and logs each address once; +while a trace is open it runs first. Each skipped address is a finding, not a fix: a correct run reaches none. + ### Compat modules: `LEONIDA_COMPAT_DIR` Opt-in. When `LEONIDA_COMPAT_DIR` names a folder, an import whose DLL is a file in that folder (matched by file name, @@ -598,7 +617,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) | -| `int3_skipped` | with `LEONIDA_SKIP_INT3` set: first int3 at each address, which is then stepped over | `at` (module+RVA) | +| `int3_skipped` | with `LEONIDA_SKIP_INT3` set: first int3 at each address, which is then stepped over; with `LEONIDA_SKIP_ASSERTS` (below): every int3 it steps over | `at` (module+RVA); `skip` (the count, `LEONIDA_SKIP_ASSERTS` only) | | `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) | | `exit` / `error` | `--run` returns, or the loader throws | `code` / `what` | @@ -606,7 +625,7 @@ from the ANSI code page. Every line also has `seq` and `t` (seconds since the trace opened). Addresses are `module+0xRVA` for mapped images, `host:name.dll+0xRVA` for host modules. The exception handler only records: it returns `EXCEPTION_CONTINUE_SEARCH`, so the process behaves the same with or without the trace (except under the diagnostic `LEONIDA_SKIP_INT3`, which -steps over int3). It works with any `LEONIDA_UNIMPLEMENTED` policy. +steps over int3; `LEONIDA_SKIP_ASSERTS` steps over int3 with or without the trace). It works with any `LEONIDA_UNIMPLEMENTED` policy. The file can quote what the game prints, so it stays on your machine. In issues and pull requests, share counts or lines from synthetic fixtures only. diff --git a/runtime/src/assert_skip.h b/runtime/src/assert_skip.h new file mode 100644 index 0000000000..9565d8dbdf --- /dev/null +++ b/runtime/src/assert_skip.h @@ -0,0 +1,151 @@ +#pragma once + +// LEONIDA_SKIP_ASSERTS=N (diagnostic, off by default): step over up to N int3 breakpoints in guest code. +// +// A title's assert ends in an int3 (0xCC). With a debugger attached that is a stop to look at; without one it is an +// unhandled EXCEPTION_BREAKPOINT and the run ends there. gta6rt installs this vectored handler when the variable is a +// number above 0 (main.cpp, before any image code runs). For an EXCEPTION_BREAKPOINT whose address is in no host +// module (the Windows loader knows none at that address: the mapped game images, or code a title wrote itself) and +// whose byte there is 0xCC, it logs one line, moves RIP past the int3 and continues; after N of them it stops doing so, +// says so once, and the next int3 ends the run as before. An int3 in a host module (gta6rt, its shims, a system DLL, +// DbgBreakPoint) is never stepped over. +// +// RIP: on Windows x64 an int3's ExceptionAddress and the context's RIP both name the 0xCC (the kernel backs RIP up +// over it), and Wine reports the same; the handler also takes a RIP one past the 0xCC, which it leaves as it is. +// Any other RIP is not this int3's, and the exception is passed on. +// +// Unlike LEONIDA_SKIP_INT3 (startup_trace.h), it needs no --trace, counts every int3 rather than each address once, +// and stops after N, so a loop of asserts can't spin forever. With --trace each skip is also an `int3_skipped` event, +// and the trace's own handler, which runs first, records the exception and its stack. The handler is added last, so +// every other vectored handler sees the breakpoint first. Each skipped address is a finding, not a fix: a correct run +// reaches none. +// +// The log lines quote addresses as module+RVA only; they stay on the player's machine like the startup trace does. + +#include + +#include +#include +#include +#include +#include +#include + +#include "../shims/shims.h" +#include "../shims/startup_trace.h" + +namespace rt::assert_skip { + +inline constexpr wchar_t kVariable[] = L"LEONIDA_SKIP_ASSERTS"; +// Far above any useful count; it only keeps the number in an unsigned. +inline constexpr unsigned long kMaxLimit = 1000000; + +// The limit LEONIDA_SKIP_ASSERTS asks for: 0 when the text is null or empty, "0", or not a decimal number in +// 0..kMaxLimit (then `*valid`, when given, is false). +inline unsigned parse_limit(const wchar_t* text, bool* valid = nullptr) { + if (valid) *valid = true; + if (!text || !*text) return 0; + if (*text < L'0' || *text > L'9') { // no sign, no leading space: wcstoul would take "-1" as ULONG_MAX + if (valid) *valid = false; + return 0; + } + errno = 0; + wchar_t* end = nullptr; + const unsigned long n = std::wcstoul(text, &end, 10); + if (errno != 0 || !end || *end != L'\0' || n > kMaxLimit) { + if (valid) *valid = false; + return 0; + } + return static_cast(n); +} + +// LEONIDA_SKIP_ASSERTS as a limit; a value that is not one is reported on stderr and counts as 0 (off). +inline unsigned limit_from_environment() { + wchar_t value[32] = {}; + const DWORD length = GetEnvironmentVariableW(kVariable, value, 32); + if (length == 0) return 0; + bool valid = false; + const unsigned limit = length < 32 ? parse_limit(value, &valid) : 0; + if (length >= 32 || !valid) + std::fprintf(stderr, "[rt] LEONIDA_SKIP_ASSERTS: expected a number of int3 to step over (0..%lu); off\n", + kMaxLimit); + return limit; +} + +namespace detail { +inline std::atomic limit{0}; +inline std::atomic claimed{0}; // skips handed out, may pass `limit` by the declined ones +inline std::atomic exhausted_logged{false}; +inline void* veh = nullptr; + +// True when the Windows loader knows a module at `address`: gta6rt, its DLLs, system DLLs. The Loader's mapped +// images are not among them. +inline bool in_host_module(const void* address) { + HMODULE module = nullptr; + return GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + static_cast(address), &module) && module; +} + +// The byte at `address`, read only when the page is committed and readable. +inline bool read_byte(const void* address, unsigned char& byte) { + MEMORY_BASIC_INFORMATION mbi{}; + if (!address || !VirtualQuery(address, &mbi, sizeof mbi) || mbi.State != MEM_COMMIT) return false; + if (mbi.Protect & (PAGE_GUARD | PAGE_NOACCESS)) return false; + const DWORD readable = PAGE_READONLY | PAGE_READWRITE | PAGE_WRITECOPY | PAGE_EXECUTE_READ | + PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY; + if (!(mbi.Protect & readable)) return false; + byte = *static_cast(address); + return true; +} + +inline LONG CALLBACK handler(EXCEPTION_POINTERS* info) { + if (!info || !info->ExceptionRecord || !info->ContextRecord) return EXCEPTION_CONTINUE_SEARCH; + const EXCEPTION_RECORD& r = *info->ExceptionRecord; + if (r.ExceptionCode != EXCEPTION_BREAKPOINT) return EXCEPTION_CONTINUE_SEARCH; + const unsigned max = limit.load(std::memory_order_relaxed); + if (max == 0) return EXCEPTION_CONTINUE_SEARCH; + const void* at = r.ExceptionAddress; + const auto at_word = reinterpret_cast(at); + CONTEXT& c = *info->ContextRecord; + if (c.Rip != at_word && c.Rip != at_word + 1) return EXCEPTION_CONTINUE_SEARCH; // not this int3's RIP + unsigned char byte = 0; + if (in_host_module(at) || !read_byte(at, byte) || byte != 0xCC) return EXCEPTION_CONTINUE_SEARCH; + const unsigned n = claimed.fetch_add(1, std::memory_order_relaxed) + 1; + if (n > max) { + if (!exhausted_logged.exchange(true, std::memory_order_relaxed)) + std::fprintf(stderr, "[rt] LEONIDA_SKIP_ASSERTS: %u int3 stepped over; the one at %s is not\n", max, + shims::module_offset(at_word).c_str()); + return EXCEPTION_CONTINUE_SEARCH; + } + const std::string where = shims::module_offset(at_word); + std::fprintf(stderr, "[rt] LEONIDA_SKIP_ASSERTS: int3 at %s stepped over (%u of %u)\n", where.c_str(), n, max); + std::fflush(stderr); + startup_trace::event("int3_skipped", "\"at\":" + startup_trace::quote(where) + ",\"skip\":" + std::to_string(n)); + if (c.Rip == at_word) c.Rip = at_word + 1; + return EXCEPTION_CONTINUE_EXECUTION; +} +} // namespace detail + +// Steps over up to `limit` guest int3 from now on (0 removes the handler). Returns false only when the handler could +// not be added. Called once, from main.cpp; tests call it again to start over. +inline bool install(unsigned limit) { + detail::limit.store(0, std::memory_order_relaxed); + if (detail::veh) RemoveVectoredExceptionHandler(detail::veh); + detail::veh = nullptr; + detail::claimed.store(0, std::memory_order_relaxed); + detail::exhausted_logged.store(false, std::memory_order_relaxed); + if (limit == 0) return true; + detail::limit.store(limit, std::memory_order_relaxed); + detail::veh = AddVectoredExceptionHandler(0, &detail::handler); // last: the other handlers see it first + if (!detail::veh) detail::limit.store(0, std::memory_order_relaxed); + return detail::veh != nullptr; +} + +// int3 stepped over since install. +inline unsigned skipped() { + const unsigned claimed = detail::claimed.load(std::memory_order_relaxed); + const unsigned max = detail::limit.load(std::memory_order_relaxed); + return claimed < max ? claimed : max; +} + +} // namespace rt::assert_skip diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index f019977759..3f8bf76227 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -141,6 +141,7 @@ void start_title_peek(std::uint8_t* base) { } // namespace +#include "assert_skip.h" #include "base_reservation.h" #include "code_hooks.h" #include "cpu_emulation.h" @@ -407,6 +408,17 @@ int wmain(int argc, wchar_t** argv) { if (!rt::cpu_emulation::install()) std::fprintf(stderr, "[rt] cpu: AddVectoredExceptionHandler failed (error %lu); instructions this CPU lacks " "will stop the game\n", GetLastError()); + // LEONIDA_SKIP_ASSERTS=N (src/assert_skip.h): up to N int3 in guest code are logged and stepped over. Off unless + // set; installed before the first TLS callback, the first image code that runs. + if (init || run) + if (const unsigned skip_asserts = rt::assert_skip::limit_from_environment()) { + if (rt::assert_skip::install(skip_asserts)) + std::fprintf(stderr, "[rt] LEONIDA_SKIP_ASSERTS=%u: up to %u int3 in guest code are stepped over " + "(diagnostic)\n", skip_asserts, skip_asserts); + else + std::fprintf(stderr, "[rt] LEONIDA_SKIP_ASSERTS: AddVectoredExceptionHandler failed (error %lu); off\n", + GetLastError()); + } try { rt::Loader loader(game_dir); diff --git a/runtime/test/assert_skip_test.cpp b/runtime/test/assert_skip_test.cpp new file mode 100644 index 0000000000..6715fa2d79 --- /dev/null +++ b/runtime/test/assert_skip_test.cpp @@ -0,0 +1,135 @@ +// LEONIDA_SKIP_ASSERTS=N (src/assert_skip.h), with synthetic code only: +// - the value: unset, empty and 0 are off; a decimal number up to 1000000 is the limit; anything else is off; +// - off, nothing is installed and an int3 reaches the next handler as before; +// - on, an int3 in memory no host module owns (a page this test writes, as the loader maps a game image) is +// stepped over, logged with its address, and the code after it runs; +// - only N of them: the next one is passed on, as are an int3 in a host module (this executable's own +// __debugbreak) and a breakpoint whose address holds no 0xCC (the two-byte `int 3`). +// A fallback vectored handler added after the one under test stands in for "the run ends": it counts what reaches it +// and steps over it, so the test keeps going. No game data, no mapped images. +#include + +#include +#include +#include + +#include "assert_skip.h" + +namespace { + +int g_failed = 0; + +void check(bool ok, const char* what) { + std::printf("%s %s\n", ok ? "ok " : "FAIL ", what); + if (!ok) ++g_failed; +} + +// What the handler under test passed on. +volatile LONG g_passed_on = 0; +// What it saw for RIP in the first breakpoint (Windows and Wine report the 0xCC's own address). +volatile LONG g_rip_at_int3 = -1; + +LONG CALLBACK fallback(EXCEPTION_POINTERS* info) { + if (info->ExceptionRecord->ExceptionCode != EXCEPTION_BREAKPOINT) return EXCEPTION_CONTINUE_SEARCH; + const auto at = reinterpret_cast(info->ExceptionRecord->ExceptionAddress); + InterlockedIncrement(&g_passed_on); + if (g_rip_at_int3 < 0) InterlockedExchange(&g_rip_at_int3, info->ContextRecord->Rip == at ? 1 : 0); + if (info->ContextRecord->Rip == at) info->ContextRecord->Rip += 1; // over the one-byte int3 + return EXCEPTION_CONTINUE_EXECUTION; +} + +// int3; mov eax, imm32; ret. The value shows that the code after the int3 ran. +using Site = int (*)(); +Site write_site(unsigned char* at, std::uint32_t value) { + const unsigned char code[] = {0xCC, 0xB8, 0, 0, 0, 0, 0xC3}; + std::memcpy(at, code, sizeof code); + std::memcpy(at + 2, &value, sizeof value); + return reinterpret_cast(at); +} + +__declspec(noinline) int host_int3() { + __debugbreak(); + return 7; +} + +} // namespace + +int main() { + SetErrorMode(GetErrorMode() | SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX); + std::setvbuf(stdout, nullptr, _IONBF, 0); + namespace as = rt::assert_skip; + + bool valid = false; + check(as::parse_limit(nullptr, &valid) == 0 && valid, "a null value is off"); + check(as::parse_limit(L"", &valid) == 0 && valid, "an empty value is off"); + check(as::parse_limit(L"0", &valid) == 0 && valid, "0 is off"); + check(as::parse_limit(L"3", &valid) == 3 && valid, "3 is a limit of 3"); + check(as::parse_limit(L"1000000", &valid) == 1000000 && valid, "1000000 is the largest limit"); + check(as::parse_limit(L"1000001", &valid) == 0 && !valid, "1000001 is refused"); + check(as::parse_limit(L"-1", &valid) == 0 && !valid, "a negative number is refused"); + check(as::parse_limit(L" 2", &valid) == 0 && !valid, "a leading space is refused"); + check(as::parse_limit(L"2x", &valid) == 0 && !valid, "trailing text is refused"); + check(as::parse_limit(L"yes", &valid) == 0 && !valid, "text is refused"); + SetEnvironmentVariableW(L"LEONIDA_SKIP_ASSERTS", nullptr); + check(as::limit_from_environment() == 0, "unset in the environment: off"); + SetEnvironmentVariableW(L"LEONIDA_SKIP_ASSERTS", L"2"); + check(as::limit_from_environment() == 2, "LEONIDA_SKIP_ASSERTS=2 in the environment: 2"); + SetEnvironmentVariableW(L"LEONIDA_SKIP_ASSERTS", L"many"); + check(as::limit_from_environment() == 0, "LEONIDA_SKIP_ASSERTS=many in the environment: off"); + + // Executable memory no host module owns, the way the loader maps a title's image. + auto* page = static_cast( + VirtualAlloc(nullptr, 4096, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE)); + check(page != nullptr, "allocate an executable page"); + if (!page) return 1; + HMODULE owner = nullptr; + check(!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + reinterpret_cast(page), &owner), + "the page is in no host module"); + const Site a = write_site(page, 0x41); + const Site b = write_site(page + 16, 0x42); + const Site c = write_site(page + 32, 0x43); + FlushInstructionCache(GetCurrentProcess(), page, 4096); + + void* const fallback_handler = AddVectoredExceptionHandler(0, &fallback); + check(fallback_handler != nullptr, "add the fallback handler"); + + // Off: the int3 reaches the fallback, which stands in for the end of the run. + check(as::install(0) && as::skipped() == 0, "install(0) adds nothing"); + check(a() == 0x41 && g_passed_on == 1, "off: a guest int3 is passed on"); + check(g_rip_at_int3 == 1, "the breakpoint's RIP is the 0xCC's own address"); + + // On, limit 2. The handler is added last, so it must come before the fallback to see anything: remove the + // fallback and add it again behind it. + RemoveVectoredExceptionHandler(fallback_handler); + check(as::install(2), "install(2) adds the handler"); + void* const fallback_again = AddVectoredExceptionHandler(0, &fallback); + check(fallback_again != nullptr, "add the fallback handler behind it"); + check(a() == 0x41 && as::skipped() == 1 && g_passed_on == 1, "the first guest int3 is stepped over"); + check(b() == 0x42 && as::skipped() == 2 && g_passed_on == 1, "the second, at another address, too"); + check(host_int3() == 7 && as::skipped() == 2 && g_passed_on == 2, "an int3 in a host module is passed on"); + check(c() == 0x43 && as::skipped() == 2 && g_passed_on == 3, "past the limit, a guest int3 is passed on"); + check(a() == 0x41 && as::skipped() == 2 && g_passed_on == 4, "and so is every later one"); + + // A breakpoint whose reported address holds no 0xCC: the two-byte `int 3` (CD 03), for which Windows and Wine + // report the byte after the CD. It is passed on, whatever the budget. + check(as::install(5), "install(5) starts over"); + RemoveVectoredExceptionHandler(fallback_again); + void* const fallback_last = AddVectoredExceptionHandler(0, &fallback); + page[48] = 0xCD; // int 3, two bytes; then the site's mov eax, 0x44; ret + page[49] = 0x03; + const Site d = reinterpret_cast(page + 48); + write_site(page + 49, 0x44); + page[49] = 0x03; // write_site's 0xCC is the second byte of the int 3 + FlushInstructionCache(GetCurrentProcess(), page, 4096); + const LONG before = g_passed_on; + check(d() == 0x44 && as::skipped() == 0 && g_passed_on == before + 1, + "a breakpoint with no 0xCC at its address is passed on"); + check(b() == 0x42 && as::skipped() == 1 && g_passed_on == before + 1, "a guest int3 after a restart is stepped over"); + + as::install(0); + RemoveVectoredExceptionHandler(fallback_last); + VirtualFree(page, 0, MEM_RELEASE); + std::printf("%s\n", g_failed ? "FAILED" : "all checks passed"); + return g_failed ? 1 : 0; +} diff --git a/runtime/test/fixtures/tiny_game.cpp b/runtime/test/fixtures/tiny_game.cpp index 21feaad5f5..ec203400b9 100644 --- a/runtime/test/fixtures/tiny_game.cpp +++ b/runtime/test/fixtures/tiny_game.cpp @@ -1,6 +1,6 @@ // A stand-in game executable for `gta6rt --run`: what a real title does in its first second, run through the // loader instead of the Windows one. Test code, not game content. Run as -// gta6rt --run tiny_game.exe -- hello 42 [tls] [eh] [stack] [game-file] [exit-code] [abort-mode] +// gta6rt --run tiny_game.exe -- hello 42 [tls] [eh] [stack] [game-file] [int3] [exit-code] [abort-mode] // It checks its arguments, a static initializer, the heap, a file round trip, a second thread, and the // xgameruntime.dll imports (answered by the shim registry: the stub DLL it links against would fail them), then // calls exit() with 0 (42 with `exit-code`) when everything held. Two checks are opt-in, by argument (CTest's @@ -12,6 +12,8 @@ // A fourth, `stack` (run_tiny_game_stack), checks that the entry thread and a thread created with dwStackSize 0 // both have 8 MB of stack: a driver compiles shaders on the game's threads (BUILD-1457). Only under gta6rt: run // natively, the second thread gets this program's own 1 MB. +// `int3` (run_tiny_game_skip_asserts) runs two soft asserts, an int3 at each of two sites followed by code that +// carries on, as a title's assert does; it passes only under LEONIDA_SKIP_ASSERTS=2 or more, which steps over them. // An `abort-*` mode installs a SIGABRT disposition and calls abort(); tiny_game_abort_test compares the exact exit // codes with a native run of the same image. No game files. // On MSVC, `eh` needs more than the unwind tables #176 registers: _CxxThrowException finds the throwing image @@ -64,6 +66,16 @@ void abort_crt_exit_handler(int) { } void abort_return_handler(int) {} +// Two int3 sites: noinline, so the optimizer cannot fold them into one. +__declspec(noinline) void soft_assert_a(volatile int& reached) { + __debugbreak(); + reached = reached + 1; +} +__declspec(noinline) void soft_assert_b(volatile int& reached) { + __debugbreak(); + reached = reached + 1; +} + // The reserved stack of the calling thread, in bytes, written to *out (a size_t). DWORD WINAPI stack_reserve(void* out) { ULONG_PTR low = 0, high = 0; @@ -178,6 +190,15 @@ int main(int argc, char** argv) { std::printf("tiny_game: SKIP GAME: file (argument game-file)\n"); } + if (has_arg(argc, argv, "int3")) { + volatile int reached = 0; + soft_assert_a(reached); + soft_assert_b(reached); + check(reached == 2, "the code after each of two int3 ran (LEONIDA_SKIP_ASSERTS stepped over them)"); + } else { + std::printf("tiny_game: SKIP int3 (argument int3)\n"); + } + if (has_arg(argc, argv, "abort-handler")) { std::signal(SIGABRT, abort_exit_handler); std::abort(); diff --git a/tools/boot_trace.py b/tools/boot_trace.py index e65ab74348..917db10c18 100644 --- a/tools/boot_trace.py +++ b/tools/boot_trace.py @@ -373,7 +373,8 @@ def format_text(summary: dict, limit: int) -> str: if startup["file_open_failed_count"] > limit: out.append(f" ... {startup['file_open_failed_count'] - limit} more") if startup["int3_skipped"]: - out.append(f" int3 skipped (LEONIDA_SKIP_INT3, one per address): {_tail(startup['int3_skipped'], limit)}") + out.append(" int3 skipped (LEONIDA_SKIP_INT3: one per address; LEONIDA_SKIP_ASSERTS: each one): " + f"{_tail(startup['int3_skipped'], limit)}") if startup["exception_count"]: out.append(f" exceptions: {startup['exception_count']}") for exc in startup["exceptions"][:limit]: