[depend] Clip context glyph sets to the face and charge a build budget

While extracting GSUB dependencies, find_or_create_glyph_set() turned
every Coverage (format 3 contexts) and class (format 2 contexts) that
a contextual rule mentions into an hb_set_t holding every glyph id the
table lists, whether the face has it or not, with no budget on the
expansion or on the second copy kept in the returned graph.  In
experimental builds, where the depend API and 24-bit Coverage formats
live together, a 6.5 KB font with 400 backtrack Coverage format 4
tables each covering [0, 0xFFFFFF] drives a build to ~2 GB.

Clip the cached sets to the face in find_or_create_glyph_set(), the
one place every context position goes through (the first input
Coverage and ReverseChainSingleSubst already clip this way), so the
copies kept in the graph shrink with them.  For real fonts this
changes nothing; only broken fonts stop carrying glyph ids past the
face.

Clipping alone is not enough: the temporary is still expanded in full,
and the glyph count itself is cheap to inflate with a MAXP table.  So
add a work budget for the whole build in the existing hb_budget_spend()
style: HB_BUDGET_DEPEND, budget_remaining on hb_depend_data_builder_t,
and charge_set(), which spends HB_BUDGET_4 per page while expanding a
temporary and HB_BUDGET_32 per page for every set the graph or context
keeps (new_set(), find_or_create_glyph_set(), find_or_create_recurse_set()).
Running out fails the build and hb_subset_depend_from_face_or_fail()
returns NULL, as it already does on allocation failure.  A
get_page_count() accessor is added to the bit-set types so the charge
does not reach into set internals; no public API change.

The 400-position font now peaks at ~11 MB instead of ~2 GB, the 64 KB
/ 4000-position font is refused, and hb-subset-depend output is
unchanged for real fonts.  Regression tests build the face with
hb_face_builder_create(): one asserts no glyph above the face count is
kept, the other that a 600-range face is refused.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 committed 2026-11-10 16:42:16 -06:00
1 parent 1af279ed98
commit 9b4e3df364
7 files changed
+146 -6

No files matched your search

