[limits] depend build budget + GSUB closure budget (#6526)

* [subset] Charge Coverage and ClassDef glyph walks to a work budget

Coverage::subset() expands a format 2 Coverage glyph by glyph, up to
numGlyphs, every time an offset to it is subset, and nothing bounded
that work.  A 10-byte range Coverage referenced by 30,000 two-byte
offsets in a subtable listed 8 times costs 1.6e10 iterator steps,
while the sanitizer only bounds work by table bytes; a 60 KB font
keeps hb-subset busy ~50 s.  GDEF's MarkGlyphSets reaches the same
walk with no feature or lookup needing to survive planning.

Give hb_subset_context_t a per-table work budget
(HB_BUDGET_SUBSET_TABLE, in the hb_budget_spend() idiom of
hb-limits.hh) with a check_ops() helper that fails the serializer once
exhausted, and charge Coverage::subset() its capped population before
the walk.  Charging inside Coverage::subset() covers every caller
(ReverseChainSingleSubst, (Chain)ContextFormat3, GDEF MarkGlyphSets,
...) without touching them.  The same unbounded per-reference pattern
in ClassDefFormat1_3 / ClassDefFormat2_4::subset() (the range or
glyphset walk and the glyph_filter count) is charged the same way.

A hostile table that exceeds the budget now fails the subset cleanly
(hb_subset_or_fail returns NULL) in a fraction of a second instead of
succeeding slowly; valid-font output is byte-identical and the
heaviest local table spends a small fraction of the budget.
test_subset_shared_coverage_budget builds the reverse-chaining GSUB in
memory and asserts 256 shared references subset while 2048 fail.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [subset] Charge layout glyph walks to the existing subset budget

Drop the separate HB_BUDGET_SUBSET_TABLE budget and the second
hb_subset_context_t field it needed; check_ops() now charges
budget_remaining (HB_BUDGET_SUBSET_GLYF, 1 << 28), the per-table
budget glyf instancing already uses.  Both are per-table and shared
across buffer-size retries, and no table spends both.

The larger budget still bounds the attack (fails after 2^28 glyph
steps, ~3 s), so test_subset_shared_coverage_budget now expects the
failure at 8192 shared offsets instead of 2048.

Subsetting all glyphs with --layout-features='*' of Gulzar,
NotoSansKR-VF and GoogleSansFlex-VF keeps GSUB/GPOS/GDEF with output
byte-identical to before; the heaviest table (Gulzar GPOS) spends
~98K units, under 0.04% of the budget.  Full meson test: 278 OK.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [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>

* [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>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Behdad EsfahbodandClaude Opus 4.8 authored and GitHub committed 2026-11-10 22:29:59 -06:00
commit 0601dd774e
20 files changed
+387 -21

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
{
@@ -70,6 +70,7 @@ struct AlternateSubstFormat1_2
if (alternateSet.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);
@@ -81,6 +82,7 @@ struct AlternateSubstFormat1_2
return;
}
if (unlikely (!c->spend_budget (HB_BUDGET_1, alternateSet.len))) return;
+ hb_zip (cov, alternateSet)
| hb_filter (glyph_set, 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); })
@@ -78,6 +78,7 @@ struct LigatureSubstFormat1_2
if (ligatureSet.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);
@@ -89,6 +90,7 @@ struct LigatureSubstFormat1_2
return;
}
if (unlikely (!c->spend_budget (HB_BUDGET_1, ligatureSet.len))) return;
+ hb_zip (cov, ligatureSet)
| hb_filter (glyph_set, hb_first)
| hb_map (hb_second)
@@ -41,6 +41,7 @@ struct MultipleSubstFormat1_2
if (sequence.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);
@@ -52,6 +53,7 @@ struct MultipleSubstFormat1_2
return;
}
if (unlikely (!c->spend_budget (HB_BUDGET_1, sequence.len))) return;
+ hb_zip (cov, sequence)
| hb_filter (glyph_set, hb_first)
| hb_map (hb_second)
@@ -158,16 +158,20 @@ 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;
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
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);
@@ -179,6 +183,7 @@ struct ReverseChainSingleSubstFormat1_2
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)
+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)
+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;
+18
View File
@@ -287,6 +287,24 @@ 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
/* 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
@@ -2160,6 +2160,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)
+87 -17
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;
};
@@ -686,6 +710,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 ();
@@ -1760,13 +1787,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 ();
@@ -2901,6 +2944,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))
@@ -3276,6 +3321,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)
@@ -3569,6 +3615,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);
@@ -3801,13 +3849,17 @@ struct ContextFormat2_5
if (!(this+coverage).intersects (c->glyphs))
return;
/* Charge the rule-set walk and both ClassDef scans. */
const ClassDef &class_def = this+classDef;
if (unlikely (!c->spend_budget (HB_BUDGET_1, ruleSet.len) ||
!c->spend_budget (HB_BUDGET_2, 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_set_t cache;
class_def.intersected_classes (c->glyphs, &cache);
intersected_class_cache_t intersected_cache;
@@ -4106,6 +4158,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;
@@ -4350,6 +4404,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,
@@ -4913,6 +4969,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)
@@ -5231,6 +5288,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 (),
@@ -5473,15 +5532,21 @@ struct ChainContextFormat2_5
if (!(this+coverage).intersects (c->glyphs))
return;
/* Charge the rule-set walk and the ClassDef scans for both glyph sets. */
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 (HB_BUDGET_1, ruleSet.len) ||
!c->spend_budget (HB_BUDGET_1, 1 + backtrack_class_def.get_record_count ()) ||
!c->spend_budget (HB_BUDGET_2, 1 + input_class_def.get_record_count ()) ||
!c->spend_budget (HB_BUDGET_1, 1 + lookahead_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_set_t caches[3] = {};
backtrack_class_def.intersected_classes (c->glyphs, &caches[0]);
input_class_def.intersected_classes (c->glyphs, &caches[1]);
@@ -5898,14 +5963,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,
+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
@@ -812,6 +812,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
@@ -829,6 +927,8 @@ main (int argc, char **argv)
hb_test_add (test_depend_feature_variations);
#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 ();
+116
View File
@@ -348,6 +348,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)
@@ -480,6 +595,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