[layout] Charge GSUB glyph closure to a per-session work budget

hb_closure_context_t bounded GSUB glyph closure only by counting lookup
visits (HB_MAX_LOOKUP_VISIT_COUNT per round), and forgets which lookups
are done whenever the glyph set grows, so what one visit walks -- through
aliased offsets, many times the table size -- was charged to nothing.  A
LigatureSubst with 9000 ligatureSet offsets aliased onto one 64-component
Ligature, re-walked after every one of 1700 Context triggers that each
add a glyph, makes hb-subset --unicodes='*' of an 86 KB font spend ~110 s
of CPU.  A ReverseChain/ChainContextFormat3 with aliased backtrack
Coverages is a second way in.

Give the context an int64_t budget (HB_BUDGET_LAYOUT_CLOSURE records, in
the hb_budget_spend() idiom) spent as each closure() implementation walks
its arrays: dispatch() charges a visit its coverage record count;
ligature sets / ligatures / components, sequences, alternates,
single-subst intersections, rule sets and rules, context Coverages (via a
spend_coverages() helper) and the per-rule-set ClassDef walks are each
charged.  Coverage and ClassDef gain a get_record_count() helper because
intersects()/intersect_set() are linear in records, not population.  Once
the budget is spent, should_visit_lookup()/lookup_limit_exceeded() stop
the closure early, exactly as they do at the visit limit.

The budget lives in the context, so both public closure entry points (also
reached from hb_ot_shape_glyphs_closure() and the subset plan) are covered
with no API/ABI change.  Both attack shapes drop to ~1 s and the control
font's closure output is byte-identical; the heaviest real font in the
corpora uses about 1% of the budget.  A hostile GSUB now gets a truncated
closure, as one hitting the visit limit already does.
test_ot_layout_lookups_substitute_closure_budget builds the hostile table
in C and drives hb_ot_layout_lookups_substitute_closure() over it.

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

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 9b4e3df364
commit 70df3ff279
15 files changed
+233 -15

No files matched your search

