From 89ef51691b674711b89458d643718f2f2b7fbda1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 27 Mar 2026 15:18:47 -0600 Subject: [PATCH] [gpu] Add GPU encode fuzzer Encodes up to 16 glyphs per fuzzed font input, exercising draw accumulation, cu2qu, encode, extents, and blob recycling. Co-Authored-By: Claude Opus 4.6 (1M context) --- test/fuzzing/hb-gpu-fuzzer.cc | 46 +++++++++++++++++++++++++++++++++++ test/fuzzing/meson.build | 22 +++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/fuzzing/hb-gpu-fuzzer.cc diff --git a/test/fuzzing/hb-gpu-fuzzer.cc b/test/fuzzing/hb-gpu-fuzzer.cc new file mode 100644 index 000000000..4232ac457 --- /dev/null +++ b/test/fuzzing/hb-gpu-fuzzer.cc @@ -0,0 +1,46 @@ +#include "hb-shape-input.hh" + +#include + +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; + + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + if (!draw) + return 0; + + unsigned glyph_count = hb_face_get_glyph_count (input.face); + unsigned limit = glyph_count > 16 ? 16 : glyph_count; + + volatile unsigned counter = !glyph_count; + + for (unsigned gid = 0; gid < limit; gid++) + { + hb_gpu_draw_reset (draw); + hb_gpu_draw_glyph (draw, input.font, gid); + + hb_glyph_extents_t extents; + hb_gpu_draw_get_extents (draw, &extents); + counter += extents.width; + + hb_blob_t *blob = hb_gpu_draw_encode (draw); + if (blob) + { + unsigned length = 0; + const char *blob_data = hb_blob_get_data (blob, &length); + counter += length; + if (blob_data && length) + counter += (unsigned char) blob_data[0]; + hb_gpu_draw_recycle_blob (draw, blob); + } + } + + hb_gpu_draw_destroy (draw); + return counter ? 0 : 0; +} diff --git a/test/fuzzing/meson.build b/test/fuzzing/meson.build index 2ed5f4ff5..6877cf230 100644 --- a/test/fuzzing/meson.build +++ b/test/fuzzing/meson.build @@ -3,6 +3,7 @@ tests = [ 'hb-subset-fuzzer.cc', 'hb-raster-fuzzer.cc', 'hb-vector-fuzzer.cc', + 'hb-gpu-fuzzer.cc', 'hb-repacker-fuzzer.cc', ] @@ -37,6 +38,10 @@ foreach file_name : tests continue endif + if test_name.contains('gpu') and get_option('gpu').disabled() + continue + endif + link_with_libs = [libharfbuzz] if test_name.contains('subset') or test_name.contains('repacker') link_with_libs += [libharfbuzz_subset] @@ -47,6 +52,9 @@ foreach file_name : tests if test_name.contains('vector') link_with_libs += [libharfbuzz_vector] endif + if test_name.contains('gpu') + link_with_libs += [libharfbuzz_gpu] + endif exe = executable(test_name, sources, cpp_args: cpp_args + extra_cpp_args, @@ -125,6 +133,20 @@ if not get_option('vector').disabled() endforeach endif +if not get_option('gpu').disabled() + i = 0 + foreach chunk : fonts_glob_chunks + test('gpu-fuzzer-chunk-@0@'.format(i), + hb_gpu_fuzzer_exe, + args: chunk, + workdir: meson.current_build_dir() / '..' / '..', + protocol: 'tap', + suite: ['fuzzing'], + ) + i += 1 + endforeach +endif + if not get_option('subset').disabled() i = 0 foreach chunk : fonts_glob_chunks + subset_fonts_glob_chunks