diff --git a/docs/startup-runtime.md b/docs/startup-runtime.md index 81d60e433c..6f47b668c1 100644 --- a/docs/startup-runtime.md +++ b/docs/startup-runtime.md @@ -1730,6 +1730,41 @@ FALSE without the switch, like `x2w_d3d12_budget_snapshot`. `startup_resource_re runs the same steps without the switch. The hardware cases report 77 without a physical adapter, and the debug-layer cases report 77 where the debug layer is not installed. +## D3D12 view allocation failure + +A new view record goes into three containers: the record vector, the vector of +materialization flags and the map from content to id. If one of them cannot +allocate, the record goes into none of them, the process continues and the first +eight failures are logged: + +`x2w-d3d12 note_view: out of memory in -> view of slot not recorded, destination descriptor untracked` + +The device's view slots return nothing, so the log line is the only report. The +destination descriptor is left untracked: its shadow entry and its token in +title memory are cleared, and the view it named before loses its reference. A +draw then sees a descriptor without a view, not the earlier one. If the desktop +view was created, its resources leave the retire class of +`X2W_D3D12_RELEASE_RETIRED` and are not released early. Earlier ids stay valid, +and a later view is recorded if its allocations succeed. Records already copied +elsewhere are not touched. + +This handles the failure; it does not reduce the memory the records use, and it +does not cover other allocations, such as the map of render and depth targets. + +`startup_view_alloc__` fails the +allocation at one site three times through the fixture-only export +`x2w_d3d12_fail_view_allocations`. The failure is injected, not produced by +exhausting memory. For `view_ids` it is raised before the insertion, so the +fixture relies on the standard guarantee that a failed single insertion leaves +an `unordered_map` unchanged. The `control` variants never inject it and must +fail. The failure is exercised on slot 18 and on a slot 19 view with a counter +resource. For slot 19 only the reference counts are checked: resources that +allow unordered access are never in the retire class. + +```powershell +ctest --preset dev --no-tests=error -R '^startup_view_alloc_' --output-on-failure +``` + ## D3D12 view census The stand-in keeps one record per distinct view: slot, resource, counter, diff --git a/src/startup_stubs/d3d12_capture.inl b/src/startup_stubs/d3d12_capture.inl index a5b9479404..ff63b03c3a 100644 --- a/src/startup_stubs/d3d12_capture.inl +++ b/src/startup_stubs/d3d12_capture.inl @@ -88,6 +88,18 @@ std::size_t view_census_format(char* out, std::size_t capacity, const char* what return n < 0 ? 0 : (std::min)(static_cast(n), capacity - 1); } +// Fixture only (x2w_d3d12_fail_view_allocations, startup_view_alloc): the next `count` allocations at a site fail as an +// exhausted heap would. The vector sites fail only when the vector has to grow; view_ids fails before any insertion, +// because each new entry allocates. +enum ViewAllocation { view_records_site, view_materialized_site, view_ids_site, view_allocation_sites }; +volatile LONG view_allocations_to_fail[view_allocation_sites]; + +void view_allocation_gate(ViewAllocation site) +{ + for (LONG n = view_allocations_to_fail[site]; n > 0; n = view_allocations_to_fail[site]) + if (InterlockedCompareExchange(&view_allocations_to_fail[site], n - 1, n) == n) throw std::bad_alloc(); +} + // 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) { @@ -356,6 +368,62 @@ void note_heap(Wrapper* w, UINT type, UINT count, UINT logical_count, UINT incre static_cast(VirtualAlloc(nullptr, (u64{count} + 1) * 4, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE))}); ReleaseSRWLockExclusive(¬e_lock); } + +// Room for one more element, with the 1.5x growth push_back would use: reserve(size + 1) would copy on every insert. +template +void reserve_one_view_entry(std::vector& v, ViewAllocation site) +{ + if (v.size() < v.capacity()) return; + view_allocation_gate(site); + v.reserve(v.capacity() + v.capacity() / 2 + 1); +} + +// Adds a new record to the three view containers, or to none of them (note_lock held). Everything that can allocate +// comes first: the two reservations change no size, and a failed try_emplace leaves the map unchanged. The two +// push_back calls then have room and cannot fail. Returns the name of the container that could not allocate, or null. +const char* store_view(const ViewRecord& record, std::uint8_t created, u64 hash) +{ + const char* growing = "view_records"; + try { + reserve_one_view_entry(view_records(), view_records_site); + growing = "view_materialized"; + reserve_one_view_entry(view_materialized(), view_materialized_site); + growing = "view_ids"; + view_allocation_gate(view_ids_site); + view_ids().try_emplace(hash, record.id); + } catch (const std::bad_alloc&) { + return growing; + } + view_records().push_back(record); + view_materialized().push_back(created); + return nullptr; +} + +// A new view could not be recorded. The desktop descriptor already holds it, so the destination must stop naming the +// view it held before: the slot and its title token are cleared, and a draw refuses the descriptor instead of binding +// the old view. Nothing records the new view's use of its resource, so the resource leaves the retire class and is not +// released early. Allocates nothing. Enters with note_lock held and releases it. +void drop_unrecorded_view(const ViewRecord& record, bool materialized, const char* container) +{ + if (std::uint32_t* slot_ptr = shadow_slot(record.dest_desktop)) { + const std::uint32_t prev = *slot_ptr; + *slot_ptr = 0; + if (prev && prev - 1 < view_records().size() && view_materialized()[prev - 1]) + view_refs_release(view_records()[prev - 1].slot, view_records()[prev - 1].resource, + view_records()[prev - 1].counter); + } + if (materialized) view_refs_leave_class(record.slot, record.resource, record.counter, RetireLeft::view); + ReleaseSRWLockExclusive(¬e_lock); + const std::size_t token_bytes = record.slot == 22 ? x2w_startup::title_sampler_stride : 32u; + if (record.dest_title != record.dest_desktop && title_backed(record.dest_title, token_bytes) && + x2w_local::writable(reinterpret_cast(record.dest_title), token_bytes)) + std::memset(reinterpret_cast(record.dest_title), 0, token_bytes); + static LONG failed; + if (first_calls(failed, 8)) + log("x2w-d3d12 note_view: out of memory in %s -> view of slot %u not recorded, destination descriptor untracked", + container, record.slot); +} + // A view is created: remembered by id; the id is also written into title-owned descriptor memory so // that copies the title makes there itself can be followed. void note_view(int slot, u64 resource, u64 counter, const void* desc, std::size_t desc_bytes, u64 dest_title, u64 dest_desktop, @@ -407,9 +475,10 @@ void note_view(int slot, u64 resource, u64 counter, const void* desc, std::size_ log("%s (new slot %u)", line, record.slot); } } - view_records().push_back(record); - view_materialized().push_back(created); - view_ids()[hash] = record.id; + if (const char* container = store_view(record, created, hash)) { + drop_unrecorded_view(record, materialized, container); + return; + } ++views_distinct; if (census) { if (record.slot >= 17 && record.slot <= 22) ++view_census.distinct[record.slot - 17]; diff --git a/src/startup_stubs/d3d12_xs_device.cpp b/src/startup_stubs/d3d12_xs_device.cpp index c0ec421371..57c89ef8ed 100644 --- a/src/startup_stubs/d3d12_xs_device.cpp +++ b/src/startup_stubs/d3d12_xs_device.cpp @@ -5753,6 +5753,35 @@ extern "C" __declspec(dllexport) std::size_t x2w_d3d12_view_census_line(char* ou return view_census_line(out, capacity); } +// Fixture only (startup_view_alloc): see view_allocation_gate. +extern "C" __declspec(dllexport) void x2w_d3d12_fail_view_allocations(int site, LONG count) +{ + if (site >= 0 && site < view_allocation_sites) InterlockedExchange(&view_allocations_to_fail[site], count); +} + +// Fixture probe (startup_view_alloc): the sizes of the three view containers, the id + 1 that the tracked slot of a +// title descriptor handle names (0: none), and whether a resource is still in the retire class. +struct ViewStoreProbe { + unsigned long long records, materialized, ids, records_capacity, materialized_capacity; + unsigned shadow, view_refs, retire_when_idle; +}; +extern "C" __declspec(dllexport) void x2w_d3d12_view_probe(unsigned long long title_handle, void* resource, ViewStoreProbe* out) +{ + *out = {}; + out->shadow = shadow_id(desktop_cpu_handle(title_handle)); + AcquireSRWLockShared(¬e_lock); + out->records = view_records().size(); + out->materialized = view_materialized().size(); + out->ids = view_ids().size(); + out->records_capacity = view_records().capacity(); + out->materialized_capacity = view_materialized().capacity(); + ReleaseSRWLockShared(¬e_lock); + if (Wrapper* r = as_wrapper(resource); r && r->own().kind == resource_kind) { + out->view_refs = static_cast(InterlockedCompareExchange(&r->own().retire_view_refs, 0, 0)); + out->retire_when_idle = r->own().retire_when_idle; + } +} + // Fixture-only: the next `count` wrapper allocations fail as an exhausted heap would (#2653). extern "C" __declspec(dllexport) void x2w_d3d12_fail_wraps(LONG count) { diff --git a/tests/cmake/startup_view_alloc.cmake b/tests/cmake/startup_view_alloc.cmake new file mode 100644 index 0000000000..46e6da25b2 --- /dev/null +++ b/tests/cmake/startup_view_alloc.cmake @@ -0,0 +1,25 @@ +# #1199: when a view container cannot allocate, nothing is recorded, the destination slot is untracked and the process +# keeps running. One test per allocation site. `control` never arms the failure, so the fixture must print FAIL. +add_executable(startup_view_alloc tests/startup_view_alloc.cpp) +target_compile_features(startup_view_alloc PRIVATE cxx_std_17) +target_link_libraries(startup_view_alloc PRIVATE d3d12_xs_shim d3d12 dxgi) +foreach(backend IN ITEMS warp hardware) + foreach(site IN ITEMS records materialized ids) + foreach(mode IN ITEMS on control) + if(mode STREQUAL "on") + set(alloc_test startup_view_alloc_${site}_${backend}) + add_test(NAME ${alloc_test} COMMAND startup_view_alloc ${backend} ${site}) + else() + set(alloc_test startup_view_alloc_${site}_control_${backend}) + add_test(NAME ${alloc_test} COMMAND startup_view_alloc ${backend} ${site} control) + endif() + set(alloc_env "X2W_D3D12_BUDGET=1;X2W_D3D12_RELEASE_RETIRED=1;X2W_D3D12_PERMISSIVE=1;X2W_D3D12_RENDER=1") + list(APPEND alloc_env "X2W_D3D12_DEBUG=0" "X2W_TRAP_LOG=${CMAKE_CURRENT_BINARY_DIR}/${alloc_test}.log") + set_tests_properties(${alloc_test} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77 ENVIRONMENT "${alloc_env}") + if(mode STREQUAL "control") + set_tests_properties(${alloc_test} PROPERTIES + PASS_REGULAR_EXPRESSION "FAIL: a failed publication changed a view container") + endif() + endforeach() + endforeach() +endforeach() diff --git a/tests/startup_view_alloc.cpp b/tests/startup_view_alloc.cpp new file mode 100644 index 0000000000..84dd71acbc --- /dev/null +++ b/tests/startup_view_alloc.cpp @@ -0,0 +1,307 @@ +// A view is published into three containers (view_records, view_materialized, view_ids). When one of them cannot +// allocate, the stand-in must record nothing, untrack the destination descriptor and keep running (#1199, +// docs/startup-runtime.md#d3d12-view-allocation-failure). The fixture-only export x2w_d3d12_fail_view_allocations +// makes the allocations at one site fail. +// +// startup_view_alloc [control] +// (X2W_D3D12_BUDGET=1 X2W_D3D12_RELEASE_RETIRED=1 X2W_D3D12_PERMISSIVE=1 X2W_D3D12_RENDER=1 X2W_TRAP_LOG=) +// "control" never arms the failure, so the armed expectations must fail and the fixture must print FAIL. +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +using Microsoft::WRL::ComPtr; + +struct ViewStoreProbe { + unsigned long long records, materialized, ids, records_capacity, materialized_capacity; + unsigned shadow, view_refs, retire_when_idle; +}; +extern "C" __declspec(dllimport) HRESULT WINAPI D3D12XboxCreateDevice(IUnknown*, const void*, REFIID, void**); +extern "C" __declspec(dllimport) void x2w_d3d12_fail_view_allocations(int, LONG); +extern "C" __declspec(dllimport) void x2w_d3d12_view_probe(unsigned long long, void*, ViewStoreProbe*); + +namespace { + +constexpr std::uint32_t token_magic = 0x56573258; // "X2WV" +constexpr LONG failing_calls = 3; +constexpr UINT fill_limit = 100000; + +template +Result invoke(IUnknown* object, unsigned slot, Args... args) +{ + using Call = Result (WINAPI*)(IUnknown*, Args...); + return reinterpret_cast((*reinterpret_cast(object))[slot])(object, args...); +} + +int failures; + +void expect(bool ok, const char* what) +{ + if (!ok) { + std::printf("FAIL: %s\n", what); + ++failures; + } +} + +ComPtr pick_adapter(IDXGIFactory4* factory, bool warp) +{ + ComPtr adapter; + if (warp) { + factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)); + return adapter; + } + wchar_t name[128]{}; + GetEnvironmentVariableW(L"XBOX2WINDOWS_ADAPTER", name, static_cast(std::size(name))); + for (UINT i = 0; factory->EnumAdapters1(i, &adapter) == S_OK; ++i, adapter.Reset()) { + DXGI_ADAPTER_DESC1 desc{}; + if (SUCCEEDED(adapter->GetDesc1(&desc)) && !(desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) && + (!name[0] || std::wcsstr(desc.Description, name)) && + SUCCEEDED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_12_0, __uuidof(ID3D12Device), nullptr))) + return adapter; + } + return nullptr; +} + +std::string read_log() +{ + wchar_t path[MAX_PATH]; + const DWORD length = GetEnvironmentVariableW(L"X2W_TRAP_LOG", path, MAX_PATH); + std::string text; + if (!length || length >= MAX_PATH) return text; + const HANDLE file = CreateFileW(path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, + OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + if (file == INVALID_HANDLE_VALUE) return text; + char chunk[4096]; + DWORD got = 0; + while (ReadFile(file, chunk, sizeof(chunk), &got, nullptr) && got) text.append(chunk, got); + CloseHandle(file); + return text; +} + +// The view id + 1 in the token the stand-in writes into title descriptor memory, or 0 when it holds no token. +std::uint32_t token(UINT64 handle) +{ + std::uint32_t words[2]; + std::memcpy(words, reinterpret_cast(handle), sizeof(words)); + return words[0] == token_magic ? words[1] + 1 : 0; +} + +ViewStoreProbe probe(UINT64 handle, void* resource = nullptr) +{ + ViewStoreProbe p{}; + x2w_d3d12_view_probe(handle, resource, &p); + return p; +} + +} // namespace + +int main(int argc, char** argv) +{ + const char* const sites[] = {"records", "materialized", "ids"}; + int site = -1; + for (int i = 0; argc >= 3 && i < 3; ++i) + if (!std::strcmp(argv[2], sites[i])) site = i; + if (argc < 3 || argc > 4 || (std::strcmp(argv[1], "warp") && std::strcmp(argv[1], "hardware")) || site < 0 || + (argc == 4 && std::strcmp(argv[3], "control"))) + return 2; + const bool control = argc == 4; + std::setvbuf(stdout, nullptr, _IONBF, 0); // the lines before a crash must reach the test log + wchar_t log_path[MAX_PATH]; + const DWORD log_length = GetEnvironmentVariableW(L"X2W_TRAP_LOG", log_path, MAX_PATH); + if (!log_length || log_length >= MAX_PATH) { + std::puts("FAIL: X2W_TRAP_LOG is not set"); + return 2; + } + DeleteFileW(log_path); // before the stand-in writes its first line + + ComPtr factory; + if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return 3; + const auto adapter = pick_adapter(factory.Get(), std::strcmp(argv[1], "warp") == 0); + if (!adapter) return 77; + DXGI_ADAPTER_DESC1 adapter_desc{}; + if (SUCCEEDED(adapter->GetDesc1(&adapter_desc))) std::printf("adapter=%ls\n", adapter_desc.Description); + ComPtr guest, heap; + std::array parameters{}; + if (FAILED(D3D12XboxCreateDevice(adapter.Get(), parameters.data(), __uuidof(ID3D12Device), + reinterpret_cast(guest.GetAddressOf())))) + return 3; + const struct { + D3D12_DESCRIPTOR_HEAP_DESC desc; + UINT64 backing, bytes; + } heap_desc{{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 3}, 0, 0}; + if (FAILED(invoke(guest.Get(), 14, &heap_desc, &__uuidof(ID3D12DescriptorHeap), heap.GetAddressOf()))) return 3; + const UINT64 first = invoke(heap.Get(), 9); + const UINT stride = invoke(guest.Get(), 15, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV); + const UINT64 earlier = first, filler = first + stride, target = first + 2 * UINT64{stride}; + + D3D12_HEAP_PROPERTIES default_heap{}; + default_heap.Type = D3D12_HEAP_TYPE_DEFAULT; + D3D12_RESOURCE_DESC texture{}; + texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + texture.Width = texture.Height = 16; + texture.DepthOrArraySize = texture.MipLevels = 1; + texture.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + texture.SampleDesc.Count = 1; + ComPtr textures[3]; // replaced view, failed view, later view + const IID iid = __uuidof(ID3D12Resource); + for (auto& t : textures) + if (FAILED(invoke(guest.Get(), 27, &default_heap, D3D12_HEAP_FLAG_NONE, &texture, D3D12_RESOURCE_STATE_COMMON, + static_cast(nullptr), &iid, + reinterpret_cast(t.GetAddressOf())))) + return 3; + + // The stand-in identifies a view by every byte of its description, padding included, so each description is zeroed + // with memset before its fields are set: brace initialization need not clear the padding. + D3D12_SHADER_RESOURCE_VIEW_DESC srv; + std::memset(&srv, 0, sizeof(srv)); + srv.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + srv.ViewDimension = D3D12_SRV_DIMENSION_TEXTURE2D; + srv.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING; + srv.Texture2D.MipLevels = 1; + const auto texture_view = [&](int index, UINT64 handle) { + invoke(guest.Get(), 18, static_cast(textures[index].Get()), &srv, handle); + }; + const auto constant_view = [&](UINT index, UINT64 handle) { + D3D12_CONSTANT_BUFFER_VIEW_DESC desc; + std::memset(&desc, 0, sizeof(desc)); + desc.BufferLocation = 0x100000ull + 256ull * index; + desc.SizeInBytes = 256; + invoke(guest.Get(), 17, &desc, handle); + }; + + // Two views published before any failure. + constant_view(0, earlier); + const std::uint32_t earlier_id = token(earlier); + texture_view(0, target); + const std::uint32_t replaced_id = token(target); + if (!earlier_id || !replaced_id || probe(earlier).shadow != earlier_id || probe(target).shadow != replaced_id) { + std::puts("FAIL: the views published before the failure are not tracked"); + return 3; + } + const auto first_texture = probe(target, textures[0].Get()); + if (first_texture.view_refs != 1 || !first_texture.retire_when_idle) { + std::puts("FAIL: the first texture is not in the retire class with one tracked view"); + return 3; + } + + // A vector allocates only when it is full: distinct views until the next one has to grow it. + UINT filled = 1; + const auto fill = [&] { + if (site == 2) return true; + for (; filled < fill_limit; ++filled) { + const auto p = probe(target); + if (site == 0 ? p.records == p.records_capacity : p.materialized == p.materialized_capacity) return true; + constant_view(filled, filler); + } + std::puts("FAIL: the vector never filled up"); + return false; + }; + if (!fill()) return 3; + + const auto before = probe(target); + std::printf("site=%s records=%llu capacity=%llu: arming %ld failures\n", sites[site], before.records, + before.records_capacity, control ? 0 : failing_calls); + if (!control) x2w_d3d12_fail_view_allocations(site, failing_calls); + for (LONG call = 0; call < failing_calls; ++call) { + texture_view(1, target); + const auto p = probe(target, textures[1].Get()); + expect(p.records == before.records && p.materialized == before.materialized && p.ids == before.ids, + "a failed publication changed a view container"); + expect(!p.shadow, "the destination slot still names a view after a failed publication"); + expect(!token(target), "title memory still holds a view token after a failed publication"); + expect(!p.view_refs && !p.retire_when_idle, "the resource of the failed view can still be released early"); + } + x2w_d3d12_fail_view_allocations(site, 0); + const auto replaced = probe(target, textures[0].Get()); + expect(!replaced.view_refs && replaced.retire_when_idle, "the replaced view kept its reference"); + + // Views published before the failure keep their ids and add no record. + const unsigned long long held = probe(target).records; + constant_view(0, earlier); + expect(token(earlier) == earlier_id && probe(earlier).shadow == earlier_id, "an earlier view changed its id"); + texture_view(0, target); + expect(token(target) == replaced_id && probe(target).shadow == replaced_id, "the replaced view changed its id"); + expect(probe(target).records == held, "an earlier view added a record"); + + // The view that failed, and then a view never seen before, are published normally. + for (int index = 1; index < 3; ++index) { + const auto start = probe(target); + texture_view(index, target); + const std::uint32_t id = token(target); + const auto p = probe(target, textures[index].Get()); + expect(id && p.shadow == id, "a later view is not tracked"); + // In "control" the second texture view was already published by the unarmed calls. + expect(p.records == start.records + (control && index == 1 ? 0 : 1) && p.materialized == p.records && + p.ids == p.records, + "a later view did not add one record to each container"); + expect(p.view_refs == 1, "a later view holds no reference"); + texture_view(index, target); + expect(token(target) == id && probe(target).records == p.records, "a later view did not keep its id"); + } + + // Slot 19 names two resources, the view's and its counter. A failed UAV publication over a tracked UAV drops both + // references of the earlier one and adds none. UAV-capable textures and buffers are never in the retire class, so + // only the reference counts are observable here. + D3D12_RESOURCE_DESC buffer{}; + buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + buffer.Width = 4096; + buffer.Height = buffer.DepthOrArraySize = buffer.MipLevels = 1; + buffer.SampleDesc.Count = 1; + buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + buffer.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS; + ComPtr buffers[4]; // earlier resource and counter, then the failed view's resource and counter + for (auto& b : buffers) + if (FAILED(invoke(guest.Get(), 27, &default_heap, D3D12_HEAP_FLAG_NONE, &buffer, D3D12_RESOURCE_STATE_COMMON, + static_cast(nullptr), &iid, + reinterpret_cast(b.GetAddressOf())))) + return 3; + D3D12_UNORDERED_ACCESS_VIEW_DESC uav; + std::memset(&uav, 0, sizeof(uav)); + uav.ViewDimension = D3D12_UAV_DIMENSION_BUFFER; + uav.Buffer.NumElements = 16; + uav.Buffer.StructureByteStride = 16; + const auto counted_view = [&](int index) { + invoke(guest.Get(), 19, static_cast(buffers[index].Get()), + static_cast(buffers[index + 1].Get()), &uav, target); + }; + const auto refs = [&](int index) { return probe(target, buffers[index].Get()).view_refs; }; + counted_view(0); + const std::uint32_t counted_id = token(target); + if (!counted_id || probe(target).shadow != counted_id || refs(0) != 1 || refs(1) != 1) { + std::puts("FAIL: the slot 19 view and its counter are not tracked"); + return 3; + } + if (!fill()) return 3; + const auto counted = probe(target); + if (!control) x2w_d3d12_fail_view_allocations(site, 1); + counted_view(2); + x2w_d3d12_fail_view_allocations(site, 0); + const auto dropped = probe(target); + expect(dropped.records == counted.records && dropped.materialized == counted.materialized && dropped.ids == counted.ids, + "a failed slot 19 publication changed a view container"); + expect(!dropped.shadow && !token(target), "the destination still names a view after a failed slot 19 publication"); + expect(!refs(0), "the replaced slot 19 view kept its resource reference"); + expect(!refs(1), "the replaced slot 19 view kept its counter reference"); + expect(control || (!refs(2) && !refs(3)), "the failed slot 19 view holds a reference"); + counted_view(2); + expect(token(target) && probe(target).shadow == token(target) && refs(2) == 1 && refs(3) == 1, + "a later slot 19 view and its counter are not tracked"); + counted_view(0); + expect(token(target) == counted_id && refs(0) == 1 && refs(1) == 1 && !refs(2) && !refs(3), + "the earlier slot 19 view did not take its slot back"); + + const std::string line = std::string("x2w-d3d12 note_view: out of memory in view_") + sites[site]; + expect(read_log().find(line) != std::string::npos, "the log does not name the failed allocation"); + + if (failures) return 5; + std::printf("view allocation failure checks passed (%s)\n", sites[site]); + return 0; +}