From f97d6bbb0d59ba1f7a85f97a877d3b4172f96ce2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:54:46 -0600 Subject: [PATCH 1/3] [gpu] Count ignored rectangle clips so their pop does not pop a real clip The GPU paint encoder left push_clip_rectangle at the no-op default while pop_clip unconditionally decremented clip_depth. COLR emits a rectangle push/pop pair around every PaintColrGlyph that has a ClipBox, so that pop removed whatever glyph/path clip sat below it on the stack. When such a PaintColrGlyph is nested inside a PaintGlyph, the sibling layers encoded afterwards carried one clip outline too few, painting ink outside the enclosing outline, or were dropped entirely when they reached an emitter with clip_depth == 0. Register a push_clip_rectangle callback that records the push in a small per-depth counter (rect_clips[d] = rectangles pushed while clip_depth was d). pop_clip consumes a non-zero count at the current depth first and only otherwise pops a real clip. Rectangle clips are still left unencoded, exactly as the v1 design notes state; the counter only keeps the glyph/path clip depth balanced across the ignored pair. This is correct for any LIFO push/pop order without allocation, and changes nothing for paint trees without rectangle clips (the top-level ClipBox in COLR::paint_glyph is already balanced). Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-ZEL64M07. Co-Authored-By: Claude Opus 4.8 --- src/hb-gpu-paint.cc | 28 ++++++++++++++++++++++++++ src/hb-gpu-paint.hh | 8 ++++++++ test/api/test-gpu.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+) diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 0b9503a07..7b72d7c6f 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -104,9 +104,35 @@ hb_gpu_paint_pop_clip (hb_paint_funcs_t *funcs HB_UNUSED, void *user_data HB_UNUSED) { hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; + + /* A rectangle clip pushed at this depth is popped before the + * glyph/path clip below it; its pop leaves clip_depth alone. */ + unsigned &rects = c->rect_clips[hb_min (c->clip_depth, (unsigned) HB_GPU_PAINT_MAX_CLIP_DEPTH)]; + if (rects) + { + rects--; + return; + } + if (likely (c->clip_depth > 0)) c->clip_depth--; } +/* Clip rectangles are not encoded in v1 (see the design notes in + * hb-gpu-paint.hh). Only count the push so that the matching + * pop_clip does not pop the glyph/path clip below it. */ +static void +hb_gpu_paint_push_clip_rectangle (hb_paint_funcs_t *funcs HB_UNUSED, + void *paint_data, + float xmin HB_UNUSED, + float ymin HB_UNUSED, + float xmax HB_UNUSED, + float ymax HB_UNUSED, + void *user_data HB_UNUSED) +{ + hb_gpu_paint_t *c = (hb_gpu_paint_t *) paint_data; + c->rect_clips[hb_min (c->clip_depth, (unsigned) HB_GPU_PAINT_MAX_CLIP_DEPTH)]++; +} + /* Arbitrary-path clip: hand the caller the same hb_gpu_draw * funcs the encoder uses for glyph outlines, with scratch_draw * as the accumulator. push_clip_path_end encodes the captured @@ -1084,6 +1110,7 @@ static struct hb_gpu_paint_funcs_lazy_loader_t hb_paint_funcs_set_push_transform_func (funcs, hb_gpu_paint_push_transform, nullptr, nullptr); hb_paint_funcs_set_pop_transform_func (funcs, hb_gpu_paint_pop_transform, nullptr, nullptr); hb_paint_funcs_set_push_clip_glyph_func (funcs, hb_gpu_paint_push_clip_glyph, nullptr, nullptr); + hb_paint_funcs_set_push_clip_rectangle_func (funcs, hb_gpu_paint_push_clip_rectangle, nullptr, nullptr); hb_paint_funcs_set_push_clip_path_start_func (funcs, hb_gpu_paint_push_clip_path_start, nullptr, nullptr); hb_paint_funcs_set_push_clip_path_end_func (funcs, hb_gpu_paint_push_clip_path_end, nullptr, nullptr); hb_paint_funcs_set_pop_clip_func (funcs, hb_gpu_paint_pop_clip, nullptr, nullptr); @@ -1646,6 +1673,7 @@ hb_gpu_paint_clear (hb_gpu_paint_t *paint) paint->sub_blobs.reset (); paint->sub_bytes = 0; paint->clip_depth = 0; + hb_memset (paint->rect_clips, 0, sizeof (paint->rect_clips)); paint->pending_clip_path = false; paint->unsupported = false; paint->recharge_budget (); diff --git a/src/hb-gpu-paint.hh b/src/hb-gpu-paint.hh index 1a8803d25..1ce180a01 100644 --- a/src/hb-gpu-paint.hh +++ b/src/hb-gpu-paint.hh @@ -325,6 +325,14 @@ struct hb_gpu_paint_t pending_clip_t clip_stack[3]; unsigned clip_depth = 0; + /* Rectangle clips are not encoded (see "Out of scope for v1"), + * but their pop_clip must not pop the glyph/path clip below them. + * rect_clips[d] counts the rectangle clips pushed while clip_depth + * was d; the paint walk pops them before anything at depth d, so + * a non-zero count at the current depth identifies a rectangle + * pop. The last slot collects everything pushed past the cap. */ + unsigned rect_clips[3 + 1] = {}; + /* Carries the cur_transform captured at push_clip_path_start * through to push_clip_path_end, where the encoded clip is * committed to clip_stack. */ diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc index 37cd582ce..88188cc4b 100644 --- a/test/api/test-gpu.cc +++ b/test/api/test-gpu.cc @@ -741,6 +741,53 @@ test_paint_clip_path_depth_overflow (void) hb_gpu_paint_destroy (p); } +static void +test_paint_clip_rectangle_pop_balance (void) +{ + /* Rectangle clips are not encoded, but their pop_clip must not + * pop the glyph clip below them. COLRv1 emits such a pair around + * every PaintColrGlyph that has a ClipBox; when that sits inside a + * PaintGlyph, an unbalanced pop un-clipped (or dropped) the sibling + * layers that followed. */ + hb_face_t *face = hb_test_open_font_file (FONT_FILE); + g_assert_nonnull (face); + hb_font_t *font = hb_font_create (face); + + hb_gpu_paint_t *p = hb_gpu_paint_create_or_fail (); + g_assert_nonnull (p); + hb_gpu_paint_set_scale (p, 1000, 1000); + + hb_paint_funcs_t *pf = hb_gpu_paint_get_funcs (p); + + hb_codepoint_t gid; + g_assert_true (hb_font_get_nominal_glyph (font, 'a', &gid)); + + hb_paint_push_clip_glyph (pf, p, gid, font); + hb_paint_push_clip_rectangle (pf, p, 0.f, 0.f, 100.f, 100.f); + hb_paint_pop_clip (pf, p); + + /* Still inside the glyph clip: this layer must be emitted. */ + hb_paint_color (pf, p, false, HB_COLOR (0, 0, 255, 255)); + + hb_paint_pop_clip (pf, p); + + /* Outside every clip: this layer must be dropped. */ + hb_paint_color (pf, p, false, HB_COLOR (255, 0, 0, 255)); + + hb_blob_t *blob = hb_gpu_paint_encode (p, nullptr); + g_assert_nonnull (blob); + unsigned texel_count; + const hb_gpu_test_texel_t *texels = blob_as_texels (blob, &texel_count); + g_assert_cmpuint (texel_count, >, 0); + /* Header texel 0 .r holds num_ops. */ + g_assert_cmpint (texels[0].r, ==, 1); + hb_blob_destroy (blob); + + hb_gpu_paint_destroy (p); + hb_font_destroy (font); + hb_face_destroy (face); +} + static unsigned repro_color_stops (hb_color_line_t *cl HB_UNUSED, void *cl_data HB_UNUSED, unsigned start, unsigned *count, @@ -1033,6 +1080,7 @@ main (int argc, char **argv) hb_test_add (test_paint_reset); hb_test_add (test_paint_recycle_blob); hb_test_add (test_paint_clip_path_depth_overflow); + hb_test_add (test_paint_clip_rectangle_pop_balance); hb_test_add (test_paint_gradient_overflow_transform); hb_test_add (test_paint_empty_clip); From 462a55492113a79854d8fb638f76f7d147b7dbab Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:55:11 -0600 Subject: [PATCH 2/3] [gpu] Charge each cu2qu subdivision node to the work budget hb_gpu_cubic_to_quadratics() split a cubic up to 10 levels deep (2,047 nodes, each with a fit test) without charging that work to the per-glyph budget and without checking whether the accumulator still accepted output. Once the budget was spent, acc_emit() dropped every curve but left g->success set, so each further cubic was fully subdivided for nothing. A VARC composite of CFF glyphs that shares one budget across many cubic-producing components could make a single hb_gpu_draw_glyph() (or hb_gpu_paint_glyph() clip) call spend seconds of CPU, far past the budget, as a CPU denial of service from a small crafted font. Charge one unit per node and return at the top of the recursion once g->success is false or the budget is exhausted, the same approach the raster flattener already takes. Returning is equivalent to emitting here because acc_emit() discards everything once the budget is negative, so the encoded outline is unchanged for any glyph that stays within budget; the only cost is one extra unit per node, negligible against HB_BUDGET_GLYPH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-LZRUF6D2. Co-Authored-By: Claude Opus 4.8 --- src/hb-gpu-cu2qu.hh | 7 +++++++ test/api/test-gpu.cc | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/hb-gpu-cu2qu.hh b/src/hb-gpu-cu2qu.hh index c98b140a1..7635938fe 100644 --- a/src/hb-gpu-cu2qu.hh +++ b/src/hb-gpu-cu2qu.hh @@ -170,6 +170,13 @@ hb_gpu_cubic_to_quadratics (hb_gpu_draw_t *g, hb_gpu_cu2qu_point_t c3, double tolerance, unsigned depth) { + /* Charge one unit per node for its fit test and split, and stop once + * the accumulator has failed or the work budget is spent: whatever we + * would emit from here is dropped by acc_emit() anyway. */ + if (unlikely (!g->success || + !hb_budget_spend (*g->get_budget_remaining (), HB_BUDGET_1))) + return; + hb_gpu_cu2qu_point_t q1; if (hb_gpu_cubic_approx_quadratic (c0, c1, c2, c3, tolerance, &q1)) { diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc index 88188cc4b..ab2663c07 100644 --- a/test/api/test-gpu.cc +++ b/test/api/test-gpu.cc @@ -250,6 +250,24 @@ test_budget (void) hb_face_destroy (face); } +static void +test_budget_cubic (void) +{ + hb_gpu_draw_t *draw = hb_gpu_draw_create_or_fail (); + hb_draw_funcs_t *draw_funcs = hb_gpu_draw_get_funcs (draw); + hb_draw_state_t draw_state = HB_DRAW_STATE_DEFAULT; + + /* A cubic that fits no quadratic at any depth must not be subdivided + * once the budget is spent; the subdivision itself is charged. */ + g_assert_true (hb_draw_set_budget (draw_funcs, draw, 0)); + hb_draw_move_to (draw_funcs, draw, &draw_state, 0.f, 0.f); + hb_draw_cubic_to (draw_funcs, draw, &draw_state, + 1e9f, 3e8f, 1e9f, -7e8f, 3.f, 3e9f); + g_assert_cmpint (hb_draw_get_budget_remaining (draw_funcs, draw), ==, -1); + + hb_gpu_draw_destroy (draw); +} + static void test_draw_funcs (void) { @@ -1061,6 +1079,7 @@ main (int argc, char **argv) hb_test_add (test_encode_glyph); hb_test_add (test_reset); hb_test_add (test_budget); + hb_test_add (test_budget_cubic); hb_test_add (test_draw_funcs); hb_test_add (test_shader_sources); hb_test_add (test_encode_quantizes_extents_outward); From 4a8f1cd54288f905ffca24f678d1d7703e16122d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:55:57 -0600 Subject: [PATCH 3/3] [gpu] Charge layer clip-outline references to the paint budget The paint encoder memoizes a clip glyph's sub-blob, so layers painted under the same clip stack cost it only a cached index and 24 bytes of op stream each. The fragment shader, however, walks every curve of every clip outline again for every layer in every fragment. Ops, clips per layer and outline size each had a cap, but nothing bounded their product: a small COLRv1 font nesting a few PaintGlyph clips of a dense outline under tens of thousands of PaintSolid layers made the shader do hundreds of millions of curve-loop iterations per fragment, a denial of service on the GPU (or the CPU under a software rasterizer). Charge each layer's reference to each clip sub-blob -- cached or freshly encoded, glyph or path clip -- to the existing session work budget, in encoded RGBA16I texels, which is what the shader's band walk scales with. This lives in emit_all_clip_sub_blobs(), the one path all four layer emitters go through; exhausting the budget marks the paint unsupported like the other encoder limits, so hb_gpu_paint_encode() returns NULL. It adds no new tunable: the budget defaults to HB_BUDGET_GLYPH and hb_paint_set_budget(), including HB_BUDGET_UNLIMITED, governs it as it does the outline work. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FUCZJR1Y. Co-Authored-By: Claude Opus 4.8 --- src/hb-gpu-paint.cc | 15 ++++++++++++++- src/hb-gpu-paint.hh | 3 ++- test/api/test-gpu.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/src/hb-gpu-paint.cc b/src/hb-gpu-paint.cc index 7b72d7c6f..0775d0a67 100644 --- a/src/hb-gpu-paint.cc +++ b/src/hb-gpu-paint.cc @@ -611,7 +611,8 @@ emit_clip_sub_blob (hb_gpu_paint_t *c, /* Emit a sub-blob for every clip currently on the stack and return * their indices in @out (set to -1 for unused slots). Returns * false if any clip's outline is empty (caller should skip the - * layer entirely) or hb_gpu_draw_encode failed. */ + * layer entirely), hb_gpu_draw_encode failed, or the session + * budget is exhausted. */ static bool emit_all_clip_sub_blobs (hb_gpu_paint_t *c, int out[HB_GPU_PAINT_MAX_CLIP_DEPTH]) { @@ -622,6 +623,18 @@ emit_all_clip_sub_blobs (hb_gpu_paint_t *c, int out[HB_GPU_PAINT_MAX_CLIP_DEPTH] out[i] = emit_clip_sub_blob (c, c->clip_stack[i]); if (out[i] < 0) return false; + + /* The memoized clip outline is encoded once however many layers + * share it, but the fragment shader walks its band tables again + * for every layer painted under it. Charge each reference to the + * session budget, in RGBA16I texels, so the layer count cannot + * multiply with the clip size past it. */ + unsigned texels = hb_blob_get_length (c->sub_blobs.arrayZ[out[i]]) / 8; + if (unlikely (!hb_budget_spend (c->budget_remaining, HB_BUDGET_1, texels))) + { + c->unsupported = true; + return false; + } } return true; } diff --git a/src/hb-gpu-paint.hh b/src/hb-gpu-paint.hh index 1ce180a01..f66df0d84 100644 --- a/src/hb-gpu-paint.hh +++ b/src/hb-gpu-paint.hh @@ -287,7 +287,8 @@ struct hb_gpu_paint_t /* Work budget for the current paint session; reset by * hb_gpu_paint_clear(). All the session's work is outline-derived - * (outline traversal and the curves generated from it -- the GPU + * (outline traversal, the curves generated from it, and the clip + * outline texels the fragment shader walks per layer -- the GPU * backend never rasterizes), so a single counter suffices. It is the * public draw/paint budget: seeded into the scratch encoder through the * public draw-budget API before each glyph and read back afterwards. */ diff --git a/test/api/test-gpu.cc b/test/api/test-gpu.cc index ab2663c07..ff989eb87 100644 --- a/test/api/test-gpu.cc +++ b/test/api/test-gpu.cc @@ -806,6 +806,49 @@ test_paint_clip_rectangle_pop_balance (void) hb_face_destroy (face); } +static void +test_paint_layers_under_shared_clip (void) +{ + /* Layers painted under an already-encoded clip cost the encoder a + * cached index and 24 bytes of op stream each, but the fragment + * shader walks every clip outline again for every layer in every + * fragment. A COLRv1 font can paint tens of thousands of layers + * under a few nested PaintGlyph clips; the encoder must bound the + * product of layers and clip size, not just each factor. */ + hb_face_t *face = hb_test_open_font_file (FONT_FILE); + hb_font_t *font = hb_font_create (face); + hb_codepoint_t gid; + g_assert_true (hb_font_get_nominal_glyph (font, 'a', &gid)); + + hb_gpu_paint_t *paint = hb_gpu_paint_create_or_fail (); + hb_paint_funcs_t *paint_funcs = hb_gpu_paint_get_funcs (paint); + + /* A modest number of layers under three nested clips encodes. */ + for (unsigned i = 0; i < 3; i++) + hb_paint_push_clip_glyph (paint_funcs, paint, gid, font); + for (unsigned i = 0; i < 100; i++) + hb_paint_color (paint_funcs, paint, false, HB_COLOR (0, 0, 0, 255)); + for (unsigned i = 0; i < 3; i++) + hb_paint_pop_clip (paint_funcs, paint); + hb_blob_t *blob = hb_gpu_paint_encode (paint, nullptr); + g_assert_nonnull (blob); + g_assert_cmpuint (hb_blob_get_length (blob), >, 0); + hb_blob_destroy (blob); + + /* As many layers as the op count allows does not. */ + for (unsigned i = 0; i < 3; i++) + hb_paint_push_clip_glyph (paint_funcs, paint, gid, font); + for (unsigned i = 0; i < 0x7fff; i++) + hb_paint_color (paint_funcs, paint, false, HB_COLOR (0, 0, 0, 255)); + for (unsigned i = 0; i < 3; i++) + hb_paint_pop_clip (paint_funcs, paint); + g_assert_null (hb_gpu_paint_encode (paint, nullptr)); + + hb_gpu_paint_destroy (paint); + hb_font_destroy (font); + hb_face_destroy (face); +} + static unsigned repro_color_stops (hb_color_line_t *cl HB_UNUSED, void *cl_data HB_UNUSED, unsigned start, unsigned *count, @@ -1100,6 +1143,7 @@ main (int argc, char **argv) hb_test_add (test_paint_recycle_blob); hb_test_add (test_paint_clip_path_depth_overflow); hb_test_add (test_paint_clip_rectangle_pop_balance); + hb_test_add (test_paint_layers_under_shared_clip); hb_test_add (test_paint_gradient_overflow_transform); hb_test_add (test_paint_empty_clip);