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
3 changes: 2 additions & 1 deletion runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,8 @@ if(TARGET Vulkan::Headers)
endif()
add_test(NAME vk_presenter_sync COMMAND vk_presenter_sync_test)
add_test(NAME vk_presenter_sparse_capture COMMAND vk_presenter_sync_test --sparse-capture)
set_tests_properties(vk_presenter_sync vk_presenter_sparse_capture PROPERTIES SKIP_RETURN_CODE 77)
add_test(NAME vk_presenter_probe_timeout COMMAND vk_presenter_sync_test --probe-timeout)
set_tests_properties(vk_presenter_sync vk_presenter_sparse_capture vk_presenter_probe_timeout PROPERTIES SKIP_RETURN_CODE 77)
set(conformance_src ${CMAKE_CURRENT_SOURCE_DIR}/../tools/conformance/native/src)
add_executable(vk_present_dump_test test/vk_present_dump_test.cpp
${conformance_src}/png_io.cpp ${conformance_src}/diagnostics.cpp)
Expand Down
42 changes: 38 additions & 4 deletions runtime/src/vulkan_present.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class VulkanPresenter {
if (frame.done) vkDestroyFence_(device, frame.done, nullptr);
if (frame.acquired) vk.vkDestroySemaphore(device, frame.acquired, nullptr);
}
probe_reclaim(true); // LOCAL DIAGNOSTIC: the queue wait above has finished a timed-out readback's copy
if (probe_fence_) vkDestroyFence_(device, probe_fence_, nullptr); // LOCAL DIAGNOSTIC
if (pool_) vk.vkDestroyCommandPool(device, pool_, nullptr);
if (surface_ && vkDestroySurfaceKHR_) vkDestroySurfaceKHR_(instance_, surface_, nullptr);
Expand Down Expand Up @@ -722,6 +723,9 @@ class VulkanPresenter {
int probe_written_ = 0;
VkCommandBuffer probe_commands_ = VK_NULL_HANDLE;
VkFence probe_fence_ = VK_NULL_HANDLE;
// The staging buffer of a readback whose fence wait timed out: the copy may still write it (probe_reclaim).
VkBuffer probe_parked_buffer_ = VK_NULL_HANDLE;
VkDeviceMemory probe_parked_memory_ = VK_NULL_HANDLE;

static float probe_half(std::uint16_t h) {
const std::uint32_t sign = (h >> 15) & 1, exponent = (h >> 10) & 0x1f, mantissa = h & 0x3ff;
Expand Down Expand Up @@ -808,6 +812,21 @@ class VulkanPresenter {
}
}

// Frees the staging buffer a timed-out readback left behind once its fence has signalled (or unconditionally
// when `idle`: the destructor's queue wait has finished the copy). True when nothing is in flight any more, so
// the probe's command buffer and fence may be reset.
bool probe_reclaim(bool idle) {
if (!probe_parked_buffer_ && !probe_parked_memory_) return true;
const VkDevice device = context_.device();
if (!idle && vkWaitForFences_(device, 1, &probe_fence_, VK_TRUE, 0) != VK_SUCCESS) return false;
auto& vk = context_.vk;
if (probe_parked_memory_) vk.vkFreeMemory(device, probe_parked_memory_, nullptr);
if (probe_parked_buffer_) vk.vkDestroyBuffer(device, probe_parked_buffer_, nullptr);
probe_parked_buffer_ = VK_NULL_HANDLE;
probe_parked_memory_ = VK_NULL_HANDLE;
return true;
}

// Copies mip 0, layer 0 of `id` to host memory on the presenter's queue (waited for), logs and writes it.
void probe_read_source(ResourceId id, std::uint64_t number, const char* path, const char* prefix = "source") {
const bool is_source = std::strcmp(prefix, "source") == 0;
Expand All @@ -827,6 +846,11 @@ class VulkanPresenter {
return;
}
}
if (!probe_reclaim(false)) { // an earlier readback timed out and its copy is still running
std::fprintf(stderr, "[rt] present-probe: %s frame %llu skipped: the last readback is still in flight\n",
prefix, static_cast<unsigned long long>(number));
return;
}
std::shared_lock resource_lock(context_.resource_mutex_);
const ResourceBinding* found = context_.resolve(id);
if (!found || !found->image) return;
Expand Down Expand Up @@ -934,13 +958,23 @@ class VulkanPresenter {
"vkQueueSubmit (present probe)");
if (submitted == VK_SUCCESS) context_.note_image_work_locked();
}
if (submitted != VK_SUCCESS ||
context_.observe_result(vkWaitForFences_(device, 1, &probe_fence_, VK_TRUE, 5'000'000'000ull),
"vkWaitForFences (present probe)") != VK_SUCCESS) {
std::fprintf(stderr, "[rt] present-probe: readback submit/wait failed (%d)\n", int(submitted));
if (submitted != VK_SUCCESS) {
std::fprintf(stderr, "[rt] present-probe: readback submit failed (%d)\n", int(submitted));
cleanup();
return;
}
if (const VkResult waited = context_.observe_result(
vkWaitForFences_(device, 1, &probe_fence_, VK_TRUE, 5'000'000'000ull), "vkWaitForFences (present probe)");
waited != VK_SUCCESS) {
// The copy was submitted and may still run: the GPU can write the staging buffer and still owns the
// command buffer and the fence. Give up on this readback, but park the buffer and its memory until the
// fence signals (probe_reclaim, or the destructor after its queue wait) instead of freeing them now.
std::fprintf(stderr, "[rt] present-probe: readback wait failed (%d); its buffer is freed once the copy is "
"done\n", int(waited));
probe_parked_buffer_ = buffer;
probe_parked_memory_ = memory;
return;
}
const std::size_t texels = std::size_t(binding.width) * binding.height;
const auto* bytes = static_cast<const std::uint8_t*>(mapped);
std::vector<std::uint8_t> rgb(texels * 3);
Expand Down
190 changes: 190 additions & 0 deletions runtime/test/vk_presenter_probe_timeout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
// vk_presenter_sync_test --probe-timeout (RT-PRESENT-PROBE-TIMEOUT-UAF): the present-source readback probe
// (LEONIDA_CAPTURE_SOURCE) gives up when its fence wait fails, but the copy it submitted may still be running. The
// driver hooks in vk_presenter_sync_test.cpp hold that copy on a timeline semaphore and make the wait time out at
// once. The probe must not free the staging buffer or its memory while the copy is pending, must not reset and
// resubmit its command buffer and fence for a later present while they are in use (that probe is skipped), must take
// readbacks again once the copy is done, and must leave nothing behind at vkDestroyDevice (run it with
// LEONIDA_VK_VALIDATION=1 for the layer's own checks). Included after the driver hooks. No game data.
#pragma once

namespace probe_timeout_test {
namespace fs = std::filesystem;

// A fresh owner/SYSTEM-only capture folder; only the files the probe and the capture write in it are removed.
struct Folder {
fs::path path;
bool create() {
auto candidate = fs::temp_directory_path() / ("leonida-probe-timeout-" + std::to_string(GetCurrentProcessId()) +
"-" + std::to_string(GetTickCount64()));
PSECURITY_DESCRIPTOR sd = nullptr;
if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(L"D:P(A;OICI;FA;;;OW)(A;OICI;FA;;;SY)",
SDDL_REVISION_1, &sd, nullptr)) return false;
SECURITY_ATTRIBUTES sa{sizeof(sa), sd, FALSE};
const bool fresh = CreateDirectoryW(candidate.c_str(), &sa) != FALSE;
LocalFree(sd);
if (fresh) path = std::move(candidate);
return fresh;
}
~Folder() {
if (path.empty()) return;
std::error_code ec;
for (const auto& e : fs::directory_iterator(path, ec)) {
const std::string name = e.path().filename().string();
const bool ours = name == "index.txt" || name == "source_index.txt" ||
((name.rfind("frame_", 0) == 0 || name.rfind("source_", 0) == 0) &&
e.path().extension() == ".ppm");
if (ours && fs::is_regular_file(e.symlink_status())) fs::remove(e.path(), ec);
}
fs::remove(path, ec); // leave unknown contents alone
}
};

inline int run() {
using namespace leonida;
Folder folder;
if (!folder.create()) {
std::fprintf(stderr, "probe timeout: cannot make a capture folder\n");
return 1;
}
_putenv_s("LEONIDA_CAPTURE", folder.path.string().c_str());
_putenv_s("LEONIDA_CAPTURE_SOURCE", "1");
_putenv_s("LEONIDA_CAPTURE_EVERY", "1");

auto d = std::make_unique<vktest::Device>();
if (!d->create(true)) {
std::fprintf(stderr, "vk_presenter_sync_test --probe-timeout: no Vulkan device with presentation, skipped\n");
return 77;
}
WNDCLASSW wc{};
wc.lpfnWndProc = window_proc;
wc.hInstance = GetModuleHandleW(nullptr);
wc.lpszClassName = L"LeonidaPresenterProbeTimeoutTest";
EXPECT(RegisterClassW(&wc));
constexpr std::uint32_t width = 64, height = 48;
RECT fr{0, 0, LONG(width), LONG(height)};
AdjustWindowRect(&fr, WS_OVERLAPPEDWINDOW, FALSE);
HWND window = CreateWindowExW(WS_EX_NOACTIVATE | WS_EX_TOOLWINDOW, wc.lpszClassName, L"Leonida probe timeout test",
WS_OVERLAPPEDWINDOW, 80, 80, fr.right - fr.left, fr.bottom - fr.top, nullptr, nullptr,
wc.hInstance, nullptr);
EXPECT(window);
ShowWindow(window, SW_SHOWNOACTIVATE);
pump();
driver::install(d->instance_proc, d->instance, d->device);

const auto create_semaphore =
reinterpret_cast<PFN_vkCreateSemaphore>(driver::real_get_device(d->device, "vkCreateSemaphore"));
const auto destroy_semaphore =
reinterpret_cast<PFN_vkDestroySemaphore>(driver::real_get_device(d->device, "vkDestroySemaphore"));
const auto signal_semaphore =
reinterpret_cast<PFN_vkSignalSemaphore>(driver::real_get_device(d->device, "vkSignalSemaphore"));
EXPECT(create_semaphore && destroy_semaphore && signal_semaphore);
VkSemaphoreTypeCreateInfo type{VK_STRUCTURE_TYPE_SEMAPHORE_TYPE_CREATE_INFO};
type.semaphoreType = VK_SEMAPHORE_TYPE_TIMELINE;
VkSemaphoreCreateInfo semaphore_info{VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, &type};
VkSemaphore hold = VK_NULL_HANDLE;
EXPECT(create_semaphore(d->device, &semaphore_info, nullptr, &hold) == VK_SUCCESS);

bool first_read = false, kept = false, one_submit_while_held = false, read_again = false, idle = false;
VkResult presents[4] = {VK_ERROR_UNKNOWN, VK_ERROR_UNKNOWN, VK_ERROR_UNKNOWN, VK_ERROR_UNKNOWN};
{
VulkanContext context(driver::get_device, d->device, true);
EXPECT(context.loaded());
VulkanQueue queue(context, d->queue, d->family);
EXPECT(queue.ok());
const VkImage image = d->image(VK_FORMAT_R8G8B8A8_UNORM, 32, 32, 1,
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_TRANSFER_SRC_BIT);
EXPECT(image != VK_NULL_HANDLE);
ResourceBinding rb{};
rb.image = image;
rb.format = VK_FORMAT_R8G8B8A8_UNORM;
rb.aspects = VK_IMAGE_ASPECT_COLOR_BIT;
rb.width = 32;
rb.height = 32;
rb.mip_levels = 1;
rb.array_layers = 1;
rb.depth = 1;
rb.samples = 1;
rb.color_attachment = true;
context.add_resource(1, rb, VK_IMAGE_LAYOUT_UNDEFINED);
{ // a written source: the probe only reads back what a tracked GPU operation wrote
CommandList clear;
EXPECT(clear.transition({1, 0, d3d12_state::common, d3d12_state::render_target}));
EXPECT(clear.clear_render_target({1, 0, 0, 1}, {0.2f, 0.6f, 0.9f, 1.0f}));
EXPECT(clear.transition({1, 0, d3d12_state::render_target, d3d12_state::common}));
EXPECT(clear.close());
const CommandList* ptr = &clear;
EXPECT(queue.execute({&ptr, 1}) == ReplayResult::success);
EXPECT(queue.idle());
}
driver::probe_image = image;

const auto create_surface = reinterpret_cast<PFN_vkCreateWin32SurfaceKHR>(
d->instance_proc(d->instance, "vkCreateWin32SurfaceKHR"));
EXPECT(create_surface);
VkWin32SurfaceCreateInfoKHR surface_info{VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR};
surface_info.hinstance = GetModuleHandleW(nullptr);
surface_info.hwnd = window;
VkSurfaceKHR surface = VK_NULL_HANDLE;
EXPECT(create_surface(d->instance, &surface_info, nullptr, &surface) == VK_SUCCESS);
{
VulkanPresenter presenter(context, driver::get_instance, d->instance, d->physical, d->queue, d->family,
surface);
EXPECT(presenter.ok());

// 1. A plain readback: the probe is on and copies the source.
presents[0] = presenter.present(1, true);
first_read = driver::probe_submits.load() == 1;

// 2. The next readback's copy is held and its wait times out. The frame's own submit is made to fail
// (nothing may be presented behind the held copy), so this present reports that failure. No EXPECT
// may return from here until the semaphore is signalled: the destructor would wait for the queue.
driver::hold = hold;
driver::held.store(true);
presents[1] = presenter.present(1, true);
kept = !driver::freed_in_flight.load() && driver::held_buffer != VK_NULL_HANDLE;

// 3. A present while the copy is still held: its probe, which runs first, must not reset the busy
// command buffer and fence and submit again. The rest of that present waits for the queue (the image
// the failed present gave back is fenced behind the held copy) until the release thread signals.
VkResult signalled = VK_ERROR_UNKNOWN;
std::thread release([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(500));
driver::held.store(false);
VkSemaphoreSignalInfo signal{VK_STRUCTURE_TYPE_SEMAPHORE_SIGNAL_INFO};
signal.semaphore = hold;
signal.value = 1;
signalled = signal_semaphore(d->device, &signal);
});
presents[2] = presenter.present(1, true);
release.join();
one_submit_while_held = driver::probe_submits_held.load() == 1 && driver::probe_submits.load() == 2 &&
!driver::freed_in_flight.load() && signalled == VK_SUCCESS;

// 4. With the copy done, the parked buffer is freed and the probe reads back again.
presents[3] = presenter.present(1, true);
read_again = driver::probe_submits.load() == 3;
idle = queue.idle();
driver::probe_image = VK_NULL_HANDLE;
}
pump();
context.remove_resource(1);
}
destroy_semaphore(d->device, hold, nullptr);
DestroyWindow(window);
UnregisterClassW(wc.lpszClassName, wc.hInstance);
d.reset(); // vkDestroyDevice: the layer reports any object still alive here
for (int i = 0; i < 4; ++i) std::fprintf(stderr, "probe timeout: present %d returned %d\n", i, int(presents[i]));
EXPECT(presents[0] == VK_SUCCESS && presents[1] == VK_ERROR_OUT_OF_HOST_MEMORY &&
presents[2] == VK_SUCCESS && presents[3] == VK_SUCCESS);
EXPECT(first_read);
EXPECT(kept);
EXPECT(one_submit_while_held);
EXPECT(read_again);
EXPECT(idle);
EXPECT(vktest::validation_error_count() == 0);
std::printf("vk_presenter_sync_test --probe-timeout: ok\n");
return 0;
}

} // namespace probe_timeout_test
Loading
Loading