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 2fb59ee98..f220797a2 100644 --- a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh +++ b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh @@ -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) 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 9b12f1598..f48e258aa 100644 --- a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh +++ b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh @@ -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) diff --git a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh index 1d8082e30..92b49534f 100644 --- a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh @@ -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) diff --git a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh index b1f1434ab..21afb3005 100644 --- a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh @@ -158,16 +158,20 @@ 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; + 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) 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-bit-set-invertible.hh b/src/hb-bit-set-invertible.hh index 0bb3cffa1..ee0b071b1 100644 --- a/src/hb-bit-set-invertible.hh +++ b/src/hb-bit-set-invertible.hh @@ -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 diff --git a/src/hb-bit-set.hh b/src/hb-bit-set.hh index 62dc24825..ba9a7a2b1 100644 --- a/src/hb-bit-set.hh +++ b/src/hb-bit-set.hh @@ -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 ()) diff --git a/src/hb-depend-data.hh b/src/hb-depend-data.hh index db4157570..b50c82791 100644 --- a/src/hb-depend-data.hh +++ b/src/hb-depend-data.hh @@ -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 lookup_feature_offsets; hb_vector_t edge_slots; unsigned int edge_population = 0; + int64_t budget_remaining = HB_BUDGET_DEPEND; hb_hashmap_t set_to_index; hb_vector_t free_set_list; hb_codepoint_t current_context_set_index; diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 5bffa574f..5bcb33dd3 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -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 diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 0fa0d2803..02473f971 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -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 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 645b9ea0e..32420de0d 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; }; @@ -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 (); 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 (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/src/hb-set.hh b/src/hb-set.hh index b9ef5df23..c52974a8c 100644 --- a/src/hb-set.hh +++ b/src/hb-set.hh @@ -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 (); } diff --git a/test/api/test-ot-depend.c b/test/api/test-ot-depend.c index 6530a80a9..449e8a4c4 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -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 (); diff --git a/test/api/test-ot-layout.c b/test/api/test-ot-layout.c index 51e094996..f3f727383 100644 --- a/test/api/test-ot-layout.c +++ b/test/api/test-ot-layout.c @@ -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