mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-28 09:09:13 +01:00
[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>
This commit is contained in:
1 parent
6a2f8c6b42
commit
95086deff1
2 files changed
+98
-15
No files matched your search
+29
-15
@@ -1016,12 +1016,7 @@ public:
|
||||
// incoming edges to root_idx should be all 32 bit in length so we don't need to de-dup these
|
||||
// set the subgraph incoming edge count to match all of root_idx's incoming edges
|
||||
hb_set_t parents;
|
||||
for (unsigned root_idx : roots)
|
||||
{
|
||||
if (unlikely (root_idx >= vertices_.length))
|
||||
return Err(OUT_OF_BOUNDS);
|
||||
subgraph.set (root_idx, wide_parents (root_idx, parents));
|
||||
}
|
||||
TRY (wide_parents (roots, subgraph, parents));
|
||||
for (unsigned root_idx : roots)
|
||||
{
|
||||
TRY (find_subgraph (root_idx, subgraph));
|
||||
@@ -1771,26 +1766,45 @@ public:
|
||||
}
|
||||
|
||||
/*
|
||||
* Returns the numbers of incoming edges that are 24 or 32 bits wide.
|
||||
* For each node in nodes, sets counts[node] to the number of incoming edges that
|
||||
* are 24 or 32 bits wide. The sources of those edges are added to parents.
|
||||
* counts is expected to be empty.
|
||||
*/
|
||||
unsigned wide_parents (unsigned node_idx, hb_set_t& parents) const
|
||||
graph_result_t<void> wide_parents (const hb_set_t& nodes,
|
||||
hb_map_t& counts,
|
||||
hb_set_t& parents) const
|
||||
{
|
||||
unsigned count = 0;
|
||||
for (unsigned p : vertices_[node_idx].parents_iter ())
|
||||
// A single parent may link to many of the nodes (eg. a ConditionSet with
|
||||
// thousands of 32 bit offsets), so collect the parents first and walk the
|
||||
// links of each one only once.
|
||||
hb_set_t all_parents;
|
||||
for (unsigned node_idx : nodes)
|
||||
{
|
||||
if (unlikely (node_idx >= vertices_.length))
|
||||
return Err(OUT_OF_BOUNDS);
|
||||
counts.set (node_idx, 0);
|
||||
for (unsigned p : vertices_[node_idx].parents_iter ())
|
||||
all_parents.add (p);
|
||||
}
|
||||
TRY (graph_result_t<void>::from (all_parents, ALLOCATION_FAILURE));
|
||||
TRY (graph_result_t<void>::from (counts, ALLOCATION_FAILURE));
|
||||
|
||||
for (unsigned p : all_parents)
|
||||
{
|
||||
// Only real links can be wide
|
||||
for (const auto& l : vertices_[p].obj().real_links)
|
||||
{
|
||||
if (l.objidx == node_idx
|
||||
&& (l.width == 3 || l.width == 4)
|
||||
&& !l.is_signed)
|
||||
hb_codepoint_t *count;
|
||||
if ((l.width == 3 || l.width == 4)
|
||||
&& !l.is_signed
|
||||
&& counts.has (l.objidx, &count))
|
||||
{
|
||||
count++;
|
||||
(*count)++;
|
||||
parents.add (p);
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
return graph_result_t<void>::from (parents, ALLOCATION_FAILURE);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
@@ -3220,6 +3220,74 @@ test_32bit_roots_traversal ()
|
||||
free (buffer);
|
||||
}
|
||||
|
||||
static void
|
||||
test_isolate_subgraph_shared_wide_parents ()
|
||||
{
|
||||
// Several parents each have 32 bit links to the same set of leaves, as
|
||||
// FeatureVariations ConditionSets do. One leaf also has a 16 bit link from
|
||||
// outside the subgraph, so isolating the leaves must duplicate that leaf only.
|
||||
//
|
||||
// root -16-> p1, p2, p3
|
||||
// root -16-> l1
|
||||
// p1, p2, p3 -32-> l1, l2, l3
|
||||
constexpr unsigned num_parents = 3;
|
||||
constexpr unsigned num_leaves = 3;
|
||||
|
||||
size_t buffer_size = 1000;
|
||||
void* buffer = malloc (buffer_size);
|
||||
hb_serialize_context_t c (buffer, buffer_size);
|
||||
|
||||
c.start_serialize ();
|
||||
unsigned leaves[num_leaves];
|
||||
for (unsigned i = 0; i < num_leaves; i++)
|
||||
leaves[i] = add_object ("leaf", 4, &c);
|
||||
|
||||
unsigned parents[num_parents];
|
||||
for (unsigned i = 0; i < num_parents; i++)
|
||||
{
|
||||
start_object ("parent", 6, &c);
|
||||
for (unsigned j = 0; j < num_leaves; j++)
|
||||
add_wide_offset (leaves[j], &c);
|
||||
parents[i] = c.pop_pack (false);
|
||||
}
|
||||
|
||||
start_object ("root", 4, &c);
|
||||
for (unsigned i = 0; i < num_parents; i++)
|
||||
add_offset (parents[i], &c);
|
||||
add_offset (leaves[0], &c);
|
||||
c.pop_pack (false);
|
||||
c.end_serialize ();
|
||||
|
||||
graph_t graph = graph_t::create (c.object_graph ()).value ();
|
||||
unsigned num_vertices = graph.vertices_.length;
|
||||
hb_always_assert (num_vertices == 1 + num_parents + num_leaves);
|
||||
|
||||
hb_set_t roots;
|
||||
for (unsigned i = 0; i < num_leaves; i++)
|
||||
roots.add (leaves[i] - 1);
|
||||
|
||||
// leaves[0] has an incoming 16 bit edge from root, so it's duplicated.
|
||||
hb_always_assert (graph.isolate_subgraph (roots).value ());
|
||||
hb_always_assert (graph.vertices_.length == num_vertices + 1);
|
||||
hb_always_assert (roots.get_population () == num_leaves);
|
||||
hb_always_assert (!roots.has (leaves[0] - 1));
|
||||
hb_always_assert (roots.has (num_vertices));
|
||||
for (unsigned i = 1; i < num_leaves; i++)
|
||||
hb_always_assert (roots.has (leaves[i] - 1));
|
||||
|
||||
hb_always_assert (graph.update_parents ().is_ok ());
|
||||
hb_always_assert (graph.vertices_[leaves[0] - 1].incoming_edges () == 1);
|
||||
hb_always_assert (graph.vertices_[num_vertices].incoming_edges () == num_parents);
|
||||
for (unsigned i = 1; i < num_leaves; i++)
|
||||
hb_always_assert (graph.vertices_[leaves[i] - 1].incoming_edges () == num_parents);
|
||||
|
||||
// Now that all incoming edges to the roots are 32 bit, nothing is left to isolate.
|
||||
hb_always_assert (!graph.isolate_subgraph (roots).value ());
|
||||
hb_always_assert (graph.vertices_.length == num_vertices + 1);
|
||||
|
||||
free (buffer);
|
||||
}
|
||||
|
||||
static void test_invalid_link_objidx ()
|
||||
{
|
||||
size_t buffer_size = 100;
|
||||
@@ -3398,6 +3466,7 @@ main (int argc, char **argv)
|
||||
test_duplicate_subgraph_vertex_limit ();
|
||||
test_resolve_shared_overflow_vertex_limit ();
|
||||
test_32bit_roots_traversal ();
|
||||
test_isolate_subgraph_shared_wide_parents ();
|
||||
|
||||
// TODO(grieger): have run overflow tests compare graph equality not final packed binary.
|
||||
// TODO(grieger): split test where multiple subtables in one lookup are split to test link ordering.
|
||||
|
||||
Reference in new issue
Block a user