//! Clipboard model: the host clipboard service driven only through `ot_clipboard_*`, from two //! Contexts, against a scripted Wayland compositor that stands in for libwayland. No display, //! socket or desktop clipboard is touched: the compositor is this file's fake symbols, and the //! service's own Wayland connection runs its real handshake, dispatch, provider and transfer code //! against them. //! //! The ABI owns every command. The fake sits behind it in two places only: the service's //! libraries say Wayland is present, and before a poll the fake accepts a connection by //! installing a Wayland connection that uses the fake symbols. The production connect path //! opens a socket named by the environment, which would reach a real compositor. //! //! Properties, named for the bug class each catches: //! newest-wins: a mutation takes effect inside the poll that reports it written or cleared, //! never in another call, and never after a newer mutation of the same service and //! selection (a stale write); the compositor's selection always equals the newest settled //! effect, and what another application pastes is its text. //! own-result: a settled status fits the request and what happened to it, and a read returns //! a value its selection held between its start and its completion; a handle polled //! through the other Context is invalid (a result delivered to the wrong request). //! one-completion: a request settles once: its status never changes after it, cancel and //! destroy agree with it, and a destroyed request is invalid. //! closed-session: a closing service takes no new request, publishes nothing, releases its //! selections, and its requests are invalid once it is destroyed. //! owner-thread: only the thread that owns the Context calls the connection. //! closed-connection: no call reaches a destroyed proxy or a disconnected display. //! no-leak: every connection disconnects, every proxy a client received is destroyed, and //! open file descriptors return to their count before the history. //! settles: once the compositor delivers events again, every request settles. const std = @import("std"); const builtin = @import("builtin"); const c = @import("context_abi_c"); const abi = @import("../context-abi.zig"); const clip = @import("../clipboard-abi.zig"); const host = @import("../clipboard/host.zig"); const linux = @import("../clipboard/linux.zig"); const wayland = @import("../clipboard/wayland.zig"); const posix_io = @import("../clipboard/posix-io.zig"); const model = @import("model.zig"); const WlProxy = linux.WlProxy; const WlDisplay = linux.WlDisplay; const WlArgument = linux.WlArgument; const WlInterface = linux.WlInterface; const Fd = std.posix.fd_t; const Status = host.OperationStatus; extern "c" fn __errno_location() *c_int; const operations_max = 16; const timeout_ms = 600_000; const proxy_base: u32 = 0x1000; const external: u8 = 255; const external_label: u32 = 0x8000_0000; /// host.zig's ErrorCode.wayland_provider. const wayland_provider_error: u32 = 103; // The compositor const Kind = enum { display, registry, seat, callback, manager, device, source, offer }; const Mime = enum { text, text_utf8, png, other }; fn mimeName(mime: Mime) [:0]const u8 { return switch (mime) { .text => "text/plain", .text_utf8 => "text/plain;charset=utf-8", .png => "image/png", .other => "application/x-other", }; } fn parseMime(name: ?[*:0]const u8) Mime { const text = std.mem.span(name orelse return .other); inline for (.{ Mime.text, Mime.text_utf8, Mime.png }) |mime| { if (std.mem.eql(u8, text, mimeName(mime))) return mime; } return .other; } const Proxy = struct { kind: Kind, client: u8, /// Not yet destroyed by the client (a display: not yet disconnected). live: bool = true, /// An offer exists for the client once its data_offer event is dispatched. announced: bool = true, listener: ?[*]const *const anyopaque = null, data: ?*anyopaque = null, /// A source: the request whose poll created it (0 outside a poll), or an external label. label: u32 = 0, mimes: [4]Mime = undefined, mime_count: u8 = 0, /// A source the compositor no longer serves. dead: bool = false, /// An offer: its source. source: u32 = 0, /// A source: the compositor delivered it a transfer. served: bool = false, }; const Event = union(enum) { done: u32, data_offer: u32, mime: struct { offer: u32, mime: Mime }, selection: struct { primary: bool, offer: u32 }, send: struct { source: u32, mime: Mime, fd: Fd }, cancelled: u32, }; const Client = struct { ctx: u1, display: u32, connected: bool = true, /// The compositor dropped the client: dispatch and flush fail, requests do nothing. broken: bool = false, device: u32 = 0, events: std.ArrayListUnmanaged(Event) = .empty, }; const Landing = struct { sel: u1, label: u32, source: u32, ctx: u1 }; const Compositor = struct { gpa: std.mem.Allocator, owner: std.Thread.Id, proxies: std.ArrayListUnmanaged(Proxy) = .empty, clients: std.ArrayListUnmanaged(Client) = .empty, selection: [2]u32 = .{ 0, 0 }, /// Every label each selection held, in order. history: [2]std.ArrayListUnmanaged(u32) = .{ .empty, .empty }, landings: std.ArrayListUnmanaged(Landing) = .empty, held: [2]bool = .{ false, false }, /// The compositor is slow to tell a Context's connection that its source was replaced. lagged: [2]bool = .{ false, false }, closing: [2]bool = .{ false, false }, /// The request being polled, so a source records which call created it. current: u32 = 0, violation: ?[]const u8 = null, fn flag(f: *Compositor, property: []const u8) void { if (f.violation == null) f.violation = property; } fn at(f: *Compositor, id: u32) *Proxy { return &f.proxies.items[id - proxy_base]; } fn create(f: *Compositor, kind: Kind, client: u8) u32 { f.proxies.append(f.gpa, .{ .kind = kind, .client = client }) catch @panic("fake compositor out of memory"); return proxy_base + @as(u32, @intCast(f.proxies.items.len - 1)); } /// Every call a client makes passes here. fn touch(f: *Compositor, id: u32) ?*Proxy { if (std.Thread.getCurrentId() != f.owner) f.flag("owner-thread"); if (id < proxy_base or id - proxy_base >= f.proxies.items.len) { f.flag("closed-connection"); return null; } const proxy = f.at(id); if (!proxy.live) f.flag("closed-connection"); if (proxy.client != external and !f.clients.items[proxy.client].connected) f.flag("closed-connection"); return proxy; } fn queue(f: *Compositor, client: u8, event: Event) void { f.clients.items[client].events.append(f.gpa, event) catch @panic("fake compositor out of memory"); } fn labelOf(f: *Compositor, source: u32) u32 { return if (source == 0) 0 else f.at(source).label; } fn connect(f: *Compositor, ctx: u1) struct { display: u32, registry: u32, seat: u32 } { const index: u8 = @intCast(f.clients.items.len); std.debug.assert(index < external); f.clients.append(f.gpa, .{ .ctx = ctx, .display = 0 }) catch @panic("fake compositor out of memory"); const display = f.create(.display, index); f.clients.items[index].display = display; return .{ .display = display, .registry = f.create(.registry, index), .seat = f.create(.seat, index) }; } fn announce(f: *Compositor, client: u8, sel: u1) void { const source = f.selection[sel]; var offer: u32 = 0; if (source != 0) { offer = f.create(.offer, client); f.at(offer).announced = false; f.at(offer).source = source; f.queue(client, .{ .data_offer = offer }); const mimes = f.at(source).mimes; for (mimes[0..f.at(source).mime_count]) |mime| f.queue(client, .{ .mime = .{ .offer = offer, .mime = mime } }); } f.queue(client, .{ .selection = .{ .primary = sel == 1, .offer = offer } }); } fn setSelection(f: *Compositor, sel: u1, source: u32, cancel_old: bool) void { const old = f.selection[sel]; if (old == source) return; if (old != 0 and cancel_old) { const previous = f.at(old); if (previous.client == external) { previous.dead = true; } else if (!previous.dead and f.clients.items[previous.client].connected and !f.clients.items[previous.client].broken) { f.queue(previous.client, .{ .cancelled = old }); } } f.selection[sel] = source; f.history[sel].append(f.gpa, f.labelOf(source)) catch @panic("fake compositor out of memory"); for (f.clients.items, 0..) |client, index| { if (client.connected and !client.broken and client.device != 0) f.announce(@intCast(index), sel); } } fn dropSource(f: *Compositor, source: u32) void { f.at(source).dead = true; for (0..2) |sel| { if (f.selection[sel] == source) f.setSelection(@intCast(sel), 0, false); } } fn closeEvents(f: *Compositor, client: u8) void { const events = &f.clients.items[client].events; for (events.items) |event| switch (event) { .send => |send| posix_io.close(send.fd), else => {}, }; events.clearRetainingCapacity(); } /// The compositor drops a client: its sources stop serving and its selections empty. fn drop(f: *Compositor, ctx: u1) bool { for (f.clients.items, 0..) |*client, index| { if (client.ctx != ctx or !client.connected or client.broken) continue; client.broken = true; f.closeEvents(@intCast(index)); // Dropping a source announces the empty selection, which adds offers to the list. var id: u32 = proxy_base; while (id - proxy_base < f.proxies.items.len) : (id += 1) { const proxy = f.at(id); if (proxy.kind == .source and proxy.client == index and !proxy.dead) f.dropSource(id); } return true; } return false; } /// A dropped connection its service has not noticed yet. fn dropped(f: *Compositor, ctx: u1) bool { for (f.clients.items) |client| { if (client.ctx == ctx and client.connected and client.broken) return true; } return false; } fn receive(f: *Compositor, offer: u32, mime: Mime, fd: Fd) void { const source = f.at(offer).source; const copy = posix_io.duplicate(fd) catch { f.flag("no-leak"); return; }; if (source == 0 or f.at(source).dead) return posix_io.close(copy); const owner = f.at(source).client; if (owner == external) { _ = posix_io.write(copy, harness.?.bytes(f.at(source).label)) catch {}; return posix_io.close(copy); } const client = f.clients.items[owner]; if (!client.connected or client.broken) return posix_io.close(copy); f.queue(owner, .{ .send = .{ .source = source, .mime = mime, .fd = copy } }); } fn deliver(f: *Compositor, client: u8, event: Event) void { const Done = *const fn (?*anyopaque, ?*WlProxy, u32) callconv(.c) void; const Object = *const fn (?*anyopaque, ?*WlProxy, ?*WlProxy) callconv(.c) void; const Text = *const fn (?*anyopaque, ?*WlProxy, [*:0]const u8) callconv(.c) void; const Send = *const fn (?*anyopaque, ?*WlProxy, [*:0]const u8, i32) callconv(.c) void; const Plain = *const fn (?*anyopaque, ?*WlProxy) callconv(.c) void; const device = f.clients.items[client].device; switch (event) { .done => |id| { const proxy = f.at(id); const listener = proxy.listener orelse return; if (!proxy.live) return; const call: Done = @ptrCast(listener[0]); call(proxy.data, ptr(id), 0); }, .data_offer => |id| { f.at(id).announced = true; const listener = f.at(device).listener orelse return; if (!f.at(device).live) return; const call: Object = @ptrCast(listener[0]); call(f.at(device).data, ptr(device), ptr(id)); }, .mime => |mime| { const proxy = f.at(mime.offer); const listener = proxy.listener orelse return; if (!proxy.live) return; const call: Text = @ptrCast(listener[0]); call(proxy.data, ptr(mime.offer), mimeName(mime.mime).ptr); }, .selection => |selection| { const listener = f.at(device).listener orelse return; if (!f.at(device).live) return; const offer: ?*WlProxy = if (selection.offer != 0 and f.at(selection.offer).live) ptr(selection.offer) else null; const call: Object = @ptrCast(listener[if (selection.primary) 3 else 1]); call(f.at(device).data, ptr(device), offer); }, .send => |send| { const proxy = f.at(send.source); const listener = proxy.listener orelse return posix_io.close(send.fd); if (!proxy.live) return posix_io.close(send.fd); proxy.served = true; const call: Send = @ptrCast(listener[0]); call(proxy.data, ptr(send.source), mimeName(send.mime).ptr, send.fd); }, .cancelled => |id| { const proxy = f.at(id); const listener = proxy.listener orelse return; if (!proxy.live) return; const call: Plain = @ptrCast(listener[1]); call(proxy.data, ptr(id)); }, } } fn deinit(f: *Compositor) void { for (f.clients.items, 0..) |*client, index| { f.closeEvents(@intCast(index)); client.events.deinit(f.gpa); } f.proxies.deinit(f.gpa); f.clients.deinit(f.gpa); f.history[0].deinit(f.gpa); f.history[1].deinit(f.gpa); f.landings.deinit(f.gpa); } }; var fake: ?*Compositor = null; var harness: ?*Harness = null; fn ptr(id: u32) *WlProxy { return @ptrFromInt(id); } fn idOf(pointer: *const anyopaque) u32 { return @intCast(@intFromPtr(pointer)); } fn fakeMarshal(target: *WlProxy, opcode: u32, args: [*]WlArgument, interface: ?*const WlInterface, _: u32) callconv(.c) ?*WlProxy { const f = fake.?; const id = idOf(target); const proxy = f.touch(id) orelse return null; const kind = proxy.kind; const client = proxy.client; const broken = f.clients.items[client].broken; switch (kind) { .display => if (opcode == 0) { const callback = f.create(.callback, client); if (!broken) f.queue(client, .{ .done = callback }); return ptr(callback); }, .registry => if (opcode == 0) { const name = std.mem.span(interface.?.name); return ptr(f.create(if (std.mem.endsWith(u8, name, "manager_v1")) .manager else .seat, client)); }, .manager => switch (opcode) { 0 => { const source = f.create(.source, client); f.at(source).label = f.current; return ptr(source); }, 1 => { const device = f.create(.device, client); f.clients.items[client].device = device; if (!broken) { f.announce(client, 0); f.announce(client, 1); } return ptr(device); }, else => {}, }, .device => if ((opcode == 0 or opcode == 2) and !broken) { const sel: u1 = if (opcode == 2) 1 else 0; const ctx = f.clients.items[client].ctx; if (f.closing[ctx]) f.flag("closed-session"); const source: u32 = if (args[0].o) |object| idOf(object) else 0; f.landings.append(f.gpa, .{ .sel = sel, .label = f.current, .source = source, .ctx = ctx }) catch @panic("fake compositor out of memory"); f.setSelection(sel, source, true); }, .source => switch (opcode) { 0 => if (proxy.mime_count < proxy.mimes.len) { proxy.mimes[proxy.mime_count] = parseMime(args[0].s); proxy.mime_count += 1; }, 1 => if (!broken) f.dropSource(id), else => {}, }, .offer => if (opcode == 0 and !broken) f.receive(id, parseMime(args[0].s), args[1].h), else => {}, } return null; } fn fakeAddListener(target: *WlProxy, listener: [*]const *const anyopaque, data: ?*anyopaque) callconv(.c) c_int { const proxy = fake.?.touch(idOf(target)) orelse return -1; proxy.listener = listener; proxy.data = data; return 0; } fn fakeDestroy(target: *WlProxy) callconv(.c) void { const proxy = fake.?.touch(idOf(target)) orelse return; proxy.live = false; } fn fakeVersion(target: *WlProxy) callconv(.c) u32 { _ = fake.?.touch(idOf(target)); return 1; } fn displayClient(display: *WlDisplay) ?u8 { const proxy = fake.?.touch(idOf(display)) orelse return null; return proxy.client; } fn fakeDispatch(display: *WlDisplay) callconv(.c) c_int { const f = fake.?; const client = displayClient(display) orelse return -1; if (f.clients.items[client].broken) return -1; if (f.held[f.clients.items[client].ctx]) return 0; // Deliver what is queued now; events the handlers cause wait for the next dispatch. A lagged // connection keeps its cancelled events. const lagged = f.lagged[f.clients.items[client].ctx]; const count = f.clients.items[client].events.items.len; var kept: usize = 0; for (0..count) |index| { const event = f.clients.items[client].events.items[index]; if (lagged and event == .cancelled) { f.clients.items[client].events.items[kept] = event; kept += 1; } else f.deliver(client, event); } const events = &f.clients.items[client].events; const rest = events.items.len - count; std.mem.copyForwards(Event, events.items[kept..][0..rest], events.items[count..]); events.shrinkRetainingCapacity(kept + rest); return @intCast(count - kept); } fn fakeError(display: *WlDisplay) callconv(.c) c_int { const client = displayClient(display) orelse return 1; return if (fake.?.clients.items[client].broken) 32 else 0; } fn fakeFlush(display: *WlDisplay) callconv(.c) c_int { const client = displayClient(display) orelse return -1; if (!fake.?.clients.items[client].broken) return 0; __errno_location().* = @intCast(@backingInt(std.posix.E.PIPE)); return -1; } fn fakeDisconnect(display: *WlDisplay) callconv(.c) void { const f = fake.?; const client = displayClient(display) orelse return; f.closeEvents(client); f.clients.items[client].connected = false; f.at(idOf(display)).live = false; } fn fakePrepareRead(display: *WlDisplay) callconv(.c) c_int { _ = displayClient(display); return -1; } fn fakeUnused(_: *WlDisplay) callconv(.c) c_int { fake.?.flag("closed-connection"); return -1; } fn fakeCancelRead(_: *WlDisplay) callconv(.c) void { fake.?.flag("closed-connection"); } fn fakeConnect(_: ?[*:0]const u8) callconv(.c) ?*WlDisplay { fake.?.flag("closed-connection"); return null; } fn fakeConnectFd(_: c_int) callconv(.c) ?*WlDisplay { fake.?.flag("closed-connection"); return null; } fn interfaceNamed(comptime name: [:0]const u8) WlInterface { return .{ .name = name, .version = 1, .method_count = 0, .methods = null, .event_count = 0, .events = null }; } const interfaces = struct { const registry = interfaceNamed("wl_registry"); const callback = interfaceNamed("wl_callback"); const seat = interfaceNamed("wl_seat"); const compositor = interfaceNamed("wl_compositor"); const surface = interfaceNamed("wl_surface"); const shm = interfaceNamed("wl_shm"); const shm_pool = interfaceNamed("wl_shm_pool"); const buffer = interfaceNamed("wl_buffer"); const shell = interfaceNamed("wl_shell"); const shell_surface = interfaceNamed("wl_shell_surface"); const keyboard = interfaceNamed("wl_keyboard"); const data_device_manager = interfaceNamed("wl_data_device_manager"); const data_device = interfaceNamed("wl_data_device"); }; const symbols: linux.WaylandSymbols = .{ .wl_display_connect = fakeConnect, .wl_display_connect_to_fd = fakeConnectFd, .wl_display_disconnect = fakeDisconnect, .wl_display_get_fd = fakeUnused, .wl_display_dispatch_pending = fakeDispatch, .wl_display_dispatch_pending_single = null, .wl_display_get_error = fakeError, .wl_display_set_max_buffer_size = null, .wl_display_flush = fakeFlush, .wl_display_prepare_read = fakePrepareRead, .wl_display_read_events = fakeUnused, .wl_display_cancel_read = fakeCancelRead, .wl_proxy_marshal_array_constructor_versioned = fakeMarshal, .wl_proxy_add_listener = fakeAddListener, .wl_proxy_destroy = fakeDestroy, .wl_proxy_get_version = fakeVersion, .wl_registry_interface = &interfaces.registry, .wl_callback_interface = &interfaces.callback, .wl_seat_interface = &interfaces.seat, .wl_compositor_interface = &interfaces.compositor, .wl_surface_interface = &interfaces.surface, .wl_shm_interface = &interfaces.shm, .wl_shm_pool_interface = &interfaces.shm_pool, .wl_buffer_interface = &interfaces.buffer, .wl_shell_interface = &interfaces.shell, .wl_shell_surface_interface = &interfaces.shell_surface, .wl_keyboard_interface = &interfaces.keyboard, .wl_data_device_manager_interface = &interfaces.data_device_manager, .wl_data_device_interface = &interfaces.data_device, }; fn openDescriptors() u32 { var count: u32 = 0; var fd: i32 = 0; while (fd < 4096) : (fd += 1) { if (std.posix.errno(std.posix.system.fcntl(fd, std.posix.F.GETFD, @as(c_int, 0))) == .SUCCESS) count += 1; } return count; } // The model const OpKind = enum { read, write, clear }; /// Read requests: text; PNG then text; PNG only; a type no backend implements. const read_types = [_][]const []const u8{ &.{"text/plain"}, &.{ "image/png", "text/plain" }, &.{"image/png"}, &.{"application/x-model"}, }; const Op = struct { ctx: u1, kind: OpKind, sel: u1, generation: u32, handle: c.ot_handle = undefined, seq: u32, types: u2 = 0, zero: bool = false, /// The history index of the selection when the request started. admit: usize = 0, settled: ?Status = null, cancel: bool = false, closing: bool = false, destroyed: bool = false, /// Its service was destroyed. gone: bool = false, landed: bool = false, /// The compositor dropped its connection while it was pending. trouble: bool = false, fn hostTerminal(op: Op) bool { return op.settled != null or op.cancel or op.zero or op.closing; } }; const Pick = struct { pick: u8 }; const Side = struct { ctx: u1 }; const ModelCommand = union(enum) { write: struct { ctx: u1, sel: u1, zero: bool }, clear: struct { ctx: u1, sel: u1 }, read: struct { ctx: u1, sel: u1, types: u2 }, poll: Pick, cancel: Pick, destroy: Pick, /// Poll a request through the other Context. cross: Pick, drain: Side, /// Another application takes the selection. copy: struct { sel: u1, image: bool }, /// Another application pastes the selection. paste: struct { sel: u1 }, /// The compositor stops delivering events to a Context's connection, or delivers all but /// the cancelled events of its replaced sources; release resumes both. hold: Side, lag: Side, release: Side, /// The compositor drops a Context's connection. disconnect: Side, /// Begin a service's shutdown. close: Side, /// Finish the shutdown, destroy the service, and create the Context's next one. finish: Side, }; const Harness = struct { pub const Command = ModelCommand; arena: std.mem.Allocator = undefined, f: *Compositor = undefined, contexts: [2]*abi.ContextHandle = undefined, generation: [2]u32 = .{ 0, 0 }, closing: [2]bool = .{ false, false }, ops: std.ArrayListUnmanaged(Op) = .empty, externals: std.ArrayListUnmanaged([]const u8) = .empty, images: std.ArrayListUnmanaged(bool) = .empty, expected: [2]u32 = .{ 0, 0 }, landings_seen: usize = 0, settled_now: ?struct { id: u32, status: Status } = null, step: usize = 0, /// MODEL_SEED prints why a check failed. verbose: bool = false, /// Whether the known failures pass (see the gated rows below). gate: bool = true, /// Each service's provider transfer limit; four lets concurrent reads reach it. transfers: u32 = 4, fn fail(h: *Harness, property: []const u8, comptime why: []const u8) ?model.Failure { if (h.verbose) std.debug.print("step {d}: {s}: " ++ why ++ "\n", .{ h.step, property }); return model.fail(property, h.step); } fn bytes(h: *Harness, label: u32) []const u8 { if (label & external_label != 0) return h.externals.items[label & ~external_label]; return std.fmt.allocPrint(h.arena, "w{d}", .{label}) catch @panic("model out of memory"); } fn isImage(h: *Harness, label: u32) bool { return label & external_label != 0 and h.images.items[label & ~external_label]; } fn opOf(h: *Harness, label: u32) ?*Op { if (label == 0 or label & external_label != 0) return null; return &h.ops.items[label - 1]; } fn serviceOf(h: *Harness, ctx: u1) ?*host.Service { const table = &h.contexts[ctx].core.objects; var cursor: usize = 0; const handle = table.next(.clipboard_service, &cursor) orelse return null; return table.get(handle, .clipboard_service, host.Service) catch null; } fn createService(h: *Harness, ctx: u1) !void { try std.testing.expectEqual(@as(i32, 0), clip.ot_clipboard_service_create(h.contexts[ctx], operations_max, h.transfers, null, 0)); h.serviceOf(ctx).?.libraries = .{ .wayland = true }; } /// The compositor accepts a connection where the service would open its own. fn install(h: *Harness, ctx: u1) !void { if (h.closing[ctx]) return; const service = h.serviceOf(ctx) orelse return; if (service.wayland != null) return; const ids = h.f.connect(ctx); const connection = try service.allocator.create(wayland.Connection); connection.* = wayland.Connection.init(service.allocator, &symbols, "", "", service.max_provider_transfers); connection.display = @ptrCast(ptr(ids.display)); connection.registry = ptr(ids.registry); connection.phase = .registry; connection.sync_done = true; connection.ext_global = 1; connection.seats[0] = .{ .global_name = 2, .proxy = ptr(ids.seat) }; connection.seat_count = 1; service.wayland = connection; model.count("connection"); } fn handleOf(h: *Harness, pick: u8) ?usize { if (h.ops.items.len == 0) return null; return pick % h.ops.items.len; } fn liveOps(h: *Harness, ctx: u1) u32 { var count: u32 = 0; for (h.ops.items) |op| { if (op.ctx == ctx and !op.destroyed and !op.gone) count += 1; } return count; } fn start(h: *Harness, ctx: u1, kind: OpKind, sel: u1, types: u2, zero: bool) ?model.Failure { const id: u32 = @intCast(h.ops.items.len + 1); var op: Op = .{ .ctx = ctx, .kind = kind, .sel = sel, .generation = h.generation[ctx], .seq = id, .types = types, .zero = zero }; op.admit = h.f.history[sel].items.len - 1; op.trouble = h.f.dropped(ctx); const timeout: u32 = if (zero) 0 else timeout_ms; const context = h.contexts[ctx]; const status = switch (kind) { .write => blk: { const text = h.bytes(id); break :blk clip.ot_clipboard_write_operation_start(context, text.ptr, @intCast(text.len), sel, timeout, &op.handle); }, .clear => clip.ot_clipboard_clear_operation_start(context, sel, timeout, &op.handle), .read => blk: { var request: std.ArrayListUnmanaged(u8) = .empty; const names = read_types[types]; request.appendSlice(h.arena, &std.mem.toBytes(std.mem.nativeToLittle(u32, @intCast(names.len)))) catch @panic("model out of memory"); for (names) |name| { request.appendSlice(h.arena, &std.mem.toBytes(std.mem.nativeToLittle(u32, @intCast(name.len)))) catch @panic("model out of memory"); request.appendSlice(h.arena, name) catch @panic("model out of memory"); } break :blk clip.ot_clipboard_read_operation_start(context, request.items.ptr, @intCast(request.items.len), sel, 4096, 1 << 20, 1 << 20, timeout, &op.handle); }, }; const want: host.StartStatus = if (h.closing[ctx]) .shutting_down else if (h.liveOps(ctx) >= operations_max) .limit_exceeded else .ok; if (status != @backingInt(want)) return h.fail("closed-session", "start status"); if (want != .ok) { model.count("start rejected"); return null; } h.ops.append(h.arena, op) catch @panic("model out of memory"); model.count(switch (kind) { .read => "start read", .write => "start write", .clear => "start clear", }); return null; } fn poll(h: *Harness, index: usize, through: u1) ?model.Failure { const op = &h.ops.items[index]; if (through == op.ctx) h.install(op.ctx) catch return h.fail("OutOfMemory", "install"); h.f.current = @intCast(index + 1); const raw = clip.ot_clipboard_operation_poll(h.contexts[through], &op.handle); h.f.current = 0; if (through != op.ctx) { model.count("cross"); return if (raw == @backingInt(Status.invalid_handle)) null else h.fail("own-result", "handle polled through the other Context"); } return h.observe(index, raw); } fn observe(h: *Harness, index: usize, raw: u8) ?model.Failure { const op = &h.ops.items[index]; const status = std.enums.fromInt(Status, raw) orelse return h.fail("own-result", "unknown status"); if (op.destroyed or op.gone) { return if (status == .invalid_handle) null else h.fail("one-completion", "destroyed request is not invalid"); } if (status == .invalid_handle) return h.fail("one-completion", "live request is invalid"); if (status == .pending) { return if (op.settled != null or op.zero) h.fail("one-completion", "settled request went back to pending") else null; } if (op.settled) |settled| { return if (settled == status) null else h.fail("one-completion", "settled status changed"); } op.settled = status; model.count(@tagName(status)); if (op.cancel or op.closing) { return if (status == .cancelled) null else h.fail("one-completion", "cancelled request settled otherwise"); } const fits = switch (status) { .written => op.kind == .write, .cleared => op.kind == .clear, .read, .empty => op.kind == .read and op.types != 3, .unsupported => op.kind == .read and op.types == 3, .timed_out => op.zero, .failed => op.trouble, else => false, }; if (!fits and !h.known(op, status)) return h.fail("own-result", "status does not fit the request"); if (status == .written or status == .cleared) h.settled_now = .{ .id = @intCast(index + 1), .status = status }; return h.checkResult(op, status); } /// Known defects the walks gate (the fixed rows below fail without the gate): a write fails /// with the provider error while a superseded provider of its selection still holds a slot, /// because the compositor has not yet cancelled it or it was asked for a transfer; and a read /// of a service's own selection settles as zero-byte text when the provider is at its /// transfer limit. fn known(h: *Harness, op: *Op, status: Status) bool { if (!h.gate or status != .failed or op.kind != .write) return false; var code: u32 = 0; if (clip.ot_clipboard_operation_result_error_code(h.contexts[op.ctx], &op.handle, &code) != @backingInt(host.CopyStatus.ok)) return false; if (code != wayland_provider_error) return false; for (h.f.proxies.items, 0..) |proxy, index| { const id = proxy_base + @as(u32, @intCast(index)); const writer = h.opOf(proxy.label) orelse continue; if (proxy.kind != .source or !proxy.live or writer.ctx != op.ctx or writer.sel != op.sel or h.f.selection[op.sel] == id) continue; const client = h.f.clients.items[proxy.client]; const uncancelled = for (client.events.items) |event| { if (event == .cancelled and event.cancelled == id) break true; } else false; if (!client.connected or !(uncancelled or proxy.served)) continue; model.count("gated provider limit"); return true; } return false; } fn copyResult(h: *Harness, op: *Op, comptime which: enum { mime, data }) ?[]u8 { const context = h.contexts[op.ctx]; var length: u32 = 0; const length_status = switch (which) { .mime => clip.ot_clipboard_operation_result_mime_length(context, &op.handle, &length), .data => clip.ot_clipboard_operation_result_data_length(context, &op.handle, &length), }; if (length_status != @backingInt(host.CopyStatus.ok)) return null; const buffer = h.arena.alloc(u8, length) catch @panic("model out of memory"); const copy_status = switch (which) { .mime => clip.ot_clipboard_operation_result_mime_copy(context, &op.handle, buffer.ptr, length), .data => clip.ot_clipboard_operation_result_data_copy(context, &op.handle, buffer.ptr, length), }; return if (copy_status == @backingInt(host.CopyStatus.ok)) buffer else null; } /// A read's value must be one its selection held between its start and now. fn checkResult(h: *Harness, op: *Op, status: Status) ?model.Failure { if (status != .read) { var length: u32 = 0; const data = clip.ot_clipboard_operation_result_data_length(h.contexts[op.ctx], &op.handle, &length); if (data != @backingInt(host.CopyStatus.invalid_state)) return h.fail("own-result", "non-read result has data"); } if (op.kind != .read or (status != .read and status != .empty)) return null; const window = h.f.history[op.sel].items[op.admit..]; const wants_png = op.types == 1 or op.types == 2; const wants_text = op.types != 2; if (status == .empty) { for (window) |label| { const image = label != 0 and h.isImage(label); if (label == 0 or (image and !wants_png) or (!image and !wants_text)) return null; } return h.fail("own-result", "empty with no empty value in the window"); } const mime = h.copyResult(op, .mime) orelse return h.fail("own-result", "mime copy"); const data = h.copyResult(op, .data) orelse return h.fail("own-result", "data copy"); const png = std.mem.eql(u8, mime, "image/png"); if (png) model.count("read image"); if (!png and !std.mem.eql(u8, mime, "text/plain")) return h.fail("own-result", "mime"); if ((png and !wants_png) or (!png and !wants_text)) return h.fail("own-result", "mime not requested"); for (window) |label| { if (label == 0) { // A source that vanished mid-transfer reads as zero-byte text. if (!png and data.len == 0) return null; continue; } if (h.isImage(label) == png and std.mem.eql(u8, h.bytes(label), data)) return null; } if (h.gate and !png and data.len == 0) { for (window) |label| if (h.opOf(label) != null) { model.count("gated transfer limit"); return null; }; } return h.fail("own-result", "read value not in the window"); } fn cancel(h: *Harness, index: usize) ?model.Failure { const op = &h.ops.items[index]; const raw = clip.ot_clipboard_operation_cancel(h.contexts[op.ctx], &op.handle); const want: host.CancelStatus = if (op.destroyed or op.gone) .invalid_handle else if (op.hostTerminal()) .already_terminal else .requested; if (raw != @backingInt(want)) return h.fail("one-completion", "cancel status"); if (want == .requested) { op.cancel = true; model.count("cancel requested"); } return null; } fn destroy(h: *Harness, index: usize) ?model.Failure { const op = &h.ops.items[index]; const raw = clip.ot_clipboard_operation_destroy(h.contexts[op.ctx], &op.handle); const want: host.DestroyStatus = if (op.destroyed or op.gone) .invalid_handle else if (op.hostTerminal()) .destroyed else .not_ready; if (raw != @backingInt(want)) return h.fail("one-completion", "destroy status"); if (want == .destroyed) { op.destroyed = true; model.count("destroyed"); } return null; } fn markTrouble(h: *Harness, ctx: u1) void { for (h.ops.items) |*op| { if (op.ctx == ctx and op.settled == null) op.trouble = true; } } /// Selections the Context's service published end with its connection or service. fn releaseExpected(h: *Harness, ctx: u1) void { for (&h.expected) |*label| { if (h.opOf(label.*)) |op| { if (op.ctx == ctx) label.* = 0; } } } fn close(h: *Harness, ctx: u1) ?model.Failure { const raw = clip.ot_clipboard_service_begin_shutdown(h.contexts[ctx]); if (raw != @backingInt(host.ShutdownStatus.pending)) return h.fail("closed-session", "begin shutdown"); h.closing[ctx] = true; h.f.closing[ctx] = true; for (h.ops.items) |*op| { if (op.ctx == ctx and op.generation == h.generation[ctx] and !op.hostTerminal()) op.closing = true; } h.releaseExpected(ctx); return null; } fn finish(h: *Harness, ctx: u1, reopen: bool) !?model.Failure { const context = h.contexts[ctx]; const ready = clip.ot_clipboard_service_poll_shutdown(context); const destroyed = clip.ot_clipboard_service_destroy(context); if (!h.closing[ctx]) { const pending = ready == @backingInt(host.ShutdownStatus.pending) and destroyed == @backingInt(host.DestroyStatus.not_ready); return if (pending) null else h.fail("closed-session", "shutdown before close"); } if (ready != @backingInt(host.ShutdownStatus.ready) or destroyed != @backingInt(host.DestroyStatus.destroyed)) { return h.fail("closed-session", "shutdown not ready"); } for (h.ops.items) |*op| { if (op.ctx == ctx and op.generation == h.generation[ctx]) op.gone = true; } h.generation[ctx] += 1; h.closing[ctx] = false; h.f.closing[ctx] = false; if (reopen) try h.createService(ctx); model.count("service replaced"); return null; } fn paste(h: *Harness, sel: u1) ?model.Failure { const f = h.f; const source = f.selection[sel]; if (source == 0 or f.at(source).client == external) return null; const owner = f.at(source).client; const ctx = f.clients.items[owner].ctx; if (f.held[ctx] or h.closing[ctx]) return null; const pipe = posix_io.pipe(.{ .CLOEXEC = true }) catch return h.fail("no-leak", "pipe"); defer posix_io.close(pipe[0]); const flags = posix_io.getFlags(pipe[0]) catch return h.fail("no-leak", "pipe"); posix_io.setFlags(pipe[0], flags | @as(u32, @bitCast(std.posix.O{ .NONBLOCK = true }))) catch return h.fail("no-leak", "pipe"); f.queue(owner, .{ .send = .{ .source = source, .mime = .text_utf8, .fd = pipe[1] } }); var received: std.ArrayListUnmanaged(u8) = .empty; var ended = false; var turns: u32 = 0; while (!ended and turns < 16) : (turns += 1) { _ = clip.ot_clipboard_service_drain(h.contexts[ctx]); while (true) { var buffer: [256]u8 = undefined; const count = std.posix.read(pipe[0], &buffer) catch break; if (count == 0) { ended = true; break; } received.appendSlice(h.arena, buffer[0..count]) catch @panic("model out of memory"); } } model.count("paste"); if (!ended or !std.mem.eql(u8, received.items, h.bytes(f.labelOf(source)))) return h.fail("newest-wins", "paste"); return null; } fn apply(h: *Harness, command: Command) !?model.Failure { switch (command) { .write => |w| return h.start(w.ctx, .write, w.sel, 0, w.zero), .clear => |w| return h.start(w.ctx, .clear, w.sel, 0, false), .read => |r| return h.start(r.ctx, .read, r.sel, r.types, false), .poll => |p| { const index = h.handleOf(p.pick) orelse return null; model.count("poll"); return h.poll(index, h.ops.items[index].ctx); }, .cross => |p| { const index = h.handleOf(p.pick) orelse return null; return h.poll(index, ~h.ops.items[index].ctx); }, .cancel => |p| return h.cancel(h.handleOf(p.pick) orelse return null), .destroy => |p| return h.destroy(h.handleOf(p.pick) orelse return null), .drain => |d| { _ = clip.ot_clipboard_service_drain(h.contexts[d.ctx]); model.count("drain"); }, .copy => |copy| { const label = external_label | @as(u32, @intCast(h.externals.items.len)); const text = try std.fmt.allocPrint(h.arena, "{s}{d}", .{ if (copy.image) "\x89PNG" else "x", h.externals.items.len }); try h.externals.append(h.arena, text); try h.images.append(h.arena, copy.image); const source = h.f.create(.source, external); h.f.at(source).label = label; h.f.at(source).mimes[0] = if (copy.image) .png else .text_utf8; h.f.at(source).mime_count = 1; h.f.setSelection(copy.sel, source, true); h.expected[copy.sel] = label; model.count("copy"); }, .paste => |p| return h.paste(p.sel), .hold => |d| { h.f.held[d.ctx] = true; model.count("hold"); }, .lag => |d| { h.f.lagged[d.ctx] = true; model.count("lag"); }, .release => |d| { h.f.held[d.ctx] = false; h.f.lagged[d.ctx] = false; model.count("release"); }, .disconnect => |d| { if (!h.f.drop(d.ctx)) return null; h.markTrouble(d.ctx); h.releaseExpected(d.ctx); model.count("disconnect"); }, .close => |d| { model.count("close"); return h.close(d.ctx); }, .finish => |d| return h.finish(d.ctx, true), } return null; } /// The landings of the last call, and the selection against the newest settled effect. fn check(h: *Harness) ?model.Failure { if (h.f.violation) |property| return h.fail(property, "fake"); var matched = false; const settled = h.settled_now; h.settled_now = null; for (h.f.landings.items[h.landings_seen..]) |landing| { const now = settled orelse return h.fail("newest-wins", "landed outside a settling poll"); const op = h.opOf(now.id).?; if (landing.label != now.id or landing.ctx != op.ctx or landing.sel != op.sel) return h.fail("newest-wins", "landed for another request"); const published = landing.source != 0 and h.f.labelOf(landing.source) == now.id; if ((now.status == .written) != published or (now.status == .cleared) != (landing.source == 0)) return h.fail("newest-wins", "landing does not match status"); // A stale write: an older mutation lands after a newer one of its service and selection. for (h.ops.items) |other| { if (other.landed and other.ctx == op.ctx and other.generation == op.generation and other.sel == op.sel and other.seq > op.seq) { return h.fail("newest-wins", "older mutation landed after a newer one"); } } if (matched) return h.fail("newest-wins", "two landings"); matched = true; op.landed = true; model.count("landed"); } h.landings_seen = h.f.landings.items.len; if (settled) |now| { if (!matched) return h.fail("newest-wins", "settled without landing"); h.expected[h.opOf(now.id).?.sel] = if (now.status == .written) now.id else 0; } for (0..2) |sel| { if (h.f.labelOf(h.f.selection[sel]) != h.expected[sel]) return h.fail("newest-wins", "selection differs from the newest settled effect"); } return null; } fn settle(h: *Harness) !?model.Failure { h.f.held = .{ false, false }; h.f.lagged = .{ false, false }; // A closing service dispatches nothing until it is destroyed, so a transfer the // compositor asked it for waits on it; a host finishes the shutdown first. for (0..2) |ctx| { if (!h.closing[ctx]) continue; if (try h.finish(@intCast(ctx), true)) |failure| return failure; if (h.check()) |failure| return failure; } var rounds: u32 = 0; while (rounds < 64) : (rounds += 1) { var pending = false; for (0..2) |ctx| _ = clip.ot_clipboard_service_drain(h.contexts[ctx]); for (h.ops.items, 0..) |op, index| { if (op.settled != null or op.destroyed or op.gone) continue; if (h.poll(index, op.ctx)) |failure| return failure; if (h.check()) |failure| return failure; if (h.ops.items[index].settled == null) pending = true; } if (!pending) break; } else return h.fail("settles", "requests still pending"); for (0..2) |sel| { if (h.paste(@intCast(sel))) |failure| return failure; if (h.check()) |failure| return failure; } for (0..2) |ctx| { if (!h.closing[ctx]) { if (h.close(@intCast(ctx))) |failure| return failure; } if (h.check()) |failure| return failure; if (try h.finish(@intCast(ctx), false)) |failure| return failure; if (h.check()) |failure| return failure; } for (h.ops.items) |*op| { const raw = clip.ot_clipboard_operation_poll(h.contexts[op.ctx], &op.handle); if (raw != @backingInt(Status.invalid_handle)) return h.fail("closed-session", "request of a destroyed service is live"); } for (h.f.clients.items) |client| { if (client.connected) return h.fail("no-leak", "connection never disconnected"); } for (h.f.proxies.items) |proxy| { if (proxy.client != external and proxy.kind != .display and proxy.live and proxy.announced) return h.fail("no-leak", "proxy never destroyed"); } return null; } // model.check's interface pub fn generate(_: *Harness, arena: std.mem.Allocator, random: std.Random, steps: usize) ![]Command { const commands = try arena.alloc(Command, steps); const weights = [_]u8{ 12, 4, 9, 28, 4, 5, 2, 8, 4, 2, 3, 3, 3, 2, 2, 2 }; comptime std.debug.assert(weights.len == @typeInfo(Command).@"union".field_names.len); for (commands) |*command| { const ctx = random.int(u1); const sel = random.int(u1); const pick = random.int(u8); command.* = switch (random.weightedIndex(u8, &weights)) { 0 => .{ .write = .{ .ctx = ctx, .sel = sel, .zero = random.uintLessThan(u8, 8) == 0 } }, 1 => .{ .clear = .{ .ctx = ctx, .sel = sel } }, 2 => .{ .read = .{ .ctx = ctx, .sel = sel, .types = random.int(u2) } }, 3 => .{ .poll = .{ .pick = pick } }, 4 => .{ .cancel = .{ .pick = pick } }, 5 => .{ .destroy = .{ .pick = pick } }, 6 => .{ .cross = .{ .pick = pick } }, 7 => .{ .drain = .{ .ctx = ctx } }, 8 => .{ .copy = .{ .sel = sel, .image = random.boolean() } }, 9 => .{ .paste = .{ .sel = sel } }, 10 => .{ .hold = .{ .ctx = ctx } }, 11 => .{ .lag = .{ .ctx = ctx } }, 12 => .{ .release = .{ .ctx = ctx } }, 13 => .{ .disconnect = .{ .ctx = ctx } }, 14 => .{ .close = .{ .ctx = ctx } }, else => .{ .finish = .{ .ctx = ctx } }, }; } return commands; } pub fn run(h: *Harness, arena: std.mem.Allocator, commands: []const Command) !?model.Failure { var compositor: Compositor = .{ .gpa = std.testing.allocator, .owner = std.Thread.getCurrentId() }; defer compositor.deinit(); const gate = h.gate; const transfers = h.transfers; h.* = .{ .arena = arena, .f = &compositor, .verbose = model.environment(arena, "MODEL_SEED") != null, .gate = gate, .transfers = transfers }; fake = &compositor; harness = h; defer fake = null; defer harness = null; // A service that tried to connect by itself would find no library instead of a display. const previous = linux.testSwapWaylandSymbols(null); defer _ = linux.testSwapWaylandSymbols(previous); try compositor.history[0].append(compositor.gpa, 0); try compositor.history[1].append(compositor.gpa, 0); const descriptors = openDescriptors(); var created: usize = 0; defer for (h.contexts[0..created]) |context| { _ = abi.ot_context_destroy(context); }; for (&h.contexts) |*context| { context.* = try abi.createTestContext(.{ .object_capacity = 64, .render_cells_max = 4 }); created += 1; } try h.createService(0); try h.createService(1); for (commands, 0..) |command, step| { h.step = step; if (try h.apply(command)) |failure| return failure; if (h.check()) |failure| return failure; } h.step = commands.len; if (try h.settle()) |failure| return failure; created = 0; for (h.contexts) |context| { if (abi.ot_context_destroy(context) != c.OT_OK) return h.fail("no-leak", "context destroy"); } if (compositor.violation) |property| return h.fail(property, "fake"); if (openDescriptors() != descriptors) return h.fail("no-leak", "descriptors"); return null; } pub fn simplify(_: *Harness, arena: std.mem.Allocator, command: Command) ![]const Command { const options: []const Command = switch (command) { .write => |w| &.{ .{ .write = .{ .ctx = 0, .sel = w.sel, .zero = w.zero } }, .{ .write = .{ .ctx = w.ctx, .sel = 0, .zero = w.zero } }, .{ .write = .{ .ctx = w.ctx, .sel = w.sel, .zero = false } } }, .clear => |w| &.{ .{ .clear = .{ .ctx = 0, .sel = w.sel } }, .{ .clear = .{ .ctx = w.ctx, .sel = 0 } } }, .read => |r| &.{ .{ .read = .{ .ctx = 0, .sel = r.sel, .types = r.types } }, .{ .read = .{ .ctx = r.ctx, .sel = 0, .types = r.types } }, .{ .read = .{ .ctx = r.ctx, .sel = r.sel, .types = 0 } } }, .poll => &.{.{ .poll = .{ .pick = 0 } }}, .cancel => &.{.{ .cancel = .{ .pick = 0 } }}, .destroy => &.{.{ .destroy = .{ .pick = 0 } }}, .cross => &.{.{ .cross = .{ .pick = 0 } }}, .copy => |copy| &.{ .{ .copy = .{ .sel = 0, .image = copy.image } }, .{ .copy = .{ .sel = copy.sel, .image = false } } }, else => &.{}, }; return arena.dupe(Command, options); } pub fn format(_: *Harness, command: Command, writer: *std.Io.Writer) void { const written = switch (command) { .write => |w| writer.print("write ctx={d} sel={d}{s}", .{ w.ctx, w.sel, if (w.zero) " timeout=0" else "" }), .clear => |w| writer.print("clear ctx={d} sel={d}", .{ w.ctx, w.sel }), .read => |r| writer.print("read ctx={d} sel={d} types={d}", .{ r.ctx, r.sel, r.types }), .poll, .cancel, .destroy, .cross => |p| writer.print("{t} pick={d}", .{ command, p.pick }), .copy => |copy| writer.print("copy sel={d}{s}", .{ copy.sel, if (copy.image) " image" else "" }), .paste => |p| writer.print("paste sel={d}", .{p.sel}), .drain, .hold, .lag, .release, .disconnect, .close, .finish => |d| writer.print("{t} ctx={d}", .{ command, d.ctx }), }; written catch {}; } }; test "Clipboard ABI model: the newest request wins and only the owning thread uses a connection (seeded)" { if (comptime builtin.os.tag != .linux) return error.SkipZigTest; var model_harness: Harness = .{}; // CI runs 64 seeds of 40 commands; MODEL_SEEDS widens it, MODEL_SEED runs one. try model.check(Harness, &model_harness, .{ .seeds = 64, .steps = 40 }); } test "Clipboard ABI model: the newest write fails and a read reads empty text at the provider limits (known, gated)" { // Each row fails its property today. When the defect is fixed, a row passes and this test // fails with a message: delete it and `known`. if (comptime builtin.os.tag != .linux) return error.SkipZigTest; var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); const rows = [_]struct { transfers: u32 = 4, trace: []const ModelCommand }{ // Write 1 is published; the service's own read of it asks write 1's provider for a // transfer; write 3 replaces it; write 4 fails, and the selection keeps write 3. .{ .trace = &.{ .{ .write = .{ .ctx = 0, .sel = 0, .zero = false } }, .{ .poll = .{ .pick = 0 } }, .{ .poll = .{ .pick = 0 } }, .{ .poll = .{ .pick = 0 } }, .{ .read = .{ .ctx = 0, .sel = 0, .types = 0 } }, .{ .poll = .{ .pick = 1 } }, .{ .poll = .{ .pick = 1 } }, .{ .write = .{ .ctx = 0, .sel = 0, .zero = false } }, .{ .poll = .{ .pick = 2 } }, .{ .write = .{ .ctx = 0, .sel = 0, .zero = false } }, .{ .poll = .{ .pick = 3 } }, } }, // Two quick writes after write 1: the compositor has not yet told the service that write 2 // replaced write 1, so write 3, the newest, fails and the selection keeps write 2. .{ .trace = &.{ .{ .write = .{ .ctx = 0, .sel = 0, .zero = false } }, .{ .poll = .{ .pick = 0 } }, .{ .poll = .{ .pick = 0 } }, .{ .poll = .{ .pick = 0 } }, .{ .lag = .{ .ctx = 0 } }, .{ .write = .{ .ctx = 0, .sel = 0, .zero = false } }, .{ .poll = .{ .pick = 1 } }, .{ .write = .{ .ctx = 0, .sel = 0, .zero = false } }, .{ .poll = .{ .pick = 2 } }, } }, // With one transfer per provider, the second of two reads of write 1 is refused its // transfer and settles as a successful read of zero-byte text. .{ .transfers = 1, .trace = &.{ .{ .write = .{ .ctx = 0, .sel = 0, .zero = false } }, .{ .poll = .{ .pick = 0 } }, .{ .poll = .{ .pick = 0 } }, .{ .poll = .{ .pick = 0 } }, .{ .read = .{ .ctx = 0, .sel = 0, .types = 0 } }, .{ .read = .{ .ctx = 0, .sel = 0, .types = 0 } }, .{ .poll = .{ .pick = 1 } }, .{ .poll = .{ .pick = 2 } }, .{ .poll = .{ .pick = 1 } }, .{ .poll = .{ .pick = 2 } }, .{ .poll = .{ .pick = 1 } }, .{ .poll = .{ .pick = 2 } }, } }, }; for (rows) |row| { var row_harness: Harness = .{ .gate = false, .transfers = row.transfers }; const failure = try row_harness.run(arena_state.allocator(), row.trace) orelse { std.debug.print("the defect is fixed: remove the gate and these rows\n", .{}); return error.KnownDefectFixed; }; try std.testing.expectEqualStrings("own-result", failure.property); try std.testing.expectEqual(row.trace.len - 1, failure.step); } }