diff --git a/src/OT/Layout/Common/Coverage.hh b/src/OT/Layout/Common/Coverage.hh index 49b0bac96..b0d048c09 100644 --- a/src/OT/Layout/Common/Coverage.hh +++ b/src/OT/Layout/Common/Coverage.hh @@ -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 bool serialize (hb_serialize_context_t *c, Iterator glyphs) diff --git a/src/OT/Layout/GSUB/AlternateSet.hh b/src/OT/Layout/GSUB/AlternateSet.hh index 747ae6727..05d20d1f4 100644 --- a/src/OT/Layout/GSUB/AlternateSet.hh +++ b/src/OT/Layout/GSUB/AlternateSet.hh @@ -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 { diff --git a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh index b4b279b3a..166a9587d 100644 --- a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh +++ b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh @@ -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) diff --git a/src/OT/Layout/GSUB/Ligature.hh b/src/OT/Layout/GSUB/Ligature.hh index e99b1ded1..3009e2c68 100644 --- a/src/OT/Layout/GSUB/Ligature.hh +++ b/src/OT/Layout/GSUB/Ligature.hh @@ -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); } diff --git a/src/OT/Layout/GSUB/LigatureSet.hh b/src/OT/Layout/GSUB/LigatureSet.hh index 40fd6940b..1411654ef 100644 --- a/src/OT/Layout/GSUB/LigatureSet.hh +++ b/src/OT/Layout/GSUB/LigatureSet.hh @@ -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 &_) { _.closure (c); }) diff --git a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh index 7295bffd9..3b40a195f 100644 --- a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh +++ b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh @@ -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) diff --git a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh index 1f686c92f..79da5baf8 100644 --- a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh @@ -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) diff --git a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh index acf46d12c..20159dac3 100644 --- a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh @@ -142,11 +142,15 @@ struct ReverseChainSingleSubstFormat1_2 void closure (hb_closure_context_t *c) const { - if (!intersects (c->glyphs)) return; - const auto &lookahead = StructAfter (backtrack); const auto &substitute = StructAfter (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) diff --git a/src/OT/Layout/GSUB/Sequence.hh b/src/OT/Layout/GSUB/Sequence.hh index 9c5dc864d..94ae1cc1a 100644 --- a/src/OT/Layout/GSUB/Sequence.hh +++ b/src/OT/Layout/GSUB/Sequence.hh @@ -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 { diff --git a/src/OT/Layout/GSUB/SingleSubstFormat1.hh b/src/OT/Layout/GSUB/SingleSubstFormat1.hh index d072a66f7..44526d847 100644 --- a/src/OT/Layout/GSUB/SingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/SingleSubstFormat1.hh @@ -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) diff --git a/src/OT/Layout/GSUB/SingleSubstFormat2.hh b/src/OT/Layout/GSUB/SingleSubstFormat2.hh index 1b537983c..2c1d62147 100644 --- a/src/OT/Layout/GSUB/SingleSubstFormat2.hh +++ b/src/OT/Layout/GSUB/SingleSubstFormat2.hh @@ -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) diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 75ab4d9ca..befcf9e02 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -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 diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index abeb01519..c8120dbd6 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -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 bool serialize (hb_serialize_context_t *c, Iterator it_with_class_zero) diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 443d83ced..5e030c581 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -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 - 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 + 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> *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 (input); + const auto &lookup = StructAfter (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 (input); - const auto &lookup = StructAfter (lookahead); struct ChainContextClosureLookupContext lookup_context = { {intersects_coverage, intersected_coverage_glyphs}, ContextFormat::CoverageBasedContext, diff --git a/test/api/test-ot-layout.c b/test/api/test-ot-layout.c index 69cbf22ba..b6bec8895 100644 --- a/test/api/test-ot-layout.c +++ b/test/api/test-ot-layout.c @@ -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