mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-29 17:44:06 +01:00
[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 <noreply@anthropic.com>
This commit is contained in:
1 parent
fb350edda9
commit
0cf5addc3e
8 files changed
+279
-18
No files matched your search
@@ -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]))
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<hb_codepoint_t, const typename Types::template OffsetTo<AlternateSet<Types>>&> &_)
|
||||
{
|
||||
const AlternateSet<Types>& 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<Types> &_) { _.closure (c); })
|
||||
|
||||
@@ -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<hb_codepoint_t, const typename Types::template OffsetTo<LigatureSet<Types>>&> &_)
|
||||
{
|
||||
const LigatureSet<Types>& 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<Types> &_) { _.closure (c); })
|
||||
|
||||
@@ -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<Types> &_) { _.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<hb_codepoint_t, const typename Types::template OffsetTo<Sequence<Types>>&> &_)
|
||||
{
|
||||
const Sequence<Types>& s = this+_.second;
|
||||
|
||||
@@ -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<decltype (lookaheadX)> (backtrack);
|
||||
const auto &substitute = StructAfter<decltype (substituteX)> (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)
|
||||
;
|
||||
|
||||
@@ -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 (_);
|
||||
|
||||
Reference in new issue
Block a user