274 lines
12 KiB
C++
274 lines
12 KiB
C++
|
|
//SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
//SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
// Issue #169, on Wayland: a Window whose constructor throws leaves nothing
|
||
|
|
// behind. The constructor puts the window in Device::windows, and gives its
|
||
|
|
// xdg_surface, its xdg_toplevel and its fractional scale the window as
|
||
|
|
// listener data, before the steps that can throw: the Vulkan surface, the
|
||
|
|
// surface's formats and capabilities, the swapchain. ~Window, which takes all
|
||
|
|
// of that back, did not run for a window whose constructor threw, and the
|
||
|
|
// exception released the window's storage. An application that caught the
|
||
|
|
// error and carried on had a dangling pointer in Device::windows, which the
|
||
|
|
// event loop and the seat's enter events walk, and proxies whose events are
|
||
|
|
// dispatched into the freed window; the Vulkan surface leaked.
|
||
|
|
//
|
||
|
|
// The test makes the constructor throw from its CreateSwapchain, once all of
|
||
|
|
// the above but the swapchain has been made: with
|
||
|
|
// Device::surfaceMaintenance1Supported set, the swapchain's capabilities are
|
||
|
|
// asked for through Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR,
|
||
|
|
// where the test puts a function of its own that fails with
|
||
|
|
// VK_ERROR_SURFACE_LOST_KHR. Another window stays open throughout. Checked:
|
||
|
|
//
|
||
|
|
// - the constructor throws the error Device::CheckVkResult makes of that
|
||
|
|
// result
|
||
|
|
// - afterwards Device::windows holds the other window and nothing else
|
||
|
|
// - before the exception reached the test, the Vulkan surface the window
|
||
|
|
// had made was destroyed (the test defines vkDestroySurfaceKHR, which
|
||
|
|
// the library calls by name, and passes each call on to the loader), and
|
||
|
|
// so was each of its Wayland objects: its wl_surface, xdg_surface and
|
||
|
|
// xdg_toplevel, and its wp_fractional_scale_v1,
|
||
|
|
// zxdg_toplevel_decoration_v1 and wp_viewport where the compositor offers
|
||
|
|
// their globals. The test runs with WAYLAND_DEBUG set and stderr in a
|
||
|
|
// file, as ResizeViewport does, and finds each object's destroy request
|
||
|
|
// in that log between the failure and the catch
|
||
|
|
// - the compositor raises no protocol error over it
|
||
|
|
// - a window made after it renders and closes, and the other window
|
||
|
|
// renders throughout
|
||
|
|
// - the validation layer reports no error (only with
|
||
|
|
// CRAFTER_GRAPHICS_VALIDATION set)
|
||
|
|
//
|
||
|
|
// The checks of the failed window come before the test dispatches the
|
||
|
|
// connection's events again: without the fix, an enter event can walk the
|
||
|
|
// freed window in Device::windows from there on, so those checks report
|
||
|
|
// first. The log is kept when a check fails. Needs a live Wayland compositor
|
||
|
|
// and a Vulkan device.
|
||
|
|
|
||
|
|
#include <wayland-client.h>
|
||
|
|
#include <dlfcn.h>
|
||
|
|
#include <fcntl.h>
|
||
|
|
#include <unistd.h>
|
||
|
|
#include <cstdio>
|
||
|
|
#include <cstdlib>
|
||
|
|
#include "vulkan/vulkan.h"
|
||
|
|
|
||
|
|
import Crafter.Graphics;
|
||
|
|
import std;
|
||
|
|
using namespace Crafter;
|
||
|
|
namespace fs = std::filesystem;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
|
||
|
|
int failures = 0;
|
||
|
|
|
||
|
|
// Each line flushed as it is printed: without the fix the test can crash
|
||
|
|
// once it dispatches events again, and stdout into crafter-build's pipe would
|
||
|
|
// lose the lines of the checks before.
|
||
|
|
void Check(bool ok, std::string_view what) {
|
||
|
|
std::println("{} {}", ok ? "PASS" : "FAIL", what);
|
||
|
|
std::fflush(stdout);
|
||
|
|
if (!ok) ++failures;
|
||
|
|
}
|
||
|
|
|
||
|
|
// stderr into `path` while it lives, where libwayland writes WAYLAND_DEBUG.
|
||
|
|
struct StderrTo {
|
||
|
|
int saved = -1;
|
||
|
|
explicit StderrTo(const fs::path& path) {
|
||
|
|
std::fflush(stderr);
|
||
|
|
const int file = open(path.c_str(), O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0600);
|
||
|
|
if (file < 0) return;
|
||
|
|
saved = dup(STDERR_FILENO);
|
||
|
|
dup2(file, STDERR_FILENO);
|
||
|
|
close(file);
|
||
|
|
}
|
||
|
|
~StderrTo() {
|
||
|
|
std::fflush(stderr);
|
||
|
|
if (saved < 0) return;
|
||
|
|
dup2(saved, STDERR_FILENO);
|
||
|
|
close(saved);
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
// A line the test writes into the log between libwayland's.
|
||
|
|
void Mark(std::string_view name) {
|
||
|
|
std::println(stderr, "MARK {}", name);
|
||
|
|
std::fflush(stderr);
|
||
|
|
}
|
||
|
|
|
||
|
|
// A roundtrip fails, and the display keeps the error, once the compositor
|
||
|
|
// has raised a protocol error over anything sent before it.
|
||
|
|
bool NoProtocolError() {
|
||
|
|
return wl_display_roundtrip(Device::display) != -1 && wl_display_get_error(Device::display) == 0;
|
||
|
|
}
|
||
|
|
|
||
|
|
// A Wayland object, as WAYLAND_DEBUG names it.
|
||
|
|
struct Proxy {
|
||
|
|
std::string interface;
|
||
|
|
std::uint32_t id = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
// What the failing capabilities query saw of the window being made.
|
||
|
|
struct Failed {
|
||
|
|
int calls = 0;
|
||
|
|
std::uintptr_t window = 0;
|
||
|
|
VkSurfaceKHR surface = VK_NULL_HANDLE;
|
||
|
|
std::vector<Proxy> proxies;
|
||
|
|
};
|
||
|
|
Failed failed;
|
||
|
|
|
||
|
|
// Every surface the library passed to vkDestroySurfaceKHR, in order.
|
||
|
|
std::vector<VkSurfaceKHR> destroyedSurfaces;
|
||
|
|
|
||
|
|
VkResult FailCapabilities(VkPhysicalDevice, const VkPhysicalDeviceSurfaceInfo2KHR* info, VkSurfaceCapabilities2KHR*) {
|
||
|
|
++failed.calls;
|
||
|
|
// The window being made: its constructor put it in Device::windows
|
||
|
|
// first thing, behind the other window.
|
||
|
|
const Window* window = Device::windows.back();
|
||
|
|
failed.window = reinterpret_cast<std::uintptr_t>(window);
|
||
|
|
failed.surface = info->surface;
|
||
|
|
for (void* object : { static_cast<void*>(window->surface), static_cast<void*>(window->xdgSurface),
|
||
|
|
static_cast<void*>(window->xdgToplevel), static_cast<void*>(window->wp_scale),
|
||
|
|
static_cast<void*>(window->decoration), static_cast<void*>(window->wpViewport) }) {
|
||
|
|
if (object == nullptr) continue;
|
||
|
|
wl_proxy* proxy = static_cast<wl_proxy*>(object);
|
||
|
|
failed.proxies.push_back({ wl_proxy_get_class(proxy), wl_proxy_get_id(proxy) });
|
||
|
|
}
|
||
|
|
Mark("fail");
|
||
|
|
return VK_ERROR_SURFACE_LOST_KHR;
|
||
|
|
}
|
||
|
|
|
||
|
|
// The log's lines from the mark `from` to the mark `to`.
|
||
|
|
std::vector<std::string> LinesBetween(const fs::path& log, std::string_view from, std::string_view to) {
|
||
|
|
std::vector<std::string> lines;
|
||
|
|
std::ifstream file(log);
|
||
|
|
bool inside = false;
|
||
|
|
for (std::string line; std::getline(file, line);) {
|
||
|
|
if (line == std::format("MARK {}", from)) {
|
||
|
|
inside = true;
|
||
|
|
} else if (line == std::format("MARK {}", to)) {
|
||
|
|
break;
|
||
|
|
} else if (inside) {
|
||
|
|
lines.push_back(std::move(line));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return lines;
|
||
|
|
}
|
||
|
|
|
||
|
|
// A request line destroys `proxy`:
|
||
|
|
// [09:41:07.118030] {Default Queue} -> xdg_toplevel#25.destroy()
|
||
|
|
// Older libwayland writes an `@` for the `#`.
|
||
|
|
bool DestroyedIn(const std::vector<std::string>& lines, const Proxy& proxy) {
|
||
|
|
return std::ranges::any_of(lines, [&](const std::string& line) {
|
||
|
|
return line.contains(std::format("-> {}#{}.destroy(", proxy.interface, proxy.id))
|
||
|
|
|| line.contains(std::format("-> {}@{}.destroy(", proxy.interface, proxy.id));
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
} // namespace
|
||
|
|
|
||
|
|
// The library calls vkDestroySurfaceKHR by name, so it reaches this
|
||
|
|
// definition, which notes the surface and passes the call on to the loader's.
|
||
|
|
extern "C" VKAPI_ATTR void VKAPI_CALL vkDestroySurfaceKHR(VkInstance instance, VkSurfaceKHR surface, const VkAllocationCallbacks* allocator) {
|
||
|
|
destroyedSurfaces.push_back(surface);
|
||
|
|
static const auto next = reinterpret_cast<PFN_vkDestroySurfaceKHR>(dlsym(RTLD_NEXT, "vkDestroySurfaceKHR"));
|
||
|
|
if (next != nullptr) next(instance, surface, allocator);
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
const fs::path log = fs::temp_directory_path() / std::format("crafter-window-constructor-throw-{}.log", getpid());
|
||
|
|
// Read when the display connects, in Device::Initialize. libwayland
|
||
|
|
// colours the log when FORCE_COLOR says so, also into a file.
|
||
|
|
setenv("WAYLAND_DEBUG", "1", 1);
|
||
|
|
unsetenv("FORCE_COLOR");
|
||
|
|
setenv("NO_COLOR", "1", 1);
|
||
|
|
|
||
|
|
std::optional<std::string> thrown;
|
||
|
|
std::optional<std::string> unexpected;
|
||
|
|
{
|
||
|
|
StderrTo redirect(log);
|
||
|
|
try {
|
||
|
|
Device::Initialize();
|
||
|
|
Device::validationErrorCount = 0;
|
||
|
|
|
||
|
|
Window other(160, 120, "WindowConstructorThrow: the other window");
|
||
|
|
for (int i = 0; i < Window::numFrames; ++i) other.Render();
|
||
|
|
|
||
|
|
const bool surfaceMaintenance1 = Device::surfaceMaintenance1Supported;
|
||
|
|
const PFN_vkGetPhysicalDeviceSurfaceCapabilities2KHR capabilities2 = Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR;
|
||
|
|
Device::surfaceMaintenance1Supported = true;
|
||
|
|
Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR = FailCapabilities;
|
||
|
|
std::size_t destroyedAtCatch = 0;
|
||
|
|
try {
|
||
|
|
auto window = std::make_unique<Window>(320, 240, "WindowConstructorThrow: the window that fails");
|
||
|
|
} catch (const std::runtime_error& error) {
|
||
|
|
Mark("caught");
|
||
|
|
thrown = error.what();
|
||
|
|
destroyedAtCatch = destroyedSurfaces.size();
|
||
|
|
}
|
||
|
|
Device::surfaceMaintenance1Supported = surfaceMaintenance1;
|
||
|
|
Device::vkGetPhysicalDeviceSurfaceCapabilities2KHR = capabilities2;
|
||
|
|
|
||
|
|
Check(thrown == "VK_ERROR_SURFACE_LOST_KHR",
|
||
|
|
std::format("the constructor throws the error of the failed query (threw: {})", thrown.value_or("nothing")));
|
||
|
|
Check(failed.calls == 1 && failed.window != 0 && failed.window != reinterpret_cast<std::uintptr_t>(&other),
|
||
|
|
std::format("the query failed once, inside the window's constructor ({} calls)", failed.calls));
|
||
|
|
std::vector<std::uintptr_t> listed;
|
||
|
|
for (const Window* window : Device::windows) listed.push_back(reinterpret_cast<std::uintptr_t>(window));
|
||
|
|
Check(listed == std::vector{ reinterpret_cast<std::uintptr_t>(&other) },
|
||
|
|
std::format("Device::windows holds the other window and nothing else ({} entries, the failed window {})",
|
||
|
|
listed.size(), std::ranges::contains(listed, failed.window) ? "among them" : "not among them"));
|
||
|
|
Check(failed.surface != VK_NULL_HANDLE
|
||
|
|
&& std::ranges::contains(std::span(destroyedSurfaces).first(destroyedAtCatch), failed.surface),
|
||
|
|
"the failed window's Vulkan surface was destroyed before the exception reached the caller");
|
||
|
|
// The objects the failed window made, each destroyed between the
|
||
|
|
// moment it failed and the catch: by ~Window, as the exception
|
||
|
|
// left the constructor. Read before the connection's events are
|
||
|
|
// dispatched again, as everything above is: where the window is
|
||
|
|
// still in Device::windows, the seat's enter events walk it there.
|
||
|
|
const std::vector<std::string> unwinding = LinesBetween(log, "fail", "caught");
|
||
|
|
std::vector<std::string> names;
|
||
|
|
for (const Proxy& proxy : failed.proxies) names.push_back(proxy.interface);
|
||
|
|
Check(std::ranges::contains(names, "wl_surface") && std::ranges::contains(names, "xdg_surface") && std::ranges::contains(names, "xdg_toplevel"),
|
||
|
|
std::format("the failed window had made its Wayland objects when it failed: {}", names));
|
||
|
|
for (const Proxy& proxy : failed.proxies) {
|
||
|
|
Check(DestroyedIn(unwinding, proxy),
|
||
|
|
std::format("its {}#{} was destroyed before the exception reached the caller", proxy.interface, proxy.id));
|
||
|
|
}
|
||
|
|
|
||
|
|
Check(NoProtocolError(), "no protocol error after the failed constructor");
|
||
|
|
|
||
|
|
{
|
||
|
|
Window after(320, 240, "WindowConstructorThrow: the window after");
|
||
|
|
for (int i = 0; i < Window::numFrames * 2; ++i) {
|
||
|
|
after.Render();
|
||
|
|
other.Render();
|
||
|
|
}
|
||
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
||
|
|
}
|
||
|
|
other.Render();
|
||
|
|
Device::CheckVkResult(vkQueueWaitIdle(Device::queue));
|
||
|
|
Check(NoProtocolError(), "a window made after it renders and closes, beside the other window, with no protocol error");
|
||
|
|
wl_display_flush(Device::display);
|
||
|
|
} catch (const std::exception& error) {
|
||
|
|
unexpected = error.what();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
if (unexpected) {
|
||
|
|
std::println("FAIL the run threw: {}", *unexpected);
|
||
|
|
std::println(" the log is {}", log.string());
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (std::getenv("CRAFTER_GRAPHICS_VALIDATION") != nullptr) {
|
||
|
|
Check(Device::validationErrorCount == 0,
|
||
|
|
std::format("no Vulkan validation errors ({} seen)", Device::validationErrorCount));
|
||
|
|
}
|
||
|
|
if (failures != 0) {
|
||
|
|
std::println("{} check(s) failed", failures);
|
||
|
|
std::println("the log is {}", log.string());
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
std::error_code ignored;
|
||
|
|
fs::remove(log, ignored);
|
||
|
|
std::println("all checks passed");
|
||
|
|
return EXIT_SUCCESS;
|
||
|
|
}
|