// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Mutation fuzzers over everything that reads outside input: font files, //! configuration files, font names, and the cache. //! //! Random bytes almost never get past a table directory or an XML prolog, //! so each input picks one of the fixtures and then a series of edits to //! it, which keeps enough structure intact for the edits to reach the code //! deep inside. //! //! zig build test --fuzz # until interrupted //! zig build test --fuzz=1M # a bounded run //! //! Without `--fuzz` each test runs its corpus once: every fixture, unedited. const std = @import("std"); const fc = @import("font_config"); const fonts = [_][]const u8{ @embedFile("fonts/fixture-sans.ttf"), @embedFile("fonts/fixture-sans.otf"), @embedFile("fonts/fixture-sans-variable.otf"), @embedFile("fonts/fixture-sans.woff"), @embedFile("fonts/fixture-mono.ttf"), @embedFile("fonts/noto-sans-variable.ttf"), @embedFile("fonts/noto-sans-tagalog.otf"), @embedFile("fonts/noto-color-emoji.ttf"), @embedFile("fonts/nabla.ttf"), @embedFile("fonts/sbix.ttf"), @embedFile("fonts/colr-v1-paints.ttf"), @embedFile("fonts/collection.ttc"), @embedFile("fonts/spleen-12x24.bdf"), @embedFile("fonts/spleen-12x24.pcf"), @embedFile("fonts/spleen-12x24.pcf.gz"), @embedFile("fonts/odd-metrics.pcf"), @embedFile("fonts/spleen-6x12-8x16.otb"), }; const configs = [_][]const u8{ @embedFile("config/fonts.conf"), @embedFile("config/conf.d/30-aliases.conf"), @embedFile("config/conf.d/60-generic.conf"), }; const names = [_][]const u8{ "", "DejaVu Sans-12:bold", "Times,Liberation Serif:italic:lang=en|de", "sans:weight=[light bold]:width=[50 100]:size=[8 12]", "mono:charset=20-7e 4e00:matrix=1 0.2 0 1", "Foo\\-Bar\\:Baz:antialias=dontcare:hintstyle=hintslight", }; /// One corpus entry per fixture: `Smith.index` reads a little-endian `u64`, /// and an input with nothing after it makes no edits. fn corpusFor(comptime n: usize) [n][]const u8 { var entries: [n][]const u8 = undefined; for (&entries, 0..) |*e, i| { var b: [8]u8 = undefined; std.mem.writeInt(u64, &b, i, .little); const copy = b; e.* = © } return entries; } const font_corpus = corpusFor(fonts.len); const config_corpus = corpusFor(configs.len); const name_corpus = corpusFor(names.len); const Edit = enum { byte, zero16, max16, min_i16, max_i16, zero32, max32, swap }; fn put(comptime T: type, data: []u8, at: usize, v: T) void { const n = @sizeOf(T); if (at + n > data.len) return; std.mem.writeInt(T, data[at..][0..n], v, .big); } /// A copy of one of `sources`, with edits. fn mutate(gpa: std.mem.Allocator, smith: *std.testing.Smith, sources: []const []const u8) ![]u8 { const original = sources[smith.index(sources.len)]; // String literals and embedded files are read-only once LLVM compiles // the binary, so the edits go to a copy. const data = try gpa.dupe(u8, original); if (data.len == 0) return data; var edits: usize = 0; while (!smith.eosWeightedSimple(1, 4) and edits < 64) : (edits += 1) { const at = smith.index(data.len); switch (smith.value(Edit)) { .byte => data[at] = smith.value(u8), .zero16 => put(u16, data, at, 0), .max16 => put(u16, data, at, 0xffff), .min_i16 => put(u16, data, at, 0x8000), .max_i16 => put(u16, data, at, 0x7fff), .zero32 => put(u32, data, at, 0), .max32 => put(u32, data, at, 0xffffffff), .swap => { const other = smith.index(data.len); std.mem.swap(u8, &data[at], &data[other]); }, } } return data; } fn fuzzFont(_: void, smith: *std.testing.Smith) anyerror!void { const gpa = std.testing.allocator; const data = try mutate(gpa, smith, &fonts); defer gpa.free(data); var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); var faces: std.ArrayList(*fc.Pattern) = .empty; defer { for (faces.items) |f| { f.deinit(); gpa.destroy(f); } faces.deinit(gpa); } try fc.scan.queryAll(gpa, arena.allocator(), data, "/fuzz/font.ttf", &faces); // What becomes of a face afterwards: printed, and matched. var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); for (faces.items) |f| try f.format(&out.writer); var q = try fc.name.parse(gpa, "sans-serif:bold:lang=en"); defer q.deinit(); var best = try fc.match.bestFont(gpa, faces.items, &q); if (best) |*b| b.deinit(); } fn fuzzConfig(_: void, smith: *std.testing.Smith) anyerror!void { const gpa = std.testing.allocator; const data = try mutate(gpa, smith, &configs); defer gpa.free(data); const config = try fc.Config.fromMemory(gpa, null, data, .{ .home = "/home/u" }, .{}); defer config.deinit(); // And the rules, applied to a query. var p = try fc.name.parse(gpa, "monospace:bold:italic:size=48:pixelsize=5"); defer p.deinit(); try config.prepare(&p); } /// What the cache holds for each fixture font, made once: reading the /// fonts again for every input would be most of the work. var encoded: ?[]const []const u8 = null; fn encodedFonts() ![]const []const u8 { if (encoded) |e| return e; // Kept for the life of the process, so not the leak-checked allocator. const gpa = std.heap.page_allocator; var arena: std.heap.ArenaAllocator = .init(gpa); const a = arena.allocator(); const out = try a.alloc([]const u8, fonts.len); for (fonts, out) |data, *e| { var faces: std.ArrayList(*fc.Pattern) = .empty; try fc.scan.queryAll(gpa, a, data, "/fuzz/font.ttf", &faces); var w: std.Io.Writer.Allocating = .init(a); try fc.cache.encode(gpa, &w.writer, faces.items); e.* = w.written(); for (faces.items) |f| { f.deinit(); gpa.destroy(f); } faces.deinit(gpa); } encoded = out; return out; } fn fuzzCache(_: void, smith: *std.testing.Smith) anyerror!void { const gpa = std.testing.allocator; const data = try mutate(gpa, smith, try encodedFonts()); defer gpa.free(data); var arena: std.heap.ArenaAllocator = .init(gpa); defer arena.deinit(); var faces: std.ArrayList(*fc.Pattern) = .empty; defer { for (faces.items) |f| { f.deinit(); gpa.destroy(f); } faces.deinit(gpa); } fc.cache.decode(gpa, arena.allocator(), data, &faces) catch |err| switch (err) { error.Corrupt => return, else => return err, }; // Whatever was decoded is used as a scanned font would be. var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); for (faces.items) |f| try f.format(&out.writer); var q = try fc.name.parse(gpa, "sans-serif:bold:lang=en"); defer q.deinit(); var best = try fc.match.bestFont(gpa, faces.items, &q); if (best) |*b| b.deinit(); } fn fuzzName(_: void, smith: *std.testing.Smith) anyerror!void { const gpa = std.testing.allocator; const data = try mutate(gpa, smith, &names); defer gpa.free(data); var p = fc.name.parse(gpa, data) catch |err| switch (err) { error.InvalidName => return, else => return err, }; defer p.deinit(); var out: std.Io.Writer.Allocating = .init(gpa); defer out.deinit(); try p.format(&out.writer); } test "fuzz the font reader" { try std.testing.fuzz({}, fuzzFont, .{ .corpus = &font_corpus }); } test "fuzz the configuration reader" { try std.testing.fuzz({}, fuzzConfig, .{ .corpus = &config_corpus }); } test "fuzz the cache reader" { try std.testing.fuzz({}, fuzzCache, .{ .corpus = &font_corpus }); } test "fuzz the name parser" { try std.testing.fuzz({}, fuzzName, .{ .corpus = &name_corpus }); }