From c5b920a0f90aa94958212b4a790f4cd1b4b8ba4f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 17:23:33 -0600 Subject: [PATCH 1/4] [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 --- src/OT/Layout/Common/Coverage.hh | 5 ++ src/hb-limits.hh | 8 +++ src/hb-ot-layout-common.hh | 13 ++++- src/hb-subset.hh | 6 ++ test/api/test-subset.c | 97 ++++++++++++++++++++++++++++++++ 5 files changed, 127 insertions(+), 2 deletions(-) diff --git a/src/OT/Layout/Common/Coverage.hh b/src/OT/Layout/Common/Coverage.hh index 4b12a9e8f..49b0bac96 100644 --- a/src/OT/Layout/Common/Coverage.hh +++ b/src/OT/Layout/Common/Coverage.hh @@ -190,6 +190,11 @@ struct Coverage { TRACE_SUBSET (this); const hb_subset_plan_t *plan = c->plan; + + /* Ranges are walked glyph by glyph, once per offset that points at us. */ + if (unlikely (!c->check_ops (hb_min (get_population (), plan->source->get_num_glyphs ())))) + return_trace (false); + auto it = + iter () | hb_take (plan->source->get_num_glyphs ()) diff --git a/src/hb-limits.hh b/src/hb-limits.hh index b18e4e64c..af14c353c 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -276,6 +276,14 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_AAT_MAP ((int64_t) 1 << 24) #endif +/* Subsetting one table, in glyphs visited while walking its Coverage and + * ClassDef tables. A glyph range costs a few bytes and any number of + * offsets can share it, so neither the table size nor the sanitizer's + * budget bounds that work. */ +#ifndef HB_BUDGET_SUBSET_TABLE +#define HB_BUDGET_SUBSET_TABLE ((int64_t) 1 << 26) +#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 ba274a7f3..abeb01519 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -1614,6 +1614,8 @@ struct ClassDefFormat1_3 if (use_class_zero) { const hb_set_t& glyphset = *c->plan->glyphset_gsub (); + if (glyph_filter && unlikely (!c->check_ops (glyphset.get_population ()))) + return_trace (false); unsigned glyph_count = glyph_filter ? hb_len (hb_iter (glyphset) | hb_filter (glyph_filter)) : glyphset.get_population (); @@ -1866,8 +1868,13 @@ struct ClassDefFormat2_4 hb_sorted_vector_t glyph_and_klass; hb_set_t orig_klasses; - if (glyph_set.get_population () * hb_bit_storage ((unsigned) rangeRecord.len) - < get_population ()) + /* Either walk is repeated for every offset that points at us. */ + unsigned glyph_set_cost = glyph_set.get_population () * hb_bit_storage ((unsigned) rangeRecord.len); + unsigned population = get_population (); + if (unlikely (!c->check_ops (hb_min (glyph_set_cost, population)))) + return_trace (false); + + if (glyph_set_cost < population) { for (hb_codepoint_t g : glyph_set) { @@ -1902,6 +1909,8 @@ struct ClassDefFormat2_4 } const hb_set_t& glyphset = *c->plan->glyphset_gsub (); + if (glyph_filter && unlikely (!c->check_ops (glyphset.get_population ()))) + return_trace (false); unsigned glyph_count = glyph_filter ? hb_len (hb_iter (glyphset) | hb_filter (glyph_filter)) : glyphset.get_population (); diff --git a/src/hb-subset.hh b/src/hb-subset.hh index a44eb7b94..8827078bb 100644 --- a/src/hb-subset.hh +++ b/src/hb-subset.hh @@ -55,6 +55,11 @@ struct hb_subset_context_t : dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN ( _dispatch (obj, hb_prioritize, std::forward (ds)...) ) + /* Charge COUNT units of work that the size of the source table does + * not bound. Fails the table once its budget is exhausted. */ + bool check_ops (unsigned count) + { return serializer->check_success (hb_budget_spend (budget, count)); } + hb_blob_t *source_blob; hb_subset_plan_t *plan; hb_serialize_context_t *serializer; @@ -62,6 +67,7 @@ struct hb_subset_context_t : /* Outline work left for instancing this table. Lives here rather than in * the table's subset() so retries with a larger buffer share it. */ int64_t budget_remaining = HB_BUDGET_SUBSET_GLYF; + int64_t budget = HB_BUDGET_SUBSET_TABLE; hb_subset_context_t (hb_blob_t *source_blob_, hb_subset_plan_t *plan_, diff --git a/test/api/test-subset.c b/test/api/test-subset.c index 82d4b8bf5..a93b0e198 100644 --- a/test/api/test-subset.c +++ b/test/api/test-subset.c @@ -94,6 +94,102 @@ test_subset_crash (void) hb_face_destroy (face); } +static char * +_put16 (char *p, unsigned v) +{ + *p++ = (char) (v >> 8); + *p++ = (char) (v & 0xFF); + return p; +} + +/* A GSUB with one reverse-chaining lookup whose backtrack_count backtrack + * offsets all point at one Coverage holding the single range 0..range_last, + * in a face declaring 65535 glyphs. Each offset costs two bytes but makes + * the subsetter walk the whole range. */ +static hb_face_t * +_create_shared_coverage_face (unsigned backtrack_count, unsigned range_last) +{ + static const char cmap_data[] = { + 0, 0, 0, 1, 0, 3, 0, 1, 0, 0, 0, 12, + 0, 6, 0, 12, 0, 0, 0, 'a', 0, 1, 0, 1, /* format 6: 'a' -> glyph 1 */ + }; + static const char maxp_data[] = { 0, 0, 0x50, 0, (char) 0xFF, (char) 0xFF }; + + unsigned subtable_length = 12 + 2 * backtrack_count + 6 + 10; + unsigned gsub_length = 10 + 20 + 14 + 4 + 8 + subtable_length; + char *gsub = g_malloc (gsub_length); + char *p = gsub; + + p = _put16 (p, 1); p = _put16 (p, 0); + p = _put16 (p, 10); p = _put16 (p, 30); p = _put16 (p, 44); + /* ScriptList: DFLT, default LangSys using feature 0. */ + p = _put16 (p, 1); memcpy (p, "DFLT", 4); p += 4; p = _put16 (p, 8); + p = _put16 (p, 4); p = _put16 (p, 0); + p = _put16 (p, 0); p = _put16 (p, 0xFFFF); p = _put16 (p, 1); p = _put16 (p, 0); + /* FeatureList: calt, using lookup 0. */ + p = _put16 (p, 1); memcpy (p, "calt", 4); p += 4; p = _put16 (p, 8); + p = _put16 (p, 0); p = _put16 (p, 1); p = _put16 (p, 0); + /* LookupList: one type 8 lookup with one subtable. */ + p = _put16 (p, 1); p = _put16 (p, 4); + p = _put16 (p, 8); p = _put16 (p, 0); p = _put16 (p, 1); p = _put16 (p, 8); + /* ReverseChainSingleSubstFormat1 substituting glyph 1 by itself. */ + p = _put16 (p, 1); p = _put16 (p, 12 + 2 * backtrack_count); + p = _put16 (p, backtrack_count); + for (unsigned i = 0; i < backtrack_count; i++) + p = _put16 (p, 12 + 2 * backtrack_count + 6); + p = _put16 (p, 0); + p = _put16 (p, 1); p = _put16 (p, 1); + p = _put16 (p, 1); p = _put16 (p, 1); p = _put16 (p, 1); + p = _put16 (p, 2); p = _put16 (p, 1); p = _put16 (p, 0); p = _put16 (p, range_last); p = _put16 (p, 0); + g_assert_cmpuint (p - gsub, ==, gsub_length); + + hb_face_t *builder = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (gsub, gsub_length, HB_MEMORY_MODE_WRITABLE, gsub, g_free); + hb_face_builder_add_table (builder, HB_TAG ('G','S','U','B'), blob); + hb_blob_destroy (blob); + HB_FACE_ADD_TABLE (builder, "cmap", cmap_data); + HB_FACE_ADD_TABLE (builder, "maxp", maxp_data); + + blob = hb_face_reference_blob (builder); + hb_face_t *face = hb_face_create (blob, 0); + hb_blob_destroy (blob); + hb_face_destroy (builder); + return face; +} + +static void +test_subset_shared_coverage_budget (void) +{ + /* Walking 65535 glyphs per offset must run out of the per-table work + * budget (HB_BUDGET_SUBSET_TABLE) at 2048 offsets, but not at 256. */ + static const struct { unsigned backtrack_count, range_last; hb_bool_t expected; } cases[] = { + { 2048, 1, TRUE }, + { 256, 65534, TRUE }, + { 2048, 65534, FALSE }, + }; + + for (unsigned i = 0; i < G_N_ELEMENTS (cases); i++) + { + hb_face_t *face = _create_shared_coverage_face (cases[i].backtrack_count, cases[i].range_last); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_unicode_set (input), 'a'); + + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_true (!subset == !cases[i].expected); + if (subset) + { + hb_blob_t *gsub = hb_face_reference_table (subset, HB_TAG ('G','S','U','B')); + g_assert_cmpuint (hb_blob_get_length (gsub), >, 0); + hb_blob_destroy (gsub); + } + + hb_subset_input_destroy (input); + hb_face_destroy (subset); + hb_face_destroy (face); + } +} + #ifndef HB_NO_VAR static void assert_lookup_variations_shape (hb_face_t *face, @@ -1440,6 +1536,7 @@ main (int argc, char **argv) hb_test_add (test_subset_32_tables); hb_test_add (test_subset_no_inf_loop); hb_test_add (test_subset_crash); + hb_test_add (test_subset_shared_coverage_budget); #ifndef HB_NO_VAR hb_test_add (test_subset_lookup_variations); hb_test_add (test_subset_lookup_variation_fractional_value); From 1af279ed98b3bbd613acdfeb52c24003bc5ab0f1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Mon, 9 Nov 2026 22:23:09 -0600 Subject: [PATCH 2/4] [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 --- src/hb-limits.hh | 13 ++++--------- src/hb-subset.hh | 8 ++++---- test/api/test-subset.c | 6 +++--- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/hb-limits.hh b/src/hb-limits.hh index af14c353c..66fbfe433 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -243,7 +243,10 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) * variable fonts spend -- the largest glyf variable font under test/ * (MPLUS1-Variable, 6,434 glyphs retained) uses ~1.2M units, and even a * font at the 65,535-glyph maximum stays far below -- while keeping the - * adversarial worst case bounded. */ + * adversarial worst case bounded. Layout tables charge their Coverage + * and ClassDef glyph walks to the same budget: a glyph range costs a few + * bytes and any number of offsets can share it, so neither the table size + * nor the sanitizer's budget bounds that work. */ #ifndef HB_BUDGET_SUBSET_GLYF #define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28) #endif @@ -276,14 +279,6 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_AAT_MAP ((int64_t) 1 << 24) #endif -/* Subsetting one table, in glyphs visited while walking its Coverage and - * ClassDef tables. A glyph range costs a few bytes and any number of - * offsets can share it, so neither the table size nor the sanitizer's - * budget bounds that work. */ -#ifndef HB_BUDGET_SUBSET_TABLE -#define HB_BUDGET_SUBSET_TABLE ((int64_t) 1 << 26) -#endif - #ifndef HB_REPACKER_MAX_ITERATIONS #define HB_REPACKER_MAX_ITERATIONS 500 diff --git a/src/hb-subset.hh b/src/hb-subset.hh index 8827078bb..3ab7baac3 100644 --- a/src/hb-subset.hh +++ b/src/hb-subset.hh @@ -58,16 +58,16 @@ struct hb_subset_context_t : /* Charge COUNT units of work that the size of the source table does * not bound. Fails the table once its budget is exhausted. */ bool check_ops (unsigned count) - { return serializer->check_success (hb_budget_spend (budget, count)); } + { return serializer->check_success (hb_budget_spend (budget_remaining, count)); } hb_blob_t *source_blob; hb_subset_plan_t *plan; hb_serialize_context_t *serializer; hb_tag_t table_tag; - /* Outline work left for instancing this table. Lives here rather than in - * the table's subset() so retries with a larger buffer share it. */ + /* Work left for subsetting this table: outline work for glyf instancing, + * Coverage and ClassDef glyph walks for layout tables. Lives here rather + * than in the table's subset() so retries with a larger buffer share it. */ int64_t budget_remaining = HB_BUDGET_SUBSET_GLYF; - int64_t budget = HB_BUDGET_SUBSET_TABLE; hb_subset_context_t (hb_blob_t *source_blob_, hb_subset_plan_t *plan_, diff --git a/test/api/test-subset.c b/test/api/test-subset.c index a93b0e198..9527685bf 100644 --- a/test/api/test-subset.c +++ b/test/api/test-subset.c @@ -161,11 +161,11 @@ static void test_subset_shared_coverage_budget (void) { /* Walking 65535 glyphs per offset must run out of the per-table work - * budget (HB_BUDGET_SUBSET_TABLE) at 2048 offsets, but not at 256. */ + * budget (HB_BUDGET_SUBSET_GLYF) at 8192 offsets, but not at 256. */ static const struct { unsigned backtrack_count, range_last; hb_bool_t expected; } cases[] = { - { 2048, 1, TRUE }, + { 8192, 1, TRUE }, { 256, 65534, TRUE }, - { 2048, 65534, FALSE }, + { 8192, 65534, FALSE }, }; for (unsigned i = 0; i < G_N_ELEMENTS (cases); i++) From 9b4e3df36412b5ee23f6bb1db4841ef2597d05ed Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 17:31:42 -0600 Subject: [PATCH 3/4] [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 --- src/hb-bit-set-invertible.hh | 1 + src/hb-bit-set.hh | 1 + src/hb-depend-data.hh | 9 ++++ src/hb-limits.hh | 9 ++++ src/hb-ot-layout-gsubgpos.hh | 31 ++++++++--- src/hb-set.hh | 1 + test/api/test-ot-depend.c | 100 +++++++++++++++++++++++++++++++++++ 7 files changed, 146 insertions(+), 6 deletions(-) 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 66fbfe433..75ab4d9ca 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -279,6 +279,15 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_AAT_MAP ((int64_t) 1 << 24) #endif +/* One dependency graph build. A few bytes of Coverage or ClassDef range + * can name any number of glyphs, so the glyph sets built from them are + * charged per page: for the work of expanding them, and for the memory + * they hold once the graph keeps a copy. Exceeding the budget fails the + * build. */ +#ifndef HB_BUDGET_DEPEND +#define HB_BUDGET_DEPEND ((int64_t) 1 << 26) +#endif + #ifndef HB_REPACKER_MAX_ITERATIONS #define HB_REPACKER_MAX_ITERATIONS 500 diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index d1b94ca91..443d83ced 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -686,6 +686,9 @@ struct hb_depend_context_t : return true; } + if (unlikely (!depend_data->charge_set (set))) + return false; + hb_set_t *copy = hb_object_create (); if (unlikely (!copy)) return depend_data->fail (); @@ -1763,13 +1766,29 @@ hb_depend_context_t::find_or_create_glyph_set (const void *data, return nullptr; } - if (type == GLYPH_SET_COVERAGE) - collect_coverage (set, value, data); - else if (type == GLYPH_SET_CLASS) - collect_class (set, value, data); - else + /* A coverage or class range can name far more glyphs than the face has, + * and these sets are copied into the graph. Expand into a temporary, + * charged as work, and keep only the face's glyphs in a set sized for + * them, charged as memory. */ + hb_set_t expanded; + if (type == GLYPH_SET_SINGLE) set->add (value); - if (unlikely (set->in_error ())) + else + { + if (type == GLYPH_SET_COVERAGE) + collect_coverage (&expanded, value, data); + else + collect_class (&expanded, value, data); + if (unlikely (!depend_data->charge_set (expanded, HB_BUDGET_4))) + { + hb_set_destroy (set); + return nullptr; + } + expanded.intersect (*glyphs); + set->set (expanded); + } + if (unlikely (set->in_error () || expanded.in_error () || + !depend_data->charge_set (*set))) { hb_set_destroy (set); depend_data->fail (); 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 5ef9ae03d..e16c15935 100644 --- a/test/api/test-ot-depend.c +++ b/test/api/test-ot-depend.c @@ -701,6 +701,104 @@ test_depend_context_format6 (void) hb_subset_depend_destroy (depend); hb_face_destroy (face); } + +/* A GSUB with one ChainContextSubst format 3 under feature 'test': one input + * glyph, no lookahead, no lookup records, and num_coverages backtrack + * positions, each with its own Coverage format 4 holding the single range + * [position, last_glyph - position]. */ +static hb_face_t * +create_chain_context_face (unsigned num_glyphs, + unsigned num_coverages, + unsigned last_glyph) +{ + unsigned coverages_offset = 12 + num_coverages * 2; + unsigned gsub_size = 10 + 20 + 14 + 12 + coverages_offset + 6 + num_coverages * 14; + unsigned char *gsub = g_malloc0 (gsub_size); + unsigned char *p = gsub; + +#define PUT16(v) (*p++ = (unsigned char) ((v) >> 8), *p++ = (unsigned char) (v)) +#define PUT24(v) (*p++ = (unsigned char) ((v) >> 16), PUT16 (v)) + PUT16 (1); PUT16 (0); /* version */ + PUT16 (10); PUT16 (10 + 20); PUT16 (10 + 20 + 14); /* scriptList, featureList, lookupList */ + + PUT16 (1); memcpy (p, "DFLT", 4); p += 4; PUT16 (8); /* ScriptList */ + PUT16 (4); PUT16 (0); /* Script: defaultLangSys, langSysCount */ + PUT16 (0); PUT16 (0xFFFF); PUT16 (1); PUT16 (0); /* LangSys */ + + PUT16 (1); memcpy (p, "test", 4); p += 4; PUT16 (8); /* FeatureList */ + PUT16 (0); PUT16 (1); PUT16 (0); /* Feature */ + + PUT16 (1); PUT16 (4); /* LookupList */ + PUT16 (6); PUT16 (0); PUT16 (1); PUT16 (8); /* Lookup */ + + PUT16 (3); /* ChainContextSubst format */ + PUT16 (num_coverages); /* backtrackGlyphCount */ + for (unsigned i = 0; i < num_coverages; i++) + PUT16 (coverages_offset + 6 + i * 14); + PUT16 (1); PUT16 (coverages_offset); /* inputGlyphCount, input coverage */ + PUT16 (0); /* lookaheadGlyphCount */ + PUT16 (0); /* seqLookupCount */ + + PUT16 (1); PUT16 (1); PUT16 (1); /* Coverage format 1: glyph 1 */ + for (unsigned i = 0; i < num_coverages; i++) + { + PUT16 (4); PUT24 (1); /* Coverage format 4, rangeCount */ + PUT24 (i); PUT24 (last_glyph - i); PUT24 (0); /* first, last, startCoverageIndex */ + } +#undef PUT24 +#undef PUT16 + g_assert_true (p == gsub + gsub_size); + + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create ((const char *) gsub, gsub_size, + HB_MEMORY_MODE_READONLY, gsub, g_free); + g_assert_true (hb_face_builder_add_table (face, HB_TAG ('G','S','U','B'), blob)); + hb_blob_destroy (blob); + hb_face_set_glyph_count (face, num_glyphs); + + return face; +} + +/* Context sets hold no glyph the face does not have, however far the + * Coverage ranges that name them reach. */ +static void +test_depend_gsub_context_set_glyphs (void) +{ + hb_face_t *face = create_chain_context_face (1000, 4, 0xFFFFFF); + hb_subset_depend_t *depend = hb_subset_depend_from_face_or_fail (face); + g_assert_nonnull (depend); + + hb_set_t *set = hb_set_create (); + unsigned num_sets = 0; + for (hb_codepoint_t index = 0; hb_subset_depend_lookup_set (depend, index, set); index++) + { + for (hb_codepoint_t g = HB_SET_VALUE_INVALID; hb_set_next (set, &g);) + if (!(g & 0x80000000u)) /* Not a reference to another context set. */ + g_assert_cmpuint (g, <, 1000); + num_sets++; + } + g_assert_cmpuint (num_sets, >=, 4); + + /* The backtrack set of the first position covers the whole face. */ + g_assert_true (hb_subset_depend_lookup_set (depend, 0, set)); + g_assert_cmpuint (hb_set_get_population (set), ==, 1000); + g_assert_cmpuint (hb_set_get_max (set), ==, 999); + + hb_set_destroy (set); + hb_subset_depend_destroy (depend); + hb_face_destroy (face); +} + +/* A face whose context glyph sets take far more work to expand than it has + * bytes exhausts the build's budget and is refused. */ +static void +test_depend_gsub_set_budget (void) +{ + /* 600 ranges of 16,777,216 glyphs from under 10 KB of GSUB. */ + hb_face_t *face = create_chain_context_face (1000, 600, 0xFFFFFF); + g_assert_null (hb_subset_depend_from_face_or_fail (face)); + hb_face_destroy (face); +} #endif int @@ -717,6 +815,8 @@ main (int argc, char **argv) hb_test_add (test_depend_gsub_formats); #ifndef HB_NO_BEYOND_64K hb_test_add (test_depend_context_format6); + hb_test_add (test_depend_gsub_context_set_glyphs); + hb_test_add (test_depend_gsub_set_budget); #endif return hb_test_run (); From 70df3ff27937dc83db7001652d430ed7e8853082 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 17:42:04 -0600 Subject: [PATCH 4/4] [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 --- src/OT/Layout/Common/Coverage.hh | 14 +++ src/OT/Layout/GSUB/AlternateSet.hh | 5 +- src/OT/Layout/GSUB/AlternateSubstFormat1.hh | 1 + src/OT/Layout/GSUB/Ligature.hh | 1 + src/OT/Layout/GSUB/LigatureSet.hh | 1 + src/OT/Layout/GSUB/LigatureSubstFormat1.hh | 1 + src/OT/Layout/GSUB/MultipleSubstFormat1.hh | 1 + .../GSUB/ReverseChainSingleSubstFormat1.hh | 8 +- src/OT/Layout/GSUB/Sequence.hh | 5 +- src/OT/Layout/GSUB/SingleSubstFormat1.hh | 1 + src/OT/Layout/GSUB/SingleSubstFormat2.hh | 2 + src/hb-limits.hh | 9 ++ src/hb-ot-layout-common.hh | 14 +++ src/hb-ot-layout-gsubgpos.hh | 69 +++++++++-- test/api/test-ot-layout.c | 116 ++++++++++++++++++ 15 files changed, 233 insertions(+), 15 deletions(-) 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