Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ 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 <module>+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").
* The first call of each Xbox device slot that the device proxy forwards by number names its caller:
`[rt] d3d12_xs: device slot N (...) first call from <module>+0x...`, the title's call site without a debugger.
* `LEONIDA_VK_VALIDATION=1` enables the Khronos validation layer with the Vulkan backend (startup overhead; it is a
diagnostic).
* `DISABLE_RTSS_LAYER=1` and `DISABLE_VULKAN_OBS_CAPTURE=1` when MSI Afterburner or OBS is installed; their
Expand Down
5 changes: 4 additions & 1 deletion docs/d3d12_command_objects.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,10 @@ today. That is an assumption, not a measurement: the Xbox device was measured at
1/14/15/16/18/19/27/38/62/76, and slots 8/9/12/36 are exactly the ones this page needs. If the
Xbox slots differ, a generic forward-to-PC thunk calls the wrong method. The current proxy
(`runtime/shims/d3d12_xs_device.h:1-18`) forwards every unshadowed slot in PC order and logs
first calls; a wrong slot would therefore be visible as a misnamed call, not a silent success.
first calls; a wrong slot would therefore be visible as a misnamed call, not a silent success. Under gta6rt
the first call of a slot the generic thunk takes by number also names its caller, the return address in the
title's code as `module+RVA` (`device slot 25 (GetResourceAllocationInfo) first call from <image>+0x…`), so the
call site of a slot can be found without a debugger (`set_caller_describer`).

Queues and fences are wrapped the same way.

