diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-16 22:57:31 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 88c04dd6c392c6d8fc3a02408506e85d86f476d5 (patch) | |
| tree | 4462fcb19dce110c24595f000bd8303c69288689 /src/abi.zig | |
| parent | 1cb36ee39a8bbc2cf61c28003670d760841d6d3e (diff) | |
| download | sketchbook-88c04dd6c392c6d8fc3a02408506e85d86f476d5.tar.gz sketchbook-88c04dd6c392c6d8fc3a02408506e85d86f476d5.zip | |
Refuse a sketch library built by another compiler, backend or mode
abi.hash now covers the zig version, codegen backend and optimize mode: Zig's
own calling convention (Host.VTable, every Allocator) differs between them,
so a Debug host loading a ReleaseFast or LLVM library faulted on its first
draw. Libraries export sketch_build, and the host's refusal says what
differs and what to run instead.
Diffstat (limited to 'src/abi.zig')
| -rw-r--r-- | src/abi.zig | 17 |
1 files changed, 13 insertions, 4 deletions
diff --git a/src/abi.zig b/src/abi.zig index 7177f14..33f7cd6 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -5,11 +5,13 @@ //! tagged unions, `std.mem.Allocator`. Two rules keep reloads safe: //! - whatever outlives a call lives in host memory (host code is never //! unloaded, so host allocators and function pointers stay valid); -//! - `hash` covers the layout of every type here, and a host refuses a sketch -//! built against different definitions instead of misreading its memory. +//! - `hash` covers the layout of every type here and the compiler, backend +//! and mode, and a host refuses a sketch built against different +//! definitions or by another build instead of misreading its memory. //! //! Only `Event` and the reflected `Layout` are `extern`: they are written to disk. const std = @import("std"); +const builtin = @import("builtin"); pub const layout = @import("layout.zig"); pub const Key = @import("keys.zig").Key; @@ -813,16 +815,23 @@ pub const Sketch = struct { }; }; -/// Layout hash of everything above. Changes whenever a shared type does. +/// Layout hash of everything above, and of `build`. Changes whenever a shared +/// type does, or the compiler, backend or optimize mode: Zig's own calling +/// convention, which `Host.VTable` and every `std.mem.Allocator` use, differs +/// between backends and modes. pub const hash: u64 = blk: { @setEvalBranchQuota(1_000_000); var h: u64 = 0; for (.{ Rect, Color, BlendMode, Corners, Event, Mode, Stream, Input, Image, Audio, Cursor, Voice, Command, gpu.Pass, gpu.Pipeline, gpu.Draw, gpu.Dispatch, gpu.PostProcess, Host, Host.VTable, Context, Field, Layout, Info, Memory, Sketch }) |T| { h = std.hash.Wyhash.hash(h, @typeName(T) ++ layout.signature(T)); } - break :blk h; + break :blk std.hash.Wyhash.hash(h, build); }; +/// The compiler, backend and mode of this build, exported by libraries as +/// `sketch_build` so a host can say why it refuses one. +pub const build = builtin.zig_version_string ++ " " ++ @tagName(builtin.zig_backend) ++ " " ++ @tagName(builtin.mode); + test "vertex attributes and std140 checks come from the types" { const Vertex = extern struct { position: [2]f32, color: [4]u8, uv: [2]f32 }; const attrs = gpu.attributes(Vertex); |
