Skip to content
Merged
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
24 changes: 17 additions & 7 deletions src/startup_stubs/d3d12_capture.inl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ std::vector<ViewRecord>& view_records() { static std::vector<ViewRecord> v; retu
std::vector<std::uint8_t>& view_materialized() { static std::vector<std::uint8_t> v; return v; } // 1 if desktop view was created
std::unordered_map<u64, std::uint32_t>& view_ids() { static std::unordered_map<u64, std::uint32_t> m; return m; } // content hash -> view id
LONG64 views_noted, views_distinct, descriptors_copied;
// shadow_slot lookups the handle could not settle (descriptor_heap_match.h): several heaps on a boundary, or none.
LONG64 descriptor_heap_shared_boundary, descriptor_heap_off_boundary;

// X2W_D3D12_VIEW_CONTENT_TOKENS=1 (opt-in, #1199, view_content_token.h): a view that fits a content token is named in
// title memory by its content, so its record is only named by shadow slots. Such a record (freeable) is parked when
Expand Down Expand Up @@ -251,13 +253,21 @@ void view_allocation_gate(ViewAllocation site)
// The shadow entry for a desktop CPU descriptor handle (note_lock held), or null.
std::uint32_t* shadow_slot(u64 desktop_handle, std::uint32_t* room = nullptr)
{
for (const HeapInfo& h : heap_infos()) {
if (desktop_handle < h.desktop_cpu || desktop_handle >= h.desktop_cpu + u64{h.count} * h.increment || !h.shadow) continue;
const u64 index = (desktop_handle - h.desktop_cpu) / h.increment;
if (room) *room = static_cast<std::uint32_t>(h.count - index);
return h.shadow + index;
}
return nullptr;
// Desktop CPU handles need not be addresses: interleaved tagged handles, released heaps and the two cases the
// handle cannot settle are described in descriptor_heap_match.h. The counters show whether either case occurs.
const std::vector<HeapInfo>& heaps = heap_infos();
const x2w_startup::DescriptorHeapMatch match = x2w_startup::match_descriptor_heap(heaps.size(), desktop_handle,
[&](std::size_t i, x2w_startup::DescriptorHeapSpan& span) {
if (!heaps[i].shadow) return false;
span = {heaps[i].desktop_cpu, heaps[i].increment, heaps[i].count};
return true;
});
if (!match.found()) return nullptr;
if (match.boundary_matches > 1) InterlockedIncrement64(&descriptor_heap_shared_boundary);
if (match.off_boundary) InterlockedIncrement64(&descriptor_heap_off_boundary);
const HeapInfo& heap = heaps[match.heap];
if (room) *room = static_cast<std::uint32_t>(heap.count - match.index);
return heap.shadow + match.index;
}
std::uint32_t shadow_id(u64 desktop_handle)
{
Expand Down
4 changes: 4 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
#include "command_list_slot_names.h"
#include "command_signature_filter.h"
#include "console_plane_size.h"
#include "descriptor_heap_match.h"
#include "descriptor_heap_type.h"
#include "small_texture_4k.h"
#include "null_binding_cause.h"
Expand Down Expand Up @@ -8178,6 +8179,9 @@ HRESULT __stdcall queue_present(Wrapper* self, UINT planes, const void* plane_pa
"(region-information proofs %lld, region queries %lld)", views_noted, views_distinct, descriptors_copied,
static_cast<u64>(heap_infos().size()), static_cast<long long>(placed_buffer_calls),
static_cast<long long>(placed_buffer_region_info), static_cast<long long>(placed_buffer_queries));
if (descriptor_heap_shared_boundary || descriptor_heap_off_boundary)
log("x2w-d3d12 descriptor heap lookups the handle did not settle: %lld on a boundary of several heaps, "
"%lld on no boundary", descriptor_heap_shared_boundary, descriptor_heap_off_boundary);
if (view_content_tokens()) {
AcquireSRWLockShared(&note_lock);
const u64 records = view_records().size(), ids = view_ids().size();
Expand Down
66 changes: 66 additions & 0 deletions src/startup_stubs/descriptor_heap_match.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Experimental (docs/startup-runtime.md): which noted descriptor heap a desktop CPU handle belongs to.
//
// Desktop CPU handles need not be addresses. A driver may hand out small tagged values whose heaps interleave
// (one heap at 0x3, the next at 0x4, both with a 0x20 increment), so a handle inside one heap's span can belong to
// another (#3563). The lookup prefers a heap the handle sits on a descriptor boundary of.
//
// Heaps are searched newest first. A released heap keeps its entry, so when the driver hands its handle range (an
// address or a tag) to a new heap, the new heap is the one the handle names.
//
// The handle alone cannot settle two cases, and the result says when either happened:
// - boundary_matches > 1: several heaps have the handle on a boundary (starts that differ by a multiple of the
// increment, such as 0x3 and 0x23 with 0x20). The newest one is taken.
// - off_boundary: no heap has the handle on a boundary, so the newest heap whose span holds it is taken, with the
// index rounded down. With address handles this is a handle inside a descriptor; with tagged handles it is wrong.
#pragma once

#include <cstddef>
#include <cstdint>

namespace x2w_startup {

struct DescriptorHeapSpan {
std::uint64_t start;
std::uint32_t increment, count;
};

struct DescriptorHeapMatch {
static constexpr std::size_t none = static_cast<std::size_t>(-1);
std::size_t heap = none; // position in the searched sequence
std::uint64_t index = 0; // descriptor index within that heap
std::uint32_t boundary_matches = 0;
bool off_boundary = false;
bool found() const noexcept { return heap != none; }
};

// span(i) gives the i-th heap's span, or returns false for a heap that cannot be matched (no shadow, say).
template<class SpanAt>
DescriptorHeapMatch match_descriptor_heap(std::size_t heap_count, std::uint64_t handle, SpanAt span) noexcept
{
DescriptorHeapMatch match;
std::size_t fallback = DescriptorHeapMatch::none;
std::uint64_t fallback_index = 0;
for (std::size_t i = heap_count; i-- > 0;) {
DescriptorHeapSpan s{};
if (!span(i, s) || !s.increment || handle < s.start) continue;
const std::uint64_t offset = handle - s.start;
if (offset / s.increment >= s.count) continue;
if (offset % s.increment == 0) {
if (match.boundary_matches++ == 0) {
match.heap = i;
match.index = offset / s.increment;
}
} else if (fallback == DescriptorHeapMatch::none) {
fallback = i;
fallback_index = offset / s.increment;
}
}
if (!match.found() && fallback != DescriptorHeapMatch::none) {
match.heap = fallback;
match.index = fallback_index;
match.off_boundary = true;
}
return match;
}

} // namespace x2w_startup
13 changes: 13 additions & 0 deletions tests/cmake/startup_descriptor_heap_match.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Which noted descriptor heap a desktop CPU handle names (src/startup_stubs/descriptor_heap_match.h), without a
# device: interleaved tagged handles, address handles and a released heap's reused handle (#3563). The first-contains
# control is the lookup before #3563 and the oldest-first control lets a released heap shadow a new one; both must fail.
add_executable(startup_descriptor_heap_match_check tests/startup_descriptor_heap_match_check.cpp)
target_include_directories(startup_descriptor_heap_match_check PRIVATE src)
target_compile_features(startup_descriptor_heap_match_check PRIVATE cxx_std_17)
add_test(NAME startup_descriptor_heap_match COMMAND startup_descriptor_heap_match_check)
add_test(NAME startup_descriptor_heap_match_first_contains_control
COMMAND startup_descriptor_heap_match_check --mutate first-contains)
add_test(NAME startup_descriptor_heap_match_oldest_first_control
COMMAND startup_descriptor_heap_match_check --mutate oldest-first)
set_tests_properties(startup_descriptor_heap_match_first_contains_control
startup_descriptor_heap_match_oldest_first_control PROPERTIES WILL_FAIL TRUE)
126 changes: 126 additions & 0 deletions tests/startup_descriptor_heap_match_check.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
// Checks src/startup_stubs/descriptor_heap_match.h without a device: which noted heap a desktop CPU handle names
// when handles are small tagged values whose heap spans interleave (#3563), as a GTX 1060 driver hands out, and
// when they are addresses, as WARP hands out. The mutated runs are the negative controls: --mutate first-contains
// takes the first heap whose span holds the handle (the lookup before #3563), and --mutate oldest-first searches
// the heaps in the order they were noted, so a released heap shadows a new one at the same handle.
#include "startup_stubs/descriptor_heap_match.h"

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <vector>

namespace {

using x2w_startup::DescriptorHeapMatch;
using x2w_startup::DescriptorHeapSpan;

enum class Mutation { none, first_contains, oldest_first };
Mutation mutation = Mutation::none;
int failures = 0;

DescriptorHeapMatch lookup(const std::vector<DescriptorHeapSpan>& heaps, std::uint64_t handle)
{
if (mutation == Mutation::none)
return x2w_startup::match_descriptor_heap(heaps.size(), handle, [&](std::size_t i, DescriptorHeapSpan& s) {
s = heaps[i];
return true;
});
if (mutation == Mutation::oldest_first) {
// The same lookup over the heaps in reverse, so the oldest heap is searched first.
const std::size_t n = heaps.size();
DescriptorHeapMatch m = x2w_startup::match_descriptor_heap(n, handle, [&](std::size_t i, DescriptorHeapSpan& s) {
s = heaps[n - 1 - i];
return true;
});
if (m.found()) m.heap = n - 1 - m.heap;
return m;
}
DescriptorHeapMatch m;
for (std::size_t i = 0; i < heaps.size(); ++i) {
const DescriptorHeapSpan& s = heaps[i];
if (handle < s.start || handle >= s.start + std::uint64_t{s.count} * s.increment) continue;
m.heap = i;
m.index = (handle - s.start) / s.increment;
break;
}
return m;
}

void expect(const char* name, const std::vector<DescriptorHeapSpan>& heaps, std::uint64_t handle, std::size_t heap,
std::uint64_t index, std::uint32_t boundary_matches, bool off_boundary)
{
const DescriptorHeapMatch m = lookup(heaps, handle);
const bool want_found = heap != DescriptorHeapMatch::none;
bool ok = m.heap == heap && (!want_found || m.index == index);
if (mutation == Mutation::none) ok = ok && m.boundary_matches == boundary_matches && m.off_boundary == off_boundary;
if (!ok) {
std::printf("FAIL %s: handle 0x%llx gave heap %lld index %llu (boundary matches %u%s), expected heap %lld index %llu\n",
name, static_cast<unsigned long long>(handle), m.found() ? static_cast<long long>(m.heap) : -1LL,
static_cast<unsigned long long>(m.index), m.boundary_matches, m.off_boundary ? ", off boundary" : "",
want_found ? static_cast<long long>(heap) : -1LL, static_cast<unsigned long long>(index));
++failures;
}
}

constexpr std::size_t none = DescriptorHeapMatch::none;

} // namespace

int main(int argc, char** argv)
{
if (argc == 3 && std::strcmp(argv[1], "--mutate") == 0) {
if (std::strcmp(argv[2], "first-contains") == 0) mutation = Mutation::first_contains;
else if (std::strcmp(argv[2], "oldest-first") == 0) mutation = Mutation::oldest_first;
else {
std::printf("unknown mutation %s\n", argv[2]);
return 2;
}
} else if (argc != 1) {
std::printf("usage: %s [--mutate first-contains|oldest-first]\n", argv[0]);
return 2;
}

// Tagged handles: three heaps at 0x3, 0x4 and 0x5, increment 0x20, two descriptors each. Each span holds the
// start of the next heap, so only the descriptor boundary says which heap a handle names.
const std::vector<DescriptorHeapSpan> tagged{{0x3, 0x20, 2}, {0x4, 0x20, 2}, {0x5, 0x20, 2}};
expect("tagged heap 0 slot 0", tagged, 0x3, 0, 0, 1, false);
expect("tagged heap 1 slot 0", tagged, 0x4, 1, 0, 1, false);
expect("tagged heap 2 slot 0", tagged, 0x5, 2, 0, 1, false);
expect("tagged heap 1 slot 1", tagged, 0x24, 1, 1, 1, false);
expect("tagged heap 2 slot 1", tagged, 0x25, 2, 1, 1, false);
expect("tagged past every heap", tagged, 0x45, none, 0, 0, false);

// The limit: a fourth heap at 0x23 puts its slot 0 on heap 0's slot 1 boundary. The handle cannot tell them
// apart; the newest heap is taken and the ambiguity is reported.
std::vector<DescriptorHeapSpan> ambiguous = tagged;
ambiguous.push_back({0x23, 0x20, 2});
expect("tagged shared boundary", ambiguous, 0x23, 3, 0, 2, false);

// No heap has the handle on a boundary: the newest containing heap, index rounded down, reported.
expect("tagged off every boundary", tagged, 0x26, 2, 1, 0, true);

// Address handles: spans do not overlap, so the containing heap is the answer, on or off a boundary.
const std::vector<DescriptorHeapSpan> addresses{{0x10000, 32, 4}, {0x20000, 32, 8}};
expect("address heap 0 slot 3", addresses, 0x10060, 0, 3, 1, false);
expect("address heap 1 slot 7", addresses, 0x200e0, 1, 7, 1, false);
expect("address inside a descriptor", addresses, 0x20030, 1, 1, 0, true);
expect("address past heap 0", addresses, 0x10080, none, 0, 0, false);

// A released heap keeps its entry. A new heap at the same tag or address is the one the handle names.
const std::vector<DescriptorHeapSpan> reused{{0x3, 0x20, 2}, {0x4, 0x20, 2}, {0x3, 0x20, 4}};
expect("reused tag", reused, 0x63, 2, 3, 1, false);
expect("reused tag slot 0", reused, 0x3, 2, 0, 2, false);
const std::vector<DescriptorHeapSpan> reused_address{{0x10000, 32, 4}, {0x10000, 32, 4}};
expect("reused address", reused_address, 0x10020, 1, 1, 2, false);

// A zero increment never matches (and never divides).
expect("zero increment", {{0x3, 0, 2}}, 0x3, none, 0, 0, false);

if (failures) {
std::printf("descriptor heap match: %d failed\n", failures);
return 1;
}
std::printf("descriptor heap match: all cases passed\n");
return 0;
}
Loading