mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-30 10:04:24 +01:00
Add shared shape fuzzer seed support
This commit is contained in:
1 parent
638ae0edc8
commit
f111ed9d67
3 files changed
+429
No files matched your search
@@ -3,17 +3,327 @@
|
||||
#include <hb-ot.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <stdlib.h>
|
||||
#include <vector>
|
||||
|
||||
#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<hb_codepoint_t> *text,
|
||||
std::vector<hb_variation_t> *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<hb_ot_var_axis_info_t> 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<hb_codepoint_t> text;
|
||||
std::vector<hb_variation_t> 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);
|
||||
|
||||
@@ -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)
|
||||
|
||||
Executable
+71
@@ -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
|
||||
<sha1(font)>.shape-seed.
|
||||
Default: test/fuzzing/fonts/<sha1(font)>.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))
|
||||
Reference in new issue
Block a user