Commit Graph
86 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 4.8 636e97026e [repacker] Split PairPos and MarkArray subtables in linear time
When the repacker splits an oversized GPOS subtable, the splitters
resolved and re-homed the subtable's links one at a time with
graph_t::index_for_offset() and graph_t::move_child(), each of which
scans the vertex's link list from the start, so a split was quadratic
in the number of links and no repacker limit counted that work.  A
crafted 131 KB font with 20 PairPosFormat1 subtables of 65,534 PairSets
took ~45 s of CPU in hb-subset.

Use the graph's existing tools the way ligature-graph.hh already does:

- PairPosFormat1::split_subtables() builds position_to_index_map() once
  and looks each PairSet up in it; clone_range() sorts the links by
  position and moves the whole [start, end) offset range with one
  move_children() call, which keeps the moved and remaining links in
  order.
- PairPosFormat2::clone_class1_records() now moves the class1 record
  byte range (whose only links are device tables) with one
  move_children() call, folding in the former transfer_device_tables()
  and dropping its move_child()-per-value-record loop and the re-sort
  of the whole link list after each record.
- MarkArray::clone() adds the new mark-anchor links directly and
  removes the old ones in a single pass, the pattern
  AnchorMatrix::clone_rows() already uses.

The now-unused pair_set_graph_index() helper and split_context_t
fields are dropped.  The reproducer drops to ~1.5 s; a new test,
test_resolve_with_shared_pair_set_pair_pos_1_split, checks the split
keeps PairSets in order; no public API/ABI change.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 15:46:23 -06:00
Behdad EsfahbodandClaude Opus 4.8 95086deff1 [repacker] Count wide parents of all space roots in one pass
isolate_subgraph() called wide_parents() once per 32-bit space root,
and each call walked the complete real_links list of every parent of
that root, so a parent with M wide links to M roots (a
FeatureVariations ConditionSet with thousands of Offset32s) was walked
M times: N*M^2 work that no repacker limit bounds.  A crafted 223 KB
font with 150 overlapping ConditionSets kept hb-subset busy ~45-60 s.

wide_parents() now takes the whole roots set and the subgraph count
map: it seeds every root with count 0, collects the union of the
roots' parents, then walks each parent's links once, incrementing the
count of any root a wide unsigned link targets.  The per-root counts
and the 'parents' set are identical to before, so the subsets emitted
are byte-identical; only the work drops to the sum of the parents'
link counts.  wide_parents() is private to graph_t with no other
callers.

