[repacker] Un-share LigatureSets before splitting, in bounded time and memory

When the subset serializer deduplicates a LigatureSet that is reachable
from several coverage entries of one LigatureSubst, pop_pack() links all
of those entries to a single LigatureSet vertex. The LigatureSubst
splitter numbers ligatures per coverage entry and then edits the
LigatureSet vertices in place (move_children, compact_liga_set, shrink),
which assumes one vertex per entry, so the shared entries silently lose
their ligatures. #6088 removed the duplicate_shared_liga_sets pre-pass
that used to guard this, on the assumption that as_mutable_table()'s
copy-before-mutate made it redundant; but that only un-shares a child
shared with *other* parents, not one reached by several links from the
same parent, so the bug came back.

Restore the pre-pass, but make it linear in both time and memory, since
a naive restore is O(n^2) on a subtable whose n coverage entries all
share one set:

- Memory: LigatureSet::subset()/Ligature::subset() each add one virtual
  link to Coverage ("ensure Coverage is packed after this") per subset
  call, and pop_pack()'s merge_virtual_links appends them to the deduped
  vertex without collapsing, so the shared set accumulates n redundant
  virtual links. duplicate(copy_table) copies that whole vector into
  every per-entry clone, i.e. O(n^2). unshare_duplicate_children()
  collapses each set's duplicate virtual links to one (dedup_virtual_links,
  fixing the dropped links' incoming-edge counts) before cloning, so each
  clone copies O(1), and does the whole un-share in a single pass over the
  parent's links instead of an O(n) remap-and-rescan per entry.

- Time: the split's analysis passes (compute_split_points,
  total_number_ligas, liga_counts) and clone_range() each did an O(n)
  index_for_offset() per entry. Precompute the ligatureSet index -> object
  id map once per split and pass it to the analysis passes, and in
  clone_range() move each run of fully-contained sets with a single
  move_children() (and look moved sets up through the new, small subtable)
  instead of a move_child()/as_mutable_table() per entry.

The un-share runs after the analysis passes: the deep copies share the
same Ligatures and keep the same ligature.len, so the counts and split
points are unchanged.

Testing: the K86 correctness reproducer now loses 0 of 8400 ligatures
(was lossy). The 1P5QWD69 reproducer (32759 entries sharing one set, 4
split subtables) drops from ~24 s / an OOM at ~12 GB to ~0.65 s CPU and
~137 MB peak RSS; instruction count vs entry count is linear (doubling n
multiplies work by ~1.8x, not ~4x), and peak RSS is O(n). Adds
test_resolve_with_liga_split_shared_liga_set to test-repacker. The full
meson test suite (including the subset and subset+repack suites) passes.

Discovered by Anthropic's OSS Scanner, as vulnerabilities
ANT-2026-K86LK9JR (dropped ligatures) and ANT-2026-1P5QWD69 (quadratic
DoS).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-11 21:24:29 -06:00
1 parent aa7a824cc4
commit 73a5bc580a
3 files changed
+325 -16

No files matched your search

