diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 4f3320887..3ff0f5ac3 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -120,6 +120,15 @@ #define HB_GPU_DRAW_MAX_CURVES 65536 #endif +/* Tiles emitted by one hb_paint_sweep_gradient_tiles() call. Also + * sets the angular resolution (2π over this) below which a repeating + * color line is filled with its average color instead of tiled; + * a repeat/reflect color line whose stops span a tiny angle could + * otherwise emit millions of patches while covering 0..2π. */ +#ifndef HB_PAINT_MAX_SWEEP_TILES +#define HB_PAINT_MAX_SWEEP_TILES 4096 +#endif + #ifndef HB_SVG_MAX_DOCUMENT_SIZE #define HB_SVG_MAX_DOCUMENT_SIZE ((size_t) 16 << 20) #endif @@ -168,7 +177,7 @@ #endif /* One vector (SVG/PDF) paint session, in bytes of generated outline - * path data. */ + * path and sweep-gradient patch data. */ #ifndef HB_VECTOR_MAX_PAINT_WORK #define HB_VECTOR_MAX_PAINT_WORK ((int64_t) 16 << 20) #endif diff --git a/src/hb-paint.cc b/src/hb-paint.cc index df42637a3..08b00accc 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -1120,34 +1120,53 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops, else { float span = angles[n_stops - 1] - angles[0]; - if (fabsf (span) < 1e-6f) + if (!(fabsf (span) >= 1e-6f)) /* Reversed to catch NaN. */ goto done; - int k = 0; - if (angles[0] >= 0) + span = fabsf (span); + + if (span < HB_2_PI / HB_PAINT_MAX_SWEEP_TILES) { - float ss = angles[0]; - while (ss > 0) + /* The pattern repeats faster than the tile resolution; tiling + * it would cost unbounded work for sub-visual detail. Fill + * the whole circle with the period-average color instead. */ + float r = 0.f, g = 0.f, b = 0.f, a = 0.f; + for (unsigned i = 1; i < n_stops; i++) { - if (span > 0) { ss -= span; k--; } - else { ss += span; k++; } - } - } - else - { - float ee = angles[n_stops - 1]; - while (ee < 0) - { - if (span > 0) { ee += span; k++; } - else { ee -= span; k--; } + float w = (angles[i] - angles[i - 1]) / span * 0.5f; + r += w * (hb_color_get_red (colors[i - 1]) + hb_color_get_red (colors[i])); + g += w * (hb_color_get_green (colors[i - 1]) + hb_color_get_green (colors[i])); + b += w * (hb_color_get_blue (colors[i - 1]) + hb_color_get_blue (colors[i])); + a += w * (hb_color_get_alpha (colors[i - 1]) + hb_color_get_alpha (colors[i])); } + hb_color_t avg = HB_COLOR (hb_clamp_to ((double) b + 0.5), + hb_clamp_to ((double) g + 0.5), + hb_clamp_to ((double) r + 0.5), + hb_clamp_to ((double) a + 0.5)); + emit_patch (0.f, avg, HB_2_PI, avg, user_data); + goto done; } - span = fabsf (span); - for (int l = k; l < 1000; l++) + /* First repeat that can reach angle 0, computed by division; + * counting there one span at a time can take effectively forever, + * or fail to terminate outright once the span drops below the + * float ulp of the angles. One repeat of slack absorbs rounding + * differences; earlier repeats are fully clipped anyway. */ + double dk = floor (-(double) hb_max (angles[0], angles[n_stops - 1]) / (double) span) - 1.; + if (!(dk >= -1.e18 && dk <= 1.e18)) /* Non-finite angles. */ + goto done; + + unsigned iterations = 0, tiles = 0; + for (int64_t l = (int64_t) dk; ; l++) { for (unsigned i = 1; i < n_stops; i++) { + /* Bounds clipped-away segments too; those stay negligible for + * sorted stops but are unbounded if the sort contract is + * violated. */ + if (unlikely (iterations++ >= 256 * HB_PAINT_MAX_SWEEP_TILES)) + goto done; + float a0_l, a1_l; hb_color_t col0, col1; if ((l % 2 != 0) && (extend == HB_PAINT_EXTEND_REFLECT)) @@ -1166,6 +1185,8 @@ hb_paint_sweep_gradient_tiles (hb_color_stop_t *stops, } if (a1_l < 0.f) continue; + if (unlikely (tiles++ >= HB_PAINT_MAX_SWEEP_TILES)) + goto done; if (a0_l < 0.f) { float f = (0.f - a0_l) / (a1_l - a0_l); diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index 5b2f0326b..25d0c2afd 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -1375,6 +1375,7 @@ hb_pdf_add_sweep_patch (hb_vector_buf_t *mesh, /* Callback context + trampoline for hb_paint_sweep_gradient_tiles. */ struct hb_pdf_sweep_ctx_t { + hb_vector_paint_t *paint; hb_vector_buf_t *mesh; hb_vector_buf_t *alpha_mesh; float cx, cy, xlo, xhi, ylo, yhi; @@ -1386,10 +1387,21 @@ hb_pdf_sweep_emit_patch (float a0, hb_color_t c0, void *user_data) { auto *ctx = (hb_pdf_sweep_ctx_t *) user_data; + auto *paint = ctx->paint; + /* Skip patch generation when the session work budget is spent, so + * per-gradient patch counts cannot multiply with the paint-graph + * traversal limits of the font tables driving us. */ + if (unlikely (paint->work_left <= 0)) + return; + unsigned before = ctx->mesh->length + + (ctx->alpha_mesh ? ctx->alpha_mesh->length : 0); hb_pdf_add_sweep_patch (ctx->mesh, ctx->alpha_mesh, ctx->cx, ctx->cy, ctx->xlo, ctx->xhi, ctx->ylo, ctx->yhi, a0, c0, a1, c1); + unsigned after = ctx->mesh->length + + (ctx->alpha_mesh ? ctx->alpha_mesh->length : 0); + paint->work_left -= after - before; } static void @@ -1431,7 +1443,7 @@ hb_pdf_paint_sweep_gradient (hb_paint_funcs_t *, if (needs_alpha) alpha_mesh.alloc (256); - hb_pdf_sweep_ctx_t ctx { &mesh, needs_alpha ? &alpha_mesh : nullptr, + hb_pdf_sweep_ctx_t ctx { paint, &mesh, needs_alpha ? &alpha_mesh : nullptr, scx, scy, xlo, xhi, ylo, yhi }; hb_paint_sweep_gradient_tiles (stops.arrayZ, stops.length, extend, start_angle, end_angle, diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index 336a670cf..c41e4e789 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -246,6 +246,7 @@ hb_vector_svg_add_sweep_patch (hb_vector_buf_t *body, /* Callback context + trampoline for hb_paint_sweep_gradient_tiles. */ struct hb_vector_svg_sweep_ctx_t { + hb_vector_paint_t *paint; hb_vector_buf_t *body; unsigned precision; float cx, cy, radius; @@ -257,10 +258,18 @@ hb_vector_svg_sweep_emit_patch (float a0, hb_color_t c0, void *user_data) { auto *ctx = (hb_vector_svg_sweep_ctx_t *) user_data; + auto *paint = ctx->paint; + /* Skip patch generation when the session work budget is spent, so + * per-gradient patch counts cannot multiply with the paint-graph + * traversal limits of the font tables driving us. */ + if (unlikely (paint->work_left <= 0)) + return; + unsigned before = ctx->body->length; hb_vector_svg_add_sweep_patch (ctx->body, ctx->precision, ctx->cx, ctx->cy, ctx->radius, a0, hb_vector_svg_rgba_from_hb_color (c0), a1, hb_vector_svg_rgba_from_hb_color (c1)); + paint->work_left -= ctx->body->length - before; } @@ -819,7 +828,7 @@ hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, float ga1 = start_angle + mx * (end_angle - start_angle); hb_vector_svg_sweep_ctx_t ctx { - &paint->current_body (), paint->get_precision (), paint->sx (cx), paint->sy (cy), 32767.f + paint, &paint->current_body (), paint->get_precision (), paint->sx (cx), paint->sy (cy), 32767.f }; hb_paint_sweep_gradient_tiles (stops.arrayZ, stops.length, hb_color_line_get_extend (color_line), diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 28b0b62b2..45da57ccb 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -67,9 +67,10 @@ struct hb_vector_paint_t unsigned clip_path_counter = 0; /* Cumulative work budget for the current paint session; reset by * hb_vector_paint_clear(). Charged with the bytes of path data - * generated by each glyph-outline extraction, so per-glyph outline - * limits cannot multiply with the paint-graph traversal limits of - * the font tables driving us (e.g. COLR). */ + * generated by each glyph-outline extraction and of sweep-gradient + * patch data, so per-glyph and per-gradient limits cannot multiply + * with the paint-graph traversal limits of the font tables driving + * us (e.g. COLR). */ int64_t work_left = HB_VECTOR_MAX_PAINT_WORK; hb_vector_path_sink_t clip_path_sink = {nullptr, 0, 1.f, 1.f}; unsigned gradient_counter = 0; diff --git a/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-6490910482038784 b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-6490910482038784 new file mode 100644 index 000000000..4cd993db1 Binary files /dev/null and b/test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-6490910482038784 differ