A regression test, test_isolate_subgraph_shared_wide_parents(), builds
the shared-wide-parent shape and checks isolate_subgraph() duplicates
exactly the one leaf with an outside 16-bit edge.  The attack font
drops to ~1.1 s; no public API/ABI change.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 15:37:38 -06:00
Behdad EsfahbodandClaude Opus 4.8 6a2f8c6b42 [repacker] Record a parent in priority_bumped_parents even when nothing was raised
_process_overflows() calls raise_childrens_priority() (linear in the
parent's links) at most once per parent per round, using
priority_bumped_parents as the memo, but it recorded the parent only
when a priority actually changed.  Once every child of a parent sits at
the maximum priority the call returns false and is repeated for every
remaining overflow record of that parent, making a round cost
overflows x links for up to 2 x 32 rounds; a crafted 71 KB font with
one ~70 KB GPOS BaseArray pointing at ~11k distinct anchors cost
hb-subset ~30 s of CPU.

Add the parent to the set unconditionally.  Within a round priorities
only increase and a successful duplication ends the round, so a second
call for the same parent can never raise anything: the return value and
the set of changes are unchanged, only the redundant re-walks are
removed.  The attack font drops to ~0.2 s.

A regression test, test_unresolvable_priority_overflow(), builds the
same graph shape and asserts the repacker gives up; no public API/ABI
change.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 15:37:38 -06:00
Behdad EsfahbodandClaude Opus 4.8 7e28f358ae [repacker] De-duplicate overflow records without an overflow_record_t hash map
will_overflow() de-duplicated (parent, child) overflow records through
an hb_hashmap_t keyed by overflow_record_t, whose hash() collapses to
2654435761 * (31 * parent + child).  With dense vertex ids, a graph
where many consecutive vertices link the same children puts all records
into a handful of equal-hash chains, so the map degenerates into
quadratic probing and repeated over-growth in every resolution round: a
crafted 60 KB GDEF font with a LigCaretList of overlapping LigGlyphs
took ~65 s of CPU and ~520 MiB before failing.

graph.ordering_ is a permutation of the vertices, so every parent is
visited exactly once and a duplicate record can only come from two
links of the same parent to the same child.  A per-parent hb_set_t of
the children already recorded, cleared when moving to the next parent,
yields exactly the same records in the same order with no hashing of
records at all.  The now-unused operator==/!=/hash() on
overflow_record_t are removed (the struct is only used in vectors
elsewhere).

A regression test, test_will_overflow_records(), asserts one record per
overflowing (parent, child) pair when a parent links the same child
twice.  The attack font drops to ~1.3 s CPU and 133 MiB, the remaining
cost being the 1.8 million links the sanitizer budget admits; no public
API/ABI change.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 15:37:38 -06:00
Behdad EsfahbodandClaude Opus 4.8 9179b64235 [repacker] Propagate the vertex limit error during overflow resolution
HB_REPACKER_MAX_VERTICES is enforced inside graph_t::duplicate() by
returning LIMIT_EXCEEDED, but since the hb_result_t conversion the two
callers that drive overflow resolution threw that error away:
duplicate_subgraph() recorded it in a local and returned Ok, and
_resolve_shared_overflow() mapped every error to Ok(false) ("couldn't
duplicate, try something else").  hb_resolve_graph_overflows() then kept
iterating over a graph that could no longer grow: a 53 KB font with 150
MarkBasePos subtables sharing one 8400-anchor BaseArray ran one core for
~705 s before failing, instead of ~37 s.

duplicate_subgraph() now returns the recorded error with TRY(result).
_resolve_shared_overflow() treats only INVALID_ARGUMENT (the node
genuinely cannot be duplicated here) as "not resolved" and propagates
LIMIT_EXCEEDED/ALLOCATION_FAILURE/OUT_OF_BOUNDS, restoring the
pre-conversion behaviour where the sticky error ended the loop; this is
the same error-discrimination idiom already used for SANITIZE_FAILURE in
hb-repacker.hh and ORPHANED_NODES in graph.hh.

Two regression tests are added to test-repacker.cc, each failing without
its hunk.  Behaviour change: reaching the vertex limit now fails
promptly as before 14.5.0; no public API/ABI change.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 15:37:38 -06:00
Behdad EsfahbodandClaude Opus 4.8 c8e9f14fd3 [repacker] Reject link positions that wrap near UINT_MAX
graph_t::vertex_t::link_positions_valid() computed a link's last byte
as start + width - 1 in 32-bit arithmetic, so a caller-supplied
position near UINT_MAX wrapped to a small value and passed the bounds
test; the defense-in-depth guard in serialize_link_of_type() summed
link.position + link.width the same way.  A graph with a link at
position 0xFFFFFFFF was therefore accepted by
hb_subset_serialize_or_fail() and the serializer stored the offset
field about 4 GiB past the output buffer (SIGSEGV on 64-bit).

Reject the link when end < start (width is already confined to 2..4 at
that point, so this means start + width wrapped), and rewrite the
serializer guard as position > size || size - position < width, the
no-sum idiom used elsewhere for range checks.

Only a caller that computes its own bad link position reaches this;
the subsetter's own graphs always take positions from inside the
object, so no font, text or subsetting request leads here.  Valid
graphs are unaffected and there is no public API/ABI change.

A regression test, test_invalid_link_position(), is added next to the
existing invalid-link tests; it crashes on the unpatched code.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-GREQ3M7K.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 15:37:38 -06:00
Behdad Esfahbod f9fcba7272 [repacker] Prevent matrix vertex size checks from wrapping (#6491)
AnchorMatrix's offset array size was multiplied in unsigned arithmetic.
A small vertex with 46341 rows could therefore be accepted as a
46341-column matrix, leading to excessive work and allocation during
subtable splitting. PairPosFormat2 had the same problem on 32-bit
platforms.

Use the existing size_t saturating multiply helper and compare against
the payload length after checking the header. This preserves the width
of PairPosFormat2's size computation and avoids overflow in the header
addition as well.

Add bounded internal vertex tests for wrapping dimensions, exact valid
sizes, truncated payloads and headers, and empty matrices. The anchor
regression fails against the unchanged code without large allocations.

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

Testing: meson compile -C build -j4; meson test -C build
(all 274 tests passed).

Assisted-by: OpenAI Codex
2026-10-30 14:51:09 -06:00
Behdad Esfahbod d8347f079e [repacker] Count null offsets when splitting ligatures (#6476)
Ligature split points skipped null offsets while the ranges used to
clone and shrink subtables counted every slot. Advance the split index
for null offsets too, so both stages refer to the same ligatures.

Also reject an inverted range after narrowing it to the remaining
links, before calculating the number of ligatures to allocate.

Add a regression that compares the split graph with the expected
subtables when one ligature set has trailing null offsets.

Testing: meson test -C build (292 passed; check-symbols skipped on
macOS), plus the repacker test and shared-ligature font check with
ASan and UBSan.

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

Assisted-by: OpenAI Codex
2026-10-29 21:00:03 -06:00
Behdad Esfahbod 5b104f8afe [repacker] Handle extended layout graphs
Discover GSUB/GPOS 1.2 lookups through LookupList2 and treat the
32-bit lookup-list link like other wide graph roots.  Also preserve
parent multiplicity when identical lookup subtables are promoted to a
shared extension wrapper.

Add coverage for 1.2 extension promotion and repeated subtable links.

Assisted-by: OpenAI Codex <codex@openai.com>
2026-10-02 02:35:55 -06:00
Garret Rieger 240a7d7b8b Fix incorrect add_object sizes. (#6267) 2026-09-17 18:54:44 -06:00
Garret Rieger 2f291a46ae Convert repacker and graph code to utilize to hb_result_t.
Converts all error handling in the repacker/graph code to use hb_result_t.
2026-09-04 17:08:08 -06:00
Garret Rieger 7eac03d53c Add missing error handling for fallible operations in the repacker. 2026-08-27 14:22:26 -06:00
Garret Rieger 197d38a8a3 [subset] Convert repacker graph traversals to BFS.
None of the traversals require DFS order so BFS can be used instead which eliminates the possibility of stack overflow. For the queue we re-use the existing ordering_scratch_ space to avoid allocating any additional memory. As a bonus this fixes a bug in find_32bit_roots() that was the result of DFS traversal order.

All of the directed traversal methods are unified to a single common implementation.
2026-07-31 18:05:07 -06:00
Qunxin Liu 2c875edbb6 [subset-repack] bug fixes (#6088)
* [subset-repack] fix potential out of bounds accesses

make sure we create a deep copy of the object before we mutate

* [subset-repack] fix is_shared(): only check real parents

* [subset-repack] add tests

* [subset-repack] apply AI proposed change

* address review comments

* [subset-repack] no need to duplicate shared liga_sets beforehand

Since we now would create a memory copy of the object before mutating
it, I think checking and duplicating shared liga_sets is not needed.
2026-07-21 11:49:06 -06:00
Behdad Esfahbod fc85680fec Revert "[subset-repack] bug fixes (#6077)"
This reverts commit 0b590d6b54.
2026-07-21 10:59:48 -06:00
Qunxin Liu 0b590d6b54 [subset-repack] bug fixes (#6077)
* [subset-repack] fix potential out of bounds accesses

make sure we create a deep copy of the object before we mutate

* [subset-repack] fix is_shared(): only check real parents

* [subset-repack] add tests

* [subset-repack] apply AI proposed change

* address review comments

* [subset-repack] no need to duplicate shared liga_sets beforehand

Since we now would create a memory copy of the object before mutating
it, I think checking and duplicating shared liga_sets is not needed.
2026-07-21 10:55:43 -06:00
Qunxin Liu babdb46be3 [repacker] bug fix for splitting shared subtables (#5686)
And fix splitting for ligatureSubst table with shared coverage table
2025-12-19 21:42:20 -07:00
Garret Rieger 54ab8f0df4 [repacker] make repacker object ids stable.
Prior to this change the object id for a vertex in the repacker graph is it's position in the topological ordering. As a result after each sorting the object ids change. This decouples the topological sorting from the object id assignments. A separate ordering list is retained. This simplifies handling around object ids which no longer needs to account for them shifting, and improves performance by eliminating the step of reassigned link object ids after each sort.
2025-09-15 16:34:44 -06:00
Behdad Esfahbod 75f919876f [src/test-*] Use hb_always_assert() which isn't compiled out under NDEBUG
Part of https://github.com/harfbuzz/harfbuzz/issues/5418
2025-07-25 03:00:38 -06:00
Garret Rieger e50d16f6f8 [repacker] During liga splitting re-use orphaned liga sets. 2025-07-10 00:58:49 +00:00
Garret Rieger cad418860a [repacker] fix bug where nodes with incoming virtual links could get orphaned.
When attempting to fix overflows nodes that have incoming edges from more than one parent can be duplicated. However, if all of these incoming links are virtual except for one duplicating it will effectively orphan it (one of the copies will be pointed to only by virtual links). Since parent book keeping is expensive we don't want to separately track virtual/real parents so instead just don't duplicate nodes that have at least one incoming virtual link.
2025-07-09 17:09:05 +00:00
Garret Rieger f5279200f3 [repacker] Add test which hits the move liga set pathway in clone_range(). 2025-07-08 22:50:48 +00:00
Garret Rieger 96fedd88fe [repacker] Fix handling of virtual links in liga table splitting. 2025-07-08 20:36:13 +00:00
Garret Rieger 5b47710f56 [repacker] Start implementing test for liga splitting. 2025-07-07 20:15:30 +00:00
Garret Rieger 69f9c29ec1 [repacker] add tests of serializer repack_last(). 2024-03-29 00:19:16 +00:00
Garret Rieger 01fc4a162f [repacker] Fix repacker test.
With class def size estimation changes this test is now able to pack closer to the limit.
2024-03-09 00:06:54 +00:00
Garret Rieger 7160c5b9e5 [repacker] add tests for multi-duplication.
Further improve the resolution attempt by pre-emptively raising priority of a duplicated shared node.
2023-12-05 16:25:12 -05:00
Garret Rieger 5587247d5b [repacker] create only one extension subtable per physical subtable.
During extension promotion when multiple lookups refer to a shared subtable node create and reuse a single extension subtable for it. Fixes: https://github.com/fonttools/fonttools/issues/3260.
2023-08-23 17:03:54 -06:00
Garret Rieger 554ed06fac [repacker] add cycle detection to the graph sort.
This allows us to bail early if the graph is not acyclic.
2022-12-01 21:52:37 +00:00
Behdad Esfahbod 59c45f6deb Use hb_memcpy instead of memcpy consistently 2022-11-22 12:54:50 -07:00
Garret Rieger 015ca5bc3c [repacker] fix compiler alignment warning. 2022-08-18 21:52:55 +00:00
Garret Rieger 683c5dd21e [repacker] further reduce base count. 2022-08-18 20:57:04 +00:00
Garret Rieger 5ddf41fc9c [repacker] speed up MarkBasePos test case by using a smaller basecount. 2022-08-18 20:28:05 +00:00
Garret Rieger a3ed9f9099 [repacker] fix graph comparison, and mark base pos generation for the tests. 2022-08-17 23:39:11 +00:00
Garret Rieger 19c51ed35c [repacker] Get mark base pos test working. 2022-08-17 19:15:55 +00:00
Garret Rieger 8c3db8bdfd [repacker] more progress on MarkBasePos tests. 2022-08-17 00:36:23 +00:00
Garret Rieger 1405f96b6f [repacker] change run_resolve_overflow_test to check for graph equivalence.
Replaces a check for an exact match on the final serialized bytes. The previous check enforced equivalent topological sorting between result and expected, but we only really care that the graph's are equivalent and don't overflow.
2022-08-15 23:48:00 +00:00
Garret Rieger 0e48a65d32 [repacker] estimate size of classDef1 and coverage during PairPos2 split point analysis. 2022-08-05 20:19:11 +00:00
Garret Rieger e1ab355056 [repacker] correct lookup link insertion. 2022-08-05 01:25:16 +00:00
Garret Rieger a733a9afa5 [repacker] insert new subtables immediately after the subtable they split from in the lookup. 2022-08-05 00:32:47 +00:00
Garret Rieger fdd1952c75 [repacker] PairPosFormat2 splitting - fix coverage and classdef splitting.
The old code was splitting based on coverage index, but should have been splitting on class value.
2022-08-04 19:21:16 +00:00
Garret Rieger 51a5060273 [repacker] add test for splitting a PairPos2 w/ device tables. 2022-08-03 22:30:42 +00:00
Garret Rieger 54fab21cb1 [repacker] get basic pair pos 2 split test working. 2022-08-03 21:57:37 +00:00
Garret Rieger 60d6ffb375 [repacker] always duplicate classDef2 when splitting a PairPos2.
Splits are done in a way that it shouldn't be possible to share the classDef2 between split PairPos2's so pre-emptively duplicate it.
2022-08-03 21:01:23 +00:00
Garret Rieger 6be152420f [repacker] add basic test for PairPos2 splitting. 2022-08-03 19:02:20 +00:00
psykose 04d28d94e5 [repacker] fix signedness of char in tests 2022-08-01 11:48:57 -06:00
Garret Rieger 674f0194a3 [repacker] add extension pairpos split test. 2022-07-29 22:21:31 +00:00
Garret Rieger 1d2516f037 [repack] get basic pairpos split test working. 2022-07-29 22:21:31 +00:00
Garret Rieger 4e7360f78d [repacker] begin adding tests for PairPosFormat1 splitting. 2022-07-29 22:21:31 +00:00
Garret Rieger f56e66f3f0 [repacker] for ext promotion choose lookups from highest subtables per byte to lowest.
Attempts to roughly maximize the number of subtables that are non-ext.
2022-07-26 12:22:17 -06:00