Coverage Report

Created: 2025-08-29 06:32

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