Merge pull request #5809 from harfbuzz/fuzz-image

Add raster and vector fuzzers
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-03-09 22:34:29 -06:00
commit 405eceb79a
31 files changed
+635 -572

No files matched your search

+16 -1
View File
@@ -272,6 +272,8 @@ static inline void hb_object_make_immutable (const Type *obj)
obj->header.writable = false;
}
template <typename Type>
static inline void hb_object_fini (Type *obj);
template <typename Type>
static inline Type *hb_object_reference (Type *obj)
{
hb_object_trace (obj, HB_FUNC);
@@ -282,7 +284,7 @@ static inline Type *hb_object_reference (Type *obj)
return obj;
}
template <typename Type>
static inline bool hb_object_destroy (Type *obj)
static inline bool hb_object_should_destroy (Type *obj)
{
hb_object_trace (obj, HB_FUNC);
if (unlikely (!obj || obj->header.is_inert ()))
@@ -290,12 +292,25 @@ static inline bool hb_object_destroy (Type *obj)
assert (hb_object_is_valid (obj));
if (obj->header.ref_count.dec () != 1)
return false;
return true;
}
template <typename Type>
static inline void hb_object_actually_destroy (Type *obj)
{
hb_object_fini (obj);
if (!std::is_trivially_destructible<Type>::value)
obj->~Type ();
}
template <typename Type>
static inline bool hb_object_destroy (Type *obj)
{
if (!hb_object_should_destroy (obj))
return false;
hb_object_actually_destroy (obj);
return true;
}
template <typename Type>
+4 -1
View File
@@ -150,8 +150,11 @@ hb_raster_draw_reference (hb_raster_draw_t *draw)
void
hb_raster_draw_destroy (hb_raster_draw_t *draw)
{
if (!hb_object_destroy (draw)) return;
if (!hb_object_should_destroy (draw))
return;
hb_raster_image_destroy (draw->recycled_image);
hb_object_actually_destroy (draw);
hb_free (draw);
}
+27 -6
View File
@@ -42,6 +42,18 @@ struct hb_raster_png_read_blob_t
size_t offset = 0;
};
static void
hb_raster_png_error (png_structp png,
png_const_charp msg HB_UNUSED)
{
png_longjmp (png, 1);
}
static void
hb_raster_png_warning (png_structp png HB_UNUSED,
png_const_charp msg HB_UNUSED)
{}
static void
hb_raster_png_read_blob (png_structp png, png_bytep out, png_size_t length)
{
@@ -435,7 +447,9 @@ hb_raster_image_t::deserialize_from_png (hb_blob_t *blob)
if (!blob_data || !blob_len)
return false;
png_structp png = png_create_read_struct (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
png_structp png = png_create_read_struct (PNG_LIBPNG_VER_STRING, nullptr,
hb_raster_png_error,
hb_raster_png_warning);
if (!png)
return false;
@@ -450,9 +464,13 @@ hb_raster_image_t::deserialize_from_png (hb_blob_t *blob)
reader.data = blob_data;
reader.size = (size_t) blob_len;
reader.offset = 0;
hb_vector_t<uint8_t> rgba;
hb_vector_t<png_bytep> rows;
if (setjmp (png_jmpbuf (png)))
{
png_destroy_read_struct (&png, &info, nullptr);
rgba.fini ();
rows.fini ();
return false;
}
@@ -510,14 +528,12 @@ hb_raster_image_t::deserialize_from_png (hb_blob_t *blob)
return false;
}
hb_vector_t<uint8_t> rgba;
if (!rgba.resize (rgba_size))
{
png_destroy_read_struct (&png, &info, nullptr);
return false;
}
hb_vector_t<png_bytep> rows;
if (!rows.resize ((unsigned) h))
{
png_destroy_read_struct (&png, &info, nullptr);
@@ -569,7 +585,9 @@ hb_raster_image_t::serialize_to_png_or_fail () const
if (format != HB_RASTER_FORMAT_BGRA32 || !extents.width || !extents.height)
return nullptr;
png_structp png = png_create_write_struct (PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
png_structp png = png_create_write_struct (PNG_LIBPNG_VER_STRING, nullptr,
hb_raster_png_error,
hb_raster_png_warning);
if (!png)
return nullptr;
@@ -581,9 +599,14 @@ hb_raster_image_t::serialize_to_png_or_fail () const
}
hb_raster_png_write_blob_t writer;
hb_vector_t<uint8_t> rgba;
hb_vector_t<png_bytep> rows;
if (setjmp (png_jmpbuf (png)))
{
png_destroy_write_struct (&png, &info);
writer.data.fini ();
rgba.fini ();
rows.fini ();
return nullptr;
}
@@ -596,7 +619,6 @@ hb_raster_image_t::serialize_to_png_or_fail () const
PNG_FILTER_TYPE_DEFAULT);
png_write_info (png, info);
hb_vector_t<uint8_t> rgba;
size_t rowbytes = (size_t) extents.width * 4u;
size_t rgba_size = rowbytes * (size_t) extents.height;
if (extents.height && rgba_size / (size_t) extents.height != rowbytes)
@@ -611,7 +633,6 @@ hb_raster_image_t::serialize_to_png_or_fail () const
return nullptr;
}
hb_vector_t<png_bytep> rows;
if (!rows.resize (extents.height))
{
png_destroy_write_struct (&png, &info);
+29 -9
View File
@@ -110,15 +110,28 @@ ensure_initialized (hb_raster_paint_t *c)
/* Root surface */
hb_raster_image_t *root = c->acquire_surface ();
if (unlikely (!root)) return;
c->surface_stack.push (root);
if (unlikely (!c->surface_stack.push_or_fail (root)))
{
c->release_surface (root);
return;
}
/* Initial transform */
c->transform_stack.push (c->base_transform);
if (unlikely (!c->transform_stack.push_or_fail (c->base_transform)))
{
c->release_surface (c->surface_stack.pop ());
return;
}
/* Initial clip: full coverage rectangle */
hb_raster_clip_t clip;
clip.init_full (c->fixed_extents.width, c->fixed_extents.height);
c->clip_stack.push (std::move (clip));
if (unlikely (!c->clip_stack.push_or_fail (std::move (clip))))
{
c->transform_stack.pop ();
c->release_surface (c->surface_stack.pop ());
return;
}
}
static void
@@ -132,10 +145,11 @@ hb_raster_paint_push_transform (hb_paint_funcs_t *pfuncs HB_UNUSED,
hb_raster_paint_t *c = (hb_raster_paint_t *) paint_data;
ensure_initialized (c);
if (unlikely (!c->transform_stack.length)) return;
hb_transform_t<> t = c->current_transform ();
t.multiply ({xx, yx, xy, yy, dx, dy});
c->transform_stack.push (t);
(void) c->transform_stack.push (t);
}
static void
@@ -168,7 +182,7 @@ hb_raster_paint_push_empty_clip (hb_raster_paint_t *c, unsigned w, unsigned h)
new_clip.rect_x0 = new_clip.rect_y0 = 0;
new_clip.rect_x1 = new_clip.rect_y1 = 0;
new_clip.min_x = new_clip.min_y = new_clip.max_x = new_clip.max_y = 0;
c->clip_stack.push (std::move (new_clip));
(void) c->clip_stack.push (std::move (new_clip));
}
static void
@@ -298,7 +312,8 @@ hb_raster_paint_push_clip_from_emitter (hb_raster_paint_t *c,
}
hb_raster_draw_recycle_image (rdr, mask_img);
c->clip_stack.push (std::move (new_clip));
if (unlikely (!c->clip_stack.push_or_fail (std::move (new_clip))))
hb_raster_paint_push_empty_clip (c, w, h);
}
struct hb_raster_paint_glyph_clip_data_t
@@ -526,7 +541,8 @@ hb_raster_paint_push_clip_rectangle (hb_paint_funcs_t *pfuncs HB_UNUSED,
}
}
c->clip_stack.push (std::move (new_clip));
if (unlikely (!c->clip_stack.push_or_fail (std::move (new_clip))))
hb_raster_paint_push_empty_clip (c, surf->extents.width, surf->extents.height);
}
static void
@@ -550,7 +566,8 @@ hb_raster_paint_push_group (hb_paint_funcs_t *pfuncs HB_UNUSED,
hb_raster_image_t *new_surf = c->acquire_surface ();
if (unlikely (!new_surf)) return;
c->surface_stack.push (new_surf);
if (unlikely (!c->surface_stack.push_or_fail (new_surf)))
c->release_surface (new_surf);
}
static void
@@ -1702,13 +1719,16 @@ hb_raster_paint_reference (hb_raster_paint_t *paint)
void
hb_raster_paint_destroy (hb_raster_paint_t *paint)
{
if (!hb_object_destroy (paint)) return;
if (!hb_object_should_destroy (paint))
return;
hb_map_destroy (paint->custom_palette);
hb_raster_draw_destroy (paint->clip_rdr);
for (auto *s : paint->surface_stack)
hb_raster_image_destroy (s);
for (auto *s : paint->surface_cache)
hb_raster_image_destroy (s);
hb_object_actually_destroy (paint);
hb_free (paint);
}
+2 -1
View File
@@ -140,7 +140,8 @@ struct hb_raster_paint_t
void release_surface (hb_raster_image_t *img)
{
surface_cache.push (img);
if (!surface_cache.push_or_fail (img))
hb_raster_image_destroy (img);
}
hb_raster_clip_t acquire_clip (unsigned w, unsigned h)
+1 -2
View File
@@ -83,8 +83,7 @@ svg_clip_append_shape (hb_svg_clip_collect_context_t *ctx,
clip_shape.has_transform = true;
clip_shape.transform = transform;
}
ctx->defs->clip_shapes.push (clip_shape);
if (likely (!ctx->defs->clip_shapes.in_error ()))
if (likely (ctx->defs->clip_shapes.push_or_fail (clip_shape)))
ctx->clip->shape_count++;
else if (ctx->had_alloc_failure)
*ctx->had_alloc_failure = true;
+1 -1
View File
@@ -54,7 +54,7 @@ hb_svg_defs_t::add_id_mapping (hb_hashmap_t<hb_bytes_t, unsigned> *map,
return false;
hb_memcpy (owned, id.arrayZ, n);
owned[n] = '\0';
if (unlikely (!owned_id_strings.push (owned)))
if (unlikely (!owned_id_strings.push_or_fail (owned)))
{
hb_free (owned);
return false;
+1 -2
View File
@@ -147,8 +147,7 @@ hb_raster_svg_emit_fill (const hb_svg_fill_context_t *ctx,
if (unlikely (!node.visit ((uintptr_t) cur)))
break;
chain.push (cur);
if (unlikely (chain.in_error ()))
if (unlikely (!chain.push_or_fail (cur)))
{
if (fallback_paint.len)
hb_raster_svg_emit_fill (ctx, fallback_paint, fill_opacity, object_bbox, current_color);
+1 -2
View File
@@ -90,8 +90,7 @@ svg_parse_gradient_stop (hb_svg_xml_parser_t &parser,
stop.offset = offset;
stop.color = color;
stop.is_current_color = is_current_color;
grad.stops.push (stop);
return !grad.stops.in_error ();
return grad.stops.push_or_fail (stop);
}
static void
+1 -1
View File
@@ -686,7 +686,7 @@ _serialize_cff1_charstrings (hb_serialize_context_t *c,
(cff1_charstrings[i].tail () != 0x0e && cff1_charstrings[i].tail () != 0x0b))
{
// Append endchar operator
if (unlikely (!cff1_charstrings[i].push (0x0e)))
if (unlikely (!cff1_charstrings[i].push_or_fail (0x0e)))
{
c->pop_discard ();
return false;
+4 -1
View File
@@ -598,9 +598,12 @@ hb_vector_draw_reference (hb_vector_draw_t *draw)
void
hb_vector_draw_destroy (hb_vector_draw_t *draw)
{
if (!hb_object_destroy (draw)) return;
if (!hb_object_should_destroy (draw))
return;
hb_blob_destroy (draw->recycled_blob);
hb_set_destroy (draw->defined_glyphs);
hb_object_actually_destroy (draw);
hb_free (draw);
}
+4 -1
View File
@@ -1515,10 +1515,13 @@ hb_vector_paint_reference (hb_vector_paint_t *paint)
void
hb_vector_paint_destroy (hb_vector_paint_t *paint)
{
if (!hb_object_destroy (paint)) return;
if (!hb_object_should_destroy (paint))
return;
hb_blob_destroy (paint->recycled_blob);
hb_set_destroy (paint->defined_outlines);
hb_set_destroy (paint->defined_clips);
hb_object_actually_destroy (paint);
hb_free (paint);
}
+1 -1
View File
@@ -311,7 +311,7 @@ hb_svg_subset_glyph_image (hb_face_t *face,
if (!chosen_def_marks.arrayZ[i])
{
chosen_def_marks.arrayZ[i] = 1;
if (unlikely (!chosen_defs.push (i)))
if (unlikely (!chosen_defs.push_or_fail (i)))
return false;
if (!hb_svg_collect_refs (svg + e.start, e.end - e.start,
&needed_ids, &needed_ids_set))
+1 -1
View File
@@ -51,7 +51,7 @@ hb_svg_append_len (hb_vector_t<char> *buf,
static inline bool
hb_svg_append_c (hb_vector_t<char> *buf, char c)
{
return !!buf->push (c);
return buf->push_or_fail (c);
}
static inline void
+6
View File
@@ -318,6 +318,12 @@ struct hb_vector_t
}
template <typename... Args>
HB_ALWAYS_INLINE_VECTOR_ALLOCS
bool push_or_fail (Args&&... args)
{
return push (std::forward<Args> (args)...) != std::addressof (Crap (Type));
}
template <typename... Args>
HB_ALWAYS_INLINE_VECTOR_ALLOCS
Type *push_has_room (Args&&... args)
{
/* Emplace. */
+1 -1
View File
@@ -8,7 +8,7 @@ ninja -Cfuzzbuild
Then, run the fuzzer like this:
fuzzbuild/test/fuzzing/hb-{shape,draw,subset,set}-fuzzer [-max_len=2048] [CORPUS_DIR]
fuzzbuild/test/fuzzing/hb-{shape,raster,vector,subset,set}-fuzzer [-max_len=2048] [CORPUS_DIR]
Where max_len specifies the maximal length of font files to handle.
The smaller the faster.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
-175
View File
@@ -1,175 +0,0 @@
#include <assert.h>
#include <stdlib.h>
#include <hb-ot.h>
#include "hb-fuzzer.hh"
struct _draw_data_t
{
unsigned path_len;
float path_start_x;
float path_start_y;
float path_last_x;
float path_last_y;
};
#include <cstdio>
static void
_move_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data_,
hb_draw_state_t *st,
float to_x, float to_y,
void *user_data HB_UNUSED)
{
_draw_data_t *draw_data = (_draw_data_t *) draw_data_;
assert (!st->path_open);
draw_data->path_start_x = draw_data->path_last_x = to_x;
draw_data->path_start_y = draw_data->path_last_y = to_y;
}
static void
_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data_,
hb_draw_state_t *st,
float to_x, float to_y,
void *user_data HB_UNUSED)
{
_draw_data_t *draw_data = (_draw_data_t *) draw_data_;
assert (st->path_open);
++draw_data->path_len;
draw_data->path_last_x = to_x;
draw_data->path_last_y = to_y;
}
static void
_quadratic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data_,
hb_draw_state_t *st,
float control_x HB_UNUSED, float control_y HB_UNUSED,
float to_x, float to_y,
void *user_data HB_UNUSED)
{
_draw_data_t *draw_data = (_draw_data_t *) draw_data_;
assert (st->path_open);
++draw_data->path_len;
draw_data->path_last_x = to_x;
draw_data->path_last_y = to_y;
}
static void
_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data_,
hb_draw_state_t *st,
float control1_x HB_UNUSED, float control1_y HB_UNUSED,
float control2_x HB_UNUSED, float control2_y HB_UNUSED,
float to_x, float to_y,
void *user_data HB_UNUSED)
{
_draw_data_t *draw_data = (_draw_data_t *) draw_data_;
assert (st->path_open);
++draw_data->path_len;
draw_data->path_last_x = to_x;
draw_data->path_last_y = to_y;
}
static void
_close_path (hb_draw_funcs_t *dfuncs HB_UNUSED, void *draw_data_,
hb_draw_state_t *st,
void *user_data HB_UNUSED)
{
_draw_data_t *draw_data = (_draw_data_t *) draw_data_;
assert (st->path_open && draw_data->path_len != 0);
draw_data->path_len = 0;
assert (draw_data->path_start_x == draw_data->path_last_x &&
draw_data->path_start_y == draw_data->path_last_y);
}
/* Similar to test-ot-face.c's #test_font() */
static void misc_calls_for_gid (hb_face_t *face, hb_font_t *font, hb_set_t *set, hb_codepoint_t cp)
{
/* Other gid specific misc calls */
hb_face_collect_variation_unicodes (face, cp, set);
hb_codepoint_t g;
hb_font_get_nominal_glyph (font, cp, &g);
hb_font_get_variation_glyph (font, cp, cp, &g);
hb_font_get_glyph_h_advance (font, cp);
hb_font_get_glyph_v_advance (font, cp);
hb_position_t x, y;
hb_font_get_glyph_h_origin (font, cp, &x, &y);
hb_font_get_glyph_v_origin (font, cp, &x, &y);
hb_font_get_glyph_contour_point (font, cp, 0, &x, &y);
char buf[64];
hb_font_get_glyph_name (font, cp, buf, sizeof (buf));
hb_ot_color_palette_get_name_id (face, cp);
hb_ot_color_palette_color_get_name_id (face, cp);
hb_ot_color_palette_get_flags (face, cp);
hb_ot_color_palette_get_colors (face, cp, 0, nullptr, nullptr);
hb_ot_color_glyph_get_layers (face, cp, 0, nullptr, nullptr);
hb_blob_destroy (hb_ot_color_glyph_reference_svg (face, cp));
hb_blob_destroy (hb_ot_color_glyph_reference_png (font, cp));
hb_ot_layout_get_ligature_carets (font, HB_DIRECTION_LTR, cp, 0, nullptr, nullptr);
hb_ot_math_get_glyph_italics_correction (font, cp);
hb_ot_math_get_glyph_top_accent_attachment (font, cp);
hb_ot_math_is_glyph_extended_shape (face, cp);
hb_ot_math_get_glyph_kerning (font, cp, HB_OT_MATH_KERN_BOTTOM_RIGHT, 0);
hb_ot_math_get_glyph_variants (font, cp, HB_DIRECTION_TTB, 0, nullptr, nullptr);
hb_ot_math_get_glyph_assembly (font, cp, HB_DIRECTION_BTT, 0, nullptr, nullptr, nullptr);
}
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,
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_face_t *face = hb_face_create (blob, 0);
hb_font_t *font = hb_font_create (face);
unsigned num_coords = 0;
if (size) num_coords = data[size - 1];
num_coords = hb_ot_var_get_axis_count (face) > num_coords ? num_coords : hb_ot_var_get_axis_count (face);
int *coords = (int *) calloc (num_coords, sizeof (int));
if (size > num_coords + 1)
for (unsigned i = 0; i < num_coords; ++i)
coords[i] = ((int) data[size - num_coords + i - 1] - 128) * 10;
hb_font_set_var_coords_normalized (font, coords, num_coords);
free (coords);
unsigned glyph_count = hb_face_get_glyph_count (face);
glyph_count = glyph_count > 16 ? 16 : glyph_count;
_draw_data_t draw_data = {0, 0, 0, 0, 0};
hb_draw_funcs_t *funcs = hb_draw_funcs_create ();
hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) _move_to, nullptr, nullptr);
hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) _line_to, nullptr, nullptr);
hb_draw_funcs_set_quadratic_to_func (funcs, (hb_draw_quadratic_to_func_t) _quadratic_to, nullptr, nullptr);
hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) _cubic_to, nullptr, nullptr);
hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) _close_path, nullptr, nullptr);
volatile unsigned counter = !glyph_count;
hb_set_t *set = hb_set_create ();
for (unsigned gid = 0; gid < glyph_count; ++gid)
{
hb_font_draw_glyph (font, gid, funcs, &draw_data);
/* Glyph extents also may practices the similar path, call it now that is related */
hb_glyph_extents_t extents;
if (hb_font_get_glyph_extents (font, gid, &extents))
counter += !!extents.width + !!extents.height + !!extents.x_bearing + !!extents.y_bearing;
if (!counter) counter += 1;
/* other misc calls */
misc_calls_for_gid (face, font, set, gid);
}
hb_set_destroy (set);
assert (counter);
hb_draw_funcs_destroy (funcs);
hb_font_destroy (font);
hb_face_destroy (face);
hb_blob_destroy (blob);
return 0;
}
+104
View File
@@ -0,0 +1,104 @@
#include "hb-shape-input.hh"
#include <hb-raster.h>
static void
shape_ascii_smoke (hb_font_t *font)
{
hb_buffer_t *buffer = hb_buffer_create ();
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);
}
static void
draw_shaped_text (hb_font_t *font,
hb_buffer_t *buffer,
hb_raster_draw_t *draw,
hb_raster_paint_t *paint)
{
float pen_x = 0.f;
float pen_y = 0.f;
unsigned count = hb_buffer_get_length (buffer);
hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, nullptr);
hb_glyph_position_t *positions = hb_buffer_get_glyph_positions (buffer, nullptr);
for (unsigned i = 0; i < count; i++)
{
float x = pen_x + positions[i].x_offset;
float y = pen_y + positions[i].y_offset;
hb_raster_draw_glyph (draw, font, infos[i].codepoint, x, y);
hb_raster_paint_glyph (paint, font, infos[i].codepoint, x, y, 0, HB_COLOR (0, 0, 0, 255));
pen_x += positions[i].x_advance;
pen_y += positions[i].y_advance;
}
}
extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
{
_fuzzing_skip_leading_comment (&data, &size);
alloc_state = _fuzzing_alloc_state (data, size);
_fuzzing_shape_input_t input;
if (_fuzzing_prepare_shape_input (data, size, 30, 30, &input) == HB_FUZZING_SHAPE_INPUT_MALFORMED)
return 0;
shape_ascii_smoke (input.font);
hb_buffer_t *buffer = hb_buffer_create ();
if (!input.text.empty ())
hb_buffer_add_codepoints (buffer, input.text.data (), input.text.size (), 0, input.text.size ());
hb_buffer_guess_segment_properties (buffer);
hb_shape (input.font, buffer, nullptr, 0);
hb_raster_draw_t *draw = hb_raster_draw_create_or_fail ();
hb_raster_paint_t *paint = hb_raster_paint_create_or_fail ();
hb_raster_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255));
draw_shaped_text (input.font, buffer, draw, paint);
unsigned glyph_count = hb_face_get_glyph_count (input.face);
unsigned limit = glyph_count > 16 ? 16 : glyph_count;
for (unsigned gid = 0; gid < limit; gid++)
{
float x = (float) ((gid % 4) * 40);
float y = (float) ((gid / 4) * 40);
hb_raster_draw_glyph (draw, input.font, gid, x, y);
hb_raster_paint_glyph (paint, input.font, gid, x, y, 0, HB_COLOR (0, 0, 0, 255));
}
volatile unsigned counter = !glyph_count;
hb_raster_image_t *draw_image = hb_raster_draw_render (draw);
if (draw_image)
{
hb_raster_extents_t ext;
hb_raster_image_get_extents (draw_image, &ext);
counter += ext.width + ext.height + (unsigned) (ext.x_origin != 0) + (unsigned) (ext.y_origin != 0);
const uint8_t *buf = hb_raster_image_get_buffer (draw_image);
if (buf && ext.height && ext.stride)
counter += buf[0];
hb_raster_draw_recycle_image (draw, draw_image);
}
hb_raster_image_t *paint_image = hb_raster_paint_render (paint);
if (paint_image)
{
hb_raster_extents_t ext;
hb_raster_image_get_extents (paint_image, &ext);
counter += ext.width + ext.height + (unsigned) (ext.x_origin != 0) + (unsigned) (ext.y_origin != 0);
const uint8_t *buf = hb_raster_image_get_buffer (paint_image);
if (buf && ext.height && ext.stride)
counter += buf[0];
hb_raster_paint_recycle_image (paint, paint_image);
}
hb_raster_draw_destroy (draw);
hb_raster_paint_destroy (paint);
hb_buffer_destroy (buffer);
return counter ? 0 : 0;
}
+18 -348
View File
@@ -1,375 +1,45 @@
#include "hb-fuzzer.hh"
#include <hb-ot.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <stdlib.h>
#include <vector>
#include "hb-shape-input.hh"
#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);
hb_font_t *font = hb_font_create (face);
hb_font_set_scale (font, 12, 12);
unsigned num_coords = 0;
if (size) num_coords = data[size - 1];
num_coords = hb_ot_var_get_axis_count (face) > num_coords ? num_coords : hb_ot_var_get_axis_count (face);
int *coords = (int *) calloc (num_coords, sizeof (int));
if (size > num_coords + 1)
for (unsigned i = 0; i < num_coords; ++i)
coords[i] = ((int) data[size - num_coords + i - 1] - 128) * 10;
hb_font_set_var_coords_normalized (font, coords, num_coords);
free (coords);
_fuzzing_shape_input_t input;
switch (_fuzzing_prepare_shape_input (data, size, 12, 12, &input))
{
case HB_FUZZING_SHAPE_INPUT_MALFORMED:
return 0;
case HB_FUZZING_SHAPE_INPUT_RAW:
case HB_FUZZING_SHAPE_INPUT_EXTENDED:
break;
}
{
const char text[] = "ABCDEXYZ123@_%&)*$!";
hb_buffer_t *buffer = hb_buffer_create ();
hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_VERIFY /* | HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT */));
hb_buffer_add_utf8 (buffer, text, -1, 0, -1);
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_shape (input.font, buffer, nullptr, 0);
hb_buffer_destroy (buffer);
}
uint32_t text32[16] = {0};
unsigned int len = sizeof (text32);
if (size < len)
len = size;
if (len)
memcpy (text32, data + size - len, len);
unsigned int len = input.text.size () > 16 ? 16 : (unsigned int) input.text.size ();
for (unsigned int i = 0; i < len; i++)
text32[i] = input.text[i];
/* Misc calls on font. */
text32[10] = test_font (font, text32[15]) % 256;
text32[10] = test_font (input.font, text32[15]) % 256;
hb_buffer_t *buffer = hb_buffer_create ();
// hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_VERIFY | HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT));
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_shape (input.font, buffer, nullptr, 0);
hb_buffer_destroy (buffer);
hb_font_destroy (font);
hb_face_destroy (face);
hb_blob_destroy (blob);
return 0;
}
+264
View File
@@ -0,0 +1,264 @@
#include "hb-fuzzer.hh"
#include <hb-ot.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <stdlib.h>
#include <vector>
enum _fuzzing_shape_input_status_t
{
HB_FUZZING_SHAPE_INPUT_RAW = 0,
HB_FUZZING_SHAPE_INPUT_EXTENDED = 1,
HB_FUZZING_SHAPE_INPUT_MALFORMED = 2,
};
struct _fuzzing_shape_input_t
{
hb_blob_t *blob = nullptr;
hb_face_t *face = nullptr;
hb_font_t *font = nullptr;
std::vector<hb_codepoint_t> text;
std::vector<hb_variation_t> variations;
~_fuzzing_shape_input_t ()
{
hb_font_destroy (font);
hb_face_destroy (face);
hb_blob_destroy (blob);
}
};
static bool
_fuzzing_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);
std::vector<hb_ot_var_axis_info_t> axes;
if (axis_count)
{
axes.resize (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;
if (!axis_count)
continue;
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;
}
}
return true;
}
static bool
_fuzzing_extract_extended_ops (const uint8_t *data,
size_t size,
size_t *font_len,
const uint8_t **ops,
size_t *ops_len)
{
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 parsed_ops_len = _fuzzing_read_u32_le (data + ops_len_offset);
if (parsed_ops_len > ops_len_offset)
return false;
*font_len = ops_len_offset - parsed_ops_len;
*ops = data + *font_len;
*ops_len = parsed_ops_len;
return true;
}
static _fuzzing_shape_input_status_t
_fuzzing_prepare_shape_input (const uint8_t *data,
size_t size,
int x_scale,
int y_scale,
_fuzzing_shape_input_t *input)
{
size_t font_len = 0;
const uint8_t *ops = nullptr;
size_t ops_len = 0;
bool is_extended = _fuzzing_extract_extended_ops (data, size, &font_len, &ops, &ops_len);
input->blob = hb_blob_create ((const char *) data,
is_extended ? font_len : size,
HB_MEMORY_MODE_READONLY,
nullptr, nullptr);
input->face = hb_face_create (input->blob, 0);
input->font = hb_font_create (input->face);
hb_font_set_scale (input->font, x_scale, y_scale);
if (is_extended)
{
if (!_fuzzing_apply_extended_shape_ops (input->face, &input->text, &input->variations, ops, ops_len))
return HB_FUZZING_SHAPE_INPUT_MALFORMED;
hb_font_set_variations (input->font,
input->variations.empty () ? nullptr : input->variations.data (),
input->variations.size ());
return HB_FUZZING_SHAPE_INPUT_EXTENDED;
}
unsigned num_coords = 0;
if (size)
num_coords = data[size - 1];
num_coords = hb_ot_var_get_axis_count (input->face) > num_coords ? num_coords : hb_ot_var_get_axis_count (input->face);
int *coords = (int *) calloc (num_coords, sizeof (int));
if (size > num_coords + 1)
for (unsigned i = 0; i < num_coords; ++i)
coords[i] = ((int) data[size - num_coords + i - 1] - 128) * 10;
hb_font_set_var_coords_normalized (input->font, coords, num_coords);
free (coords);
uint32_t text32[16] = {0};
unsigned int len = sizeof (text32);
if (size < len)
len = size;
if (len)
memcpy (text32, data + size - len, len);
input->text.assign (text32, text32 + sizeof (text32) / sizeof (text32[0]));
return HB_FUZZING_SHAPE_INPUT_RAW;
}
+108
View File
@@ -0,0 +1,108 @@
#include "hb-shape-input.hh"
#include <hb-vector.h>
static void
shape_ascii_smoke (hb_font_t *font)
{
hb_buffer_t *buffer = hb_buffer_create ();
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);
}
static void
draw_shaped_text (hb_font_t *font,
hb_buffer_t *buffer,
hb_vector_draw_t *draw,
hb_vector_paint_t *paint)
{
float pen_x = 0.f;
float pen_y = 0.f;
unsigned count = hb_buffer_get_length (buffer);
hb_glyph_info_t *infos = hb_buffer_get_glyph_infos (buffer, nullptr);
hb_glyph_position_t *positions = hb_buffer_get_glyph_positions (buffer, nullptr);
for (unsigned i = 0; i < count; i++)
{
float x = pen_x + positions[i].x_offset;
float y = pen_y + positions[i].y_offset;
hb_vector_draw_glyph (draw, font, infos[i].codepoint, x, y, HB_VECTOR_EXTENTS_MODE_EXPAND);
hb_vector_paint_glyph (paint, font, infos[i].codepoint, x, y, HB_VECTOR_EXTENTS_MODE_EXPAND);
pen_x += positions[i].x_advance;
pen_y += positions[i].y_advance;
}
}
extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
{
_fuzzing_skip_leading_comment (&data, &size);
alloc_state = _fuzzing_alloc_state (data, size);
_fuzzing_shape_input_t input;
if (_fuzzing_prepare_shape_input (data, size, 30, 30, &input) == HB_FUZZING_SHAPE_INPUT_MALFORMED)
return 0;
shape_ascii_smoke (input.font);
hb_buffer_t *buffer = hb_buffer_create ();
if (!input.text.empty ())
hb_buffer_add_codepoints (buffer, input.text.data (), input.text.size (), 0, input.text.size ());
hb_buffer_guess_segment_properties (buffer);
hb_shape (input.font, buffer, nullptr, 0);
hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (HB_VECTOR_FORMAT_SVG);
hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (HB_VECTOR_FORMAT_SVG);
hb_vector_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255));
unsigned precision = size ? data[size - 1] % 5 : 0;
hb_vector_svg_set_precision (draw, precision);
hb_vector_svg_paint_set_precision (paint, precision);
hb_vector_svg_set_flat (draw, size > 1 ? !!(data[size - 2] & 1) : false);
hb_vector_svg_paint_set_flat (paint, size > 2 ? !!(data[size - 3] & 1) : false);
draw_shaped_text (input.font, buffer, draw, paint);
unsigned glyph_count = hb_face_get_glyph_count (input.face);
unsigned limit = glyph_count > 16 ? 16 : glyph_count;
for (unsigned gid = 0; gid < limit; gid++)
{
float x = (float) ((gid % 4) * 40);
float y = (float) ((gid / 4) * 40);
hb_vector_draw_glyph (draw, input.font, gid, x, y, HB_VECTOR_EXTENTS_MODE_EXPAND);
hb_vector_paint_glyph (paint, input.font, gid, x, y, HB_VECTOR_EXTENTS_MODE_EXPAND);
}
volatile unsigned counter = !glyph_count;
hb_blob_t *draw_blob = hb_vector_draw_render (draw);
if (draw_blob)
{
unsigned length = 0;
const char *blob_data = hb_blob_get_data (draw_blob, &length);
counter += length;
if (blob_data && length)
counter += (unsigned char) blob_data[0];
hb_vector_draw_recycle_blob (draw, draw_blob);
}
hb_blob_t *paint_blob = hb_vector_paint_render (paint);
if (paint_blob)
{
unsigned length = 0;
const char *blob_data = hb_blob_get_data (paint_blob, &length);
counter += length;
if (blob_data && length)
counter += (unsigned char) blob_data[0];
hb_vector_paint_recycle_blob (paint, paint_blob);
}
hb_vector_draw_destroy (draw);
hb_vector_paint_destroy (paint);
hb_buffer_destroy (buffer);
return counter ? 0 : 0;
}
+22 -3
View File
@@ -2,7 +2,8 @@ tests = [
'hb-shape-fuzzer.cc',
'hb-subset-fuzzer.cc',
'hb-set-fuzzer.cc',
'hb-draw-fuzzer.cc',
'hb-raster-fuzzer.cc',
'hb-vector-fuzzer.cc',
'hb-repacker-fuzzer.cc',
]
@@ -33,6 +34,12 @@ foreach file_name : tests
if not get_option('subset').disabled()
link_with_libs += [libharfbuzz_subset]
endif
if test_name.contains('raster')
link_with_libs += [libharfbuzz_raster]
endif
if test_name.contains('vector')
link_with_libs += [libharfbuzz_vector]
endif
exe = executable(test_name, sources,
cpp_args: cpp_args + extra_cpp_args,
@@ -86,8 +93,20 @@ endforeach
i = 0
foreach chunk : fonts_glob_chunks
test('draw-fuzzer-chunk-@0@'.format(i),
hb_draw_fuzzer_exe,
test('raster-fuzzer-chunk-@0@'.format(i),
hb_raster_fuzzer_exe,
args: chunk,
workdir: meson.current_build_dir() / '..' / '..',
protocol: 'tap',
suite: ['fuzzing'],
)
i += 1
endforeach
i = 0
foreach chunk : fonts_glob_chunks
test('vector-fuzzer-chunk-@0@'.format(i),
hb_vector_fuzzer_exe,
args: chunk,
workdir: meson.current_build_dir() / '..' / '..',
protocol: 'tap',
+19 -15
View File
@@ -13,11 +13,14 @@ from hb_fuzzing_tools import (
)
def usage(prog: str) -> str:
return f"""Usage: {prog} [-o OUTPUT_OR_DIR] HB_SHAPE_COMMAND FONT_FILE [HB_SHAPE_ARGS...]
COMMAND_NAMES = ("hb-shape", "hb-raster", "hb-vector")
Generate an hb-shape-fuzzer seed using the legacy payload format:
an optional leading comment line containing the exact hb-shape command,
def usage(prog: str) -> str:
return f"""Usage: {prog} [-o OUTPUT_OR_DIR] HB_{'{SHAPE|RASTER|VECTOR}'}_COMMAND FONT_FILE [HB_TOOL_ARGS...]
Generate a shared text/render seed using the hb-shape-style payload format:
an optional leading comment line containing the exact CLI invocation,
followed by the raw font bytes.
This recorder does not encode text, features, or variations into the payload.
@@ -26,19 +29,20 @@ 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
<sha1(font)>.seed.
Default: test/fuzzing/fonts/<sha1(font)>.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.")
fail("Expected at least a HarfBuzz text/render 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}'.")
command = args[0]
base = os.path.basename(command)
if not any(name in base for name in COMMAND_NAMES):
fail(f"First argument does not look like one of {', '.join(COMMAND_NAMES)}: '{command}'.")
font_file = args[1]
if font_file.startswith("-"):
@@ -47,20 +51,20 @@ def find_font_file(args: list[str]) -> str:
return font_file
def record_shape_main(argv: list[str]) -> int:
def record_test_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
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",
default_suffix=".seed",
leading_comment=format_leading_comment(args),
)
print_recorded_seed_path(seed_path)
@@ -68,4 +72,4 @@ def record_shape_main(argv: list[str]) -> int:
if __name__ == "__main__":
raise SystemExit(main_with_user_errors(record_shape_main, sys.argv))
raise SystemExit(main_with_user_errors(record_test_main, sys.argv))