+1
View File
@@ -99,6 +99,7 @@ struct hb_bit_set_invertible_t
previous (&v);
return v;
}
unsigned int get_page_count () const { return s.get_page_count (); }
unsigned int get_population () const
{ return inverted ? INVALID - s.get_population () : s.get_population (); }
bool get_singleton (hb_codepoint_t *codepoint) const
+1
View File
@@ -1056,6 +1056,7 @@ struct hb_bit_set_t
}
bool has_population () const { return population != UINT_MAX; }
unsigned int get_page_count () const { return pages.length; }
unsigned int get_population () const
{
if (has_population ())
+9
View File
@@ -306,10 +306,18 @@ struct hb_depend_data_builder_t
check_success (free_set_list.push_or_fail (set_index));
}
/* Charge a set for its pages: the memory it holds once stored, or, at a
* lower cost, the work of filling a temporary one. */
bool charge_set (const hb_set_t &set, unsigned cost = HB_BUDGET_32)
{ return check_success (hb_budget_spend (budget_remaining, cost, 1 + set.get_page_count ())); }
hb_codepoint_t new_set (const hb_set_t &set)
{
hb_codepoint_t set_index;
if (unlikely (!charge_set (set)))
return fail_invalid ();
if (free_set_list.length > 0)
{
set_index = free_set_list.pop ();
@@ -656,6 +664,7 @@ struct hb_depend_data_builder_t
hb_vector_t<unsigned> lookup_feature_offsets;
hb_vector_t<uint64_t> edge_slots;
unsigned int edge_population = 0;
int64_t budget_remaining = HB_BUDGET_DEPEND;
hb_hashmap_t<const hb_set_t*, hb_codepoint_t> set_to_index;
hb_vector_t<hb_codepoint_t> free_set_list;
hb_codepoint_t current_context_set_index;
+9
View File
@@ -279,6 +279,15 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
#define HB_BUDGET_AAT_MAP ((int64_t) 1 << 24)
#endif
/* One dependency graph build. A few bytes of Coverage or ClassDef range
* can name any number of glyphs, so the glyph sets built from them are
* charged per page: for the work of expanding them, and for the memory
* they hold once the graph keeps a copy. Exceeding the budget fails the
* build. */
#ifndef HB_BUDGET_DEPEND
#define HB_BUDGET_DEPEND ((int64_t) 1 << 26)
#endif
#ifndef HB_REPACKER_MAX_ITERATIONS
#define HB_REPACKER_MAX_ITERATIONS 500
+25 -6
View File
@@ -686,6 +686,9 @@ struct hb_depend_context_t :
return true;
}
if (unlikely (!depend_data->charge_set (set)))
return false;
hb_set_t *copy = hb_object_create<hb_set_t> ();
if (unlikely (!copy))
return depend_data->fail ();
@@ -1763,13 +1766,29 @@ hb_depend_context_t::find_or_create_glyph_set (const void *data,
return nullptr;
}
if (type == GLYPH_SET_COVERAGE)
collect_coverage (set, value, data);
else if (type == GLYPH_SET_CLASS)
collect_class (set, value, data);
else
/* A coverage or class range can name far more glyphs than the face has,
* and these sets are copied into the graph. Expand into a temporary,
* charged as work, and keep only the face's glyphs in a set sized for
* them, charged as memory. */
hb_set_t expanded;
if (type == GLYPH_SET_SINGLE)
set->add (value);
if (unlikely (set->in_error ()))
else
{
if (type == GLYPH_SET_COVERAGE)
collect_coverage (&expanded, value, data);
else
collect_class (&expanded, value, data);
if (unlikely (!depend_data->charge_set (expanded, HB_BUDGET_4)))
{
hb_set_destroy (set);
return nullptr;
}
expanded.intersect (*glyphs);
set->set (expanded);
}
if (unlikely (set->in_error () || expanded.in_error () ||
!depend_data->charge_set (*set)))
{
hb_set_destroy (set);
depend_data->fail ();
+1
View File
@@ -158,6 +158,7 @@ struct hb_sparseset_t
unsigned int next_many (hb_codepoint_t codepoint, hb_codepoint_t *out, unsigned int size) const
{ return s.next_many (codepoint, out, size); }
unsigned int get_page_count () const { return s.get_page_count (); }
unsigned int get_population () const { return s.get_population (); }
bool get_singleton (hb_codepoint_t *codepoint) const { return s.get_singleton (codepoint); }
hb_codepoint_t get_min () const { return s.get_min (); }
+100
View File
@@ -701,6 +701,104 @@ test_depend_context_format6 (void)
hb_subset_depend_destroy (depend);
hb_face_destroy (face);
}
/* A GSUB with one ChainContextSubst format 3 under feature 'test': one input
* glyph, no lookahead, no lookup records, and num_coverages backtrack
* positions, each with its own Coverage format 4 holding the single range
* [position, last_glyph - position]. */
static hb_face_t *
create_chain_context_face (unsigned num_glyphs,
unsigned num_coverages,
unsigned last_glyph)
{
unsigned coverages_offset = 12 + num_coverages * 2;
unsigned gsub_size = 10 + 20 + 14 + 12 + coverages_offset + 6 + num_coverages * 14;
unsigned char *gsub = g_malloc0 (gsub_size);
unsigned char *p = gsub;
#define PUT16(v) (*p++ = (unsigned char) ((v) >> 8), *p++ = (unsigned char) (v))
#define PUT24(v) (*p++ = (unsigned char) ((v) >> 16), PUT16 (v))
PUT16 (1); PUT16 (0); /* version */
PUT16 (10); PUT16 (10 + 20); PUT16 (10 + 20 + 14); /* scriptList, featureList, lookupList */
PUT16 (1); memcpy (p, "DFLT", 4); p += 4; PUT16 (8); /* ScriptList */
PUT16 (4); PUT16 (0); /* Script: defaultLangSys, langSysCount */
PUT16 (0); PUT16 (0xFFFF); PUT16 (1); PUT16 (0); /* LangSys */
PUT16 (1); memcpy (p, "test", 4); p += 4; PUT16 (8); /* FeatureList */
PUT16 (0); PUT16 (1); PUT16 (0); /* Feature */
PUT16 (1); PUT16 (4); /* LookupList */
PUT16 (6); PUT16 (0); PUT16 (1); PUT16 (8); /* Lookup */
PUT16 (3); /* ChainContextSubst format */
PUT16 (num_coverages); /* backtrackGlyphCount */
for (unsigned i = 0; i < num_coverages; i++)
PUT16 (coverages_offset + 6 + i * 14);
PUT16 (1); PUT16 (coverages_offset); /* inputGlyphCount, input coverage */
PUT16 (0); /* lookaheadGlyphCount */
PUT16 (0); /* seqLookupCount */
PUT16 (1); PUT16 (1); PUT16 (1); /* Coverage format 1: glyph 1 */
for (unsigned i = 0; i < num_coverages; i++)
{
PUT16 (4); PUT24 (1); /* Coverage format 4, rangeCount */
PUT24 (i); PUT24 (last_glyph - i); PUT24 (0); /* first, last, startCoverageIndex */
}
#undef PUT24
#undef PUT16
g_assert_true (p == gsub + gsub_size);
hb_face_t *face = hb_face_builder_create ();
hb_blob_t *blob = hb_blob_create ((const char *) gsub, gsub_size,
HB_MEMORY_MODE_READONLY, gsub, g_free);
g_assert_true (hb_face_builder_add_table (face, HB_TAG ('G','S','U','B'), blob));
hb_blob_destroy (blob);
hb_face_set_glyph_count (face, num_glyphs);
return face;
}
/* Context sets hold no glyph the face does not have, however far the
* Coverage ranges that name them reach. */
static void
test_depend_gsub_context_set_glyphs (void)
{
hb_face_t *face = create_chain_context_face (1000, 4, 0xFFFFFF);
hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
hb_set_t *set = hb_set_create ();
unsigned num_sets = 0;
for (hb_codepoint_t index = 0; hb_subset_depend_lookup_set (depend, index, set); index++)
{
for (hb_codepoint_t g = HB_SET_VALUE_INVALID; hb_set_next (set, &g);)
if (!(g & 0x80000000u)) /* Not a reference to another context set. */
g_assert_cmpuint (g, <, 1000);
num_sets++;
}
g_assert_cmpuint (num_sets, >=, 4);
/* The backtrack set of the first position covers the whole face. */
g_assert_true (hb_subset_depend_lookup_set (depend, 0, set));
g_assert_cmpuint (hb_set_get_population (set), ==, 1000);
g_assert_cmpuint (hb_set_get_max (set), ==, 999);
hb_set_destroy (set);
hb_subset_depend_destroy (depend);
hb_face_destroy (face);
}
/* A face whose context glyph sets take far more work to expand than it has
* bytes exhausts the build's budget and is refused. */
static void
test_depend_gsub_set_budget (void)
{
/* 600 ranges of 16,777,216 glyphs from under 10 KB of GSUB. */
hb_face_t *face = create_chain_context_face (1000, 600, 0xFFFFFF);
g_assert_null (hb_subset_depend_from_face_or_fail (face));
hb_face_destroy (face);
}
#endif
int
@@ -717,6 +815,8 @@ main (int argc, char **argv)
hb_test_add (test_depend_gsub_formats);
#ifndef HB_NO_BEYOND_64K
hb_test_add (test_depend_context_format6);
hb_test_add (test_depend_gsub_context_set_glyphs);
hb_test_add (test_depend_gsub_set_budget);
#endif
return hb_test_run ();