|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// Copyright (c) 2026 Jonathan D.A. Jewell <j.d.a.jewell@open.ac.uk> |
| 3 | +// |
| 4 | +// The THIRD `typedwasm.ownership` producer — the language-agnosticism |
| 5 | +// proof for typed-wasm as an independent compile target. |
| 6 | +// |
| 7 | +// AffineScript (OCaml) and Ephapax (Rust) emit the carrier from full |
| 8 | +// compilers; the in-tree Rust codegen emits it from parsed `.twasm`. |
| 9 | +// This producer shares NO ancestry with any of them: it hand-assembles |
| 10 | +// a core wasm module byte-by-byte in Zig and attaches the ownership |
| 11 | +// section per the wire format of `crates/typed-wasm-verify/src/section.rs` |
| 12 | +// (u32le count; per entry u32le func_idx, u8 n_params, u8[n] param |
| 13 | +// kinds, u8 ret kind; kinds: 0=Unrestricted, 1=Linear, 2=SharedBorrow, |
| 14 | +// 3=ExclBorrow). If `typed-wasm-verify` accepts these bytes — and |
| 15 | +// rejects the double-consume mutant — the contract is demonstrably |
| 16 | +// producer-neutral, not an artefact of one toolchain's encoder. |
| 17 | +// |
| 18 | +// Fixture capture: `zig build gen-fixtures` writes |
| 19 | +// zig_clean_linear.wasm — consume(x: Linear) uses its param once |
| 20 | +// zig_double_use.wasm — the same function using it twice |
| 21 | +// which are committed under |
| 22 | +// `crates/typed-wasm-verify/tests/fixtures/zig_producer/` and pinned by |
| 23 | +// `tests/third_producer_zig.rs` (accept / reject respectively). |
| 24 | + |
| 25 | +const std = @import("std"); |
| 26 | + |
| 27 | +/// Emit a u32 as unsigned LEB128 (wasm's varuint32). |
| 28 | +fn leb128(list: *std.ArrayList(u8), gpa: std.mem.Allocator, value: u32) !void { |
| 29 | + var v = value; |
| 30 | + while (true) { |
| 31 | + const byte: u8 = @intCast(v & 0x7F); |
| 32 | + v >>= 7; |
| 33 | + if (v == 0) { |
| 34 | + try list.append(gpa, byte); |
| 35 | + return; |
| 36 | + } |
| 37 | + try list.append(gpa, byte | 0x80); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +/// Append one section: id byte, LEB128 payload size, payload. |
| 42 | +fn section(out: *std.ArrayList(u8), gpa: std.mem.Allocator, id: u8, payload: []const u8) !void { |
| 43 | + try out.append(gpa, id); |
| 44 | + try leb128(out, gpa, @intCast(payload.len)); |
| 45 | + try out.appendSlice(gpa, payload); |
| 46 | +} |
| 47 | + |
| 48 | +/// The `typedwasm.ownership` payload for one function whose single |
| 49 | +/// param is Linear (kind 1) and whose return is Unrestricted (kind 0). |
| 50 | +/// Fixed-width little-endian per the typed-wasm carrier spec — NOT |
| 51 | +/// LEB128; this is the cross-implementation parity surface. |
| 52 | +fn ownershipPayload(out: *std.ArrayList(u8), gpa: std.mem.Allocator) !void { |
| 53 | + try out.appendSlice(gpa, &std.mem.toBytes(std.mem.nativeToLittle(u32, 1))); // count |
| 54 | + try out.appendSlice(gpa, &std.mem.toBytes(std.mem.nativeToLittle(u32, 0))); // func_idx |
| 55 | + try out.append(gpa, 1); // n_params |
| 56 | + try out.append(gpa, 1); // param 0: Linear |
| 57 | + try out.append(gpa, 0); // ret: Unrestricted |
| 58 | +} |
| 59 | + |
| 60 | +/// Build the complete module. `double_use` controls whether the body |
| 61 | +/// consumes its Linear param once (honest) or twice (a wasm-level |
| 62 | +/// double-free the verifier MUST reject). |
| 63 | +pub fn buildModule(gpa: std.mem.Allocator, double_use: bool) ![]u8 { |
| 64 | + var out: std.ArrayList(u8) = .empty; |
| 65 | + errdefer out.deinit(gpa); |
| 66 | + |
| 67 | + // Header: magic + version. |
| 68 | + try out.appendSlice(gpa, &.{ 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00 }); |
| 69 | + |
| 70 | + // Type section: one functype (param i32) -> (). |
| 71 | + var payload: std.ArrayList(u8) = .empty; |
| 72 | + defer payload.deinit(gpa); |
| 73 | + try payload.appendSlice(gpa, &.{ 0x01, 0x60, 0x01, 0x7F, 0x00 }); |
| 74 | + try section(&out, gpa, 1, payload.items); |
| 75 | + |
| 76 | + // Function section: one function of type 0. |
| 77 | + payload.clearRetainingCapacity(); |
| 78 | + try payload.appendSlice(gpa, &.{ 0x01, 0x00 }); |
| 79 | + try section(&out, gpa, 3, payload.items); |
| 80 | + |
| 81 | + // Export section: "consume" -> func 0. |
| 82 | + payload.clearRetainingCapacity(); |
| 83 | + const name = "consume"; |
| 84 | + try payload.append(gpa, 0x01); |
| 85 | + try leb128(&payload, gpa, @intCast(name.len)); |
| 86 | + try payload.appendSlice(gpa, name); |
| 87 | + try payload.appendSlice(gpa, &.{ 0x00, 0x00 }); // kind=func, idx=0 |
| 88 | + try section(&out, gpa, 7, payload.items); |
| 89 | + |
| 90 | + // Code section: one body, no locals; `local.get 0; drop` once or twice. |
| 91 | + payload.clearRetainingCapacity(); |
| 92 | + var body: std.ArrayList(u8) = .empty; |
| 93 | + defer body.deinit(gpa); |
| 94 | + try body.append(gpa, 0x00); // no local declarations |
| 95 | + const uses: usize = if (double_use) 2 else 1; |
| 96 | + for (0..uses) |_| { |
| 97 | + try body.appendSlice(gpa, &.{ 0x20, 0x00, 0x1A }); // local.get 0; drop |
| 98 | + } |
| 99 | + try body.append(gpa, 0x0B); // end |
| 100 | + try payload.append(gpa, 0x01); // one code entry |
| 101 | + try leb128(&payload, gpa, @intCast(body.items.len)); |
| 102 | + try payload.appendSlice(gpa, body.items); |
| 103 | + try section(&out, gpa, 10, payload.items); |
| 104 | + |
| 105 | + // Custom section: typedwasm.ownership. |
| 106 | + payload.clearRetainingCapacity(); |
| 107 | + const section_name = "typedwasm.ownership"; |
| 108 | + try leb128(&payload, gpa, @intCast(section_name.len)); |
| 109 | + try payload.appendSlice(gpa, section_name); |
| 110 | + try ownershipPayload(&payload, gpa); |
| 111 | + try section(&out, gpa, 0, payload.items); |
| 112 | + |
| 113 | + return out.toOwnedSlice(gpa); |
| 114 | +} |
| 115 | + |
| 116 | +/// Fixture generator: `twasm-producer <output-dir>`. |
| 117 | +pub fn main() !void { |
| 118 | + var gpa_state = std.heap.GeneralPurposeAllocator(.{}){}; |
| 119 | + defer _ = gpa_state.deinit(); |
| 120 | + const gpa = gpa_state.allocator(); |
| 121 | + |
| 122 | + var args = try std.process.argsWithAllocator(gpa); |
| 123 | + defer args.deinit(); |
| 124 | + _ = args.next(); // argv[0] |
| 125 | + const dir_path = args.next() orelse { |
| 126 | + std.debug.print("usage: twasm-producer <output-dir>\n", .{}); |
| 127 | + return error.MissingOutputDir; |
| 128 | + }; |
| 129 | + |
| 130 | + var dir = try std.fs.cwd().makeOpenPath(dir_path, .{}); |
| 131 | + defer dir.close(); |
| 132 | + |
| 133 | + inline for (.{ .{ "zig_clean_linear.wasm", false }, .{ "zig_double_use.wasm", true } }) |spec| { |
| 134 | + const bytes = try buildModule(gpa, spec[1]); |
| 135 | + defer gpa.free(bytes); |
| 136 | + try dir.writeFile(.{ .sub_path = spec[0], .data = bytes }); |
| 137 | + std.debug.print("wrote {s}/{s} ({d} bytes)\n", .{ dir_path, spec[0], bytes.len }); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +test "module bytes start with wasm magic + version" { |
| 142 | + const bytes = try buildModule(std.testing.allocator, false); |
| 143 | + defer std.testing.allocator.free(bytes); |
| 144 | + try std.testing.expectEqualSlices( |
| 145 | + u8, |
| 146 | + &.{ 0x00, 0x61, 0x73, 0x6D, 0x01, 0x00, 0x00, 0x00 }, |
| 147 | + bytes[0..8], |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +test "clean and double-use differ only in the code section" { |
| 152 | + const clean = try buildModule(std.testing.allocator, false); |
| 153 | + defer std.testing.allocator.free(clean); |
| 154 | + const double = try buildModule(std.testing.allocator, true); |
| 155 | + defer std.testing.allocator.free(double); |
| 156 | + // One extra `local.get 0; drop` = 3 bytes. |
| 157 | + try std.testing.expectEqual(clean.len + 3, double.len); |
| 158 | +} |
| 159 | + |
| 160 | +test "ownership section is present with the Linear kind byte" { |
| 161 | + const bytes = try buildModule(std.testing.allocator, false); |
| 162 | + defer std.testing.allocator.free(bytes); |
| 163 | + const needle = "typedwasm.ownership"; |
| 164 | + const at = std.mem.indexOf(u8, bytes, needle) orelse return error.SectionMissing; |
| 165 | + // Payload follows the name: count=1 u32le, func_idx=0 u32le, |
| 166 | + // n_params=1, kind=Linear(1), ret=Unrestricted(0). |
| 167 | + const payload = bytes[at + needle.len ..]; |
| 168 | + try std.testing.expectEqualSlices( |
| 169 | + u8, |
| 170 | + &.{ 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0 }, |
| 171 | + payload[0..11], |
| 172 | + ); |
| 173 | +} |
| 174 | + |
| 175 | +test "generator is deterministic" { |
| 176 | + const a = try buildModule(std.testing.allocator, false); |
| 177 | + defer std.testing.allocator.free(a); |
| 178 | + const b = try buildModule(std.testing.allocator, false); |
| 179 | + defer std.testing.allocator.free(b); |
| 180 | + try std.testing.expectEqualSlices(u8, a, b); |
| 181 | +} |
0 commit comments