Commit Graph
8 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 5.5 a86ba5f203 [cairo] Remove work-budget accounting (#6602)
hb-cairo hands the actual rendering to cairo, so the work done per
paint operation (clip, gradient, compositing) is cairo's, not ours.
Any count we keep is a guess.  The guesses we had were not good:
022a9e475 charged each paint
(1 + clip + path segments) x max(weight, segments), with a weight of
up to 8192, against HB_BUDGET_GLYPH.  Real fonts blow through that.
For example, hb-view renders the Spain flag in Noto-COLRv1 (U+1F1EA
U+1F1F8) as a blank image, and about ten glyphs in that font go over.

Remove the budget accounting from hb-cairo:

  - Revert 022a9e475 "[cairo] Charge compositing operations to the
    glyph budget", with its test font and generator.
  - Revert 8f3ab5ab8 "[cairo] Share work across nested glyph
    rendering": the per-scaled-font hb_cairo_budget_t, the segment
    charges in the draw callbacks, and the set/get budget callbacks
    on the cairo draw and paint funcs.

Keep paint_color_glyph declining (fd8c9209b), so COLR paints nested
glyphs inline under the decycler and nesting limits of the walk in
progress.  Going through cairo_show_glyphs() again would bring back
the PaintColrGlyph cycle crash, which test_hb_cairo_color_glyph_cycle
covers.  Only the code comment loses its mention of work limits.

Testing: meson test -C build: 299 OK, 1 skipped.  The Spain flag
renders again, and the PaintColrGlyph cycle glyphs in
test_glyphs-glyf_colr_1.ttf render without crashing.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 13:00:15 -06:00
Garret RiegerandGemini a913b14e2c [cairo] Bound cluster walks in hb_cairo_glyphs_from_buffer() (#6578)
hb_utf_offset_to_pointer() walked `start` using `while (offset-- > 0)`
and `while (offset++ < 0)` without checking whether `start` had
reached `text + text_len` or `text`.  In addition, post-decrementing
`offset` in the first loop's condition left `offset == -1` after any
non-negative walk, causing the backward loop to execute one extra
`utf_t::prev()` step.  In hb_cairo_glyphs_from_buffer(), cluster
deltas in the `utf8_clusters` path could also advance `start` past
`utf8 + utf8_len`, or overflow `signed` when cast for
hb_utf_offset_to_pointer().

Check `start < text + text_len` and `start > text` in
hb_utf_offset_to_pointer() while adjusting `offset` inside the loop
bodies, and clamp each cluster delta in hb_cairo_glyphs_from_buffer()
to the remaining bytes in `utf8`.

Extend test_hb_cairo_glyphs_from_buffer_clusters in
test/api/test-cairo.c to cover multi-byte UTF-8 inputs on exact-sized
heap buffers and cluster deltas that exceed the string length or
overflow signed 32-bit integers.

Co-authored-by: Gemini <noreply@google.com>
2026-11-04 15:11:48 -06:00
Behdad EsfahbodandClaude Opus 4.8 022a9e475a [cairo] Charge compositing operations to the glyph budget
hb-cairo's per-glyph work budget was spent only on emitted path segments,
so a COLRv1 font could pay once for a clip outline (push_clip_glyph) and
then have cairo re-rasterize it for every PaintSolid/gradient/group
composite under it, up to the COLR edge limit.  That is the
multiplication of limits the shared budget in hb-limits.hh exists to
prevent: a 2 KB font kept hb-view busy for over a minute on one glyph.

Charge every composite in the cairo backend, where the cost is incurred
and where the sibling backends already charge per operation (hb-raster
charges pixel area per paint).  hb_cairo_context_t now counts the
segments of the current path and keeps a stack of the clip paths in
force, maintained by the push_clip_* callbacks and pop_clip; spend_paint()
charges every fill, paint, group and gradient with (1 + segments) times a
per-source weight, squared when more than one path is in force since
cairo then intersects the paths per operation.  These segment and clip
counts are inputs to the existing budget_remaining counter; they are not
a separate per-callback limit.  The weights (HB_BUDGET_512 for
solid/group, 1024 for linear/radial gradients, a new HB_BUDGET_8192 for
the sweep mesh pattern) follow per-composite timings measured on
cairo 1.18.

Nested color glyphs are already painted inline by the color_glyph
callback, so their composites are charged against the clips in force at
that point; no change is needed there.

Behaviour change: a glyph whose composites exceed the budget (about 32k
segment-composites, roughly a second of cairo time at hb-view's default
size) is truncated best-effort, as outline work already is.  All glyphs
of the COLRv1 glyf (static and variable) and CFF2 test fonts render
byte-identically through hb-view with and without this change.

A regression test with a generated 3 KB font is added to
test/api/test-cairo.c alongside its fontTools generator; it finishes in
about a second once the paints are charged and guards against
reintroduction through the meson test timeout rather than a
discriminating assertion.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-38OVH0N4.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:53:31 -06:00
Behdad EsfahbodandClaude Opus 4.8 f69c05ea38 [cairo] Map non-monotone buffer clusters to one cairo cluster
hb_cairo_glyphs_from_buffer() asserted that buffer clusters are monotone
in the buffer direction, but hb_shape() does not guarantee that:
HB_BUFFER_CLUSTER_LEVEL_CHARACTERS, shaping that hits the ops limit
mid-lookup, and Indic reordering corner cases can all produce
non-monotone clusters.  The function runs from HarfBuzz's own cairo
text_to_glyphs callback on attacker-controlled text and fonts, so the
assertion was an input-dependent abort (and, with NDEBUG, an unchecked
backward walk off the ends of the utf8 buffer).

The cluster-counting loop that already walks the buffer now detects a
step against the buffer's direction and falls back to a single cairo
cluster mapping all of the utf8 to all of the glyphs, which cairo
accepts.  The monotonicity assertions in the fill step then hold by
construction and glyph output is unchanged for monotone buffers.

A regression test in test/api/test-cairo.c feeds monotone and
non-monotone LTR/RTL buffers through both cluster modes and checks the
cluster counts and byte/glyph totals; it aborts on the pristine tree.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-T4PZ1H72.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:45:15 -06:00
Behdad EsfahbodandClaude Opus 4.8 4e215a7ec8 [cairo] Don't leak the hb_font_t created per scaled font
hb_cairo_init_scaled_font() must hand exactly one reference of the
hb_font_t to the cairo_scaled_font_t.  For a face-based font face
(hb_cairo_font_face_create_for_face()) it creates the font with a
reference count of 1, but then still called hb_font_reference() before
attaching it, so the creating reference was never released.  Every
finalized scaled font therefore leaked an hb_font_t, pinning its
hb_face_t, the font blob and the accelerators until the process exits.
Cairo finalizes scaled fonts during normal operation once its cache
fills, so the leak accumulates as content selects distinct sizes,
transforms or font options.

Take the extra reference only in the branch where the font is the
shared one owned by the cairo font face (the font-based constructor),
and transfer the freshly created font as-is.  The set_user_data result
is now checked and the font released on failure, matching the
budget/color-cache idiom a few lines below and the font-face
constructors in the same file.

A callback installed with hb_cairo_font_face_set_font_init_func() that
returns a different font keeps its historical behavior: the returned
font is referenced rather than assumed to transfer ownership, so no
in-tree contract changes and no caller can turn into a use-after-free.
hb-view uses the font-based constructor and was unaffected.

A regression test in test/api/test-cairo.c registers a face destroy
notifier, draws through a face-based font face, flushes cairo's
scaled-font cache and asserts the face was destroyed; it fails on the
unpatched library and passes with the fix.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BAUN9JN4.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:44:32 -06:00
Behdad EsfahbodandClaude Opus 4.8 fd8c9209b6 [cairo] Paint nested color glyphs inline to bound recursion
hb_cairo_paint_color_glyph() accepted the color_glyph paint callback
and drew the nested glyph with cairo_show_glyphs(), which re-enters
hb_font_paint_glyph() through a new cairo scaled font with a fresh
hb_paint_context_t.  The glyph decycler, HB_MAX_NESTING_LEVEL and the
edge budget therefore start over at every level, so two glyphs that
paint each other with PaintColrGlyph recurse until the stack is
exhausted.  The per-scaled-font hb_cairo_budget_t cannot bound this
either: the nested glyph is shown through a different scaled font, so
each level gets its own budget.

Decline the callback, returning false exactly as hb-raster's does, so
the COLR (and hb-ft-colr) walk paints the nested glyph inline under the
decycler and depth limits of the walk already in progress.

A regression test with two PaintColrGlyph glyphs that paint each other
is added to test/api/test-cairo.c; it aborts on the unpatched library
and passes with the fix.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-VPKA505E.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 14:43:49 -06:00
Behdad Esfahbod 7eab9be024 [cairo] Skip external lock inversion under TSan
Cairo's user-font rendering acquires two internal locks in both
orders. The new nested-rendering regression test exposes that existing
lock-order inversion under ThreadSanitizer, including when run against
an unmodified HarfBuzz.

Keep the end-to-end test enabled normally, but skip that workload when
compiled with TSan instead of suppressing deadlock detection globally.

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 18:34:02 -06:00
Behdad Esfahbod 8f3ab5ab87 [cairo] Share work across nested glyph rendering
Cairo's draw callbacks previously received a raw cairo_t, so paint-driven
outline traversals could not share a renderer budget.  Nested color glyphs
also construct fresh HarfBuzz paint contexts while populating the same
scaled-font glyph cache.

Attach a finite budget owner to the scaled font and let each draw/paint
context borrow it.  Recharge only for the outermost glyph callback, expose
the budget callbacks on both function sets, and charge emitted path and
nested color-glyph work.  Exercise the original vector timeout seed through
the Cairo user-font renderer.

Tests: meson test -C build (275/275 passed)

Assisted-by: OpenAI Codex <codex@openai.com>
2026-08-26 17:51:34 -06:00