From 1d1a2746a844bbef294e17e969f47929f2aefd08 Mon Sep 17 00:00:00 2001 From: Lucy <35628281+Amzi-01@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:25:13 +0000 Subject: [PATCH 1/2] d3d12_xs: the first call of a forwarded device slot names its caller (RT-526) The first call of every Xbox device slot the proxy forwards by number now logs the title code that made it as module+offset, and device_slot_first_caller(slot) returns the same text. Finding a new Xbox-only slot in the executable no longer needs a debugger. AI-assisted. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_xs_device.cpp | 22 +++++++++++++++++++--- runtime/shims/d3d12_xs_device.h | 4 ++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 187406e3d8..af1376557d 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -24,6 +24,7 @@ // behind the device before a presenter exists, so WaitFrameEventX returns at once with a fresh non-zero token. #include "d3d12_xs_device.h" #include "d3d12_trace.h" +#include "shims.h" // rt::shims::module_offset names the caller of a slot's first call #include "../src/shader_library.h" // leonida::fspx_payload_from_parts for slot 67 #include @@ -36,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -86,6 +88,8 @@ struct HeapProxy { std::atomic g_hits[kMaxSlots]; std::atomic g_first[kMaxSlots]; +// The return address of the first call of each slot that reached the generic forwarding branch (0: none yet). +std::atomic g_first_caller[kMaxSlots]; std::atomic g_beyond_warned[kMaxSlots]; std::atomic g_heap_hits[kHeapSlots]; std::atomic g_heap_first[kHeapSlots]; @@ -155,11 +159,18 @@ const char* slot_name(unsigned slot) { } } -void note_slot(unsigned slot) { +// caller: the title code that called the slot, for the slots the proxy forwards by number (RT-526). The first call +// then also names it as module+offset, so a new Xbox-only slot can be found in the executable 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) { + const auto address = reinterpret_cast(caller); + g_first_caller[slot].store(address, std::memory_order_relaxed); + std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call from %s\n", slot, slot_name(slot), + rt::shims::module_offset(address).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)); @@ -1865,7 +1876,7 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 reinterpret_cast(a5), reinterpret_cast(a6), reinterpret_cast(a7))); } else { - note_slot(Slot); + note_slot(Slot, _ReturnAddress()); // 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. @@ -2162,6 +2173,11 @@ bool device_slot_first_call_seen(unsigned slot) { return slot < kMaxSlots && g_first[slot].load(std::memory_order_relaxed); } +std::string device_slot_first_caller(unsigned slot) { + const std::uint64_t address = slot < kMaxSlots ? g_first_caller[slot].load(std::memory_order_relaxed) : 0; + return address ? rt::shims::module_offset(address) : std::string(); +} + unsigned long device_proxy_ref_count(const void* device) { if (!is_device_proxy(device)) return 0; return static_cast(device)->refs.load(std::memory_order_relaxed); diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index ac0437215d..7d27a826ee 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -51,6 +51,7 @@ #include #include +#include namespace rt::d3d12_xs { @@ -193,6 +194,9 @@ unsigned long device_proxy_ref_count(const void* device); // counts. Calls made before that are not counted (the counter stays 0); they set only the first-call flags. std::uint64_t device_slot_hits(unsigned slot); bool device_slot_first_call_seen(unsigned slot); +// For a slot the proxy forwards by number (not one with a handler of its own): the caller of its first call as +// module+offset ("host:+0x..." for host code), the same text the first-call log line names; "" before one. +std::string device_slot_first_caller(unsigned slot); std::uint64_t heap_slot_hits(unsigned slot); bool heap_slot_first_call_seen(unsigned slot); void reset_device_proxy_stats(); From 0ff37d43f4390e211ee3ea406370a19abbab1194 Mon Sep 17 00:00:00 2001 From: Lucy <35628281+Amzi-01@users.noreply.github.com> Date: Mon, 23 Nov 2026 23:30:51 +0000 Subject: [PATCH 2/2] d3d12_xs: slot caller via a formatter hook, cleared on reset (review of #9990) - The proxy no longer calls rt::shims::module_offset itself: gta6rt sets it with set_device_slot_caller_formatter; with none set the address is shown in hex, so xs_proxy_mock_test links d3d12_xs_device.cpp alone as before. - reset_device_proxy_stats() clears the stored caller with the first-call flag; xs_proxy_mock_test checks call -> caller -> reset -> empty -> call. - The accessor comment says which slots it covers and that it is a diagnostic observation (flag set before the caller is stored). AI-assisted. Co-Authored-By: Claude Opus 5.5 --- runtime/shims/d3d12_xs_device.cpp | 20 +++++++++++++++++--- runtime/shims/d3d12_xs_device.h | 11 +++++++++-- runtime/src/main.cpp | 2 ++ runtime/test/xs_proxy_mock_test.cpp | 8 ++++++++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index af1376557d..50995c4b6e 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -24,7 +24,6 @@ // behind the device before a presenter exists, so WaitFrameEventX returns at once with a fresh non-zero token. #include "d3d12_xs_device.h" #include "d3d12_trace.h" -#include "shims.h" // rt::shims::module_offset names the caller of a slot's first call #include "../src/shader_library.h" // leonida::fspx_payload_from_parts for slot 67 #include @@ -90,6 +89,16 @@ std::atomic g_hits[kMaxSlots]; std::atomic g_first[kMaxSlots]; // The return address of the first call of each slot that reached the generic forwarding branch (0: none yet). std::atomic g_first_caller[kMaxSlots]; +// Names a caller address (set_device_slot_caller_formatter); null: the bare address in hex. gta6rt sets +// rt::shims::module_offset, so this file does not depend on the module table and test targets link it alone. +std::atomic g_caller_formatter{nullptr}; + +std::string format_caller(std::uint64_t address) { + if (const CallerFormatter formatter = g_caller_formatter.load(std::memory_order_relaxed)) return formatter(address); + char text[32]; + std::snprintf(text, sizeof(text), "0x%llx", static_cast(address)); + return text; +} std::atomic g_beyond_warned[kMaxSlots]; std::atomic g_heap_hits[kHeapSlots]; std::atomic g_heap_first[kHeapSlots]; @@ -169,7 +178,7 @@ void note_slot(unsigned slot, const void* caller = nullptr) { const auto address = reinterpret_cast(caller); g_first_caller[slot].store(address, std::memory_order_relaxed); std::fprintf(stderr, "[rt] d3d12_xs: device slot %u (%s) first call from %s\n", slot, slot_name(slot), - rt::shims::module_offset(address).c_str()); + format_caller(address).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()) { @@ -2175,7 +2184,11 @@ bool device_slot_first_call_seen(unsigned slot) { std::string device_slot_first_caller(unsigned slot) { const std::uint64_t address = slot < kMaxSlots ? g_first_caller[slot].load(std::memory_order_relaxed) : 0; - return address ? rt::shims::module_offset(address) : std::string(); + return address ? format_caller(address) : std::string(); +} + +void set_device_slot_caller_formatter(CallerFormatter formatter) { + g_caller_formatter.store(formatter, std::memory_order_relaxed); } unsigned long device_proxy_ref_count(const void* device) { @@ -2217,6 +2230,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(0, std::memory_order_relaxed); g_beyond_warned[slot].store(false, std::memory_order_relaxed); } for (unsigned slot = 0; slot < kHeapSlots; ++slot) { diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index 7d27a826ee..04e448a6ed 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -194,9 +194,16 @@ unsigned long device_proxy_ref_count(const void* device); // counts. Calls made before that are not counted (the counter stays 0); they set only the first-call flags. std::uint64_t device_slot_hits(unsigned slot); bool device_slot_first_call_seen(unsigned slot); -// For a slot the proxy forwards by number (not one with a handler of its own): the caller of its first call as -// module+offset ("host:+0x..." for host code), the same text the first-call log line names; "" before one. +// For a slot that enters the proxy's generic branch (no compile-time handler of its own; Xbox slots routed at run +// time and slots refused past the PC table included): the caller of its first call since the last +// reset_device_proxy_stats, named by the formatter below, the same text the first-call log line shows; "" before +// one. Diagnostic only: the first-call flag is set before the caller is stored, so a concurrent reader can see +// device_slot_first_call_seen true with this still "". std::string device_slot_first_caller(unsigned slot); +// Names a caller address in the first-call log line and device_slot_first_caller. gta6rt sets +// rt::shims::module_offset (module+offset); with none set (the default, and in tests) the address is shown in hex. +using CallerFormatter = std::string (*)(std::uint64_t address); +void set_device_slot_caller_formatter(CallerFormatter formatter); std::uint64_t heap_slot_hits(unsigned slot); bool heap_slot_first_call_seen(unsigned slot); void reset_device_proxy_stats(); diff --git a/runtime/src/main.cpp b/runtime/src/main.cpp index fbb4b3043e..0e49736e62 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -167,6 +167,8 @@ int wmain(int argc, wchar_t** argv) { rt::d3d12_trace::set_enabled(trace_d3d12); // Slot 133 imports the title's pages; xmem commits them on first touch, so they are committed first. rt::d3d12_xs::set_placed_at_va_pages_hook(&rt::xmem::commit_physical_range); + // The first call of a forwarded device slot names its caller as module+offset (RT-526). + rt::d3d12_xs::set_device_slot_caller_formatter(&rt::shims::module_offset); if (int trace_error = 0; !trace_file.empty() && !rt::startup_trace::open(trace_file.c_str(), &trace_error)) { std::fprintf(stderr, trace_error == EEXIST ? "error: the trace file already exists; --trace only creates a new file\n" : "error: cannot create the trace file\n"); diff --git a/runtime/test/xs_proxy_mock_test.cpp b/runtime/test/xs_proxy_mock_test.cpp index 6af7ad8046..07394777fd 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -824,6 +825,13 @@ int check_xbox_slots() { // 78 SetFrameIntervalX(output, interval_us, latency, flags): S_OK, not forwarded to PC slot 78. EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK); EXPECT(mock->calls[78] == 0 && xs::device_slot_hits(78) == 1); + // RT-526: the first call of a slot in the generic branch keeps its caller (no formatter here: hex), a reset + // forgets it with the first-call flag, and the next call records it again. + EXPECT(xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).rfind("0x", 0) == 0); + xs::reset_device_proxy_stats(); + EXPECT(!xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).empty()); + EXPECT(hr_of(raw(device, 78, 0, 16667, 1, 0)) == S_OK); + EXPECT(xs::device_slot_first_call_seen(78) && xs::device_slot_first_caller(78).rfind("0x", 0) == 0); // 79 ScheduleFrameEventX(event, offset_us, pEvent, flags): S_OK, not forwarded to PC slot 79. EXPECT(hr_of(raw(device, 79, 0, 0, 0x1234, 0)) == S_OK); EXPECT(mock->calls[79] == 0 && xs::device_slot_hits(79) == 1);