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))