Expand Down
19 changes: 19 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,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
Expand Down Expand Up @@ -1134,6 +1144,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()
Expand Down Expand Up @@ -1277,6 +1290,12 @@ if(BUILD_TESTING)
"-DTOOL=$<TARGET_FILE:gta6rt>" "-DWANT=${GTA6RT_STACK}"
-P "${CMAKE_CURRENT_SOURCE_DIR}/test/check_stack_reserve.cmake")
add_test(NAME run_tiny_game_stack COMMAND gta6rt --run $<TARGET_FILE:tiny_game> -- 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 $<TARGET_FILE:tiny_game> -- 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")
# GAME: is the game folder, absolute, when gta6rt is given a bare image name or `--game-dir .`.
add_test(NAME run_tiny_game_dir COMMAND ${CMAKE_COMMAND}
"-DLOADER=$<TARGET_FILE:gta6rt>" "-DGUEST=$<TARGET_FILE:tiny_game>"
Expand Down
23 changes: 21 additions & 2 deletions runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -445,15 +464,15 @@ 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` |

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.
Expand Down
40 changes: 37 additions & 3 deletions runtime/shims/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <windows.h>
#include <dxgi1_4.h>
#include <intrin.h> // _ReturnAddress

#include <atomic>
#include <cerrno>
Expand All @@ -39,6 +40,7 @@
#include <iterator>
#include <new>
#include <memory>
#include <string>
#include <utility>

namespace rt::d3d12_xs {
Expand Down Expand Up @@ -155,11 +157,34 @@ const char* slot_name(unsigned slot) {
}
}

void note_slot(unsigned slot) {
std::atomic<CallerDescriber> g_caller_describer{nullptr};
std::atomic<const void*> g_first_caller[kMaxSlots];

// `caller` is the return address in the title's code for a slot the generic thunk forwards by number, and null for
// the slots the proxy handles itself. Its first call names the caller as module+RVA (set_caller_describer), so the
// title's call site of a slot is in the log without a debugger.
void note_slot(unsigned slot, const void* caller = nullptr) {
if (slot >= kMaxSlots) return;
count_hit(g_hits[slot]);
const bool first = first_call(g_first[slot]);
if (first) {
if (first && caller) {
g_first_caller[slot].store(caller, std::memory_order_relaxed);
const CallerDescriber describe = g_caller_describer.load(std::memory_order_acquire);
const auto address = reinterpret_cast<std::uint64_t>(caller);
std::string where;
try {
if (describe) where = describe(address);
} catch (...) {
where.clear(); // out of memory: the bare address below; nothing may unwind into the title's frame
}
if (where.empty()) {
char hex[24];
std::snprintf(hex, sizeof hex, "0x%llx", static_cast<unsigned long long>(address));
where = hex;
}
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call from %s\n", slot, slot_name(slot),
where.c_str());
} else if (first) {
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call\n", slot, slot_name(slot));
} else if (slot >= 3 && trace_enabled()) {
std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) call\n", slot, slot_name(slot));
Expand Down Expand Up @@ -1865,7 +1890,7 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64
reinterpret_cast<const D3D12_CLEAR_VALUE*>(a5), reinterpret_cast<const IID*>(a6),
reinterpret_cast<void**>(a7)));
} else {
note_slot(Slot);
note_slot(Slot, _ReturnAddress()); // the title's call site, logged with the slot's first call
// Xbox device slots, before the PC vtable is indexed: the public HLE's 78/79/80 would otherwise reach
// CreateReservedResource2/CreateSampler2 or fall past an 80-slot table, and 48/73 would reach unrelated PC
// methods. The check is before the slot-count refusal because slot 80 is outside a PC ID3D12Device11 table.
Expand Down Expand Up @@ -2041,6 +2066,10 @@ void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook) {
g_placed_at_va_pages.store(hook, std::memory_order_release);
}

void set_caller_describer(CallerDescriber describe) {
g_caller_describer.store(describe, std::memory_order_release);
}

ID3D12Device* device_proxy_real_device(const void* device) {
if (!is_device_proxy(device)) return nullptr;
return static_cast<const DeviceProxy*>(device)->real;
Expand Down Expand Up @@ -2162,6 +2191,10 @@ bool device_slot_first_call_seen(unsigned slot) {
return slot < kMaxSlots && g_first[slot].load(std::memory_order_relaxed);
}

const void* device_slot_first_caller(unsigned slot) {
return slot < kMaxSlots ? g_first_caller[slot].load(std::memory_order_relaxed) : nullptr;
}

unsigned long device_proxy_ref_count(const void* device) {
if (!is_device_proxy(device)) return 0;
return static_cast<const DeviceProxy*>(device)->refs.load(std::memory_order_relaxed);
Expand Down Expand Up @@ -2201,6 +2234,7 @@ void reset_device_proxy_stats() {
for (unsigned slot = 0; slot < kMaxSlots; ++slot) {
g_hits[slot].store(0, std::memory_order_relaxed);
g_first[slot].store(false, std::memory_order_relaxed);
g_first_caller[slot].store(nullptr, std::memory_order_relaxed);
g_beyond_warned[slot].store(false, std::memory_order_relaxed);
}
for (unsigned slot = 0; slot < kHeapSlots; ++slot) {
Expand Down
13 changes: 13 additions & 0 deletions runtime/shims/d3d12_xs_device.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

#include <cstddef>
#include <cstdint>
#include <string>

namespace rt::d3d12_xs {

Expand Down Expand Up @@ -108,6 +109,18 @@ void set_placed_at_va_handler(PlacedAtVaHandler handler);
using PlacedAtVaPagesHook = bool (*)(const void* va, std::size_t bytes);
void set_placed_at_va_pages_hook(PlacedAtVaPagesHook hook);

// The first call of each slot the generic thunk takes by number (the PC slots it passes through, the Xbox slots it
// routes, 48/73/78/79/80 at their configured indices, and the ones it refuses past the PC table; not 0-2, 8, 9, 12,
// 14, 31, 32, 34-36, 45, 46, 50, 62, 67, 75, 76 and 133, which have handlers of their own) is logged with its caller, the return address in the title's code: "device slot 25 (GetResourceAllocationInfo) first call
// from game.exe+0x1234". The describer turns that address into the text; gta6rt registers rt::shims::module_offset
// (module+RVA for a mapped image, host:name.dll+RVA for a host module). With none, or an empty answer, the bare
// address is logged. Called at most once per slot, from the title's thread.
using CallerDescriber = std::string (*)(std::uint64_t address);
void set_caller_describer(CallerDescriber describe);
// That caller (null before the slot's first forwarded call, and for the slots the proxy handles itself). Test helper;
// reset_device_proxy_stats clears it with the first-call flag.
const void* device_slot_first_caller(unsigned slot);

// True for slots the proxy handles itself rather than forwarding verbatim: IUnknown (0-2), 8 and 75 (queue
// proxies), 9 (allocator proxies), 14, 31/34/35/45/46/50 (proxies unwrapped), 32 and 36 (fence proxies), 48/73 and
// 78/79/80 (the Xbox slots above, at their configured indices), 62, 67 and 76. Slot 10 is intentionally false: the
Expand Down
Loading