mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 15:31:05 +01:00
Previously only HB_VECTOR_FORMAT_SVG was fuzzed. Factor the
create/paint/render body into a helper and call it twice per
input — once for SVG, once for PDF — so the PDF-specific code
paths get the same coverage:
- PDF content-stream encoder
- Type 2/3 shading + stitching function builder
- Type 6 Coons-patch sweep gradient encoder (hb_pdf_add_sweep_patch)
- PNG IDAT passthrough
- Indexed-PNG SMask builder (hb_pdf_build_indexed_smask, zlib inflate
+ PNG un-filter + palette-to-alpha mapping)
Input-fuzzed knobs (precision, flat-draw, flat-paint) are shared
between the two formats.
Smoke-tested on a handful of existing fuzzing/fonts corpus entries;
all pass clean.
Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
88 lines
2.8 KiB
C++
88 lines
2.8 KiB
C++
#include "hb-shape-input.hh"
|
|
|
|
#include <hb-vector.h>
|
|
|
|
static void
|
|
exercise_format (const _fuzzing_shape_input_t *input,
|
|
hb_vector_format_t format,
|
|
unsigned precision,
|
|
bool flat_draw,
|
|
bool flat_paint,
|
|
volatile unsigned *counter)
|
|
{
|
|
hb_vector_draw_t *draw = hb_vector_draw_create_or_fail (format);
|
|
hb_vector_paint_t *paint = hb_vector_paint_create_or_fail (format);
|
|
if (!draw || !paint)
|
|
{
|
|
hb_vector_draw_destroy (draw);
|
|
hb_vector_paint_destroy (paint);
|
|
return;
|
|
}
|
|
hb_vector_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255));
|
|
|
|
hb_vector_draw_set_precision (draw, precision);
|
|
hb_vector_paint_set_precision (paint, precision);
|
|
hb_vector_draw_set_flat (draw, flat_draw);
|
|
hb_vector_paint_set_flat (paint, flat_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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t 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;
|
|
|
|
unsigned precision = size ? data[size - 1] % 5 : 0;
|
|
bool flat_draw = size > 1 && (data[size - 2] & 1);
|
|
bool flat_paint = size > 2 && (data[size - 3] & 1);
|
|
|
|
unsigned glyph_count = hb_face_get_glyph_count (input.face);
|
|
volatile unsigned counter = !glyph_count;
|
|
|
|
/* Exercise both output formats. Each has its own serializer, path
|
|
* encoder, gradient machinery, and (for PDF) indexed-PNG SMask +
|
|
* Type 6 Coons-patch encoder. */
|
|
exercise_format (&input, HB_VECTOR_FORMAT_SVG, precision, flat_draw, flat_paint, &counter);
|
|
exercise_format (&input, HB_VECTOR_FORMAT_PDF, precision, flat_draw, flat_paint, &counter);
|
|
|
|
return counter ? 0 : 0;
|
|
}
|