hb-shape-fuzzer.cc 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "hb-fuzzer.hh"
  2. #include <hb-ot.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #define TEST_OT_FACE_NO_MAIN 1
  6. #include "../api/test-ot-face.c"
  7. #undef TEST_OT_FACE_NO_MAIN
  8. extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
  9. {
  10. alloc_state = _fuzzing_alloc_state (data, size);
  11. hb_blob_t *blob = hb_blob_create ((const char *)data, size,
  12. HB_MEMORY_MODE_READONLY, nullptr, nullptr);
  13. hb_face_t *face = hb_face_create (blob, 0);
  14. hb_font_t *font = hb_font_create (face);
  15. hb_ot_font_set_funcs (font);
  16. hb_font_set_scale (font, 12, 12);
  17. unsigned num_coords = 0;
  18. if (size) num_coords = data[size - 1];
  19. num_coords = hb_ot_var_get_axis_count (face) > num_coords ? num_coords : hb_ot_var_get_axis_count (face);
  20. int *coords = (int *) calloc (num_coords, sizeof (int));
  21. if (size > num_coords + 1)
  22. for (unsigned i = 0; i < num_coords; ++i)
  23. coords[i] = ((int) data[size - num_coords + i - 1] - 128) * 10;
  24. hb_font_set_var_coords_normalized (font, coords, num_coords);
  25. free (coords);
  26. {
  27. const char text[] = "ABCDEXYZ123@_%&)*$!";
  28. hb_buffer_t *buffer = hb_buffer_create ();
  29. hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_VERIFY /* | HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT */));
  30. hb_buffer_add_utf8 (buffer, text, -1, 0, -1);
  31. hb_buffer_guess_segment_properties (buffer);
  32. hb_shape (font, buffer, nullptr, 0);
  33. hb_buffer_destroy (buffer);
  34. }
  35. uint32_t text32[16] = {0};
  36. unsigned int len = sizeof (text32);
  37. if (size < len)
  38. len = size;
  39. if (len)
  40. memcpy (text32, data + size - len, len);
  41. /* Misc calls on font. */
  42. text32[10] = test_font (font, text32[15]) % 256;
  43. hb_buffer_t *buffer = hb_buffer_create ();
  44. // hb_buffer_set_flags (buffer, (hb_buffer_flags_t) (HB_BUFFER_FLAG_VERIFY | HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT));
  45. hb_buffer_add_utf32 (buffer, text32, sizeof (text32) / sizeof (text32[0]), 0, -1);
  46. hb_buffer_guess_segment_properties (buffer);
  47. hb_shape (font, buffer, nullptr, 0);
  48. hb_buffer_destroy (buffer);
  49. hb_font_destroy (font);
  50. hb_face_destroy (face);
  51. hb_blob_destroy (blob);
  52. return 0;
  53. }