From c4ef89c63d810a7bf23751c287c01da00320b3d5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Mar 2026 13:44:18 -0300 Subject: [PATCH 1/7] Add extended hb-subset fuzzer seed recorder --- test/fuzzing/hb-fuzzer.hh | 79 +++++++ test/fuzzing/hb-subset-fuzzer.cc | 243 ++++++++++++++++++++- test/fuzzing/hb_fuzzing_tools.py | 307 ++++++++++++++++++++++++++ test/fuzzing/record-subset-test.py | 338 +++++++++++++++++++++++++++++ 4 files changed, 957 insertions(+), 10 deletions(-) create mode 100644 test/fuzzing/hb_fuzzing_tools.py create mode 100755 test/fuzzing/record-subset-test.py diff --git a/test/fuzzing/hb-fuzzer.hh b/test/fuzzing/hb-fuzzer.hh index d31a183dd..f1c1fdf19 100644 --- a/test/fuzzing/hb-fuzzer.hh +++ b/test/fuzzing/hb-fuzzer.hh @@ -2,6 +2,7 @@ #include #include +#include extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size); @@ -34,3 +35,81 @@ _fuzzing_alloc_state (const uint8_t *data, size_t size) return size; } + +static const uint8_t _fuzzing_extended_magic[] = {'H', 'B', 'S', 'U', 'B', 'F', 'Z', '2'}; + +enum _fuzzing_extended_op_t +{ + HB_FUZZING_OP_SET_FLAGS = 1, + HB_FUZZING_OP_KEEP_EVERYTHING = 2, + HB_FUZZING_OP_SET_CLEAR = 3, + HB_FUZZING_OP_SET_INVERT = 4, + HB_FUZZING_OP_SET_ADD_RANGES = 5, + HB_FUZZING_OP_SET_DEL_RANGES = 6, + HB_FUZZING_OP_TEXT_ADD = 7, + HB_FUZZING_OP_TEXT_DEL = 8, + HB_FUZZING_OP_AXIS_PIN_ALL_TO_DEFAULT = 9, + HB_FUZZING_OP_AXIS_SET = 10, +}; + +enum _fuzzing_extended_axis_mode_t +{ + HB_FUZZING_AXIS_PIN_TO_DEFAULT = 0, + HB_FUZZING_AXIS_SET_RANGE = 1, +}; + +static inline uint32_t +_fuzzing_read_u32_le (const uint8_t *p) +{ + return (uint32_t) p[0] | + ((uint32_t) p[1] << 8) | + ((uint32_t) p[2] << 16) | + ((uint32_t) p[3] << 24); +} + +static inline float +_fuzzing_read_f32_le (const uint8_t *p) +{ + uint32_t bits = _fuzzing_read_u32_le (p); + float value; + memcpy (&value, &bits, sizeof (value)); + return value; +} + +template +static inline bool +_fuzzing_read_value (const uint8_t *&p, + const uint8_t *end, + T *out) +{ + if ((size_t) (end - p) < sizeof (T)) + return false; + + memcpy (out, p, sizeof (T)); + p += sizeof (T); + return true; +} + +static inline bool +_fuzzing_read_u32_value (const uint8_t *&p, + const uint8_t *end, + uint32_t *out) +{ + if ((size_t) (end - p) < 4) + return false; + *out = _fuzzing_read_u32_le (p); + p += 4; + return true; +} + +static inline bool +_fuzzing_read_f32_value (const uint8_t *&p, + const uint8_t *end, + float *out) +{ + if ((size_t) (end - p) < 4) + return false; + *out = _fuzzing_read_f32_le (p); + p += 4; + return true; +} diff --git a/test/fuzzing/hb-subset-fuzzer.cc b/test/fuzzing/hb-subset-fuzzer.cc index d6cf2f68e..fceeb61c3 100644 --- a/test/fuzzing/hb-subset-fuzzer.cc +++ b/test/fuzzing/hb-subset-fuzzer.cc @@ -5,6 +5,10 @@ #include #include +#include +#include +#include + #include "hb-subset.h" static void @@ -44,10 +48,222 @@ trySubset (hb_face_t *face, hb_subset_input_destroy (input); } -extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) +static bool +read_ranges (const uint8_t *&p, + const uint8_t *end, + hb_set_t *set, + bool is_add) { - alloc_state = _fuzzing_alloc_state (data, size); + uint32_t count; + if (!_fuzzing_read_u32_value (p, end, &count)) + return false; + for (uint32_t i = 0; i < count; i++) + { + uint32_t start, finish; + if (!_fuzzing_read_u32_value (p, end, &start) || + !_fuzzing_read_u32_value (p, end, &finish)) + return false; + + if (finish < start) + return false; + + if (is_add) + hb_set_add_range (set, start, finish); + else + hb_set_del_range (set, start, finish); + } + return true; +} + +static bool +apply_extended_ops (hb_face_t *face, + hb_subset_input_t *input, + std::vector *text, + unsigned *flags, + const uint8_t *ops, + size_t ops_len) +{ + const uint8_t *p = ops; + const uint8_t *end = ops + ops_len; + + while (p < end) + { + uint8_t op; + if (!_fuzzing_read_value (p, end, &op)) + return false; + + switch (op) + { + case HB_FUZZING_OP_SET_FLAGS: + { + uint32_t value; + if (!_fuzzing_read_u32_value (p, end, &value)) + return false; + *flags = value; + break; + } + + case HB_FUZZING_OP_KEEP_EVERYTHING: + hb_subset_input_keep_everything (input); + *flags = hb_subset_input_get_flags (input); + break; + + case HB_FUZZING_OP_SET_CLEAR: + case HB_FUZZING_OP_SET_INVERT: + { + uint8_t set_type_u8; + if (!_fuzzing_read_value (p, end, &set_type_u8)) + return false; + if (set_type_u8 > HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG) + return false; + + hb_set_t *set = hb_subset_input_set (input, (hb_subset_sets_t) set_type_u8); + if (op == HB_FUZZING_OP_SET_CLEAR) + hb_set_clear (set); + else + hb_set_invert (set); + break; + } + + case HB_FUZZING_OP_SET_ADD_RANGES: + case HB_FUZZING_OP_SET_DEL_RANGES: + { + uint8_t set_type_u8; + if (!_fuzzing_read_value (p, end, &set_type_u8)) + return false; + if (set_type_u8 > HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG) + return false; + + hb_set_t *set = hb_subset_input_set (input, (hb_subset_sets_t) set_type_u8); + if (!read_ranges (p, end, set, op == HB_FUZZING_OP_SET_ADD_RANGES)) + return false; + break; + } + + case HB_FUZZING_OP_TEXT_ADD: + case HB_FUZZING_OP_TEXT_DEL: + { + uint32_t count; + if (!_fuzzing_read_u32_value (p, end, &count)) + return false; + + for (uint32_t i = 0; i < count; i++) + { + uint32_t cp; + if (!_fuzzing_read_u32_value (p, end, &cp)) + return false; + if (op == HB_FUZZING_OP_TEXT_ADD) + text->push_back (cp); + else + text->erase (std::remove (text->begin (), text->end (), cp), text->end ()); + } + break; + } + + case HB_FUZZING_OP_AXIS_PIN_ALL_TO_DEFAULT: + if (!hb_subset_input_pin_all_axes_to_default (input, face)) + return false; + break; + + case HB_FUZZING_OP_AXIS_SET: + { + uint32_t count; + if (!_fuzzing_read_u32_value (p, end, &count)) + return false; + + for (uint32_t i = 0; i < count; i++) + { + uint32_t tag; + uint8_t mode; + float minimum, middle, maximum; + if (!_fuzzing_read_u32_value (p, end, &tag) || + !_fuzzing_read_value (p, end, &mode) || + !_fuzzing_read_f32_value (p, end, &minimum) || + !_fuzzing_read_f32_value (p, end, &middle) || + !_fuzzing_read_f32_value (p, end, &maximum)) + return false; + + if (mode == HB_FUZZING_AXIS_PIN_TO_DEFAULT) + { + if (!hb_subset_input_pin_axis_to_default (input, face, tag)) + return false; + } + else if (mode == HB_FUZZING_AXIS_SET_RANGE) + { + if (!hb_subset_input_set_axis_range (input, face, tag, minimum, maximum, middle)) + return false; + } + else + return false; + } + break; + } + + default: + return false; + } + } + + return true; +} + +static bool +try_extended_input (const uint8_t *data, size_t size) +{ + if (size < sizeof (_fuzzing_extended_magic) + 4) + return false; + + size_t magic_offset = size - sizeof (_fuzzing_extended_magic); + if (0 != memcmp (data + magic_offset, _fuzzing_extended_magic, sizeof (_fuzzing_extended_magic))) + return false; + + size_t ops_len_offset = magic_offset - 4; + uint32_t ops_len = _fuzzing_read_u32_le (data + ops_len_offset); + if (ops_len > ops_len_offset) + return false; + + size_t font_len = ops_len_offset - ops_len; + const uint8_t *ops = data + font_len; + + hb_blob_t *blob = hb_blob_create ((const char *) data, font_len, + HB_MEMORY_MODE_READONLY, nullptr, nullptr); + hb_face_t *face = hb_face_create (blob, 0); + + hb_set_t *output = hb_set_create (); + hb_face_collect_unicodes (face, output); + hb_set_destroy (output); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + if (!input) + { + hb_face_destroy (face); + hb_blob_destroy (blob); + return true; + } + + std::vector text; + unsigned flags = HB_SUBSET_FLAGS_DEFAULT; + if (apply_extended_ops (face, input, &text, &flags, ops, ops_len)) + { + trySubset (face, + text.empty () ? nullptr : text.data (), + (int) text.size (), + flags, + input); + } + else + hb_subset_input_destroy (input); + + hb_face_destroy (face); + hb_blob_destroy (blob); + + return true; +} + +static void +try_legacy_input (const uint8_t *data, size_t size) +{ hb_blob_t *blob = hb_blob_create ((const char *) data, size, HB_MEMORY_MODE_READONLY, nullptr, nullptr); hb_face_t *face = hb_face_create (blob, 0); @@ -69,19 +285,19 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { hb_face_destroy (face); hb_blob_destroy (blob); - return 0; + return; } trySubset (face, text, sizeof (text) / sizeof (hb_codepoint_t), flags, input); unsigned num_axes; hb_codepoint_t text_from_data[16]; if (size > sizeof (text_from_data) + sizeof (flags) + sizeof(num_axes)) { - hb_subset_input_t *input = hb_subset_input_create_or_fail (); - if (!input) + hb_subset_input_t *legacy_input = hb_subset_input_create_or_fail (); + if (!legacy_input) { hb_face_destroy (face); hb_blob_destroy (blob); - return 0; + return; } size -= sizeof (text_from_data); memcpy (text_from_data, @@ -112,21 +328,28 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) data + size, sizeof (value)); - hb_subset_input_pin_axis_location(input, + hb_subset_input_pin_axis_location(legacy_input, face, tag, (float) value); } } - - unsigned int text_size = sizeof (text_from_data) / sizeof (hb_codepoint_t); - trySubset (face, text_from_data, text_size, flags, input); + trySubset (face, text_from_data, text_size, flags, legacy_input); } hb_face_destroy (face); hb_blob_destroy (blob); +} +extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) +{ + alloc_state = _fuzzing_alloc_state (data, size); + + if (try_extended_input (data, size)) + return 0; + + try_legacy_input (data, size); return 0; } diff --git a/test/fuzzing/hb_fuzzing_tools.py b/test/fuzzing/hb_fuzzing_tools.py new file mode 100644 index 000000000..e604eef7b --- /dev/null +++ b/test/fuzzing/hb_fuzzing_tools.py @@ -0,0 +1,307 @@ +#!/usr/bin/env python3 + +import hashlib +import os +import struct +import sys + + +HB_FUZZING_EXTENDED_MAGIC = b"HBSUBFZ2" + +HB_SUBSET_SETS_GLYPH_INDEX = 0 +HB_SUBSET_SETS_UNICODE = 1 +HB_SUBSET_SETS_DROP_TABLE_TAG = 3 +HB_SUBSET_SETS_NAME_ID = 4 +HB_SUBSET_SETS_NAME_LANG_ID = 5 +HB_SUBSET_SETS_LAYOUT_FEATURE_TAG = 6 +HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG = 7 + +HB_FUZZING_OP_SET_FLAGS = 1 +HB_FUZZING_OP_KEEP_EVERYTHING = 2 +HB_FUZZING_OP_SET_CLEAR = 3 +HB_FUZZING_OP_SET_INVERT = 4 +HB_FUZZING_OP_SET_ADD_RANGES = 5 +HB_FUZZING_OP_SET_DEL_RANGES = 6 +HB_FUZZING_OP_TEXT_ADD = 7 +HB_FUZZING_OP_TEXT_DEL = 8 +HB_FUZZING_OP_AXIS_PIN_ALL_TO_DEFAULT = 9 +HB_FUZZING_OP_AXIS_SET = 10 + +HB_FUZZING_AXIS_PIN_TO_DEFAULT = 0 +HB_FUZZING_AXIS_SET_RANGE = 1 + +FLAG_BITS = { + "--no-hinting": 0x00000001, + "--retain-gids": 0x00000002, + "--desubroutinize": 0x00000004, + "--name-legacy": 0x00000008, + "--set-overlaps-flag": 0x00000010, + "--passthrough-tables": 0x00000020, + "--notdef-outline": 0x00000040, + "--glyph-names": 0x00000080, + "--no-prune-unicode-ranges": 0x00000100, + "--no-layout-closure": 0x00000200, + "--optimize": 0x00000400, + "--no-bidi-closure": 0x00000800, + "--iftb-requirements": 0x00001000, + "--retain-num-glyphs": 0x00002000, + "--downgrade-cff2": 0x00004000, +} + + +class UserError(Exception): + pass + + +def fail(msg: str) -> None: + raise UserError(msg) + + +def pack_u32(value: int) -> bytes: + return struct.pack(" bytes: + return struct.pack(" None: + ops.append(HB_FUZZING_OP_SET_FLAGS) + ops += pack_u32(flags) + + +def emit_keep_everything(ops: bytearray) -> None: + ops.append(HB_FUZZING_OP_KEEP_EVERYTHING) + + +def emit_set_clear(ops: bytearray, set_type: int) -> None: + ops += bytes((HB_FUZZING_OP_SET_CLEAR, set_type)) + + +def emit_set_invert(ops: bytearray, set_type: int) -> None: + ops += bytes((HB_FUZZING_OP_SET_INVERT, set_type)) + + +def emit_set_ranges(ops: bytearray, opcode: int, set_type: int, ranges: list[tuple[int, int]]) -> None: + ops += bytes((opcode, set_type)) + ops += pack_u32(len(ranges)) + for start, end in ranges: + ops += pack_u32(start) + ops += pack_u32(end) + + +def emit_text(ops: bytearray, opcode: int, codepoints: list[int]) -> None: + ops.append(opcode) + ops += pack_u32(len(codepoints)) + for cp in codepoints: + ops += pack_u32(cp) + + +def emit_pin_all_axes_to_default(ops: bytearray) -> None: + ops.append(HB_FUZZING_OP_AXIS_PIN_ALL_TO_DEFAULT) + + +def emit_axis_records(ops: bytearray, records: list[tuple[int, int, float, float, float]]) -> None: + ops.append(HB_FUZZING_OP_AXIS_SET) + ops += pack_u32(len(records)) + for tag, mode, minimum, middle, maximum in records: + ops += pack_u32(tag) + ops.append(mode) + ops += pack_f32(minimum) + ops += pack_f32(middle) + ops += pack_f32(maximum) + + +def parse_decimal_ranges(value: str) -> list[tuple[int, int]]: + ranges: list[tuple[int, int]] = [] + for chunk in value.replace(" ", ",").split(","): + if not chunk: + continue + if chunk == "*": + fail("Internal error: wildcard range should be handled before parsing.") + if "-" in chunk: + start_text, end_text = chunk.split("-", 1) + try: + start = int(start_text, 10) + end = int(end_text, 10) + except ValueError: + fail(f"Failed parsing decimal range '{chunk}'.") + else: + try: + start = end = int(chunk, 10) + except ValueError: + fail(f"Failed parsing decimal value '{chunk}'.") + if start < 0 or end < start: + fail(f"Invalid decimal range '{chunk}'.") + ranges.append((start, end)) + return ranges + + +def parse_hex_ranges(value: str) -> list[tuple[int, int]]: + ranges: list[tuple[int, int]] = [] + for chunk in value.replace(" ", ",").split(","): + if not chunk: + continue + if chunk == "*": + fail("Internal error: wildcard range should be handled before parsing.") + if "-" in chunk: + start_text, end_text = chunk.split("-", 1) + try: + start = int(start_text, 16) + end = int(end_text, 16) + except ValueError: + fail(f"Failed parsing unicode range '{chunk}'.") + else: + try: + start = end = int(chunk, 16) + except ValueError: + fail(f"Failed parsing unicode value '{chunk}'.") + if start < 0 or end < start or end > 0x10FFFF: + fail(f"Invalid unicode range '{chunk}'.") + ranges.append((start, end)) + return ranges + + +def parse_tags(value: str) -> list[tuple[int, int]]: + tags: list[tuple[int, int]] = [] + for chunk in value.replace(",", " ").split(): + if chunk == "*": + fail("Internal error: wildcard tag set should be handled before parsing.") + if len(chunk) > 4: + fail(f"Tag '{chunk}' is longer than 4 bytes.") + try: + tag_value = int.from_bytes(chunk.ljust(4, " ").encode("ascii"), byteorder="big") + except UnicodeEncodeError: + fail(f"Tag '{chunk}' is not ASCII.") + tags.append((tag_value, tag_value)) + return tags + + +def parse_text_codepoints(value: str) -> list[int]: + codepoints = [ord(ch) for ch in value] + for cp in codepoints: + if cp < 0 or cp > 0x10FFFF: + fail(f"Invalid codepoint value {cp}.") + return codepoints + + +def parse_tag_value(tag_text: str, kind: str) -> int: + if len(tag_text) > 4: + fail(f"{kind} '{tag_text}' is longer than 4 bytes.") + try: + return int.from_bytes(tag_text.ljust(4, " ").encode("ascii"), byteorder="big") + except UnicodeEncodeError: + fail(f"{kind} '{tag_text}' is not ASCII.") + + +def add_set_option(ops: bytearray, set_type: int, modifier: str, value: str, parser) -> None: + if modifier == "": + emit_set_clear(ops, set_type) + + if value == "*": + if modifier == "-": + emit_set_clear(ops, set_type) + else: + emit_set_clear(ops, set_type) + emit_set_invert(ops, set_type) + return + + ranges = parser(value) + if ranges: + emit_set_ranges( + ops, + HB_FUZZING_OP_SET_DEL_RANGES if modifier == "-" else HB_FUZZING_OP_SET_ADD_RANGES, + set_type, + ranges, + ) + + +def parse_output_arg(argv: list[str], usage_text: str) -> tuple[str, list[str]]: + out = "" + args = argv[1:] + + while args: + arg = args[0] + if arg == "-o": + if len(args) < 2: + print(usage_text, end="", file=sys.stderr) + raise SystemExit(1) + out = args[1] + args = args[2:] + continue + if arg in ("-h", "--help"): + print(usage_text, end="") + raise SystemExit(0) + if arg == "--": + args = args[1:] + break + break + + return out, args + + +def read_text_file(path: str) -> list[int]: + with open(path, "r", encoding="utf-8") as fp: + return parse_text_codepoints(fp.read()) + + +def read_range_file(path: str, parser) -> list[tuple[int, int]]: + ranges = [] + with open(path, "r", encoding="utf-8") as fp: + for line in fp: + line = line.split("#", 1)[0].strip() + if line: + ranges.extend(parser(line)) + return ranges + + +def resolve_seed_output_path(font_path: str, font_bytes: bytes, out_path: str, default_suffix: str) -> str: + digest = hashlib.sha1(font_bytes).hexdigest() + default_name = digest + default_suffix + default_dir = os.path.join(os.path.dirname(__file__), "fonts") + + if not out_path: + return os.path.abspath(os.path.join(default_dir, default_name)) + + if out_path.endswith(os.sep): + return os.path.abspath(os.path.join(out_path, default_name)) + + abs_out_path = os.path.abspath(out_path) + if os.path.isdir(abs_out_path): + return os.path.join(abs_out_path, default_name) + + return abs_out_path + + +def write_seed_file(font_file: str, out_path: str, ops: bytearray, default_suffix: str = ".susbset-seed") -> str: + font_path = os.path.abspath(font_file) + if not os.path.isfile(font_path): + fail(f"Font file not found: '{font_path}'.") + + with open(font_path, "rb") as fp: + font_bytes = fp.read() + + seed_path = resolve_seed_output_path(font_path, font_bytes, out_path, default_suffix) + parent = os.path.dirname(seed_path) + if parent: + os.makedirs(parent, exist_ok=True) + + with open(seed_path, "wb") as fp: + fp.write(font_bytes) + fp.write(ops) + fp.write(pack_u32(len(ops))) + fp.write(HB_FUZZING_EXTENDED_MAGIC) + + return seed_path + + +def print_recorded_seed_path(seed_path: str) -> None: + print(os.path.basename(seed_path)) + + +def main_with_user_errors(main_fn, argv: list[str]) -> int: + try: + return main_fn(argv) + except UserError as e: + print(e, file=sys.stderr) + return 1 diff --git a/test/fuzzing/record-subset-test.py b/test/fuzzing/record-subset-test.py new file mode 100755 index 000000000..0c59a0de5 --- /dev/null +++ b/test/fuzzing/record-subset-test.py @@ -0,0 +1,338 @@ +#!/usr/bin/env python3 + +import math +import os +import sys + +from hb_fuzzing_tools import ( + FLAG_BITS, + HB_FUZZING_AXIS_PIN_TO_DEFAULT, + HB_FUZZING_AXIS_SET_RANGE, + HB_FUZZING_OP_SET_ADD_RANGES, + HB_FUZZING_OP_TEXT_ADD, + HB_FUZZING_OP_TEXT_DEL, + HB_SUBSET_SETS_DROP_TABLE_TAG, + HB_SUBSET_SETS_GLYPH_INDEX, + HB_SUBSET_SETS_LAYOUT_FEATURE_TAG, + HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG, + HB_SUBSET_SETS_NAME_ID, + HB_SUBSET_SETS_NAME_LANG_ID, + HB_SUBSET_SETS_UNICODE, + add_set_option, + emit_axis_records, + emit_keep_everything, + emit_pin_all_axes_to_default, + emit_set_flags, + emit_set_ranges, + emit_text, + fail, + main_with_user_errors, + parse_decimal_ranges, + parse_hex_ranges, + parse_output_arg, + parse_tag_value, + parse_tags, + parse_text_codepoints, + print_recorded_seed_path, + read_range_file, + read_text_file, + write_seed_file, +) + + +KEEP_EVERYTHING_FLAGS = ( + 0x00000040 | + 0x00000080 | + 0x00000008 | + 0x00000100 | + 0x00000020 +) + +SET_OPTION_TYPES = { + "--gids": HB_SUBSET_SETS_GLYPH_INDEX, + "--name-IDs": HB_SUBSET_SETS_NAME_ID, + "--name-languages": HB_SUBSET_SETS_NAME_LANG_ID, + "--layout-features": HB_SUBSET_SETS_LAYOUT_FEATURE_TAG, + "--layout-scripts": HB_SUBSET_SETS_LAYOUT_SCRIPT_TAG, + "--drop-tables": HB_SUBSET_SETS_DROP_TABLE_TAG, + "--unicodes": HB_SUBSET_SETS_UNICODE, +} + +VALUE_OPTIONS_TO_IGNORE = { + "-o", + "--output-file", + "--num-iterations", +} + +IGNORED_OPTIONS = { + "--batch", + "--preprocess", +} + + +def usage(prog: str) -> str: + return f"""Usage: {prog} [-o OUTPUT_OR_DIR] HB_SUBSET_COMMAND [HB_SUBSET_ARGS...] + +Generate an hb-subset-fuzzer seed by appending a versioned operation trailer to +the input font bytes. Existing hb-subset-fuzzer corpus files remain valid; this +script always emits the newer extended encoding. + +Supported hb-subset inputs: + - font file via positional argument or --font-file=FILE + - boolean subset flags + - --keep-everything + - --text / --text-file and additive/subtractive forms + - --unicodes / --unicodes-file and additive/subtractive forms + - --gids / --gids-file + - --name-IDs + - --name-languages + - --layout-features + - --layout-scripts + - --drop-tables + - --instance / --variations with fixed values, ranges, and drop/default pins + +Still unsupported: + - --glyphs / --glyphs-file + - --gid-map + +Options: + -o OUTPUT_OR_DIR + Write the seed to this file, or into this directory using + .susbset-seed. + Default: test/fuzzing/fonts/.susbset-seed + -h, --help Show this help. +""" + + +def parse_axis_number(text: str) -> float: + try: + return float(text) + except ValueError: + fail(f"Failed parsing axis value '{text}'.") + + +def parse_axis_range(value: str) -> tuple[int, float, float, float]: + if value == "drop": + return HB_FUZZING_AXIS_PIN_TO_DEFAULT, 0.0, 0.0, 0.0 + + parts = value.split(":") + if len(parts) == 1: + v = parse_axis_number(parts[0]) + return HB_FUZZING_AXIS_SET_RANGE, v, v, v + if len(parts) == 2: + minimum = math.nan if parts[0] == "" else parse_axis_number(parts[0]) + maximum = math.nan if parts[1] == "" else parse_axis_number(parts[1]) + return HB_FUZZING_AXIS_SET_RANGE, minimum, math.nan, maximum + if len(parts) == 3: + minimum = math.nan if parts[0] == "" else parse_axis_number(parts[0]) + middle = math.nan if parts[1] == "" else parse_axis_number(parts[1]) + maximum = math.nan if parts[2] == "" else parse_axis_number(parts[2]) + return HB_FUZZING_AXIS_SET_RANGE, minimum, middle, maximum + fail(f"Failed parsing axis range '{value}'.") + + +def parse_variations(value: str) -> tuple[bool, list[tuple[int, int, float, float, float]]]: + pin_all = False + records: list[tuple[int, int, float, float, float]] = [] + for item in value.replace(" ", ",").split(","): + if not item: + continue + if "=" not in item: + fail(f"Unsupported variation item '{item}'. Expected tag=value.") + tag_text, axis_value_text = item.split("=", 1) + if tag_text == "*": + if axis_value_text != "drop": + fail("Only *=drop is supported for wildcard axis pinning.") + pin_all = True + continue + tag = parse_tag_value(tag_text, "Axis tag") + mode, minimum, middle, maximum = parse_axis_range(axis_value_text) + records.append((tag, mode, minimum, middle, maximum)) + return pin_all, records + + +def parser_for_set_option(base_name: str): + if base_name == "--unicodes": + return parse_hex_ranges + if base_name in ("--layout-features", "--layout-scripts", "--drop-tables"): + return parse_tags + return parse_decimal_ranges + + +def handle_file_backed_set_option(ops: bytearray, name: str, path: str) -> None: + if name == "--gids-file": + emit_set_ranges(ops, HB_FUZZING_OP_SET_ADD_RANGES, HB_SUBSET_SETS_GLYPH_INDEX, read_range_file(path, parse_decimal_ranges)) + return + if name == "--unicodes-file": + emit_set_ranges(ops, HB_FUZZING_OP_SET_ADD_RANGES, HB_SUBSET_SETS_UNICODE, read_range_file(path, parse_hex_ranges)) + return + fail(f"Unsupported file-backed option '{name}'.") + + +def record_subset_main(argv: list[str]) -> int: + usage_text = usage(argv[0]) + out, args = parse_output_arg(argv, usage_text) + + if len(args) < 2: + print(usage_text, end="", file=sys.stderr) + return 1 + + hb_subset = args[0] + if "hb-subset" not in os.path.basename(hb_subset): + fail(f"First argument does not look like hb-subset: '{hb_subset}'.") + + font_file = None + flags = 0 + ops = bytearray() + positionals: list[str] = [] + + i = 1 + while i < len(args): + arg = args[i] + + if arg in FLAG_BITS: + flags |= FLAG_BITS[arg] + emit_set_flags(ops, flags) + i += 1 + continue + + if arg == "--keep-everything": + emit_keep_everything(ops) + flags = KEEP_EVERYTHING_FLAGS + i += 1 + continue + + if arg in IGNORED_OPTIONS: + i += 1 + continue + + ignored_value = False + for prefix in VALUE_OPTIONS_TO_IGNORE: + if arg == prefix: + if i + 1 >= len(args): + fail(f"Missing value for '{arg}'.") + i += 2 + ignored_value = True + break + if arg.startswith(prefix + "="): + i += 1 + ignored_value = True + break + if ignored_value: + continue + + if arg == "--font-file": + if i + 1 >= len(args): + fail("Missing value for '--font-file'.") + font_file = args[i + 1] + i += 2 + continue + if arg.startswith("--font-file="): + font_file = arg.split("=", 1)[1] + i += 1 + continue + + if arg in ("--text", "--text+", "--text-"): + if i + 1 >= len(args): + fail(f"Missing value for '{arg}'.") + emit_text(ops, HB_FUZZING_OP_TEXT_DEL if arg.endswith("-") else HB_FUZZING_OP_TEXT_ADD, parse_text_codepoints(args[i + 1])) + i += 2 + continue + if arg.startswith("--text=") or arg.startswith("--text+=") or arg.startswith("--text-="): + name, value = arg.split("=", 1) + emit_text(ops, HB_FUZZING_OP_TEXT_DEL if name.endswith("-") else HB_FUZZING_OP_TEXT_ADD, parse_text_codepoints(value)) + i += 1 + continue + if arg == "--text-file": + if i + 1 >= len(args): + fail("Missing value for '--text-file'.") + emit_text(ops, HB_FUZZING_OP_TEXT_ADD, read_text_file(args[i + 1])) + i += 2 + continue + if arg.startswith("--text-file="): + emit_text(ops, HB_FUZZING_OP_TEXT_ADD, read_text_file(arg.split("=", 1)[1])) + i += 1 + continue + + handled_set_option = False + for base_name, set_type in SET_OPTION_TYPES.items(): + for modifier in ("", "+", "-"): + full_name = base_name + modifier + if arg == full_name: + if i + 1 >= len(args): + fail(f"Missing value for '{arg}'.") + add_set_option(ops, set_type, modifier, args[i + 1], parser_for_set_option(base_name)) + i += 2 + handled_set_option = True + break + if arg.startswith(full_name + "="): + add_set_option(ops, set_type, modifier, arg.split("=", 1)[1], parser_for_set_option(base_name)) + i += 1 + handled_set_option = True + break + if handled_set_option: + break + if handled_set_option: + continue + + if arg == "--gids-file": + if i + 1 >= len(args): + fail("Missing value for '--gids-file'.") + handle_file_backed_set_option(ops, arg, args[i + 1]) + i += 2 + continue + if arg == "--unicodes-file": + if i + 1 >= len(args): + fail("Missing value for '--unicodes-file'.") + handle_file_backed_set_option(ops, arg, args[i + 1]) + i += 2 + continue + if arg.startswith("--gids-file=") or arg.startswith("--unicodes-file="): + name, value = arg.split("=", 1) + handle_file_backed_set_option(ops, name, value) + i += 1 + continue + + if arg in ("--instance", "--variations"): + if i + 1 >= len(args): + fail(f"Missing value for '{arg}'.") + pin_all, records = parse_variations(args[i + 1]) + if pin_all: + emit_pin_all_axes_to_default(ops) + if records: + emit_axis_records(ops, records) + i += 2 + continue + if arg.startswith("--instance=") or arg.startswith("--variations="): + pin_all, records = parse_variations(arg.split("=", 1)[1]) + if pin_all: + emit_pin_all_axes_to_default(ops) + if records: + emit_axis_records(ops, records) + i += 1 + continue + + if arg.startswith("--glyphs") or arg.startswith("--gid-map"): + fail(f"Unsupported hb-subset option '{arg}'.") + + if arg.startswith("-"): + fail(f"Unsupported option '{arg}'.") + + positionals.append(arg) + i += 1 + + if font_file is None: + if not positionals: + fail("No font file found in hb-subset invocation.") + font_file = positionals[0] + positionals = positionals[1:] + + for text_arg in positionals: + emit_text(ops, HB_FUZZING_OP_TEXT_ADD, parse_text_codepoints(text_arg)) + + print_recorded_seed_path(write_seed_file(font_file, out, ops)) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main_with_user_errors(record_subset_main, sys.argv)) From 7672d6bd5219f9d317cb980ef46ecb640f5435ee Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Mar 2026 13:58:38 -0300 Subject: [PATCH 2/7] [fuzzing] Add test for --downgrade-cff --- ...c88210b09001790d1f047f3bb017655d.susbset-seed | Bin 0 -> 4781 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 test/fuzzing/fonts/9d443b16c88210b09001790d1f047f3bb017655d.susbset-seed diff --git a/test/fuzzing/fonts/9d443b16c88210b09001790d1f047f3bb017655d.susbset-seed b/test/fuzzing/fonts/9d443b16c88210b09001790d1f047f3bb017655d.susbset-seed new file mode 100644 index 0000000000000000000000000000000000000000..ed04dce5c4c7120004d16cee71a4233a6bf0419d GIT binary patch literal 4781 zcmb_g3vg7`8UD}RN0LJzml%mggi8n^AOwg!s#wh%l#oCO0TGae0HK&HgpdRXd1tfP zS2p_yn?N98LGEwMbdoRnza>dpA5rN1gWWoOAyF zKmYUm=bZoEEh#H2p|Om}NOE3bVV3u7^J_#bjVQ*jD8Hca*4~6nqA>v?TD53d$%+i= zNz9M=6!;5^R;u>2J8zgy}oHL2BUO+<;=z~^qNvh1LE>I45Jfp5Y5xf#=| ziN=P2AFteOsYr?|1AlA=@cES>#FR@v1HJ=zT4hyj12@pKz&8Mwt1JyW$hZt38;{Oh zZK>LP+;Xm*C=T+b2X*uh&cL$sE#WbjS& zMS|fpody_vvMDZ71Ud9>MUdoY>M7)fLh7Re|9VKj;jv8TK~ZyI;W;~wH+CH(qN zEedyfb9t((dkUz|$@X-oqubp*gJYP&J@SMLOTSpk-7bgQEjzymbEnJQ?LOA5;wdMr zjgxuIrGQ(N{lfFP6O-KzsC0n@;X7WNT+X{S;mK)EOu8K!n(|=Bambzs|7OQ?3O`}- z+5(GsFg#?R$)C4|nub#OKOKYV@bT>0%sY$LM8-Y3LgD@&@unb@w0XLEI`(^Q-uC8M zyxrIB?()8LFONv;!YRAk3%Gs%JRV;D$B#cczvP|B?9fj>fI|&Vr?c^Z!uwhk-hVgm zYKKs7tHb5!thRaETC#aSp6G0AUCfF%{T zTz0RVa$(_Hk=q?}c>j;N?GE0yUg16Rgha{z6_JErZd16u zR^c@^yQk~md?thF!YyDaNyJtw+-=hd(m};&^bM5eby-C5VI=H)f|PKZa}Jp zr&G|x)1PdPc!IV;4MTv*G4)5TR1H2o5D5KwAb8~Dk=Tek7&l>P!Nc)!&iGMhst-Gz zyIjfq{Z(NJLayXdCkN_}IGwxO98m5~j*ovos7bPwYS=#So`4G?tPm znNsL>nnY7*8qK6RG>`70A5#t$(ET_9%V{O8q4l(pwonz-P#x`~R_Y`Nd2xUb(PQ)k z{fb_q-_q~sP5M2(Pv_}V`kcb_6*I6nmcWwP1eV68Np*&`hAM;2Afk6V9d7w%1^e9H z?QyueCv&q~Hn|)gg-_)Oz}b!FLi3~f(8SXAp|zmxL)(v*!%Ygdwizqo?CI)t$zo^i zMjJp=_)M{Z#D0i5s^WfmBDi42j*WzU=$2LV=EH4)Mz`G6)9I4U3V&>0plzTI0&B73 z&$K?k_Z`2Vd)n{eAgLZH+$toFG=Q&&o7?@(E;*gsFN$-j@V2?UUN&t~XI*lP!lA(5 z9s-CXf{?MbU?$jl2!043N_aZ=q)o>$J=pGV!mh(k?~zSKuk0DD0wI-0CQiE)UWFgK z6u#~GSN4{_YJIN5*HSZ^5ADDi_8;i&?CS|Z%2&vb)ui!L6&`!f<6RDybKhO?$lcv@ zupcMd(S%bLk|&870jMw^o%`JU<0?qpl?p*KlbsT7Qt>$McY&LHz;#pg|WN&E@nRmJS zC-J~gPglTW^VoY^gC4sFK8E+6++Gij^?BSJkp@CTaI9@TpV}De>g@~!R`NG|EtC1U zU++1T&%+z8T&Q)1*L``xx2N5||E(!%`UfJ|XahTG1WbeNh zxH5U5b{a1^SpvNAs|7(nw6-0c#{Utvw)9}_pQ!NK{HZ*ArXz^lU=Qqj&ff-}orhY3 zd#Ccq(RL?v3i>*q11UVVw~U9kMXZM^5R&!TQXW2fa`$P4*RSFcc_7$X%zGbLfKU<} zdj}C#U@qTuWI12QzkKyUoGN2vTw|ziA>WkBgJ&YjCcFQBez1BO4ud&7uCZxoA;&)L z^`zrs6O|5ETfwZQg%2m(^Di)P|Ja2dy zea`Uk@aYi?<5{`jHpXFQ4Vnfsx*M1VOSmcnI&7d)I;z9QYj{jlL6>y8nP#)q8VsXm zX4hdx@yw&c62-Cs9X5cj>tuLeV<&XFiIUihI&7x-?3@nc@|I$CIF@Eei8>s2)lbH0 zwsfyfA4RfMt;6w@B{k{rXo{7d(cv*PT{@-13DEzc4$IL0sSb~&>4sZ$IFS|`a&`D7 zN;f!kIEnHN&rlv&X$S2=!PrJysgi1ug)HonIW!+o4%W(wlCl|ahB{jd?z?Fge)Z}v zlPn`VnY0PqRhXMbnb*mdgG*SjK-VLxwnA`LtJaob#tKNZJ57ByUes9d>wHBkf|KF@dwcKb) zyLoYe4!QwX*u!;|VOhw=+9sW?oS&Ql!dT3K&Qen}Hn6>Xe(Tkt>02EY>E<6yP z$76)F>hl=Y=R}HCY3S6drFkxD8sV;qK0^NL3T6ol-@1Z=SId&9lk5ZK^4R!`@Wn_b zqghbIG^|QAephXcRUi$OTg5lC@c94tT`a7t9LLTB~m&nL@~~I9X>t| vrfdjA_nN>YE`h1Ga%(+;+?y9|rDer=AS}*}Hugyl;Zswf_@}9GP1b(_ Date: Wed, 4 Mar 2026 13:59:15 -0300 Subject: [PATCH 3/7] Adjust subset seed recorder output paths --- test/fuzzing/hb_fuzzing_tools.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/test/fuzzing/hb_fuzzing_tools.py b/test/fuzzing/hb_fuzzing_tools.py index e604eef7b..8463dfc29 100644 --- a/test/fuzzing/hb_fuzzing_tools.py +++ b/test/fuzzing/hb_fuzzing_tools.py @@ -255,22 +255,28 @@ def read_range_file(path: str, parser) -> list[tuple[int, int]]: return ranges -def resolve_seed_output_path(font_path: str, font_bytes: bytes, out_path: str, default_suffix: str) -> str: +def resolve_seed_output_path(font_path: str, font_bytes: bytes, out_path: str, default_suffix: str) -> tuple[str, str]: digest = hashlib.sha1(font_bytes).hexdigest() default_name = digest + default_suffix default_dir = os.path.join(os.path.dirname(__file__), "fonts") if not out_path: - return os.path.abspath(os.path.join(default_dir, default_name)) + display_path = os.path.join(default_dir, default_name) + return display_path, os.path.abspath(display_path) if out_path.endswith(os.sep): - return os.path.abspath(os.path.join(out_path, default_name)) + display_path = os.path.join(out_path, default_name) + return display_path, os.path.abspath(display_path) abs_out_path = os.path.abspath(out_path) if os.path.isdir(abs_out_path): - return os.path.join(abs_out_path, default_name) + if os.path.isabs(out_path): + display_path = os.path.join(out_path, default_name) + else: + display_path = os.path.join(out_path, default_name) + return display_path, os.path.join(abs_out_path, default_name) - return abs_out_path + return out_path, abs_out_path def write_seed_file(font_file: str, out_path: str, ops: bytearray, default_suffix: str = ".susbset-seed") -> str: @@ -281,7 +287,7 @@ def write_seed_file(font_file: str, out_path: str, ops: bytearray, default_suffi with open(font_path, "rb") as fp: font_bytes = fp.read() - seed_path = resolve_seed_output_path(font_path, font_bytes, out_path, default_suffix) + display_path, seed_path = resolve_seed_output_path(font_path, font_bytes, out_path, default_suffix) parent = os.path.dirname(seed_path) if parent: os.makedirs(parent, exist_ok=True) @@ -292,11 +298,11 @@ def write_seed_file(font_file: str, out_path: str, ops: bytearray, default_suffi fp.write(pack_u32(len(ops))) fp.write(HB_FUZZING_EXTENDED_MAGIC) - return seed_path + return display_path def print_recorded_seed_path(seed_path: str) -> None: - print(os.path.basename(seed_path)) + print(seed_path) def main_with_user_errors(main_fn, argv: list[str]) -> int: From 6913fd88b85b45f10daf23a172b04aeb7e45d8b4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Mar 2026 14:07:08 -0300 Subject: [PATCH 4/7] [fuzzing] Add --downgrade-cff seed using new facilities --- ...210b09001790d1f047f3bb017655d.susbset-seed | Bin 4781 -> 4913 bytes 1 file changed, 0 insertions(+), 0 deletions(-) diff --git a/test/fuzzing/fonts/9d443b16c88210b09001790d1f047f3bb017655d.susbset-seed b/test/fuzzing/fonts/9d443b16c88210b09001790d1f047f3bb017655d.susbset-seed index ed04dce5c4c7120004d16cee71a4233a6bf0419d..73d33632db88cd1b91dfd391f0e9e24e528ebedb 100644 GIT binary patch delta 141 zcmWN`u?oU45P;#y#nC6opiW*=3LQJtf}@Ctdt)z6sN_m=Y3bw>74!`){L}X@hu`tD z7gDMc0h>7O`Wp%D4u@L}0;s7{enAtkttui*jHSEa8 ma_TIXd6Av!>*HP)*?L==d>5J);blxNfCMGpNM^JBF1tThlrPHw delta 8 Pcmdm}wpMjx%UU4-5gh}b From 638ae0edc876051c3c6d036508257022b0e2cb0a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Mar 2026 14:07:40 -0300 Subject: [PATCH 5/7] Support comment-prefixed fuzzing seeds --- test/fuzzing/hb-draw-fuzzer.cc | 1 + test/fuzzing/hb-fuzzer.hh | 15 +++++++++++++++ test/fuzzing/hb-repacker-fuzzer.cc | 1 + test/fuzzing/hb-set-fuzzer.cc | 1 + test/fuzzing/hb-shape-fuzzer.cc | 1 + test/fuzzing/hb-subset-fuzzer.cc | 1 + test/fuzzing/hb_fuzzing_tools.py | 11 ++++++++++- test/fuzzing/record-subset-test.py | 3 ++- 8 files changed, 32 insertions(+), 2 deletions(-) diff --git a/test/fuzzing/hb-draw-fuzzer.cc b/test/fuzzing/hb-draw-fuzzer.cc index 842f0f8f1..5e98992d9 100644 --- a/test/fuzzing/hb-draw-fuzzer.cc +++ b/test/fuzzing/hb-draw-fuzzer.cc @@ -119,6 +119,7 @@ static void misc_calls_for_gid (hb_face_t *face, hb_font_t *font, hb_set_t *set, extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { + _fuzzing_skip_leading_comment (&data, &size); alloc_state = _fuzzing_alloc_state (data, size); hb_blob_t *blob = hb_blob_create ((const char *) data, size, diff --git a/test/fuzzing/hb-fuzzer.hh b/test/fuzzing/hb-fuzzer.hh index f1c1fdf19..3f3a74909 100644 --- a/test/fuzzing/hb-fuzzer.hh +++ b/test/fuzzing/hb-fuzzer.hh @@ -36,6 +36,21 @@ _fuzzing_alloc_state (const uint8_t *data, size_t size) return size; } +static inline void +_fuzzing_skip_leading_comment (const uint8_t **data, size_t *size) +{ + if (!*size || **data != '$') + return; + + const uint8_t *newline = (const uint8_t *) memchr (*data, '\n', *size); + if (!newline) + return; + + size_t skipped = (size_t) (newline + 1 - *data); + *data += skipped; + *size -= skipped; +} + static const uint8_t _fuzzing_extended_magic[] = {'H', 'B', 'S', 'U', 'B', 'F', 'Z', '2'}; enum _fuzzing_extended_op_t diff --git a/test/fuzzing/hb-repacker-fuzzer.cc b/test/fuzzing/hb-repacker-fuzzer.cc index f7caeee0f..bdcb7d526 100644 --- a/test/fuzzing/hb-repacker-fuzzer.cc +++ b/test/fuzzing/hb-repacker-fuzzer.cc @@ -88,6 +88,7 @@ void add_links_to_objects (hb_subset_serialize_object_t* objects, uint16_t num_o extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { // TODO(garretrieger): move graph validity checks into repacker graph creation. + _fuzzing_skip_leading_comment (&data, &size); alloc_state = _fuzzing_alloc_state (data, size); uint16_t num_objects = 0; diff --git a/test/fuzzing/hb-set-fuzzer.cc b/test/fuzzing/hb-set-fuzzer.cc index 065e54902..8ce93bcba 100644 --- a/test/fuzzing/hb-set-fuzzer.cc +++ b/test/fuzzing/hb-set-fuzzer.cc @@ -38,6 +38,7 @@ static hb_set_t *create_set (const uint32_t *value_array, int count) extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { + _fuzzing_skip_leading_comment (&data, &size); alloc_state = _fuzzing_alloc_state (data, size); if (size < sizeof (instructions_t)) diff --git a/test/fuzzing/hb-shape-fuzzer.cc b/test/fuzzing/hb-shape-fuzzer.cc index b573b6ae5..49cb8cc02 100644 --- a/test/fuzzing/hb-shape-fuzzer.cc +++ b/test/fuzzing/hb-shape-fuzzer.cc @@ -11,6 +11,7 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { + _fuzzing_skip_leading_comment (&data, &size); alloc_state = _fuzzing_alloc_state (data, size); hb_blob_t *blob = hb_blob_create ((const char *)data, size, diff --git a/test/fuzzing/hb-subset-fuzzer.cc b/test/fuzzing/hb-subset-fuzzer.cc index fceeb61c3..03abc94db 100644 --- a/test/fuzzing/hb-subset-fuzzer.cc +++ b/test/fuzzing/hb-subset-fuzzer.cc @@ -345,6 +345,7 @@ try_legacy_input (const uint8_t *data, size_t size) extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { + _fuzzing_skip_leading_comment (&data, &size); alloc_state = _fuzzing_alloc_state (data, size); if (try_extended_input (data, size)) diff --git a/test/fuzzing/hb_fuzzing_tools.py b/test/fuzzing/hb_fuzzing_tools.py index 8463dfc29..4a14fdc7f 100644 --- a/test/fuzzing/hb_fuzzing_tools.py +++ b/test/fuzzing/hb_fuzzing_tools.py @@ -2,6 +2,7 @@ import hashlib import os +import shlex import struct import sys @@ -278,8 +279,15 @@ def resolve_seed_output_path(font_path: str, font_bytes: bytes, out_path: str, d return out_path, abs_out_path +def format_leading_comment(argv: list[str]) -> bytes: + return ("$ " + " ".join(shlex.quote(arg) for arg in argv) + "\n").encode("utf-8") -def write_seed_file(font_file: str, out_path: str, ops: bytearray, default_suffix: str = ".susbset-seed") -> str: + +def write_seed_file(font_file: str, + out_path: str, + ops: bytearray, + default_suffix: str = ".susbset-seed", + leading_comment: bytes = b"") -> str: font_path = os.path.abspath(font_file) if not os.path.isfile(font_path): fail(f"Font file not found: '{font_path}'.") @@ -293,6 +301,7 @@ def write_seed_file(font_file: str, out_path: str, ops: bytearray, default_suffi os.makedirs(parent, exist_ok=True) with open(seed_path, "wb") as fp: + fp.write(leading_comment) fp.write(font_bytes) fp.write(ops) fp.write(pack_u32(len(ops))) diff --git a/test/fuzzing/record-subset-test.py b/test/fuzzing/record-subset-test.py index 0c59a0de5..7a2d57af8 100755 --- a/test/fuzzing/record-subset-test.py +++ b/test/fuzzing/record-subset-test.py @@ -26,6 +26,7 @@ from hb_fuzzing_tools import ( emit_set_ranges, emit_text, fail, + format_leading_comment, main_with_user_errors, parse_decimal_ranges, parse_hex_ranges, @@ -330,7 +331,7 @@ def record_subset_main(argv: list[str]) -> int: for text_arg in positionals: emit_text(ops, HB_FUZZING_OP_TEXT_ADD, parse_text_codepoints(text_arg)) - print_recorded_seed_path(write_seed_file(font_file, out, ops)) + print_recorded_seed_path(write_seed_file(font_file, out, ops, leading_comment=format_leading_comment(args))) return 0 From f111ed9d6775b4751cb94bf6df4b925f2af2a622 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Mar 2026 14:17:00 -0300 Subject: [PATCH 6/7] Add shared shape fuzzer seed support --- test/fuzzing/hb-shape-fuzzer.cc | 310 ++++++++++++++++++++++++++++ test/fuzzing/hb_fuzzing_tools.py | 48 +++++ test/fuzzing/record-shape-fuzzer.py | 71 +++++++ 3 files changed, 429 insertions(+) create mode 100755 test/fuzzing/record-shape-fuzzer.py diff --git a/test/fuzzing/hb-shape-fuzzer.cc b/test/fuzzing/hb-shape-fuzzer.cc index 49cb8cc02..19ef111d5 100644 --- a/test/fuzzing/hb-shape-fuzzer.cc +++ b/test/fuzzing/hb-shape-fuzzer.cc @@ -3,17 +3,327 @@ #include #include +#include #include +#include #define TEST_OT_FACE_NO_MAIN 1 #include "../api/test-ot-face.c" #undef TEST_OT_FACE_NO_MAIN +static bool +apply_extended_shape_ops (hb_face_t *face, + std::vector *text, + std::vector *variations, + const uint8_t *ops, + size_t ops_len) +{ + variations->clear (); + unsigned axis_count = hb_ot_var_get_axis_count (face); + + if (axis_count) + { + std::vector axes (axis_count); + (void) hb_ot_var_get_axis_infos (face, 0, &axis_count, axes.data ()); + + const uint8_t *p = ops; + const uint8_t *end = ops + ops_len; + + while (p < end) + { + uint8_t op; + if (!_fuzzing_read_value (p, end, &op)) + return false; + + switch (op) + { + case HB_FUZZING_OP_TEXT_ADD: + case HB_FUZZING_OP_TEXT_DEL: + { + uint32_t count; + if (!_fuzzing_read_u32_value (p, end, &count)) + return false; + + for (uint32_t i = 0; i < count; i++) + { + uint32_t cp; + if (!_fuzzing_read_u32_value (p, end, &cp)) + return false; + if (op == HB_FUZZING_OP_TEXT_ADD) + text->push_back (cp); + else + text->erase (std::remove (text->begin (), text->end (), cp), text->end ()); + } + break; + } + + case HB_FUZZING_OP_AXIS_PIN_ALL_TO_DEFAULT: + variations->clear (); + break; + + case HB_FUZZING_OP_AXIS_SET: + { + uint32_t count; + if (!_fuzzing_read_u32_value (p, end, &count)) + return false; + + for (uint32_t i = 0; i < count; i++) + { + uint32_t tag; + uint8_t mode; + float minimum, middle, maximum; + if (!_fuzzing_read_u32_value (p, end, &tag) || + !_fuzzing_read_value (p, end, &mode) || + !_fuzzing_read_f32_value (p, end, &minimum) || + !_fuzzing_read_f32_value (p, end, &middle) || + !_fuzzing_read_f32_value (p, end, &maximum)) + return false; + + for (unsigned axis_index = 0; axis_index < axis_count; axis_index++) + { + if (axes[axis_index].tag != tag) + continue; + + float value = axes[axis_index].default_value; + if (mode == HB_FUZZING_AXIS_PIN_TO_DEFAULT) + value = axes[axis_index].default_value; + else if (mode == HB_FUZZING_AXIS_SET_RANGE) + { + if (!std::isnan (middle)) + value = middle; + else if (!std::isnan (minimum)) + value = minimum; + else if (!std::isnan (maximum)) + value = maximum; + } + else + return false; + + bool updated = false; + for (auto &variation : *variations) + if (variation.tag == tag) + { + variation.value = value; + updated = true; + break; + } + if (!updated) + variations->push_back ({tag, value}); + break; + } + } + break; + } + + case HB_FUZZING_OP_SET_FLAGS: + { + uint32_t ignored; + if (!_fuzzing_read_u32_value (p, end, &ignored)) + return false; + break; + } + + case HB_FUZZING_OP_KEEP_EVERYTHING: + break; + + case HB_FUZZING_OP_SET_CLEAR: + case HB_FUZZING_OP_SET_INVERT: + { + uint8_t ignored; + if (!_fuzzing_read_value (p, end, &ignored)) + return false; + break; + } + + case HB_FUZZING_OP_SET_ADD_RANGES: + case HB_FUZZING_OP_SET_DEL_RANGES: + { + uint8_t ignored_set_type; + uint32_t count; + if (!_fuzzing_read_value (p, end, &ignored_set_type) || + !_fuzzing_read_u32_value (p, end, &count)) + return false; + for (uint32_t i = 0; i < count; i++) + { + uint32_t ignored_start, ignored_end; + if (!_fuzzing_read_u32_value (p, end, &ignored_start) || + !_fuzzing_read_u32_value (p, end, &ignored_end)) + return false; + } + break; + } + + default: + return false; + } + } + } + else + { + const uint8_t *p = ops; + const uint8_t *end = ops + ops_len; + while (p < end) + { + uint8_t op; + if (!_fuzzing_read_value (p, end, &op)) + return false; + switch (op) + { + case HB_FUZZING_OP_TEXT_ADD: + case HB_FUZZING_OP_TEXT_DEL: + { + uint32_t count; + if (!_fuzzing_read_u32_value (p, end, &count)) + return false; + for (uint32_t i = 0; i < count; i++) + { + uint32_t cp; + if (!_fuzzing_read_u32_value (p, end, &cp)) + return false; + if (op == HB_FUZZING_OP_TEXT_ADD) + text->push_back (cp); + else + text->erase (std::remove (text->begin (), text->end (), cp), text->end ()); + } + break; + } + case HB_FUZZING_OP_SET_FLAGS: + { + uint32_t ignored; + if (!_fuzzing_read_u32_value (p, end, &ignored)) + return false; + break; + } + case HB_FUZZING_OP_KEEP_EVERYTHING: + case HB_FUZZING_OP_AXIS_PIN_ALL_TO_DEFAULT: + break; + case HB_FUZZING_OP_SET_CLEAR: + case HB_FUZZING_OP_SET_INVERT: + { + uint8_t ignored; + if (!_fuzzing_read_value (p, end, &ignored)) + return false; + break; + } + case HB_FUZZING_OP_SET_ADD_RANGES: + case HB_FUZZING_OP_SET_DEL_RANGES: + { + uint8_t ignored_set_type; + uint32_t count; + if (!_fuzzing_read_value (p, end, &ignored_set_type) || + !_fuzzing_read_u32_value (p, end, &count)) + return false; + for (uint32_t i = 0; i < count; i++) + { + uint32_t ignored_start, ignored_end; + if (!_fuzzing_read_u32_value (p, end, &ignored_start) || + !_fuzzing_read_u32_value (p, end, &ignored_end)) + return false; + } + break; + } + case HB_FUZZING_OP_AXIS_SET: + { + uint32_t count; + if (!_fuzzing_read_u32_value (p, end, &count)) + return false; + for (uint32_t i = 0; i < count; i++) + { + uint32_t ignored_tag; + uint8_t ignored_mode; + float ignored_minimum, ignored_middle, ignored_maximum; + if (!_fuzzing_read_u32_value (p, end, &ignored_tag) || + !_fuzzing_read_value (p, end, &ignored_mode) || + !_fuzzing_read_f32_value (p, end, &ignored_minimum) || + !_fuzzing_read_f32_value (p, end, &ignored_middle) || + !_fuzzing_read_f32_value (p, end, &ignored_maximum)) + return false; + } + break; + } + default: + return false; + } + } + } + + return true; +} + +static bool +try_extended_input (const uint8_t *data, size_t size) +{ + if (size < sizeof (_fuzzing_extended_magic) + 4) + return false; + + size_t magic_offset = size - sizeof (_fuzzing_extended_magic); + if (0 != memcmp (data + magic_offset, _fuzzing_extended_magic, sizeof (_fuzzing_extended_magic))) + return false; + + size_t ops_len_offset = magic_offset - 4; + uint32_t ops_len = _fuzzing_read_u32_le (data + ops_len_offset); + if (ops_len > ops_len_offset) + return false; + + size_t font_len = ops_len_offset - ops_len; + const uint8_t *ops = data + font_len; + + hb_blob_t *blob = hb_blob_create ((const char *) data, font_len, + HB_MEMORY_MODE_READONLY, nullptr, nullptr); + hb_face_t *face = hb_face_create (blob, 0); + hb_font_t *font = hb_font_create (face); + hb_font_set_scale (font, 12, 12); + + std::vector text; + std::vector variations; + if (!apply_extended_shape_ops (face, &text, &variations, ops, ops_len)) + { + hb_font_destroy (font); + hb_face_destroy (face); + hb_blob_destroy (blob); + return true; + } + + hb_font_set_variations (font, + variations.empty () ? nullptr : variations.data (), + variations.size ()); + + { + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_VERIFY)); + hb_buffer_add_utf8 (buffer, "ABCDEXYZ123@_%&)*$!", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape (font, buffer, nullptr, 0); + hb_buffer_destroy (buffer); + } + + uint32_t text32[16] = {0}; + unsigned int len = text.size () > 16 ? 16 : (unsigned int) text.size (); + for (unsigned int i = 0; i < len; i++) + text32[i] = text[i]; + + text32[10] = test_font (font, text32[15]) % 256; + + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf32 (buffer, text32, sizeof (text32) / sizeof (text32[0]), 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape (font, buffer, nullptr, 0); + hb_buffer_destroy (buffer); + + hb_font_destroy (font); + hb_face_destroy (face); + hb_blob_destroy (blob); + return true; +} + extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) { _fuzzing_skip_leading_comment (&data, &size); alloc_state = _fuzzing_alloc_state (data, size); + if (try_extended_input (data, size)) + return 0; + hb_blob_t *blob = hb_blob_create ((const char *)data, size, HB_MEMORY_MODE_READONLY, nullptr, nullptr); hb_face_t *face = hb_face_create (blob, 0); diff --git a/test/fuzzing/hb_fuzzing_tools.py b/test/fuzzing/hb_fuzzing_tools.py index 4a14fdc7f..c5ee83ec5 100644 --- a/test/fuzzing/hb_fuzzing_tools.py +++ b/test/fuzzing/hb_fuzzing_tools.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import hashlib +import math import os import shlex import struct @@ -195,6 +196,53 @@ def parse_tag_value(tag_text: str, kind: str) -> int: fail(f"{kind} '{tag_text}' is not ASCII.") +def parse_axis_number(text: str) -> float: + try: + return float(text) + except ValueError: + fail(f"Failed parsing axis value '{text}'.") + + +def parse_axis_range(value: str) -> tuple[int, float, float, float]: + if value == "drop": + return HB_FUZZING_AXIS_PIN_TO_DEFAULT, 0.0, 0.0, 0.0 + + parts = value.split(":") + if len(parts) == 1: + v = parse_axis_number(parts[0]) + return HB_FUZZING_AXIS_SET_RANGE, v, v, v + if len(parts) == 2: + minimum = math.nan if parts[0] == "" else parse_axis_number(parts[0]) + maximum = math.nan if parts[1] == "" else parse_axis_number(parts[1]) + return HB_FUZZING_AXIS_SET_RANGE, minimum, math.nan, maximum + if len(parts) == 3: + minimum = math.nan if parts[0] == "" else parse_axis_number(parts[0]) + middle = math.nan if parts[1] == "" else parse_axis_number(parts[1]) + maximum = math.nan if parts[2] == "" else parse_axis_number(parts[2]) + return HB_FUZZING_AXIS_SET_RANGE, minimum, middle, maximum + fail(f"Failed parsing axis range '{value}'.") + + +def parse_variations(value: str) -> tuple[bool, list[tuple[int, int, float, float, float]]]: + pin_all = False + records: list[tuple[int, int, float, float, float]] = [] + for item in value.replace(" ", ",").split(","): + if not item: + continue + if "=" not in item: + fail(f"Unsupported variation item '{item}'. Expected tag=value.") + tag_text, axis_value_text = item.split("=", 1) + if tag_text == "*": + if axis_value_text != "drop": + fail("Only *=drop is supported for wildcard axis pinning.") + pin_all = True + continue + tag = parse_tag_value(tag_text, "Axis tag") + mode, minimum, middle, maximum = parse_axis_range(axis_value_text) + records.append((tag, mode, minimum, middle, maximum)) + return pin_all, records + + def add_set_option(ops: bytearray, set_type: int, modifier: str, value: str, parser) -> None: if modifier == "": emit_set_clear(ops, set_type) diff --git a/test/fuzzing/record-shape-fuzzer.py b/test/fuzzing/record-shape-fuzzer.py new file mode 100755 index 000000000..e95cfaf2c --- /dev/null +++ b/test/fuzzing/record-shape-fuzzer.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 + +import os +import sys + +from hb_fuzzing_tools import ( + fail, + format_leading_comment, + main_with_user_errors, + parse_output_arg, + print_recorded_seed_path, + write_seed_file, +) + + +def usage(prog: str) -> str: + return f"""Usage: {prog} [-o OUTPUT_OR_DIR] HB_SHAPE_COMMAND FONT_FILE [HB_SHAPE_ARGS...] + +Generate an hb-shape-fuzzer seed using the legacy payload format: +an optional leading comment line containing the exact hb-shape command, +followed by the raw font bytes. + +This recorder does not encode text, features, or variations into the payload. +Those are preserved only in the leading comment line for human recovery. + +Options: + -o OUTPUT_OR_DIR + Write the seed to this file, or into this directory using + .shape-seed. + Default: test/fuzzing/fonts/.shape-seed + -h, --help Show this help. +""" + + +def find_font_file(args: list[str]) -> str: + if len(args) < 2: + fail("Expected at least HB_SHAPE_COMMAND and FONT_FILE.") + + hb_shape = args[0] + if "hb-shape" not in os.path.basename(hb_shape): + fail(f"First argument does not look like hb-shape: '{hb_shape}'.") + + font_file = args[1] + if font_file.startswith("-"): + fail("Expected FONT_FILE as the second positional argument.") + + return font_file + + +def record_shape_main(argv: list[str]) -> int: + usage_text = usage(argv[0]) + out, args = parse_output_arg(argv, usage_text) + + if len(args) < 2: + print(usage_text, end="", file=sys.stderr) + return 1 + + font_file = find_font_file(args) + seed_path = write_seed_file( + font_file, + out, + bytearray(), + default_suffix=".shape-seed", + leading_comment=format_leading_comment(args), + ) + print_recorded_seed_path(seed_path) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main_with_user_errors(record_shape_main, sys.argv)) From d8d5068341348412e6d09930909914539206be7e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 4 Mar 2026 14:32:34 -0300 Subject: [PATCH 7/7] Fix shape fuzzer build --- test/fuzzing/hb-shape-fuzzer.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/test/fuzzing/hb-shape-fuzzer.cc b/test/fuzzing/hb-shape-fuzzer.cc index 19ef111d5..000329f70 100644 --- a/test/fuzzing/hb-shape-fuzzer.cc +++ b/test/fuzzing/hb-shape-fuzzer.cc @@ -4,6 +4,7 @@ #include #include +#include #include #include