From 0cf5addc3ecd3f6ce99758fa59f30a42f099db6d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 14:54:02 -0600 Subject: [PATCH] [GSUB] Add small-active-set closure/depend fast path to Coverage-keyed lookups SingleSubstFormat2 already picks, per closure visit, between walking its whole Coverage and iterating the (usually small) active glyph set. The Multiple/Alternate/Ligature/ReverseChain closure and depend paths, the format-1 (Chain)Context closure, and Coverage::intersect_set lacked that choice, so a nested lookup visited many times over a small active set re-walked a large Coverage every time, giving quadratic GSUB closure (and thus subsetting) time. Add the same get_population()-based fast path to those paths and to CoverageFormat1/2::intersect_set. Output is byte-identical; only the per-visit work is bounded. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KNHDK379. Co-Authored-By: Claude Opus 4.8 --- src/OT/Layout/Common/CoverageFormat1.hh | 8 ++ src/OT/Layout/Common/CoverageFormat2.hh | 8 ++ src/OT/Layout/GSUB/AlternateSubstFormat1.hh | 40 ++++++++- src/OT/Layout/GSUB/LigatureSubstFormat1.hh | 40 ++++++++- src/OT/Layout/GSUB/MultipleSubstFormat1.hh | 40 ++++++++- .../GSUB/ReverseChainSingleSubstFormat1.hh | 44 ++++++++-- src/hb-ot-layout-gsubgpos.hh | 32 +++++++ test/api/test-ot-layout.c | 85 +++++++++++++++++++ 8 files changed, 279 insertions(+), 18 deletions(-) diff --git a/src/OT/Layout/Common/CoverageFormat1.hh b/src/OT/Layout/Common/CoverageFormat1.hh index ee9425ec2..56f38de49 100644 --- a/src/OT/Layout/Common/CoverageFormat1.hh +++ b/src/OT/Layout/Common/CoverageFormat1.hh @@ -97,6 +97,14 @@ struct CoverageFormat1_3 hb_requires (hb_is_sink_of (IterableOut, hb_codepoint_t))> void intersect_set (const hb_set_t &glyphs, IterableOut&& intersect_glyphs) const { + if (glyphArray.len > glyphs.get_population () * hb_bit_storage ((unsigned) glyphArray.len)) + { + for (auto g : glyphs) + if (get_coverage (g) != NOT_COVERED) + intersect_glyphs << g; + return; + } + unsigned count = glyphArray.len; for (unsigned i = 0; i < count; i++) if (glyphs.has (glyphArray[i])) diff --git a/src/OT/Layout/Common/CoverageFormat2.hh b/src/OT/Layout/Common/CoverageFormat2.hh index b22c4d782..db4053766 100644 --- a/src/OT/Layout/Common/CoverageFormat2.hh +++ b/src/OT/Layout/Common/CoverageFormat2.hh @@ -157,6 +157,14 @@ struct CoverageFormat2_4 hb_requires (hb_is_sink_of (IterableOut, hb_codepoint_t))> void intersect_set (const hb_set_t &glyphs, IterableOut&& intersect_glyphs) const { + if (rangeRecord.len > glyphs.get_population () * hb_bit_storage ((unsigned) rangeRecord.len)) + { + for (auto g : glyphs) + if (get_coverage (g) != NOT_COVERED) + intersect_glyphs << g; + return; + } + /* Break out of loop for overlapping, broken, tables, * to avoid fuzzer timouts. */ hb_codepoint_t last = 0; diff --git a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh index b4b279b3a..2fb59ee98 100644 --- a/src/OT/Layout/GSUB/AlternateSubstFormat1.hh +++ b/src/OT/Layout/GSUB/AlternateSubstFormat1.hh @@ -36,9 +36,25 @@ struct AlternateSubstFormat1_2 void depend (hb_depend_context_t *c) const { + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + // Filter by parent_active_glyphs like closure does - + hb_zip (this+coverage, alternateSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + if (alternateSet.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= alternateSet.len) + continue; + (this+alternateSet.arrayZ[i]).depend (c, g); + } + + return; + } + + + hb_zip (cov, alternateSet) + | hb_filter (glyph_set, hb_first) | hb_apply ([&] (const hb_pair_t>&> &_) { const AlternateSet& as = this+_.second; @@ -49,8 +65,24 @@ struct AlternateSubstFormat1_2 void closure (hb_closure_context_t *c) const { - + hb_zip (this+coverage, alternateSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (alternateSet.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= alternateSet.len) + continue; + (this+alternateSet.arrayZ[i]).closure (c); + } + + return; + } + + + hb_zip (cov, alternateSet) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_map (hb_add (this)) | hb_apply ([c] (const AlternateSet &_) { _.closure (c); }) diff --git a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh index 7295bffd9..9b12f1598 100644 --- a/src/OT/Layout/GSUB/LigatureSubstFormat1.hh +++ b/src/OT/Layout/GSUB/LigatureSubstFormat1.hh @@ -45,8 +45,24 @@ struct LigatureSubstFormat1_2 void depend (hb_depend_context_t *c) const { - + hb_zip (this+coverage, ligatureSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (ligatureSet.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= ligatureSet.len) + continue; + (this+ligatureSet.arrayZ[i]).depend (c, g); + } + + return; + } + + + hb_zip (cov, ligatureSet) + | hb_filter (glyph_set, hb_first) | hb_apply ([&] (const hb_pair_t>&> &_) { const LigatureSet& ls = this+_.second; @@ -57,8 +73,24 @@ struct LigatureSubstFormat1_2 void closure (hb_closure_context_t *c) const { - + hb_zip (this+coverage, ligatureSet) - | hb_filter (c->parent_active_glyphs (), hb_first) + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (ligatureSet.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= ligatureSet.len) + continue; + (this+ligatureSet.arrayZ[i]).closure (c); + } + + return; + } + + + hb_zip (cov, ligatureSet) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_map (hb_add (this)) | hb_apply ([c] (const LigatureSet &_) { _.closure (c); }) diff --git a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh index 1f686c92f..1d8082e30 100644 --- a/src/OT/Layout/GSUB/MultipleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/MultipleSubstFormat1.hh @@ -36,8 +36,24 @@ struct MultipleSubstFormat1_2 void closure (hb_closure_context_t *c) const { - + hb_zip (this+coverage, sequence) - | hb_filter (c->parent_active_glyphs (), hb_first) + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (sequence.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= sequence.len) + continue; + (this+sequence.arrayZ[i]).closure (c); + } + + return; + } + + + hb_zip (cov, sequence) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_map (hb_add (this)) | hb_apply ([c] (const Sequence &_) { _.closure (c); }) @@ -46,9 +62,25 @@ struct MultipleSubstFormat1_2 void depend (hb_depend_context_t *c) const { + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + // Filter by parent_active_glyphs like closure does - + hb_zip (this+coverage, sequence) - | hb_filter (c->parent_active_glyphs (), hb_first) + if (sequence.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= sequence.len) + continue; + (this+sequence.arrayZ[i]).depend (c, g); + } + + return; + } + + + hb_zip (cov, sequence) + | hb_filter (glyph_set, hb_first) | hb_apply ([&] (const hb_pair_t>&> &_) { const Sequence& s = this+_.second; diff --git a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh index acf46d12c..b1f1434ab 100644 --- a/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh +++ b/src/OT/Layout/GSUB/ReverseChainSingleSubstFormat1.hh @@ -132,10 +132,26 @@ struct ReverseChainSingleSubstFormat1_2 hb_codepoint_t saved_context = c->depend_data->current_context_set_index; c->depend_data->current_context_set_index = context_set_idx; - + hb_zip (this+coverage, substitute) - | hb_filter (c->parent_active_glyphs (), hb_first) - | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup (_.first, c->lookup_index, _.second); }) - ; + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (substitute.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= substitute.len) + continue; + c->depend_data->add_gsub_lookup (g, c->lookup_index, substitute.arrayZ[i]); + } + } + else + { + + hb_zip (cov, substitute) + | hb_filter (glyph_set, hb_first) + | hb_apply ([&] (const hb_codepoint_pair_t &_) { c->depend_data->add_gsub_lookup (_.first, c->lookup_index, _.second); }) + ; + } c->depend_data->current_context_set_index = saved_context; } @@ -147,8 +163,24 @@ struct ReverseChainSingleSubstFormat1_2 const auto &lookahead = StructAfter (backtrack); const auto &substitute = StructAfter (lookahead); - + hb_zip (this+coverage, substitute) - | hb_filter (c->parent_active_glyphs (), hb_first) + auto &cov = this+coverage; + auto &glyph_set = c->parent_active_glyphs (); + + if (substitute.len > glyph_set.get_population () * 4) + { + for (auto g : glyph_set) + { + unsigned i = cov.get_coverage (g); + if (i == NOT_COVERED || i >= substitute.len) + continue; + c->output->add (substitute.arrayZ[i]); + } + + return; + } + + + hb_zip (cov, substitute) + | hb_filter (glyph_set, hb_first) | hb_map (hb_second) | hb_sink (c->output) ; diff --git a/src/hb-ot-layout-gsubgpos.hh b/src/hb-ot-layout-gsubgpos.hh index 99febc159..1bcfc52c3 100644 --- a/src/hb-ot-layout-gsubgpos.hh +++ b/src/hb-ot-layout-gsubgpos.hh @@ -3587,6 +3587,22 @@ struct ContextFormat1_4 nullptr }; + if (ruleSet.len > c->previous_parent_active_glyphs ().get_population () * 4) + { + /* Recursion can reallocate the stack; fetch the set afresh each time. */ + hb_codepoint_t g = HB_SET_VALUE_INVALID; + while (c->previous_parent_active_glyphs ().next (&g)) + { + unsigned i = get_coverage ().get_coverage (g); + if (i == NOT_COVERED || i >= ruleSet.len) + continue; + (this+ruleSet.arrayZ[i]).closure (c, g, lookup_context); + } + + c->pop_cur_done_glyphs (); + return; + } + + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) | hb_filter ([&] (hb_codepoint_t _) { return c->previous_parent_active_glyphs ().has (_); @@ -5230,6 +5246,22 @@ struct ChainContextFormat1_4 {nullptr, nullptr, nullptr} }; + if (ruleSet.len > c->previous_parent_active_glyphs ().get_population () * 4) + { + /* Recursion can reallocate the stack; fetch the set afresh each time. */ + hb_codepoint_t g = HB_SET_VALUE_INVALID; + while (c->previous_parent_active_glyphs ().next (&g)) + { + unsigned i = get_coverage ().get_coverage (g); + if (i == NOT_COVERED || i >= ruleSet.len) + continue; + (this+ruleSet.arrayZ[i]).closure (c, g, lookup_context); + } + + c->pop_cur_done_glyphs (); + return; + } + + hb_zip (this+coverage, hb_range ((unsigned) ruleSet.len)) | hb_filter ([&] (hb_codepoint_t _) { return c->previous_parent_active_glyphs ().has (_); diff --git a/test/api/test-ot-layout.c b/test/api/test-ot-layout.c index 298233ba0..51e094996 100644 --- a/test/api/test-ot-layout.c +++ b/test/api/test-ot-layout.c @@ -380,6 +380,90 @@ test_ot_layout_collect_lookup_variations (void) } #endif +static char * +write_be16 (char *p, unsigned v) +{ + *p++ = (char) (v >> 8); + *p++ = (char) v; + return p; +} + +static void +test_ot_layout_lookups_substitute_closure_large_coverage_one (unsigned lookup_type) +{ + /* Lookup 0 is a ContextSubst covering NUM_GLYPHS glyphs; every rule recurses + * into lookup 1, a MultipleSubst or AlternateSubst covering the same glyphs + * and mapping each to OUT1 and OUT2. Closure visits lookup 1 once per glyph + * with a single active glyph, so the cost of a visit must not scale with the + * size of lookup 1's coverage. */ + enum { NUM_GLYPHS = 16000, OUT1 = NUM_GLYPHS + 1, OUT2 = NUM_GLYPHS + 2 }; + const unsigned context_len = 6 + 2 * NUM_GLYPHS + 4 + 8 + 10; + const unsigned subst_len = 6 + 2 * NUM_GLYPHS + 6 + 10; + const unsigned gsub_len = 10 + 6 + 16 + context_len + subst_len; + char *gsub = g_malloc0 (gsub_len); + char *p = gsub; + unsigned i; + + p = write_be16 (p, 1); p = write_be16 (p, 0); /* version 1.0 */ + p = write_be16 (p, 0); p = write_be16 (p, 0); /* no ScriptList, no FeatureList */ + p = write_be16 (p, 10); /* LookupList */ + + p = write_be16 (p, 2); /* lookupCount */ + p = write_be16 (p, 6); p = write_be16 (p, 14); /* Lookup offsets */ + p = write_be16 (p, 5); p = write_be16 (p, 0); /* ContextSubst, no flags */ + p = write_be16 (p, 1); p = write_be16 (p, 16); /* one subtable */ + p = write_be16 (p, lookup_type); p = write_be16 (p, 0); + p = write_be16 (p, 1); p = write_be16 (p, 8 + context_len); + + /* ContextSubstFormat1: all rule sets share one rule applying lookup 1 at 0. */ + p = write_be16 (p, 1); p = write_be16 (p, context_len - 10); + p = write_be16 (p, NUM_GLYPHS); + for (i = 0; i < NUM_GLYPHS; i++) + p = write_be16 (p, 6 + 2 * NUM_GLYPHS); + p = write_be16 (p, 1); p = write_be16 (p, 4); /* RuleSet */ + p = write_be16 (p, 1); p = write_be16 (p, 1); /* Rule: glyphCount, lookupCount */ + p = write_be16 (p, 0); p = write_be16 (p, 1); /* SequenceLookupRecord */ + p = write_be16 (p, 2); p = write_be16 (p, 1); /* CoverageFormat2, one range */ + p = write_be16 (p, 1); p = write_be16 (p, NUM_GLYPHS); p = write_be16 (p, 0); + + /* MultipleSubstFormat1 / AlternateSubstFormat1: all glyphs share one + * Sequence / AlternateSet. */ + p = write_be16 (p, 1); p = write_be16 (p, subst_len - 10); + p = write_be16 (p, NUM_GLYPHS); + for (i = 0; i < NUM_GLYPHS; i++) + p = write_be16 (p, 6 + 2 * NUM_GLYPHS); + p = write_be16 (p, 2); p = write_be16 (p, OUT1); p = write_be16 (p, OUT2); + p = write_be16 (p, 2); p = write_be16 (p, 1); /* CoverageFormat2, one range */ + p = write_be16 (p, 1); p = write_be16 (p, NUM_GLYPHS); p = write_be16 (p, 0); + + g_assert_cmpuint (p - gsub, ==, gsub_len); + + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (gsub, gsub_len, 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, OUT2 + 1); + + hb_set_t *glyphs = hb_set_create (); + hb_set_add_range (glyphs, 1, NUM_GLYPHS); + hb_ot_layout_lookups_substitute_closure (face, NULL, glyphs); + + g_assert_cmpuint (hb_set_get_population (glyphs), ==, NUM_GLYPHS + 2); + g_assert_true (hb_set_has (glyphs, OUT1)); + g_assert_true (hb_set_has (glyphs, OUT2)); + + hb_set_destroy (glyphs); + hb_face_destroy (face); + g_free (gsub); +} + +static void +test_ot_layout_lookups_substitute_closure_large_coverage (void) +{ + test_ot_layout_lookups_substitute_closure_large_coverage_one (2); /* MultipleSubst */ + test_ot_layout_lookups_substitute_closure_large_coverage_one (3); /* AlternateSubst */ +} + int main (int argc, char **argv) { @@ -399,5 +483,6 @@ main (int argc, char **argv) #ifndef HB_NO_VAR hb_test_add (test_ot_layout_collect_lookup_variations); #endif + hb_test_add (test_ot_layout_lookups_substitute_closure_large_coverage); return hb_test_run (); }