+85
View File
@@ -149,6 +149,32 @@ struct graph_t
return graph_result_t<void>::from (obj_.virtual_links, ALLOCATION_FAILURE);
}
// Collapses virtual links that point to the same object down to a single link, keeping the
// first occurrence of each target. The object index of every dropped link is appended to
// 'removed' so the caller can correct that target's incoming-edge bookkeeping. Linear in the
// number of virtual links.
graph_result_t<void> dedup_virtual_links (hb_vector_t<unsigned>& removed)
{
hb_set_t seen;
unsigned count = obj_.virtual_links.length;
unsigned w = 0;
for (unsigned r = 0; r < count; r++)
{
unsigned objidx = obj_.virtual_links.arrayZ[r].objidx;
if (seen.has (objidx))
{
removed.push (objidx);
continue;
}
seen.add (objidx);
obj_.virtual_links.arrayZ[w++] = obj_.virtual_links.arrayZ[r];
}
obj_.virtual_links.shrink (w);
TRY (graph_result_t<void>::from (seen, ALLOCATION_FAILURE));
TRY (graph_result_t<void>::from (removed, ALLOCATION_FAILURE));
return graph_result_t<void>::from (obj_.virtual_links, ALLOCATION_FAILURE);
}
const hb_serialize_context_t::object_t& obj() const { return obj_; }
template<typename T>
@@ -1588,6 +1614,65 @@ public:
return Ok(new_child_idx);
}
/*
* Ensures that every child linked from parent_idx by a real link at a position >=
* first_link_pos is reached from parent_idx through exactly one link. The first link to a
* given child keeps it (and has its redundant virtual links collapsed); every later link to
* an already-seen child is repointed to a fresh deep copy of that child.
*
* This is a single pass over the parent's links, so it is O(n) in both time and memory for a
* parent with n such links. (A per-link remap-then-rescan would be O(n^2) in time, and copying
* a child that carries one redundant virtual link per incoming reference would be O(n^2) in
* memory.)
*/
graph_result_t<void> unshare_duplicate_children (unsigned parent_idx, unsigned first_link_pos)
{
if (unlikely (parent_idx >= vertices_.length))
return Err(OUT_OF_BOUNDS);
positions_invalid = true;
distance_invalid = true;
// duplicate () appends to vertices_; reserve room for the clones up front (at most one per
// link) so it cannot reallocate vertices_ and invalidate the parent reference and link array
// held across the loop below.
unsigned num_links = vertices_[parent_idx].obj ().real_links.length;
if (unlikely (!vertices_.alloc (vertices_.length + num_links)))
return Err(ALLOCATION_FAILURE);
hb_set_t seen;
auto& parent = vertices_[parent_idx];
for (auto& l : parent.real_links_writer ())
{
if (l.position < first_link_pos)
continue;
unsigned child_idx = l.objidx;
if (unlikely (child_idx >= vertices_.length))
return Err(OUT_OF_BOUNDS);
if (!seen.has (child_idx))
{
seen.add (child_idx);
// A child reached from several of parent's entries carries one (redundant) virtual link
// back to the shared Coverage per entry; collapse them so the deep copies below copy
// O(1) virtual links each, not O(n), and fix the dropped links' incoming-edge counts.
hb_vector_t<unsigned> dropped;
TRY (vertices_[child_idx].dedup_virtual_links (dropped));
for (unsigned target : dropped)
if (likely (target < vertices_.length))
vertices_[target].remove_parent (child_idx);
continue;
}
TRY_ASSIGN (unsigned clone_idx, duplicate (child_idx, true));
TRY (reassign_link (l, parent_idx, clone_idx, false));
}
return graph_result_t<void>::from (seen, ALLOCATION_FAILURE);
}
/*
* Raises the sorting priority of all children.
*/
+87 -16
View File
@@ -71,12 +71,26 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
graph_result_t<hb_vector_t<unsigned>> split_subtables (gsubgpos_graph_context_t& c,
unsigned this_index)
{
TRY_ASSIGN (auto split_points, compute_split_points(c, this_index));
// Finding the object id for a ligatureSet array index with index_for_offset() is O(n), so
// precompute the whole index -> object id map once (a single scan of this_index's links) to
// keep the analysis passes below O(n) overall instead of O(n^2).
TRY_ASSIGN (auto liga_set_ids, liga_set_index_to_object_id (c, this_index));
TRY_ASSIGN (auto split_points, compute_split_points(c, liga_set_ids));
if (!split_points)
return Ok(hb_vector_t<unsigned> ());
TRY_ASSIGN (unsigned total_ligas, total_number_ligas(c, this_index));
TRY_ASSIGN (hb_vector_t<unsigned> counts, liga_counts(c, this_index));
TRY_ASSIGN (unsigned total_ligas, total_number_ligas(c, liga_set_ids));
TRY_ASSIGN (hb_vector_t<unsigned> counts, liga_counts(c, liga_set_ids));
// The split edits LigatureSet vertices in place (move_children, compact_liga_set, shrink),
// which assumes one vertex per coverage entry. The subset serializer's pop_pack() dedup can
// link one LigatureSet from several entries of this subtable, so give each sharing entry its
// own copy before splitting, otherwise their ligatures are silently dropped. This runs after
// the analysis passes above: the deep copies share the same Ligatures and keep the same
// ligature.len, so the counts and split points are unchanged (and the map is now stale).
TRY (duplicate_shared_liga_sets(c, this_index));
split_context_t split_context {
c,
this,
@@ -88,21 +102,49 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
}
private:
graph_result_t<unsigned> total_number_ligas(gsubgpos_graph_context_t& c, unsigned this_index) const {
// Gives each coverage entry its own LigatureSet copy when the subset serializer deduped a set
// shared by several entries of this subtable. as_mutable_table() does not cover this: it only
// un-shares a child shared with *other* parents, not one reached twice from this_index. The
// ligatureSet offsets are the real links at position >= min_size (coverage sits at position 2).
graph_result_t<void> duplicate_shared_liga_sets(gsubgpos_graph_context_t& c, unsigned this_index) const {
const unsigned base_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2<SmallTypes>::min_size;
return c.graph.unshare_duplicate_children (this_index, base_size);
}
// Maps each ligatureSet array index to its object id in one scan of this_index's links, so the
// analysis passes below don't each pay an O(n) index_for_offset() per entry (which is O(n^2)).
graph_result_t<hb_vector_t<unsigned>> liga_set_index_to_object_id(gsubgpos_graph_context_t& c, unsigned this_index) const {
hb_vector_t<unsigned> map;
if (!map.resize_exact(ligatureSet.len)) return Err(ALLOCATION_FAILURE);
for (unsigned i = 0; i < map.length; i++)
map[i] = HB_GRAPH_INVALID;
const unsigned base_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2<SmallTypes>::min_size;
for (const auto& l : c.graph.vertices_[this_index].obj ().real_links) {
if (l.position < base_size) continue;
unsigned array_index = (l.position - base_size) / SmallTypes::size;
if (likely (array_index < map.length))
map[array_index] = l.objidx;
}
return map;
}
graph_result_t<unsigned> total_number_ligas(gsubgpos_graph_context_t& c, const hb_vector_t<unsigned>& liga_set_ids) const {
unsigned total = 0;
for (unsigned i = 0; i < ligatureSet.len; i++)
{
TRY_ASSIGN (auto liga_set, c.graph.as_table<LigatureSet>(this_index, &ligatureSet[i]));
TRY_ASSIGN (auto liga_set, c.graph.as_table_from_index<LigatureSet, graph_t::Immutable>(liga_set_ids[i]));
total += liga_set.table->ligature.len;
}
return Ok(total);
}
graph_result_t<hb_vector_t<unsigned>> liga_counts(gsubgpos_graph_context_t& c, unsigned this_index) const {
graph_result_t<hb_vector_t<unsigned>> liga_counts(gsubgpos_graph_context_t& c, const hb_vector_t<unsigned>& liga_set_ids) const {
hb_vector_t<unsigned> result;
for (unsigned i = 0; i < ligatureSet.len; i++)
{
TRY_ASSIGN (auto liga_set, c.graph.as_table<LigatureSet>(this_index, &ligatureSet[i]));
TRY_ASSIGN (auto liga_set, c.graph.as_table_from_index<LigatureSet, graph_t::Immutable>(liga_set_ids[i]));
result.push(!liga_set.table ? 0 : liga_set.table->ligature.len);
}
return graph_result_t<hb_vector_t<unsigned>>::from(std::move(result), ALLOCATION_FAILURE);
@@ -126,7 +168,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
}
graph_result_t<hb_vector_t<unsigned>> compute_split_points(gsubgpos_graph_context_t& c,
unsigned this_index) const
const hb_vector_t<unsigned>& liga_set_ids) const
{
// For ligature subst coverage is always packed last, and as a result is where an overflow
// will happen if there is one, so we can check the estimate length of the
@@ -142,7 +184,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
accumulated += OT::HBUINT16::static_size; // for ligature set offset
accumulated += OT::Layout::GSUB_impl::LigatureSet<SmallTypes>::min_size; // for ligature set table
TRY_ASSIGN (auto liga_set, c.graph.as_table<LigatureSet>(this_index, &ligatureSet[i]));
TRY_ASSIGN (auto liga_set, c.graph.as_table_from_index<LigatureSet, graph_t::Immutable>(liga_set_ids[i]));
// Finding the object id associated with an array index is O(n)
// so to avoid O(n^2), precompute the mapping by scanning through
@@ -301,6 +343,7 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
unsigned liga_set_count = 0;
unsigned liga_set_start = HB_GRAPH_INVALID;
unsigned liga_set_end = 0; // inclusive
unsigned moved_end = 0; // liga sets before this one have already been moved to the new liga subst.
for (unsigned i = 0; i < liga_counts.length; i++)
{
unsigned num_ligas = liga_counts[i];
@@ -314,7 +357,32 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
continue;
}
TRY_ASSIGN (auto liga_set, c.graph.as_mutable_table<LigatureSet>(this_index, &ligatureSet[i]));
if (i >= moved_end && current_start >= start && current_end <= end) {
// Move the whole run of liga sets fully contained within [start, end) at once.
// Moving (and looking them up) one at a time is O(n) each because move_child and
// as_mutable_table scan this_index's links, so per-offset handling is O(n^2); one
// move_children() over the run keeps the whole clone_range linear.
unsigned run_end = current_end;
moved_end = i + 1;
while (moved_end < liga_counts.length &&
run_end < end && run_end + liga_counts[moved_end] <= end)
run_end += liga_counts[moved_end++];
const unsigned base_size = OT::Layout::GSUB_impl::LigatureSubstFormat1_2<SmallTypes>::min_size;
TRY (c.graph.move_children<OT::Offset16>(
this_index,
base_size + i * SmallTypes::size,
base_size + moved_end * SmallTypes::size,
liga_subst_prime_id,
base_size + liga_set_count * SmallTypes::size));
}
// Liga sets that were already moved are looked up through the new liga subst, which only
// holds the moved run, so these lookups stay cheap.
bool moved = i < moved_end;
TRY_ASSIGN (auto liga_set, moved
? c.graph.as_mutable_table<LigatureSet>(liga_subst_prime_id, &liga_subst_prime->ligatureSet[liga_set_count])
: c.graph.as_mutable_table<LigatureSet>(this_index, &ligatureSet[i]));
unsigned liga_set_index = liga_set.index;
// Bounds may need to be adjusted if some ligas have been previously removed.
@@ -323,15 +391,18 @@ struct LigatureSubstFormat1 : public OT::Layout::GSUB_impl::LigatureSubstFormat1
current_end = hb_min(count + liga_bounds.second, current_end);
unsigned liga_set_prime_id;
if (current_start >= start && current_end <= end) {
if (moved || (current_start >= start && current_end <= end)) {
// This liga set is fully contined within [start, end)
// We can move the entire ligaset to the new liga subset object.
// We can move the entire ligaset to the new liga subset object (if not done already).
liga_set_end = i;
if (i < liga_set_start) liga_set_start = i;
TRY_ASSIGN (liga_set_prime_id, c.graph.move_child<> (this_index,
&ligatureSet[i],
liga_subst_prime_id,
&liga_subst_prime->ligatureSet[liga_set_count++]));
liga_set_prime_id = liga_set_index;
if (!moved)
TRY (c.graph.move_child<> (this_index,
&ligatureSet[i],
liga_subst_prime_id,
&liga_subst_prime->ligatureSet[liga_set_count]));
liga_set_count++;
compact_liga_set (c, liga_set.table, liga_set.vertex);
}
else
+153
View File
@@ -1939,6 +1939,135 @@ populate_serializer_with_large_liga_overlapping_clone_result (hb_serialize_conte
c->end_serialize();
}
static void
populate_serializer_with_liga_shared_liga_set (hb_serialize_context_t* c)
{
// One LigatureSubst whose two coverage entries point at the same LigatureSet,
// as produced by the subsetter when two first glyphs have identical ligature lists.
std::string large_string(100000, 'a');
c->start_serialize<char> ();
constexpr unsigned liga_size = 30000;
unsigned liga[3];
unsigned coverage = add_coverage(0, 1, c);
for (int i = 2; i >= 0; i--) {
start_object (large_string.c_str(), liga_size, c);
add_virtual_offset(coverage, c);
liga[i] = c->pop_pack (false);
}
add_liga_set_header(3, c);
add_virtual_offset(coverage, c);
for (unsigned i = 0; i < 3; i++)
add_offset(liga[i], c);
unsigned liga_set = c->pop_pack(false);
add_liga_header(coverage, 2, c);
add_offset(liga_set, c);
add_offset(liga_set, c);
unsigned liga_subst = c->pop_pack(false);
liga_subst = add_extension(liga_subst, 4, c);
start_lookup (7, 1, c);
add_offset(liga_subst, c);
unsigned lookup = finish_lookup (c);
unsigned lookup_list = add_lookup_list (&lookup, 1, c);
add_gsubgpos_header (lookup_list, c);
c->end_serialize();
}
static void
populate_serializer_with_liga_shared_liga_set_result (hb_serialize_context_t* c)
{
// Ligatures are numbered per coverage entry, [0, 3) for glyph 0 and [3, 6) for
// glyph 1, and the split points are at 2 and 4. Both glyphs must keep all three
// ligatures across the resulting subtables.
std::string large_string(100000, 'a');
c->start_serialize<char> ();
constexpr unsigned liga_size = 30000;
unsigned liga[3];
unsigned liga_subst[3];
unsigned liga_set[2];
// LigaSubst 3: glyph 1, ligas 1 and 2
unsigned coverage = add_coverage(1, 1, c);
for (int i = 2; i >= 1; i--) {
start_object (large_string.c_str(), liga_size, c);
add_virtual_offset(coverage, c);
liga[i] = c->pop_pack (false);
}
add_liga_set_header(2, c);
add_virtual_offset(coverage, c);
for (unsigned i = 1; i < 3; i++)
add_offset(liga[i], c);
liga_set[0] = c->pop_pack(false);
add_liga_header(coverage, 1, c);
add_offset(liga_set[0], c);
liga_subst[2] = c->pop_pack(false);
// LigaSubst 2: glyph 0 liga 2, glyph 1 liga 0
coverage = add_coverage(0, 1, c);
start_object (large_string.c_str(), liga_size, c);
add_virtual_offset(coverage, c);
liga[0] = c->pop_pack (false);
add_liga_set_header(1, c);
add_virtual_offset(coverage, c);
add_offset(liga[0], c);
liga_set[1] = c->pop_pack(false);
add_liga_set_header(1, c);
add_virtual_offset(coverage, c);
add_offset(liga[2], c);
liga_set[0] = c->pop_pack(false);
add_liga_header(coverage, 2, c);
add_offset(liga_set[0], c);
add_offset(liga_set[1], c);
liga_subst[1] = c->pop_pack(false);
// LigaSubst 1: glyph 0, ligas 0 and 1
coverage = add_coverage(0, 0, c);
add_liga_set_header(2, c);
add_virtual_offset(coverage, c);
for (unsigned i = 0; i < 2; i++)
add_offset(liga[i], c);
liga_set[0] = c->pop_pack(false);
add_liga_header(coverage, 1, c);
add_offset(liga_set[0], c);
liga_subst[0] = c->pop_pack(false);
for (int l = 2; l >= 0; l--) {
liga_subst[l] = add_extension(liga_subst[l], 4, c);
}
start_lookup (7, 3, c);
for (unsigned l = 0; l < 3; l++) {
add_offset(liga_subst[l], c);
}
unsigned lookup = finish_lookup (c);
unsigned lookup_list = add_lookup_list (&lookup, 1, c);
add_gsubgpos_header (lookup_list, c);
c->end_serialize();
}
static void
populate_serializer_with_large_liga_null_offsets (hb_serialize_context_t* c, bool split)
{
@@ -2913,6 +3042,29 @@ static void test_resolve_with_liga_split_null_offsets ()
free (expected_buffer);
}
static void test_resolve_with_liga_split_shared_liga_set ()
{
size_t buffer_size = 400000;
void* buffer = malloc (buffer_size);
hb_always_assert (buffer);
hb_serialize_context_t c (buffer, buffer_size);
populate_serializer_with_liga_shared_liga_set(&c);
void* expected_buffer = malloc (buffer_size);
hb_always_assert (expected_buffer);
hb_serialize_context_t e (expected_buffer, buffer_size);
populate_serializer_with_liga_shared_liga_set_result(&e);
run_resolve_overflow_test ("test_resolve_with_liga_split_shared_liga_set",
c,
e,
20,
true,
HB_TAG('G', 'S', 'U', 'B'));
free (buffer);
free (expected_buffer);
}
static void test_resolve_with_liga_split_shared_table ()
{
size_t buffer_size = 400000;
@@ -3573,6 +3725,7 @@ main (int argc, char **argv)
test_resolve_with_liga_split_move ();
test_dont_duplicate_virtual ();
test_resolve_with_liga_split_overlapping_clone ();
test_resolve_with_liga_split_shared_liga_set ();
test_resolve_with_liga_split_null_offsets ();
test_resolve_with_liga_split_shared_table ();
test_resolve_with_liga_split_shared_coverage ();