[paint] Bound sweep-gradient tile emission (#6157)

* [paint] Bound sweep-gradient tile emission

hb_paint_sweep_gradient_tiles() walked to the first repeat that can
reach angle 0 by subtracting the color-line span one repeat at a
time, and bounded the repeat loop only by an absolute repeat index
of 1000.  A repeat/reflect color line whose stops span a tiny angle
then emits repeats-times-stops patches: the ClusterFuzz font drives
27648 stops over ~0.0008 rad, producing ~140M emit_patch callbacks
and gigabytes of Coons-patch mesh in the PDF vector backend, and a
libFuzzer timeout.  The one-span-at-a-time walk could also fail to
terminate outright for angle values reachable through variation
deltas, once the span drops below the float ulp of the angle.

Cap emitted tiles at the new HB_PAINT_MAX_SWEEP_TILES (4096); its
inverse doubles as the angular resolution of the tiling, 2π/4096.
A color line whose period is below that resolution repeats faster
than the tiles can resolve, so instead of tiling it, fill the whole
circle flat with the width-weighted average color of one period --
the band-limit of the pattern.  Compute the starting repeat directly
by division, bailing out on non-finite angles, and guard the inner
loop against clipped-segment spins that unsorted (contract-violating)
stop arrays could otherwise drive.  The absolute-index 1000 cap is
gone: repeat gradients that legitimately need more repeats to cover
the full circle now render completely instead of being silently
truncated.  The helper is also used by the hb-cairo glue, which
picks up the same bounds.

The ClusterFuzz reproducer drops from 27s / 7.8GB RSS locally to
under 0.01s / 8MB, each degenerate gradient collapsing to one flat
patch.  All 96 sweep glyphs of test_glyphs-glyf_colr_1.ttf and 98
sweep glyphs of its variable version at three variation settings
render byte-identically in both SVG and PDF; full test suite passes.

https://oss-fuzz.com/testcase-detail/6490910482038784

Assisted-by: Claude Fable 5 <noreply@anthropic.com>

* [vector] Charge sweep-gradient patches to the paint work budget

The SVG and PDF vector paint backends charge glyph-outline bytes to
the session work budget, but sweep-gradient patch emission appended
mesh/path bytes with no charge.  Even with the per-call tile cap
from the previous commit, a COLR paint graph can invoke many sweep
gradients, multiplying the per-gradient patch data by the
paint-graph traversal limits.

Charge the bytes appended by each emitted patch to the session
budget and skip patch generation once the budget is spent, matching
the existing outline-extraction pattern.

Sweep glyphs of test_glyphs-glyf_colr_1.ttf and its variable
version render byte-identically in both SVG and PDF; full test
suite passes.

Assisted-by: Claude Fable 5 <noreply@anthropic.com>

* [test] Add fuzz seed for sweep-gradient tile timeout

The font drives two sweep gradients with 27648 color stops over an
angular span of ~0.0008 rad with a repeat extend, previously
emitting ~140M Coons patches in the PDF vector backend and timing
out on ClusterFuzz before the preceding two commits.

https://oss-fuzz.com/testcase-detail/6490910482038784

Assisted-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-08-13 12:18:17 -06:00
commit add9dd95f0
6 files changed
+76 -24

No files matched your search

+10 -1
View File
@@ -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
+39 -18
View File
@@ -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<uint8_t> ((double) b + 0.5),
hb_clamp_to<uint8_t> ((double) g + 0.5),
hb_clamp_to<uint8_t> ((double) r + 0.5),
hb_clamp_to<uint8_t> ((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);
+13 -1
View File
@@ -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,
+10 -1
View File
@@ -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),
+4 -3
View File
@@ -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;