diff --git a/src/hb-object.hh b/src/hb-object.hh index 7b34d671f..3fb387978 100644 --- a/src/hb-object.hh +++ b/src/hb-object.hh @@ -272,6 +272,8 @@ static inline void hb_object_make_immutable (const Type *obj) obj->header.writable = false; } template +static inline void hb_object_fini (Type *obj); +template 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 -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 +static inline void hb_object_actually_destroy (Type *obj) +{ hb_object_fini (obj); if (!std::is_trivially_destructible::value) obj->~Type (); +} +template +static inline bool hb_object_destroy (Type *obj) +{ + if (!hb_object_should_destroy (obj)) + return false; + + hb_object_actually_destroy (obj); return true; } template diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index c6a836a5e..ba11965af 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -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); } diff --git a/src/hb-raster-image.cc b/src/hb-raster-image.cc index d5d75f11a..f339c00b7 100644 --- a/src/hb-raster-image.cc +++ b/src/hb-raster-image.cc @@ -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 rgba; + hb_vector_t 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 rgba; if (!rgba.resize (rgba_size)) { png_destroy_read_struct (&png, &info, nullptr); return false; } - hb_vector_t 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 rgba; + hb_vector_t 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 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 rows; if (!rows.resize (extents.height)) { png_destroy_write_struct (&png, &info); diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index a040c52c1..a88e0f044 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -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); } diff --git a/src/hb-raster-paint.hh b/src/hb-raster-paint.hh index d5377259b..1d6bb18a4 100644 --- a/src/hb-raster-paint.hh +++ b/src/hb-raster-paint.hh @@ -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) diff --git a/src/hb-raster-svg-clip.cc b/src/hb-raster-svg-clip.cc index 04eb986b5..edae7504e 100644 --- a/src/hb-raster-svg-clip.cc +++ b/src/hb-raster-svg-clip.cc @@ -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; diff --git a/src/hb-raster-svg-defs.cc b/src/hb-raster-svg-defs.cc index ee6787d37..034144167 100644 --- a/src/hb-raster-svg-defs.cc +++ b/src/hb-raster-svg-defs.cc @@ -54,7 +54,7 @@ hb_svg_defs_t::add_id_mapping (hb_hashmap_t *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; diff --git a/src/hb-raster-svg-fill.cc b/src/hb-raster-svg-fill.cc index 175c48f9c..98991ec50 100644 --- a/src/hb-raster-svg-fill.cc +++ b/src/hb-raster-svg-fill.cc @@ -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); diff --git a/src/hb-raster-svg-gradient.cc b/src/hb-raster-svg-gradient.cc index 36e5be8dc..cf9e51df1 100644 --- a/src/hb-raster-svg-gradient.cc +++ b/src/hb-raster-svg-gradient.cc @@ -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 diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 59748a4b2..262eeb1ab 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -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; diff --git a/src/hb-vector-svg-draw.cc b/src/hb-vector-svg-draw.cc index 54704f1a9..99379e33f 100644 --- a/src/hb-vector-svg-draw.cc +++ b/src/hb-vector-svg-draw.cc @@ -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); } diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index 438710d96..0ab69be72 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -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); } diff --git a/src/hb-vector-svg-subset.cc b/src/hb-vector-svg-subset.cc index 12b7f9581..b71dadde7 100644 --- a/src/hb-vector-svg-subset.cc +++ b/src/hb-vector-svg-subset.cc @@ -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)) diff --git a/src/hb-vector-svg-utils.hh b/src/hb-vector-svg-utils.hh index 3d9cfaa1c..138b51b66 100644 --- a/src/hb-vector-svg-utils.hh +++ b/src/hb-vector-svg-utils.hh @@ -51,7 +51,7 @@ hb_svg_append_len (hb_vector_t *buf, static inline bool hb_svg_append_c (hb_vector_t *buf, char c) { - return !!buf->push (c); + return buf->push_or_fail (c); } static inline void diff --git a/src/hb-vector.hh b/src/hb-vector.hh index e04632a63..71c0c27a1 100644 --- a/src/hb-vector.hh +++ b/src/hb-vector.hh @@ -318,6 +318,12 @@ struct hb_vector_t } template HB_ALWAYS_INLINE_VECTOR_ALLOCS + bool push_or_fail (Args&&... args) + { + return push (std::forward (args)...) != std::addressof (Crap (Type)); + } + template + HB_ALWAYS_INLINE_VECTOR_ALLOCS Type *push_has_room (Args&&... args) { /* Emplace. */ diff --git a/test/fuzzing/README.md b/test/fuzzing/README.md index 8529a0c65..d7e485887 100644 --- a/test/fuzzing/README.md +++ b/test/fuzzing/README.md @@ -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. diff --git a/test/fuzzing/fonts/noto_handwriting-cff2_colr_1.otf b/test/fuzzing/fonts/noto_handwriting-cff2_colr_1.otf new file mode 100644 index 000000000..dfb30588e Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-cff2_colr_1.otf differ diff --git a/test/fuzzing/fonts/noto_handwriting-cff_colr_1.otf b/test/fuzzing/fonts/noto_handwriting-cff_colr_1.otf new file mode 100644 index 000000000..8309d2e27 Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-cff_colr_1.otf differ diff --git a/test/fuzzing/fonts/noto_handwriting-glyf_colr_1.ttf b/test/fuzzing/fonts/noto_handwriting-glyf_colr_1.ttf new file mode 100644 index 000000000..513ea0ae2 Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-glyf_colr_1.ttf differ diff --git a/test/fuzzing/fonts/noto_handwriting-picosvg.ttf b/test/fuzzing/fonts/noto_handwriting-picosvg.ttf new file mode 100644 index 000000000..0057ee88c Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-picosvg.ttf differ diff --git a/test/fuzzing/fonts/noto_handwriting-picosvgz.ttf b/test/fuzzing/fonts/noto_handwriting-picosvgz.ttf new file mode 100644 index 000000000..5c1d529bf Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-picosvgz.ttf differ diff --git a/test/fuzzing/fonts/noto_handwriting-sbix.ttf b/test/fuzzing/fonts/noto_handwriting-sbix.ttf new file mode 100644 index 000000000..a799d8c6e Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-sbix.ttf differ diff --git a/test/fuzzing/fonts/noto_handwriting-untouchedsvg.ttf b/test/fuzzing/fonts/noto_handwriting-untouchedsvg.ttf new file mode 100644 index 000000000..47317d672 Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-untouchedsvg.ttf differ diff --git a/test/fuzzing/fonts/noto_handwriting-untouchedsvgz.ttf b/test/fuzzing/fonts/noto_handwriting-untouchedsvgz.ttf new file mode 100644 index 000000000..c0f3775c8 Binary files /dev/null and b/test/fuzzing/fonts/noto_handwriting-untouchedsvgz.ttf differ diff --git a/test/fuzzing/hb-draw-fuzzer.cc b/test/fuzzing/hb-draw-fuzzer.cc deleted file mode 100644 index 5e98992d9..000000000 --- a/test/fuzzing/hb-draw-fuzzer.cc +++ /dev/null @@ -1,175 +0,0 @@ -#include -#include - -#include - -#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 -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; -} diff --git a/test/fuzzing/hb-raster-fuzzer.cc b/test/fuzzing/hb-raster-fuzzer.cc new file mode 100644 index 000000000..713d5d69b --- /dev/null +++ b/test/fuzzing/hb-raster-fuzzer.cc @@ -0,0 +1,104 @@ +#include "hb-shape-input.hh" + +#include + +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; +} + diff --git a/test/fuzzing/hb-shape-fuzzer.cc b/test/fuzzing/hb-shape-fuzzer.cc index 000329f70..292553fe0 100644 --- a/test/fuzzing/hb-shape-fuzzer.cc +++ b/test/fuzzing/hb-shape-fuzzer.cc @@ -1,375 +1,45 @@ -#include "hb-fuzzer.hh" - -#include -#include - -#include -#include -#include -#include +#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 *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); - 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; } diff --git a/test/fuzzing/hb-shape-input.hh b/test/fuzzing/hb-shape-input.hh new file mode 100644 index 000000000..b342c6534 --- /dev/null +++ b/test/fuzzing/hb-shape-input.hh @@ -0,0 +1,264 @@ +#include "hb-fuzzer.hh" + +#include +#include + +#include +#include +#include +#include + +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 text; + std::vector 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 *text, + std::vector *variations, + const uint8_t *ops, + size_t ops_len) +{ + variations->clear (); + unsigned axis_count = hb_ot_var_get_axis_count (face); + std::vector 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; +} + diff --git a/test/fuzzing/hb-vector-fuzzer.cc b/test/fuzzing/hb-vector-fuzzer.cc new file mode 100644 index 000000000..1fdb38970 --- /dev/null +++ b/test/fuzzing/hb-vector-fuzzer.cc @@ -0,0 +1,108 @@ +#include "hb-shape-input.hh" + +#include + +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; +} + diff --git a/test/fuzzing/meson.build b/test/fuzzing/meson.build index 3e02384db..d5d8f0ea4 100644 --- a/test/fuzzing/meson.build +++ b/test/fuzzing/meson.build @@ -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', diff --git a/test/fuzzing/record-shape-test.py b/test/fuzzing/record-test.py old mode 100755 new mode 100644 similarity index 55% rename from test/fuzzing/record-shape-test.py rename to test/fuzzing/record-test.py index e95cfaf2c..522a37b3f --- a/test/fuzzing/record-shape-test.py +++ b/test/fuzzing/record-test.py @@ -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 - .shape-seed. - Default: test/fuzzing/fonts/.shape-seed + .seed. + Default: test/fuzzing/fonts/.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))