hb-subset-fuzzer.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include "hb-fuzzer.hh"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "hb-subset.h"
  7. static void
  8. trySubset (hb_face_t *face,
  9. const hb_codepoint_t text[],
  10. int text_length,
  11. unsigned flag_bits,
  12. hb_subset_input_t *input)
  13. {
  14. if (!input) return;
  15. hb_subset_input_set_flags (input, (hb_subset_flags_t) flag_bits);
  16. hb_set_t *codepoints = hb_subset_input_unicode_set (input);
  17. for (int i = 0; i < text_length; i++)
  18. hb_set_add (codepoints, text[i]);
  19. hb_face_t *result = hb_subset_or_fail (face, input);
  20. if (result)
  21. {
  22. hb_blob_t *blob = hb_face_reference_blob (result);
  23. unsigned int length;
  24. const char *data = hb_blob_get_data (blob, &length);
  25. // Something not optimizable just to access all the blob data
  26. unsigned int bytes_count = 0;
  27. for (unsigned int i = 0; i < length; ++i)
  28. if (data[i]) ++bytes_count;
  29. assert (bytes_count || !length);
  30. hb_blob_destroy (blob);
  31. }
  32. hb_face_destroy (result);
  33. hb_subset_input_destroy (input);
  34. }
  35. extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
  36. {
  37. alloc_state = _fuzzing_alloc_state (data, size);
  38. hb_blob_t *blob = hb_blob_create ((const char *) data, size,
  39. HB_MEMORY_MODE_READONLY, nullptr, nullptr);
  40. hb_face_t *face = hb_face_create (blob, 0);
  41. /* Just test this API here quickly. */
  42. hb_set_t *output = hb_set_create ();
  43. hb_face_collect_unicodes (face, output);
  44. hb_set_destroy (output);
  45. unsigned flags = HB_SUBSET_FLAGS_DEFAULT;
  46. const hb_codepoint_t text[] =
  47. {
  48. 'A', 'B', 'C', 'D', 'E', 'X', 'Y', 'Z', '1', '2',
  49. '3', '@', '_', '%', '&', ')', '*', '$', '!'
  50. };
  51. hb_subset_input_t *input = hb_subset_input_create_or_fail ();
  52. if (!input)
  53. {
  54. hb_face_destroy (face);
  55. hb_blob_destroy (blob);
  56. return 0;
  57. }
  58. trySubset (face, text, sizeof (text) / sizeof (hb_codepoint_t), flags, input);
  59. unsigned num_axes;
  60. hb_codepoint_t text_from_data[16];
  61. if (size > sizeof (text_from_data) + sizeof (flags) + sizeof(num_axes)) {
  62. hb_subset_input_t *input = hb_subset_input_create_or_fail ();
  63. if (!input)
  64. {
  65. hb_face_destroy (face);
  66. hb_blob_destroy (blob);
  67. return 0;
  68. }
  69. size -= sizeof (text_from_data);
  70. memcpy (text_from_data,
  71. data + size,
  72. sizeof (text_from_data));
  73. size -= sizeof (flags);
  74. memcpy (&flags,
  75. data + size,
  76. sizeof (flags));
  77. size -= sizeof (num_axes);
  78. memcpy (&num_axes,
  79. data + size,
  80. sizeof (num_axes));
  81. if (num_axes > 0 && num_axes < 8 && size > num_axes * (sizeof(hb_tag_t) + sizeof(int)))
  82. {
  83. for (unsigned i = 0; i < num_axes; i++) {
  84. hb_tag_t tag;
  85. int value;
  86. size -= sizeof (tag);
  87. memcpy (&tag,
  88. data + size,
  89. sizeof (tag));
  90. size -= sizeof (value);
  91. memcpy (&value,
  92. data + size,
  93. sizeof (value));
  94. hb_subset_input_pin_axis_location(input,
  95. face,
  96. tag,
  97. (float) value);
  98. }
  99. }
  100. unsigned int text_size = sizeof (text_from_data) / sizeof (hb_codepoint_t);
  101. trySubset (face, text_from_data, text_size, flags, input);
  102. }
  103. hb_face_destroy (face);
  104. hb_blob_destroy (blob);
  105. return 0;
  106. }