/src/harfbuzz/test/fuzzing/hb-raster-fuzzer.cc
Line | Count | Source |
1 | | #include "hb-shape-input.hh" |
2 | | |
3 | | #include <hb-raster.h> |
4 | | |
5 | | extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) |
6 | 21.9k | { |
7 | 21.9k | alloc_state = _fuzzing_alloc_state (data, size); |
8 | | |
9 | 21.9k | _fuzzing_shape_input_t input; |
10 | 21.9k | if (_fuzzing_prepare_shape_input (data, size, 30, 30, &input) == HB_FUZZING_SHAPE_INPUT_MALFORMED) |
11 | 340 | return 0; |
12 | | |
13 | 21.6k | hb_raster_draw_t *draw = hb_raster_draw_create_or_fail (); |
14 | 21.6k | hb_raster_paint_t *paint = hb_raster_paint_create_or_fail (); |
15 | 21.6k | if (!draw || !paint) |
16 | 176 | { |
17 | 176 | hb_raster_draw_destroy (draw); |
18 | 176 | hb_raster_paint_destroy (paint); |
19 | 176 | return 0; |
20 | 176 | } |
21 | 21.4k | hb_raster_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); |
22 | | |
23 | | /* Cap font scale so malicious fonts don't produce huge surfaces. */ |
24 | 21.4k | int x_scale, y_scale; |
25 | 21.4k | hb_font_get_scale (input.font, &x_scale, &y_scale); |
26 | 21.4k | if (x_scale > 1000 || x_scale < -1000) x_scale = 1000; |
27 | 21.4k | if (y_scale > 1000 || y_scale < -1000) y_scale = 1000; |
28 | 21.4k | hb_font_set_scale (input.font, x_scale, y_scale); |
29 | | |
30 | 21.4k | unsigned glyph_count = hb_face_get_glyph_count (input.face); |
31 | 21.4k | unsigned limit = glyph_count > 16 ? 16 : glyph_count; |
32 | | |
33 | 21.4k | volatile unsigned counter = !glyph_count; |
34 | | |
35 | 21.4k | if (limit) |
36 | 19.3k | { |
37 | 19.3k | unsigned gid = input.text.empty () ? 0 : input.text[0] % limit; |
38 | 19.3k | float x = (float) ((gid % 4) * 40); |
39 | 19.3k | float y = (float) ((gid / 4) * 40); |
40 | 19.3k | hb_raster_draw_set_transform (draw, 1.f, 0.f, 0.f, 1.f, x, y); |
41 | 19.3k | hb_raster_draw_glyph (draw, input.font, gid); |
42 | 19.3k | hb_raster_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, x, y); |
43 | | /* The paint budget grows with the area of the surface. Keep the |
44 | | * surface within 256x256, where it is still the flat budget that |
45 | | * bounds the work a font can ask for. Extents that are refused |
46 | | * make for an empty surface, not for ones taken from the glyph |
47 | | * unclamped. */ |
48 | 19.3k | hb_glyph_extents_t extents; |
49 | 19.3k | if (hb_font_get_glyph_extents (input.font, gid, &extents)) |
50 | 11.5k | { |
51 | 11.5k | if (extents.width > 256) extents.width = 256; |
52 | 11.5k | if (extents.width < -256) extents.width = -256; |
53 | 11.5k | if (extents.height > 256) extents.height = 256; |
54 | 11.5k | if (extents.height < -256) extents.height = -256; |
55 | 11.5k | if (!hb_raster_paint_set_glyph_extents (paint, &extents)) |
56 | 5.24k | { |
57 | 5.24k | hb_raster_extents_t empty = {0, 0, 0, 0, 0}; |
58 | 5.24k | hb_raster_paint_set_extents (paint, &empty); |
59 | 5.24k | } |
60 | 11.5k | } |
61 | 19.3k | hb_raster_paint_glyph (paint, input.font, gid); |
62 | | |
63 | 19.3k | hb_raster_image_t *draw_image = hb_raster_draw_render (draw); |
64 | 19.3k | if (draw_image) |
65 | 19.0k | { |
66 | 19.0k | hb_raster_extents_t ext; |
67 | 19.0k | hb_raster_image_get_extents (draw_image, &ext); |
68 | 19.0k | counter += ext.width + ext.height + (unsigned) (ext.x_origin != 0) + (unsigned) (ext.y_origin != 0); |
69 | 19.0k | const uint8_t *buf = hb_raster_image_get_buffer (draw_image); |
70 | 19.0k | if (buf && ext.height && ext.stride) |
71 | 0 | counter += buf[0]; |
72 | 19.0k | hb_raster_draw_recycle_image (draw, draw_image); |
73 | 19.0k | } |
74 | | |
75 | 19.3k | hb_raster_image_t *paint_image = hb_raster_paint_render (paint); |
76 | 19.3k | if (paint_image) |
77 | 11.5k | { |
78 | 11.5k | hb_raster_extents_t ext; |
79 | 11.5k | hb_raster_image_get_extents (paint_image, &ext); |
80 | 11.5k | counter += ext.width + ext.height + (unsigned) (ext.x_origin != 0) + (unsigned) (ext.y_origin != 0); |
81 | 11.5k | const uint8_t *buf = hb_raster_image_get_buffer (paint_image); |
82 | 11.5k | if (buf && ext.height && ext.stride) |
83 | 6.28k | counter += buf[0]; |
84 | 11.5k | hb_raster_paint_recycle_image (paint, paint_image); |
85 | 11.5k | } |
86 | 19.3k | } |
87 | | |
88 | 21.4k | hb_raster_draw_destroy (draw); |
89 | 21.4k | hb_raster_paint_destroy (paint); |
90 | 21.4k | return counter ? 0 : 0; |
91 | 21.6k | } |