From 3ad8a8117435124d5df8c1d70de62c32dd97e113 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 28 Oct 2026 21:23:53 -0300 Subject: one copy of each codec: PNG and JPEG through wuffs (ghostty's, already linked by ghostty-vt), stb_image without them, its writer and its inflate, SDL without its private stb_image; a symbol-table guard; docs/shared-libs.md ReleaseFast before, both binaries carried stb_image's PNG, JPEG and inflate beside wuffs's (whose function tables keep its decoders linked whatever pardes calls), stb_image_write and its deflate (unused), and the GUI a second stb_image of SDL's (SDL_stb.c, for SDL_LoadPNG and camera MJPEG). Now: - image.zig decodes PNG and JPEG with wuffs (`wuffs_images`, wherever ghostty-vt is in the build), sized from the IHDR or the first SOF before decoding, as stb's info call sized them; other formats and the builds without ghostty-vt keep stb_image. - build.zig compiles zstbi with -DSTBI_NO_PNG -DSTBI_NO_JPEG and patches its stb_image_write include out (one count-checked anchor; a no-op stbi_flip_vertically_on_write stays for zstbi.deinit), and SDL with SDL_DISABLE_STB. Text: tty -74,488 bytes, gui -154,392. `zig build shared-libs-check` (in unit-test, ELF) reads the linked symbol table and fails on a family the build chose against: stb's PNG/JPEG/inflate beside wuffs, a second stb_image, stb_image_write's deflate, a second zlib or FreeType. Checked against the old binaries: it names every one. docs/shared-libs.md: the inventory (implementation, users, copies and bytes before and after, allocator hook) and where one copy was not possible: zlib and wuffs both inflate (FreeType's and MuPDF's zlib API vs ghostty's kitty o=z), MuPDF's libjpeg beside wuffs (filter-dct.c is libjpeg's API), MuPDF's unreached fitz image loaders, base64 in Zig std/simdutf/MuPDF, per-library Unicode code, std.debug's flate. Co-Authored-By: Claude Opus 5.5 --- build.zig | 54 ++++++++++++++++++ docs/shared-libs.md | 120 +++++++++++++++++++++++++++++++++++++++ src/CHANGELOG.md | 8 +++ src/image.zig | 135 ++++++++++++++++++++++++++++++++++++++++++-- tools/shared_libs_check.zig | 127 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 439 insertions(+), 5 deletions(-) create mode 100644 docs/shared-libs.md create mode 100644 tools/shared_libs_check.zig diff --git a/build.zig b/build.zig index 93114103..2f1c45e1 100644 --- a/build.zig +++ b/build.zig @@ -911,6 +911,34 @@ pub fn build(b: *std.Build) void { // override uucode on vaxis LAST so it wins over ghostty's build() wiring. vaxis_mod.addImport("uucode", uucode_mod); + // One PNG and one JPEG decoder (docs/shared-libs.md): ghostty's, wuffs, + // which ghostty-vt links for kitty graphics whatever pardes does (its + // decoders stay in through wuffs's function tables). Where it is in the + // build, image.zig decodes PNG and JPEG through it, and stb_image is + // compiled without them, so without the inflate only its PNG used. Its + // writer goes too, and the deflate it carries: pardes writes no image + // through stb. A fresh fetch keeps both: the flags and the patch are + // applied here, at build time. + const wuffs_mod: ?*std.Build.Module = if (ghostty_core_vt) |vt| vt.import_table.get("wuffs") else null; + for ([_]?*std.Build.Step.Options{ opts, gui_opts }) |o| if (o) |x| x.addOption(bool, "wuffs_images", wuffs_mod != null); + if (wuffs_mod != null) if (zstbi_dep) |dep| { + var sources: usize = 0; + for (zstbi_mod.link_objects.items) |obj| switch (obj) { + .c_source_file => |csf| { + sources += 1; + const src = configureRead(b, io, dep.builder.root, "src/zstbi.c", 1 << 20) catch @panic("read zstbi.c"); + const writer = "#define STB_IMAGE_WRITE_IMPLEMENTATION\n#include \"stb_image_write.h\"\n"; + if (std.mem.count(u8, src, writer) != 1) std.debug.panic("zstbi.c changed at `{s}`: redo its patch in build.zig", .{writer}); + // zstbi.deinit still resets the writer's flip flag. + const patched = std.mem.replaceOwned(u8, b.allocator, src, writer, "void stbi_flip_vertically_on_write(int flag) { (void)flag; }\n") catch @panic("OOM"); + csf.file = b.addWriteFiles().add("zstbi.c", patched); + csf.flags = std.mem.concat(b.allocator, []const u8, &.{ csf.flags, &.{ "-DSTBI_NO_PNG", "-DSTBI_NO_JPEG" } }) catch @panic("OOM"); + }, + else => {}, + }; + if (sources != 1) @panic("zstbi's build no longer compiles one C file: redo its patch in build.zig"); + }; + // One call per module instead of one line per module per dependency, and // BEFORE the fold below, which reads what these imports put in the table. for (core_mods) |m| wireCore(m, .{ @@ -925,6 +953,7 @@ pub fn build(b: *std.Build) void { .ghostty_vt = ghostty_core_vt, .vaxis = vaxis_mod, .uucode = uucode_mod, + .wuffs = wuffs_mod, }); const iso_off = b.addOptions(); @@ -973,6 +1002,10 @@ pub fn build(b: *std.Build) void { const sdl_dep = b.lazyDependency("sdl", .{ .target = target, .optimize = optimize, .sanitize_c = .off, .preferred_linkage = .static }); if (sdl_dep) |dep| { const sdl_lib = dep.artifact("SDL3"); + // SDL's own stb_image (SDL_stb.c, for SDL_LoadPNG and a camera's + // MJPEG, neither of which pardes calls) is a second PNG and JPEG + // decoder beside the build's: off (docs/shared-libs.md). + sdl_lib.root_module.addCMacro("SDL_DISABLE_STB", "1"); gm.linkLibrary(sdl_lib); gm.addIncludePath(dep.path("include")); // gui.zig's `c`: SDL and the font shim, which @cImport gave it before Zig 0.17. @@ -1682,6 +1715,25 @@ pub fn build(b: *std.Build) void { b.step("history", "compare command output and test/build runtime across jj revisions").dependOn(&run_history.step); const unit_step = b.step("unit-test", "run native shell and module unit tests"); + // One copy of each codec in the binary (docs/shared-libs.md): the + // linked symbol table, read for the families the build chose against. + // ELF only; it runs with every unit-test, like the perf gate. + if (target.result.ofmt == .elf) { + const libs_check = b.addExecutable(.{ + .name = "pardes-shared-libs-check", + .root_module = b.createModule(.{ + .target = b.graph.host, + .optimize = .debug, + .root_source_file = b.path("tools/shared_libs_check.zig"), + .link_libc = true, + }), + }); + const run_libs_check = b.addRunArtifact(libs_check); + run_libs_check.addArtifactArg(exe); + const libs_check_step = b.step("shared-libs-check", "fail when the binary links a second copy of a codec the build chose one of (docs/shared-libs.md)"); + libs_check_step.dependOn(&run_libs_check.step); + unit_step.dependOn(&run_libs_check.step); + } // The perf gate runs with every unit-test (a regression like a // contrast search per grapheme costs 100x, far past the gate). unit_step.dependOn(perf_gate_step); @@ -2055,6 +2107,7 @@ fn wireCore(m: *std.Build.Module, d: struct { ghostty_vt: ?*std.Build.Module = null, vaxis: ?*std.Build.Module = null, uucode: ?*std.Build.Module = null, + wuffs: ?*std.Build.Module = null, }) void { if (d.themes) |x| m.addImport("generated_themes", x); if (d.tree_sitter) |x| m.addImport("tree-sitter", x); @@ -2067,6 +2120,7 @@ fn wireCore(m: *std.Build.Module, d: struct { if (d.ghostty_vt) |x| m.addImport("ghostty-vt", x); if (d.vaxis) |x| m.addImport("vaxis", x); if (d.uucode) |x| m.addImport("uucode", x); + if (d.wuffs) |x| m.addImport("wuffs", x); } /// The CPU feature set for the ESP32-P4 firmware target, as a `Cpu.Feature.Set`. Spelled as a diff --git a/docs/shared-libs.md b/docs/shared-libs.md new file mode 100644 index 00000000..79f3cdb9 --- /dev/null +++ b/docs/shared-libs.md @@ -0,0 +1,120 @@ +# One copy of each library + +The policy (2026-09-24): + +- **One copy per library in a binary.** When a dependency bundles a library + the build also has, a build change makes it use the build's copy. Duplicates + link silently: same symbol names, the linker keeps one of each, and the two + copies were compiled with different options. +- **Every C library allocates through a Zig allocator** where it has a hook, + so tests run its allocations under the Debug allocator. +- **Patches to fetched packages are applied in build.zig at build time**, each + behind a count-checked anchor (as for vaxis and mvzr), because a fresh fetch + would clobber a patch made in zig-pkg. +- **This file lists what is shared, and where the policy could not be met.** + +`zig build shared-libs-check -Dplatform=tty|gui` (it also runs with every +`unit-test`) reads the linked binary's ELF symbol table. It fails when a family +the build chose against is linked again: stb_image's PNG, JPEG or inflate +beside wuffs, a second stb_image, stb_image_write's deflate, a second zlib, or +a second FreeType (tools/shared_libs_check.zig). It counts static functions +too, and several functions per family, because a release build inlines some +of them. + +## Inventory (ghostty 1.4.0, 2026-10-28) + +Sizes are function bytes in the ReleaseFast symbol tables (`nm -S`). The tty +and gui columns are the two binaries. + +| Job | Implementation | Who uses it | Copies before → after | Bytes before → after (tty / gui) | Allocator | +|---|---|---|---|---|---| +| PNG decode | wuffs (ghostty-vt's `pkg/wuffs`) | ghostty-vt; **now pardes's image panes and its kitty `f=100` hook** | 1 → 1 | 35.0 KiB, unchanged (already linked: wuffs's function tables keep its decoders in) | Zig allocator: ghostty's wrapper allocates every work buffer | +| PNG decode | stb_image (zstbi) | image panes, kitty `f=100` | 1 / 2 → **0** | 17.9 / 25.2 KiB → 0 | removed | +| PNG and JPEG decode | SDL's private stb_image (`SDL_stb.c`) | `SDL_LoadPNG` and camera MJPEG, which pardes never calls | gui 1 → **0** (`SDL_DISABLE_STB`) | counted in the stb rows; glue 4.9 → 0.6 KiB | (SDL_malloc) | +| PNG decode | MuPDF's `fz_load_png` (on our zlib) | `fz_new_image_from_buffer` only, which PDF rendering does not call | 1 → 1 | 6.3 KiB | fz allocator | +| JPEG decode | wuffs | **now image panes** | 1 → 1 | 39.4 KiB, unchanged | Zig allocator | +| JPEG decode | stb_image | image panes | 1 / 2 → **0** | 18.1 / 38.1 KiB → 0 | removed | +| JPEG decode | MuPDF's libjpeg | PDF `DCTDecode` | 1 → 1 | 114.9 KiB | fz allocator (MuPDF's memory manager) | +| GIF, BMP, TGA, PSD, HDR, PIC, PNM | stb_image | image panes | 1 → 1 | 22.3 KiB | Zig allocator (`zstbi.init`) | +| Image resize | stb_image_resize | image panes (downscale) | 1 → 1 | 28.4 KiB | Zig allocator | +| Image write (and its deflate) | stb_image_write | nothing | 1 → **0** (patched out of zstbi.c) | 30.6 KiB → 0 | removed | +| Inflate | zlib (allyourcodebase) | FreeType gzip, MuPDF `FlateDecode` | 1 → 1 (MuPDF links FreeType's, mupdf.zig) | 15.5 + 9.7 + 4.6 KiB | per stream: MuPDF's `fz_zlib_alloc`, FreeType's `FT_Memory` | +| Inflate | wuffs zlib/deflate | ghostty-vt: kitty `o=z` | 1 → 1 | 17.2 KiB | Zig allocator | +| Inflate | stb_image's own | stb PNG | 1 / 2 → **0** | 1.1 / 2.3 KiB → 0 (most of it was inlined into the PNG decoder) | removed | +| Fonts | FreeType (allyourcodebase) | gui glyph atlas, MuPDF (built against it, not its slim bundle) | 1 → 1 | 331 KiB | gui: `ui_malloc` (c_heap); MuPDF's library: `FT_MemoryRec` on fz | +| Shaping | HarfBuzz 11.0.0 (the tarball ghostty pins) | gui | 1 → 1 | 143 KiB | `hb_*_impl` → `ui_*` | +| JPEG 2000, JBIG2 | openjpeg, jbig2dec (inside MuPDF) | PDF | 1 → 1 | 66 KiB (jbig2) | `fz_opj_malloc`, `Jbig2Allocator` on fz | +| Unicode properties | uucode | ghostty-vt, vaxis, pardes: one module (`external_uucode`) | 1 → 1 | | Zig | +| UTF-8, base64 (SIMD) | simdutf, highway | ghostty-vt | 1 → 1 | 206 KiB (debug) | none: allocates nothing in these paths | +| Regex | mvzr | pardes | 1 → 1 | 17 KiB | Zig | +| Parsing | tree-sitter | pardes | 1 → 1 | | `ts_set_allocator` → c_heap | +| Window, input, GPU | SDL 3.4.18 | gui | 1 → 1 | 1009 KiB | `SDL_SetMemoryFunctions` | + +Text section: tty 61,369,343 → 61,294,855 bytes (**−74,488**); gui +67,336,392 → 67,182,000 bytes (**−154,392**). + +## What changed (this change) + +- **PNG and JPEG are wuffs's**, the decoders ghostty itself uses. image.zig + calls them wherever ghostty-vt is in the build (`wuffs_images`). Each image + is sized from its header first: PNG's IHDR, JPEG's first SOF. Other formats, + and builds without ghostty-vt (esp32p4, web), stay on stb_image. +- **zstbi is compiled with `-DSTBI_NO_PNG -DSTBI_NO_JPEG`.** That also drops + stb's inflate, which only PNG used. Its `stb_image_write` include is patched + out (one count-checked anchor), with a no-op `stbi_flip_vertically_on_write` + left for `zstbi.deinit`. Both happen in build.zig, so a fresh fetch keeps + them. +- **SDL is compiled with `SDL_DISABLE_STB`.** `SDL_LoadPNG` and MJPEG camera + frames then report unsupported; pardes calls neither. + +## Where one copy was not possible + +- **Two inflates: zlib and wuffs.** FreeType and MuPDF call zlib's C API + (`inflateInit2_`, `inflate`, raw and zlib streams). ghostty-vt calls + `wuffs.zlib.decode` for kitty `o=z`. One of them would have to go: + - Dropping zlib needs a zlib-API shim over wuffs's streaming decoder. + - Dropping wuffs needs a patch to ghostty's `graphics_image.zig` that points + away from ghostty's own choice. + + Neither is small, and each copy is about 17 KiB. +- **Two JPEG decoders: MuPDF's libjpeg and wuffs.** MuPDF's `DCTDecode` filter + (`filter-dct.c`) is written against libjpeg's streaming source-manager API. + Moving PDFs onto wuffs means a new DCT filter for fitz. Moving image panes + onto libjpeg instead would leave the builds without MuPDF (esp32p4, web) + with no JPEG decoder. +- **MuPDF's fitz image loaders** (`fz_load_png`, `_jpeg`, `_gif`, `_bmp`, + `_tiff`, `_pnm`, `_psd`; 16.7 KiB together). They are compiled because + mupdf.zig builds all of `source/fitz`. They are reachable only through + `fz_new_image_from_buffer`, which PDF rendering does not call. Dropping + them means stubbing fitz's image-type dispatch (`image.c`) at build time. + Left in. +- **base64**: + - Zig's `std.base64`: pardes (dumps, the tty's kitty output) and vaxis. + - simdutf's: ghostty-vt, for kitty payloads. + - MuPDF's own: unused paths. + + Pure Zig beside C++, so lower priority. Going through simdutf from pardes + would mean importing ghostty's SIMD module surface. +- **Unicode text**: uucode is shared. Each of these still carries its own + UTF-8 and property code for its own job: + - ghostty's generated `unicode_tables` (built from the same uucode); + - HarfBuzz's UCD; + - SDL's `SDL_utf8*`; + - MuPDF's `fz_chartorune`; + - simdutf. + + None of them is a separate copy of the same library. +- **Image scaling**: stb_image_resize (image panes) beside MuPDF's pixmap + scaler and SDL's blitter. These are three algorithms with different jobs, + not one library twice. +- **Zig's `std.compress.flate`** (15 KiB): std.debug pulls it in to read + compressed debug sections for stack traces. It is not a codec pardes + calls. +- **zigimg** (vaxis's image loader, pure Zig): vaxis only uses it to load + images from paths, which pardes never asks for. Zig compiles only what is + referenced, so none of it is linked. +- **libc++**: simdutf's build links Zig's bundled libc++ (`link_libcpp` unless + MSVC). It is one copy, and its `new`/`delete` go to libc's malloc, not a Zig + allocator. +- **simdutf and highway**: no allocator hook, and none needed; the paths + ghostty uses allocate nothing (`HWY_NO_LIBCXX`, `SIMDUTF_NO_LIBCXX`). diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index 7aab597d..4466ad26 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,14 @@ ## Unreleased +- One PNG and one JPEG decoder in a binary: images decode through wuffs, + the decoders ghostty uses (ghostty-vt already links them), and stb_image + keeps only the other formats. stb's own inflate and image writer go, as + does SDL's private stb_image, which pardes never called: 73 KB less code + in the tty binary, 151 KB in the GUI. `zig build shared-libs-check` (run + with unit-test) fails if a second copy of one of them comes back; + docs/shared-libs.md lists what is shared and where one copy was not + possible. - Kitty graphics in a terminal pane take every medium kitty and ghostty do: a file (`t=f`), a temporary file (`t=t`) and shared memory (`t=s`) beside direct data, so `kitten icat` no longer falls back to base64 diff --git a/src/image.zig b/src/image.zig index 5ceadd76..e334965a 100644 --- a/src/image.zig +++ b/src/image.zig @@ -756,6 +756,71 @@ var started = false; extern fn stbi_info_from_memory(buffer: [*]const u8, len: c_int, x: *c_int, y: *c_int, comp: *c_int) c_int; +/// PNG and JPEG are ghostty's decoders (wuffs, which ghostty-vt links for +/// kitty graphics) wherever ghostty-vt is in the build; stb_image is then +/// compiled without them, and without the inflate only its PNG used +/// (build.zig, docs/shared-libs.md). Other formats, and builds without +/// ghostty-vt, are stb_image's. +const wuffs_images = @hasDecl(@import("pardes_config"), "wuffs_images") and @import("pardes_config").wuffs_images; +const wuffs = if (wuffs_images) @import("wuffs") else struct {}; + +const Format = enum { png, jpeg, other }; + +fn format(bytes: []const u8) Format { + if (std.mem.startsWith(u8, bytes, "\x89PNG\r\n\x1a\n")) return .png; + if (bytes.len >= 3 and bytes[0] == 0xff and bytes[1] == 0xd8 and bytes[2] == 0xff) return .jpeg; + return .other; +} + +const Size = struct { w: usize, h: usize }; + +/// An image's size from its header alone, before anything is decoded. +fn headerSize(bytes: []const u8) ?Size { + if (comptime wuffs_images) switch (format(bytes)) { + .png => { + // The signature, then IHDR first: length, type, width, height. + if (bytes.len < 24 or !std.mem.eql(u8, bytes[12..16], "IHDR")) return null; + return .{ .w = std.mem.readInt(u32, bytes[16..20], .big), .h = std.mem.readInt(u32, bytes[20..24], .big) }; + }, + .jpeg => return jpegSize(bytes), + .other => {}, + }; + var iw: c_int = 0; + var ih: c_int = 0; + var ic: c_int = 0; + if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null; + return .{ .w = @intCast(@max(0, iw)), .h = @intCast(@max(0, ih)) }; +} + +/// A JPEG's size, from its first start-of-frame segment (any SOFn but the +/// DHT, JPG and DAC markers that share their range). +fn jpegSize(bytes: []const u8) ?Size { + var at: usize = 2; + while (at + 4 <= bytes.len) { + if (bytes[at] != 0xff) return null; + const marker = bytes[at + 1]; + // Fill bytes, and the markers that stand alone. + if (marker == 0xff) { + at += 1; + continue; + } + if (marker == 0x01 or (marker >= 0xd0 and marker <= 0xd7)) { + at += 2; + continue; + } + const len = std.mem.readInt(u16, bytes[at + 2 ..][0..2], .big); + if (len < 2) return null; + const sof = marker >= 0xc0 and marker <= 0xcf and marker != 0xc4 and marker != 0xc8 and marker != 0xcc; + if (sof) { + if (at + 9 > bytes.len) return null; + return .{ .w = std.mem.readInt(u16, bytes[at + 7 ..][0..2], .big), .h = std.mem.readInt(u16, bytes[at + 5 ..][0..2], .big) }; + } + if (marker == 0xda) return null; // scan data before any frame + at += 2 + len; + } + return null; +} + /// Whether `bytes` are an image `decode` can read, from its header alone. pub fn readable(bytes: []const u8) bool { if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return false; @@ -763,6 +828,7 @@ pub fn readable(bytes: []const u8) bool { var h: c_int = 0; var c: c_int = 0; if (!started) return true; // ponytail: no stb_image yet (a unit test), so no judging + if (comptime wuffs_images) if (format(bytes) != .other) return headerSize(bytes) != null; return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &w, &h, &c) == 1; } @@ -776,13 +842,33 @@ pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded { if (!started) return null; if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return null; // Bounded before decoding: a few KB of PNG can name gigapixels. - var iw: c_int = 0; - var ih: c_int = 0; - var ic: c_int = 0; - if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null; - if (@as(usize, @intCast(@max(0, iw))) * @as(usize, @intCast(@max(0, ih))) > @import("memory.zig").limits.image_decode_pixels) return null; + const size = headerSize(bytes) orelse return null; + const pixels = std.math.mul(usize, size.w, size.h) catch return null; + if (pixels > @import("memory.zig").limits.image_decode_pixels) return null; + if (comptime wuffs_images) switch (format(bytes)) { + inline .png, .jpeg => |f| { + const got = @field(wuffs, @tagName(f)).decode(gpa, bytes) catch return null; + defer gpa.free(got.data); + if (got.data.len != @as(usize, got.width) * got.height * 4) return null; + return finish(gpa, .{ + .data = got.data, + .width = got.width, + .height = got.height, + .num_components = 4, + .bytes_per_component = 1, + .bytes_per_row = got.width * 4, + .is_hdr = false, + }); + }, + .other => {}, + }; var img = zstbi.Image.loadFromMemory(bytes, 4) catch return null; // 4 = RGBA defer img.deinit(); + return finish(gpa, img); +} + +/// The decoded RGBA, scaled to MAX_DIM at most, in a gpa copy. +fn finish(gpa: std.mem.Allocator, img: zstbi.Image) ?Decoded { var scaled: ?zstbi.Image = null; defer if (scaled) |*s| s.deinit(); const longest = @max(img.width, img.height); @@ -796,6 +882,45 @@ pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded { return .{ .rgba = rgba, .w = src.width, .h = src.height }; } +test "PNG and JPEG decode through wuffs, sized from their headers first; other formats through stb_image" { + const gpa = std.testing.allocator; + start(std.testing.io, gpa); + defer stop(); + const dec = std.base64.standard.Decoder; + const Fixture = struct { b64: []const u8, w: usize, h: usize, rgb: [3]u8, tolerance: u8 }; + const fixtures = [_]Fixture{ + // 4x2, red left, blue right (a hand-built PNG). + .{ .b64 = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAIAAADwyuo0AAAAE0lEQVR4nGP4z8AARGDiPwMyBwBnsgf5Q2OaZwAAAABJRU5ErkJggg==", .w = 4, .h = 2, .rgb = .{ 255, 0, 0 }, .tolerance = 0 }, + // 16x8 of (200,30,30), baseline and progressive (Pillow, quality 95). + .{ .b64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAIABADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5Dooor8LP9VD/2Q==", .w = 16, .h = 8, .rgb = .{ 200, 30, 30 }, .tolerance = 6 }, + .{ .b64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wgARCAAIABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAVAQEBAAAAAAAAAAAAAAAAAAAGCP/aAAwDAQACEAMQAAABkIC1V//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAQUCP//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Bf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Bf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEABj8CP//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8hP//aAAwDAQACAAMAAAAQA//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Qf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Qf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8QP//Z", .w = 16, .h = 8, .rgb = .{ 200, 30, 30 }, .tolerance = 6 }, + }; + for (fixtures) |f| { + const bytes = try gpa.alloc(u8, try dec.calcSizeForSlice(f.b64)); + defer gpa.free(bytes); + try dec.decode(bytes, f.b64); + try std.testing.expect(readable(bytes)); + const size = headerSize(bytes).?; + try std.testing.expectEqual(Size{ .w = f.w, .h = f.h }, size); + const got = decode(gpa, bytes) orelse return error.NotDecoded; + defer gpa.free(got.rgba); + try std.testing.expectEqual(f.w, got.w); + try std.testing.expectEqual(f.h, got.h); + for (got.rgba[0..3], f.rgb) |have, want| try std.testing.expect(@max(have, want) - @min(have, want) <= f.tolerance); + try std.testing.expectEqual(@as(u8, 255), got.rgba[3]); + } + // A PPM still goes through stb_image. + const ppm = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7"; + try std.testing.expectEqual(Format.other, format(ppm)); + const got = decode(gpa, ppm) orelse return error.NotDecoded; + defer gpa.free(got.rgba); + try std.testing.expectEqualSlices(u8, &.{ 0x0d, 0x4d, 0xe7, 255 }, got.rgba[0..4]); + // Truncated headers are no image, never a read past the end. + try std.testing.expect(headerSize("\x89PNG\r\n\x1a\n\x00\x00") == null); + try std.testing.expect(headerSize("\xff\xd8\xff\xe0\x00\x10JFIF") == null); + try std.testing.expect(decode(gpa, "\xff\xd8\xff\xe0\x00\x10JFIF") == null); +} + // ---- the image pane ---- const GridKey = struct { diff --git a/tools/shared_libs_check.zig b/tools/shared_libs_check.zig new file mode 100644 index 00000000..d18c6850 --- /dev/null +++ b/tools/shared_libs_check.zig @@ -0,0 +1,127 @@ +//! One copy of each codec in a pardes binary (docs/shared-libs.md). +//! +//! zig build shared-libs-check -Dplatform=tty|gui +//! +//! Reads the linked executable's ELF symbol table and fails when a family of +//! symbols the build has chosen against is in it: a second PNG, JPEG or +//! inflate implementation beside the one docs/shared-libs.md names for the +//! job. The families are named by functions each copy defines (static ones +//! too: the symbol table keeps locals), so a dependency that brings its own +//! copy back shows here, whatever its symbols are called in the end. +const std = @import("std"); + +const Family = struct { + /// What the family is, and what the build uses instead. + what: []const u8, + /// Functions only that copy defines; an optimized build inlines some + /// of them, so a copy counts as linked when any is there, as many + /// times as the most of them are. + symbols: []const []const u8, + /// How many copies may be linked (0: none). + most: usize = 0, +}; + +const stb_png = [_][]const u8{ "stbi__png_load", "stbi__parse_png_file", "stbi__create_png_image_raw" }; +const stb_jpeg = [_][]const u8{ "stbi__jpeg_load", "stbi__decode_jpeg_header", "stbi__jpeg_decode_block" }; +const stb_inflate = [_][]const u8{ "stbi__parse_zlib", "stbi__do_zlib", "stbi__zbuild_huffman", "stbi__zhuffman_decode" }; +const stb_deflate = [_][]const u8{ "stbi_zlib_compress", "stbiw__zlib_bitrev", "stbi_write_png_to_mem" }; + +/// Banned in every binary that links ghostty-vt (wuffs decodes PNG and +/// JPEG; zlib and wuffs are the two inflates, see docs/shared-libs.md). +const with_wuffs = [_]Family{ + .{ .what = "stb_image's PNG decoder (wuffs decodes PNG: STBI_NO_PNG)", .symbols = &stb_png }, + .{ .what = "stb_image's JPEG decoder (wuffs decodes JPEG: STBI_NO_JPEG)", .symbols = &stb_jpeg }, + .{ .what = "stb_image's own inflate (zlib and wuffs inflate)", .symbols = &stb_inflate }, +}; + +/// Banned everywhere. +const always = [_]Family{ + // SDL's SDL_stb.c (SDL_LoadPNG, camera MJPEG) is off: SDL_DISABLE_STB. + .{ .what = "a second stb_image PNG decoder (SDL_stb.c)", .symbols = &stb_png, .most = 1 }, + .{ .what = "a second stb_image JPEG decoder (SDL_stb.c)", .symbols = &stb_jpeg, .most = 1 }, + // zstbi's stb_image_write, patched out: its own deflate beside zlib's. + .{ .what = "stb_image_write and its deflate (zlib deflates)", .symbols = &stb_deflate }, + // One zlib: FreeType's, which MuPDF links (mupdf.zig). + .{ .what = "a second zlib", .symbols = &.{ "inflate_table", "inflate_fast", "deflate_slow" }, .most = 1 }, + // One FreeType: the build's, which MuPDF is compiled against. + .{ .what = "a second FreeType", .symbols = &.{ "FT_Init_FreeType", "FT_New_Library" }, .most = 1 }, +}; + +pub fn main(init: std.process.Init) !void { + var arena_state: std.heap.ArenaAllocator = .init(std.heap.page_allocator); + defer arena_state.deinit(); + const arena = arena_state.allocator(); + const args = try init.minimal.args.toSlice(arena); + if (args.len != 2) return error.BadArgs; + const bytes = try readFile(arena, try arena.dupeSentinel(u8, args[1], 0)); + + var counts = std.StringHashMap(usize).init(arena); + try symbols(bytes, &counts); + if (counts.count() == 0) { + std.debug.print("shared-libs-check: {s} has no symbol table (stripped?): nothing to check\n", .{args[1]}); + return; + } + const wuffs = counts.get("wuffs_zlib__decoder__transform_io") != null or + counts.get("wuffs_base__pixel_swizzler__prepare") != null; + var failed = false; + for ([2][]const Family{ &always, if (wuffs) &with_wuffs else &.{} }) |set| for (set) |family| { + var n: usize = 0; + var seen: []const u8 = family.symbols[0]; + for (family.symbols) |symbol| { + const here = counts.get(symbol) orelse 0; + if (here > n) { + n = here; + seen = symbol; + } + } + if (n > family.most) { + failed = true; + std.debug.print("shared-libs-check: {s}: `{s}` is linked {d} times (at most {d})\n", .{ family.what, seen, n, family.most }); + } + }; + if (failed) return error.DuplicateLibrary; + std.debug.print("shared-libs-check: one copy of each ({d} symbols{s})\n", .{ counts.count(), if (wuffs) ", wuffs present" else "" }); +} + +/// Counts every name in the ELF64 little-endian symbol tables of `bytes`. +fn symbols(bytes: []const u8, counts: *std.StringHashMap(usize)) !void { + if (bytes.len < @sizeOf(std.elf.Elf64_Ehdr) or !std.mem.eql(u8, bytes[0..4], "\x7fELF")) return error.NotElf; + if (bytes[std.elf.EI.CLASS] != std.elf.ELFCLASS64 or bytes[std.elf.EI.DATA] != std.elf.ELFDATA2LSB) return error.UnsupportedElf; + const eh = std.mem.bytesAsValue(std.elf.Elf64_Ehdr, bytes[0..@sizeOf(std.elf.Elf64_Ehdr)]); + const shoff: usize = @intCast(eh.e_shoff); + const shnum: usize = eh.e_shnum; + const shsize: usize = eh.e_shentsize; + if (shsize != @sizeOf(std.elf.Elf64_Shdr) or shoff + shnum * shsize > bytes.len) return error.BadSections; + const sections = std.mem.bytesAsSlice(std.elf.Elf64_Shdr, @as([]align(1) const u8, bytes[shoff..][0 .. shnum * shsize])); + for (sections) |section| { + if (section.sh_type != std.elf.SHT_SYMTAB) continue; + const strtab = sections[section.sh_link]; + const strings = bytes[@intCast(strtab.sh_offset)..][0..@intCast(strtab.sh_size)]; + const table = bytes[@intCast(section.sh_offset)..][0..@intCast(section.sh_size)]; + const syms = std.mem.bytesAsSlice(std.elf.Elf64_Sym, @as([]align(1) const u8, table)); + for (syms) |sym| { + // Functions only: a family is the code it brings. + if (sym.st_type() != std.elf.STT_FUNC or sym.st_shndx == 0) continue; + const name = std.mem.sliceTo(strings[sym.st_name..], 0); + const gop = try counts.getOrPut(name); + gop.value_ptr.* = if (gop.found_existing) gop.value_ptr.* + 1 else 1; + } + } +} + +fn readFile(arena: std.mem.Allocator, path: [:0]const u8) ![]u8 { + const c = std.c; + const fd = c.open(path, .{ .ACCMODE = .RDONLY }, @as(c.mode_t, 0)); + if (fd < 0) return error.OpenFailed; + defer _ = c.close(fd); + const end = c.lseek(fd, 0, c.SEEK.END); + if (end < 0 or c.lseek(fd, 0, c.SEEK.SET) < 0) return error.ReadFailed; + const out = try arena.alloc(u8, @intCast(end)); + var filled: usize = 0; + while (filled < out.len) { + const n = c.read(fd, out[filled..].ptr, out.len - filled); + if (n <= 0) return error.ReadFailed; + filled += @intCast(n); + } + return out; +} -- cgit v1.3