+14
View File
@@ -139,6 +139,20 @@ struct Coverage
}
}
/* Number of glyph or range records; what walking the table costs. */
unsigned get_record_count () const
{
switch (u.format.v) {
case 1: hb_barrier (); return u.format1.glyphArray.len;
case 2: hb_barrier (); return u.format2.rangeRecord.len;
#ifndef HB_NO_BEYOND_64K
case 3: hb_barrier (); return u.format3.glyphArray.len;
case 4: hb_barrier (); return u.format4.rangeRecord.len;
#endif
default:return 0;
}
}
template <typename Iterator,
hb_requires (hb_is_sorted_source_of (Iterator, hb_codepoint_t))>
bool serialize (hb_serialize_context_t *c, Iterator glyphs)
+4 -1
View File
@@ -27,7 +27,10 @@ struct AlternateSet
{ return hb_any (alternates, glyphs); }
void closure (hb_closure_context_t *c) const
{ c->output->add_array (alternates.arrayZ, alternates.len); }
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, alternates.len))) return;
c->output->add_array (alternates.arrayZ, alternates.len);
}
void depend (hb_depend_context_t *c, hb_codepoint_t source) const
{
@@ -49,6 +49,7 @@ struct AlternateSubstFormat1_2
void closure (hb_closure_context_t *c) const
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, alternateSet.len))) return;
+ hb_zip (this+coverage, alternateSet)
| hb_filter (c->parent_active_glyphs (), hb_first)
| hb_map (hb_second)
+1
View File
@@ -34,6 +34,7 @@ struct Ligature
void closure (hb_closure_context_t *c) const
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, component.lenP1))) return;
if (!intersects (c->glyphs)) return;
c->output->add (ligGlyph);
}
+1
View File
@@ -48,6 +48,7 @@ struct LigatureSet
void closure (hb_closure_context_t *c) const
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, ligature.len))) return;
+ hb_iter (ligature)
| hb_map (hb_add (this))
| hb_apply ([c] (const Ligature<Types> &_) { _.closure (c); })
@@ -57,6 +57,7 @@ struct LigatureSubstFormat1_2
void closure (hb_closure_context_t *c) const
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, ligatureSet.len))) return;
+ hb_zip (this+coverage, ligatureSet)
| hb_filter (c->parent_active_glyphs (), hb_first)
| hb_map (hb_second)
@@ -36,6 +36,7 @@ struct MultipleSubstFormat1_2
void closure (hb_closure_context_t *c) const
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, sequence.len))) return;
+ hb_zip (this+coverage, sequence)
| hb_filter (c->parent_active_glyphs (), hb_first)
| hb_map (hb_second)
@@ -142,11 +142,15 @@ struct ReverseChainSingleSubstFormat1_2
void closure (hb_closure_context_t *c) const
{
if (!intersects (c->glyphs)) return;
const auto &lookahead = StructAfter<decltype (lookaheadX)> (backtrack);
const auto &substitute = StructAfter<decltype (substituteX)> (lookahead);
if (unlikely (!c->spend_coverages (this, backtrack) ||
!c->spend_coverages (this, lookahead)))
return;
if (!intersects (c->glyphs)) return;
if (unlikely (!c->spend_budget (HB_BUDGET_1, substitute.len))) return;
+ hb_zip (this+coverage, substitute)
| hb_filter (c->parent_active_glyphs (), hb_first)
| hb_map (hb_second)
+4 -1
View File
@@ -26,7 +26,10 @@ struct Sequence
{ return hb_all (substitute, glyphs); }
void closure (hb_closure_context_t *c) const
{ c->output->add_array (substitute.arrayZ, substitute.len); }
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, substitute.len))) return;
c->output->add_array (substitute.arrayZ, substitute.len);
}
void depend (hb_depend_context_t *c, hb_codepoint_t source) const
{
+1
View File
@@ -77,6 +77,7 @@ void single_subst_closure (hb_closure_context_t *c,
(min_before <= max_after && max_after <= max_before)))
return;
if (unlikely (!c->spend_budget (HB_BUDGET_1, intersection.get_population ()))) return;
+ hb_iter (intersection)
| hb_map ([d, mask] (hb_codepoint_t g) { return (g + d) & mask; })
| hb_sink (c->output)
+2
View File
@@ -66,6 +66,7 @@ struct SingleSubstFormat2_4
if (substitute.len > glyph_set.get_population () * 4)
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, glyph_set.get_population ()))) return;
for (auto g : glyph_set)
{
unsigned i = cov.get_coverage (g);
@@ -77,6 +78,7 @@ struct SingleSubstFormat2_4
return;
}
if (unlikely (!c->spend_budget (HB_BUDGET_1, substitute.len))) return;
+ hb_zip (cov, substitute)
| hb_filter (glyph_set, hb_first)
| hb_map (hb_second)
+9
View File
@@ -288,6 +288,15 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
#define HB_BUDGET_DEPEND ((int64_t) 1 << 26)
#endif
/* One GSUB glyph-closure session, in records (glyphs, offsets, ranges,
* rules) walked. HB_MAX_LOOKUP_VISIT_COUNT bounds how many times lookups
* are entered per round, not what one visit walks: through shared offsets
* a subtable can walk many times the size of its table, and every glyph
* the closure gains makes all lookups visitable again. */
#ifndef HB_BUDGET_LAYOUT_CLOSURE
#define HB_BUDGET_LAYOUT_CLOSURE ((int64_t) 1 << 28)
#endif
#ifndef HB_REPACKER_MAX_ITERATIONS
#define HB_REPACKER_MAX_ITERATIONS 500
+14
View File
@@ -2139,6 +2139,20 @@ struct ClassDef
}
}
/* Number of class or range records; what walking the table costs. */
unsigned get_record_count () const
{
switch (u.format.v) {
case 1: hb_barrier (); return u.format1.classValue.len;
case 2: hb_barrier (); return u.format2.rangeRecord.len;
#ifndef HB_NO_BEYOND_64K
case 3: hb_barrier (); return u.format3.classValue.len;
case 4: hb_barrier (); return u.format4.rangeRecord.len;
#endif
default:return 0;
}
}
template<typename Iterator,
hb_requires (hb_is_sorted_source_of (Iterator, hb_codepoint_t))>
bool serialize (hb_serialize_context_t *c, Iterator it_with_class_zero)
+58 -11
View File
@@ -70,7 +70,13 @@ struct hb_closure_context_t :
{
typedef return_t (*recurse_func_t) (hb_closure_context_t *c, unsigned lookup_index, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index);
template <typename T>
return_t dispatch (const T &obj) { obj.closure (this); return hb_empty_t (); }
return_t dispatch (const T &obj)
{
/* Every subtable visit walks its coverage at least once. */
if (likely (spend_budget (HB_BUDGET_1, 1 + obj.get_coverage ().get_record_count ())))
obj.closure (this);
return hb_empty_t ();
}
static return_t default_return_value () { return hb_empty_t (); }
void recurse (unsigned lookup_index, hb_set_t *covered_seq_indicies, unsigned seq_index, unsigned end_index)
{
@@ -86,11 +92,28 @@ struct hb_closure_context_t :
{ lookup_count = 0; }
bool lookup_limit_exceeded ()
{ return lookup_count > HB_MAX_LOOKUP_VISIT_COUNT; }
{ return lookup_count > HB_MAX_LOOKUP_VISIT_COUNT || budget < 0; }
/* The visit count does not bound the work: a visit can walk many times
* the size of the table through shared offsets, and every glyph added to
* the closure makes all lookups visitable again. closure() implementations
* charge what they walk here; once the budget is spent the closure stops,
* as it does at the visit limit. */
bool spend_budget (unsigned int cost, unsigned int mult = 1)
{ return hb_budget_spend (budget, cost, mult); }
template <typename Base, typename Offsets>
bool spend_coverages (const Base *base, const Offsets &coverages)
{
for (const auto &offset : coverages)
if (unlikely (!spend_budget (HB_BUDGET_1, 1 + (base+offset).get_record_count ())))
return false;
return true;
}
bool should_visit_lookup (unsigned int lookup_index)
{
if (lookup_count++ > HB_MAX_LOOKUP_VISIT_COUNT)
if (lookup_count++ > HB_MAX_LOOKUP_VISIT_COUNT || budget < 0)
return false;
if (is_lookup_done (lookup_index))
@@ -200,6 +223,7 @@ struct hb_closure_context_t :
hb_map_t *done_lookups_glyph_count;
hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> *done_lookups_glyph_set;
unsigned int lookup_count = 0;
int64_t budget = HB_BUDGET_LAYOUT_CLOSURE;
};
@@ -2937,6 +2961,8 @@ static inline void context_closure_lookup (hb_closure_context_t *c,
unsigned value, /* Index of first glyph in Coverage or Class value in ClassDef table */
ContextClosureLookupContext &lookup_context)
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, inputCount + lookupCount))) return;
if (context_intersects (c->glyphs,
inputCount, input,
lookup_context))
@@ -3312,6 +3338,7 @@ struct RuleSetOf
ContextClosureLookupContext &lookup_context) const
{
if (unlikely (c->lookup_limit_exceeded ())) return;
if (unlikely (!c->spend_budget (HB_BUDGET_1, rule.len))) return;
return
+ hb_iter (rule)
@@ -3605,6 +3632,8 @@ struct ContextFormat1_4
void closure (hb_closure_context_t *c) const
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, ruleSet.len))) return;
hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs)) return;
get_coverage ().intersect_set (c->previous_parent_active_glyphs (), *cur_active_glyphs);
@@ -3819,13 +3848,16 @@ struct ContextFormat2_5
if (!(this+coverage).intersects (c->glyphs))
return;
/* Filtering the rule sets walks the ClassDef once per set. */
const ClassDef &class_def = this+classDef;
if (unlikely (!c->spend_budget (ruleSet.len, 1 + class_def.get_record_count ())))
return;
hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs)) return;
get_coverage ().intersect_set (c->previous_parent_active_glyphs (),
*cur_active_glyphs);
const ClassDef &class_def = this+classDef;
hb_map_t cache;
intersected_class_cache_t intersected_cache;
struct ContextClosureLookupContext lookup_context = {
@@ -4107,6 +4139,8 @@ struct ContextFormat3_6
{
if (!(this+coverageZ[0]).intersects (c->glyphs))
return;
if (unlikely (!c->spend_coverages (this, coverageZ.as_array (glyphCount))))
return;
hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs)) return;
@@ -4351,6 +4385,8 @@ static inline void chain_context_closure_lookup (hb_closure_context_t *c,
unsigned value,
ChainContextClosureLookupContext &lookup_context)
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, backtrackCount + inputCount + lookaheadCount + lookupCount))) return;
if (chain_context_intersects (c->glyphs,
backtrackCount, backtrack,
inputCount, input,
@@ -4914,6 +4950,7 @@ struct ChainRuleSetOf
void closure (hb_closure_context_t *c, unsigned value, ChainContextClosureLookupContext &lookup_context) const
{
if (unlikely (c->lookup_limit_exceeded ())) return;
if (unlikely (!c->spend_budget (HB_BUDGET_1, rule.len))) return;
return
+ hb_iter (rule)
@@ -5232,6 +5269,8 @@ struct ChainContextFormat1_4
void closure (hb_closure_context_t *c) const
{
if (unlikely (!c->spend_budget (HB_BUDGET_1, ruleSet.len))) return;
hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs)) return;
get_coverage ().intersect_set (c->previous_parent_active_glyphs (),
@@ -5452,15 +5491,18 @@ struct ChainContextFormat2_5
if (!(this+coverage).intersects (c->glyphs))
return;
/* Filtering the rule sets walks the input ClassDef once per set. */
const ClassDef &backtrack_class_def = this+backtrackClassDef;
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
if (unlikely (!c->spend_budget (ruleSet.len, 1 + input_class_def.get_record_count ())))
return;
hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs)) return;
get_coverage ().intersect_set (c->previous_parent_active_glyphs (),
*cur_active_glyphs);
const ClassDef &backtrack_class_def = this+backtrackClassDef;
const ClassDef &input_class_def = this+inputClassDef;
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
hb_map_t caches[3] = {};
intersected_class_cache_t intersected_cache;
struct ChainContextClosureLookupContext lookup_context = {
@@ -5851,14 +5893,19 @@ struct ChainContextFormat3
if (!(this+input[0]).intersects (c->glyphs))
return;
const auto &lookahead = StructAfter<decltype (lookaheadX)> (input);
const auto &lookup = StructAfter<decltype (lookupX)> (lookahead);
if (unlikely (!c->spend_coverages (this, backtrack) ||
!c->spend_coverages (this, input) ||
!c->spend_coverages (this, lookahead)))
return;
hb_set_t* cur_active_glyphs = c->push_cur_active_glyphs ();
if (unlikely (!cur_active_glyphs))
return;
get_coverage ().intersect_set (c->previous_parent_active_glyphs (),
*cur_active_glyphs);
const auto &lookahead = StructAfter<decltype (lookaheadX)> (input);
const auto &lookup = StructAfter<decltype (lookupX)> (lookahead);
struct ChainContextClosureLookupContext lookup_context = {
{intersects_coverage, intersected_coverage_glyphs},
ContextFormat::CoverageBasedContext,
+116
View File
@@ -293,6 +293,121 @@ test_ot_layout_language_get_feature_tags (void)
hb_face_destroy (face);
}
static char *
put16 (char *p, unsigned v)
{
*p++ = (char) (v >> 8);
*p++ = (char) v;
return p;
}
static void
test_ot_layout_lookups_substitute_closure_budget (void)
{
/* A GSUB whose glyph closure walks far more than the table's size per
* lookup visit: a LigatureSubst with SETS offsets to one LigatureSet of
* four offsets to one Ligature of COMPS components, all already in the
* closure. CHAINS ContextSubst lookups each add one glyph to the
* closure through a SingleSubst and then recurse into the ligature
* lookup with every glyph active, so the memo of visited lookups is
* voided and the ligature lookup is walked again after each of them,
* in each closure round. Unbudgeted, that is over a minute of work
* for a table the sanitizer accepts; the work budget must stop it. */
const unsigned CHAINS = 1000, STRIDE = 56, SETS = 9000, COMPS = 64;
const unsigned first_chain = 1;
const unsigned first_comp = first_chain + CHAINS * STRIDE;
const unsigned lig_glyph = first_comp + SETS;
const unsigned lookup_count = 2 + CHAINS;
const unsigned single_size = 8 + 6 + 4 + 6 * CHAINS;
const unsigned ligature_size = 8 + 6 + 2 * SETS + 10 + 10 + 4 + 2 * (COMPS - 1);
const unsigned context_size = 8 + 16 + 10;
const unsigned lookup_list = 14;
const unsigned table_size = lookup_list + 2 + 2 * lookup_count +
single_size + ligature_size + CHAINS * context_size;
/* The sanitizer walks the aliased ligatures too, and allows 64 bytes
* of that per table byte. */
const unsigned gsub_size = table_size + 32768;
char *gsub = g_malloc0 (gsub_size);
char *p = gsub;
unsigned lookup, i;
p = put16 (p, 1); p = put16 (p, 0); /* Version 1.0 */
p = put16 (p, 10); p = put16 (p, 12); p = put16 (p, lookup_list);
p = put16 (p, 0); /* Empty ScriptList */
p = put16 (p, 0); /* Empty FeatureList */
p = put16 (p, lookup_count);
lookup = 2 + 2 * lookup_count;
p = put16 (p, lookup); lookup += single_size;
p = put16 (p, lookup); lookup += ligature_size;
for (i = 0; i < CHAINS; i++)
{
p = put16 (p, lookup); lookup += context_size;
}
/* Lookup 0: SingleSubstFormat1 with delta +2 over each chain's range,
* so every chain gains one glyph per visit. */
p = put16 (p, 1); p = put16 (p, 0); p = put16 (p, 1); p = put16 (p, 8);
p = put16 (p, 1); p = put16 (p, 6); p = put16 (p, 2);
p = put16 (p, 2); p = put16 (p, CHAINS);
for (i = 0; i < CHAINS; i++)
{
unsigned start = first_chain + i * STRIDE;
p = put16 (p, start); p = put16 (p, start + STRIDE - 4); p = put16 (p, i * (STRIDE - 3));
}
/* Lookup 1: LigatureSubstFormat1 with SETS aliased LigatureSet offsets. */
p = put16 (p, 4); p = put16 (p, 0); p = put16 (p, 1); p = put16 (p, 8);
p = put16 (p, 1); p = put16 (p, 6 + 2 * SETS); p = put16 (p, SETS);
for (i = 0; i < SETS; i++)
p = put16 (p, 6 + 2 * SETS + 10);
p = put16 (p, 2); p = put16 (p, 1);
p = put16 (p, first_comp); p = put16 (p, first_comp + SETS - 1); p = put16 (p, 0);
p = put16 (p, 4);
for (i = 0; i < 4; i++)
p = put16 (p, 10);
p = put16 (p, lig_glyph); p = put16 (p, COMPS);
for (i = 1; i < COMPS; i++)
p = put16 (p, first_comp + i);
/* Lookups 2..: ContextSubstFormat3 over one chain, recursing into
* lookup 0 and then lookup 1 at sequence index 0. */
for (i = 0; i < CHAINS; i++)
{
unsigned start = first_chain + i * STRIDE;
p = put16 (p, 5); p = put16 (p, 0); p = put16 (p, 1); p = put16 (p, 8);
p = put16 (p, 3); p = put16 (p, 1); p = put16 (p, 2); p = put16 (p, 16);
p = put16 (p, 0); p = put16 (p, 0);
p = put16 (p, 0); p = put16 (p, 1);
p = put16 (p, 2); p = put16 (p, 1);
p = put16 (p, start); p = put16 (p, start + STRIDE - 1); p = put16 (p, 0);
}
g_assert_cmpuint (p - gsub, ==, table_size);
hb_face_t *face = hb_face_builder_create ();
hb_blob_t *blob = hb_blob_create (gsub, gsub_size, HB_MEMORY_MODE_READONLY, NULL, NULL);
g_assert_true (hb_face_builder_add_table (face, HB_OT_TAG_GSUB, blob));
hb_blob_destroy (blob);
hb_face_set_glyph_count (face, lig_glyph + 1);
g_assert_cmpuint (hb_ot_layout_table_get_lookup_count (face, HB_OT_TAG_GSUB), ==, lookup_count);
hb_set_t *glyphs = hb_set_create ();
for (i = 0; i < CHAINS; i++)
hb_set_add (glyphs, first_chain + i * STRIDE);
hb_set_add_range (glyphs, first_comp, first_comp + SETS - 1);
hb_ot_layout_lookups_substitute_closure (face, NULL, glyphs);
/* Wherever the budget cut in, the first round's plain substitutions
* and the ligature were reached before it. */
for (i = 0; i < CHAINS; i++)
g_assert_true (hb_set_has (glyphs, first_chain + i * STRIDE + 2));
g_assert_true (hb_set_has (glyphs, lig_glyph));
hb_set_destroy (glyphs);
hb_face_destroy (face);
g_free (gsub);
}
#ifndef HB_NO_VAR
static void
test_ot_layout_collect_lookup_variations (void)
@@ -340,6 +455,7 @@ main (int argc, char **argv)
hb_test_add (test_ot_layout_script_get_language_tags);
hb_test_add (test_ot_layout_table_get_feature_tags);
hb_test_add (test_ot_layout_language_get_feature_tags);
hb_test_add (test_ot_layout_lookups_substitute_closure_budget);
#ifndef HB_NO_VAR
hb_test_add (test_ot_layout_collect_lookup_variations);
#endif