From f1119e2906dcd166ecc5afea05bab8e9616dbeca 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 | 21 ++++++++++++++++++--- runtime/shims/d3d12_xs_device.h | 3 +++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/runtime/shims/d3d12_xs_device.cpp b/runtime/shims/d3d12_xs_device.cpp index 90dcc61136..97f115d104 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -41,6 +41,7 @@ #include "../src/frame_pacer.h" #include "../src/local_diagnostics.h" #include "d3d12_xs_pso_stream.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 "../src/title_page_watch.h" // leonida::kernel_writes for slot 14 @@ -387,6 +388,8 @@ void local_mirror_table(const DeviceProxy* device, std::uint64_t gpu_handle) { 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]; @@ -479,11 +482,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)); @@ -6022,7 +6032,7 @@ std::uint64_t WINAPI proxy_slot(DeviceProxy* self, std::uint64_t a1, std::uint64 reinterpret_cast(self), reinterpret_cast(a1), static_cast(a2), static_cast(static_cast(a3)))); } - 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. @@ -6689,6 +6699,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; if (const ReferenceSettler settle = g_reference_settler.load(std::memory_order_acquire)) settle(); diff --git a/runtime/shims/d3d12_xs_device.h b/runtime/shims/d3d12_xs_device.h index 67097a7d4a..d72fb13f4b 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -518,6 +518,9 @@ void set_reference_settler(ReferenceSettler settle); // 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 94ba78348ee98b2ae7271eeb218d2f4befc2a714 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 97f115d104..517e1e024b 100644 --- a/runtime/shims/d3d12_xs_device.cpp +++ b/runtime/shims/d3d12_xs_device.cpp @@ -41,7 +41,6 @@ #include "../src/frame_pacer.h" #include "../src/local_diagnostics.h" #include "d3d12_xs_pso_stream.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 "../src/title_page_watch.h" // leonida::kernel_writes for slot 14 @@ -390,6 +389,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]; @@ -492,7 +501,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()) { @@ -6701,7 +6710,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) { @@ -6744,6 +6757,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 d72fb13f4b..2abd0e7155 100644 --- a/runtime/shims/d3d12_xs_device.h +++ b/runtime/shims/d3d12_xs_device.h @@ -518,9 +518,16 @@ void set_reference_settler(ReferenceSettler settle); // 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 f019977759..7e5e751c40 100644 --- a/runtime/src/main.cpp +++ b/runtime/src/main.cpp @@ -393,6 +393,8 @@ int wmain(int argc, wchar_t** argv) { leonida::kernel_writes::set_committer(&rt::xmem::commit_physical_range); rt::d3d12_xs::set_texture_page_provider(&rt::xmem::pin_mapped_data); leonida::kernel_writes::set_unguarder(&rt::xmem::reopen_unowned); + // 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 27af09aec3..be22d544ae 100644 --- a/runtime/test/xs_proxy_mock_test.cpp +++ b/runtime/test/xs_proxy_mock_test.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -1820,6 +1821,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);