mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 15:31:05 +01:00
[ot] Collect class-based contextual rule classes once per ClassDef in collect_glyphs
Rule::collect_second_glyphs() resolved the second input glyph of a class-based contextual rule by scanning the whole input ClassDef once per rule, so a RuleSet with many rules sharing one large ClassDef was quadratic in collect_glyphs() (and thus in subsetting). Collect the needed classes into a set and walk each ClassDef once for all of them: add ClassDef::collect_classes(), and have collect_second_glyphs() accumulate the classes and resolve them in a single pass. The collected sets are identical; only the work is reduced. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-5FCXNOQF. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
fd57025171
commit
fb350edda9
3 files changed
+210
-31
No files matched your search
@@ -1670,6 +1670,15 @@ struct ClassDefFormat1_3
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename set_t>
|
||||
bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const
|
||||
{
|
||||
unsigned int count = classValue.len;
|
||||
for (unsigned int i = 0; i < count; i++)
|
||||
if (klasses.has (classValue[i])) glyphs->add (startGlyph + i);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool intersects (const hb_set_t *glyphs) const
|
||||
{
|
||||
hb_codepoint_t start = startGlyph;
|
||||
@@ -1954,6 +1963,18 @@ struct ClassDefFormat2_4
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename set_t>
|
||||
bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const
|
||||
{
|
||||
for (auto &range : rangeRecord)
|
||||
{
|
||||
if (klasses.has (range.value))
|
||||
if (unlikely (!range.collect_coverage (glyphs)))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool intersects (const hb_set_t *glyphs) const
|
||||
{
|
||||
if (rangeRecord.len > glyphs->get_population () * hb_bit_storage ((unsigned) rangeRecord.len))
|
||||
@@ -2296,6 +2317,21 @@ struct ClassDef
|
||||
}
|
||||
}
|
||||
|
||||
/* Like collect_class(), for all of klasses in one walk of the table. */
|
||||
template <typename set_t>
|
||||
bool collect_classes (set_t *glyphs, const hb_set_t &klasses) const
|
||||
{
|
||||
switch (u.format.v) {
|
||||
case 1: hb_barrier (); return u.format1.collect_classes (glyphs, klasses);
|
||||
case 2: hb_barrier (); return u.format2.collect_classes (glyphs, klasses);
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
case 3: hb_barrier (); return u.format3.collect_classes (glyphs, klasses);
|
||||
case 4: hb_barrier (); return u.format4.collect_classes (glyphs, klasses);
|
||||
#endif
|
||||
default:return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool intersects (const hb_set_t *glyphs) const
|
||||
{
|
||||
switch (u.format.v) {
|
||||
|
||||
@@ -1736,6 +1736,12 @@ static inline void collect_class (hb_set_t *glyphs, unsigned value, const void *
|
||||
const ClassDef &class_def = *reinterpret_cast<const ClassDef *>(data);
|
||||
class_def.collect_class (glyphs, value);
|
||||
}
|
||||
static inline void collect_classes (hb_set_t *glyphs, const hb_set_t &classes, const ClassDef &class_def)
|
||||
{
|
||||
if (unlikely (classes.in_error ()))
|
||||
glyphs->err ();
|
||||
class_def.collect_classes (glyphs, classes);
|
||||
}
|
||||
static inline void collect_coverage (hb_set_t *glyphs, unsigned value, const void *data)
|
||||
{
|
||||
Offset32To<Coverage> coverage;
|
||||
@@ -3137,7 +3143,7 @@ struct Rule
|
||||
}
|
||||
|
||||
void collect_second_glyphs (hb_set_digest_t *digest,
|
||||
const ClassDef *class_def = nullptr) const
|
||||
hb_set_t *classes = nullptr) const
|
||||
{
|
||||
if (inputCount <= 1)
|
||||
{
|
||||
@@ -3146,12 +3152,12 @@ struct Rule
|
||||
}
|
||||
|
||||
unsigned second = inputZ.arrayZ[0];
|
||||
if (!class_def)
|
||||
if (!classes)
|
||||
digest->add (second);
|
||||
else if (!second)
|
||||
*digest = hb_set_digest_t::full ();
|
||||
else
|
||||
class_def->collect_class (digest, second);
|
||||
classes->add (second);
|
||||
}
|
||||
|
||||
bool would_apply (hb_would_apply_context_t *c,
|
||||
@@ -3307,11 +3313,11 @@ struct RuleSetOf
|
||||
}
|
||||
|
||||
void collect_second_glyphs (hb_set_digest_t *digest,
|
||||
const ClassDef *class_def = nullptr) const
|
||||
hb_set_t *classes = nullptr) const
|
||||
{
|
||||
+ hb_iter (rule)
|
||||
| hb_map (hb_add (this))
|
||||
| hb_apply ([=] (const RuleType &_) { _.collect_second_glyphs (digest, class_def); })
|
||||
| hb_apply ([=] (const RuleType &_) { _.collect_second_glyphs (digest, classes); })
|
||||
;
|
||||
}
|
||||
|
||||
@@ -3847,25 +3853,40 @@ struct ContextFormat2_5
|
||||
{
|
||||
(this+coverage).collect_coverage (c->input);
|
||||
|
||||
const ClassDef &class_def = this+classDef;
|
||||
/* Rules repeat classes, and can be shared. Collect the class values
|
||||
* first, such that the ClassDef is walked once instead of per value. */
|
||||
hb_set_t input_classes;
|
||||
hb_set_t *input = c->input;
|
||||
c->input = &input_classes;
|
||||
|
||||
struct ContextCollectGlyphsLookupContext lookup_context = {
|
||||
{collect_class},
|
||||
&class_def
|
||||
{collect_glyph},
|
||||
nullptr
|
||||
};
|
||||
|
||||
+ hb_iter (ruleSet)
|
||||
| hb_map (hb_add (this))
|
||||
| hb_apply ([&] (const RuleSet &_) { _.collect_glyphs (c, lookup_context); })
|
||||
;
|
||||
|
||||
c->input = input;
|
||||
|
||||
collect_classes (c->input, input_classes, this+classDef);
|
||||
}
|
||||
|
||||
void collect_second_glyphs (hb_set_digest_t *digest) const
|
||||
{
|
||||
const ClassDef &class_def = this+classDef;
|
||||
/* Ditto. */
|
||||
hb_set_t classes;
|
||||
+ hb_iter (ruleSet)
|
||||
| hb_map (hb_add (this))
|
||||
| hb_apply ([=, &class_def] (const RuleSet &_) { _.collect_second_glyphs (digest, &class_def); })
|
||||
| hb_apply ([=, &classes] (const RuleSet &_) { _.collect_second_glyphs (digest, &classes); })
|
||||
;
|
||||
|
||||
if (unlikely (classes.in_error ()))
|
||||
*digest = hb_set_digest_t::full ();
|
||||
else
|
||||
(this+classDef).collect_classes (digest, classes);
|
||||
}
|
||||
|
||||
bool would_apply (hb_would_apply_context_t *c) const
|
||||
@@ -4669,23 +4690,23 @@ struct ChainRule
|
||||
}
|
||||
|
||||
void collect_second_glyphs (hb_set_digest_t *digest,
|
||||
const ClassDef *input_class_def = nullptr,
|
||||
const ClassDef *lookahead_class_def = nullptr) const
|
||||
hb_set_t *input_classes = nullptr,
|
||||
hb_set_t *lookahead_classes = nullptr) const
|
||||
{
|
||||
const auto &input = StructAfter<decltype (inputX)> (backtrack);
|
||||
const auto &lookahead = StructAfter<decltype (lookaheadX)> (input);
|
||||
|
||||
unsigned second;
|
||||
const ClassDef *class_def;
|
||||
hb_set_t *classes;
|
||||
if (input.lenP1 > 1)
|
||||
{
|
||||
second = input.arrayZ[0];
|
||||
class_def = input_class_def;
|
||||
classes = input_classes;
|
||||
}
|
||||
else if (lookahead.len)
|
||||
{
|
||||
second = lookahead.arrayZ[0];
|
||||
class_def = lookahead_class_def;
|
||||
classes = lookahead_classes;
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -4693,12 +4714,12 @@ struct ChainRule
|
||||
return;
|
||||
}
|
||||
|
||||
if (!class_def)
|
||||
if (!classes)
|
||||
digest->add (second);
|
||||
else if (!second)
|
||||
*digest = hb_set_digest_t::full ();
|
||||
else
|
||||
class_def->collect_class (digest, second);
|
||||
classes->add (second);
|
||||
}
|
||||
|
||||
bool would_apply (hb_would_apply_context_t *c,
|
||||
@@ -4909,13 +4930,13 @@ struct ChainRuleSetOf
|
||||
}
|
||||
|
||||
void collect_second_glyphs (hb_set_digest_t *digest,
|
||||
const ClassDef *input_class_def = nullptr,
|
||||
const ClassDef *lookahead_class_def = nullptr) const
|
||||
hb_set_t *input_classes = nullptr,
|
||||
hb_set_t *lookahead_classes = nullptr) const
|
||||
{
|
||||
+ hb_iter (rule)
|
||||
| hb_map (hb_add (this))
|
||||
| hb_apply ([=] (const ChainRuleType &_)
|
||||
{ _.collect_second_glyphs (digest, input_class_def, lookahead_class_def); })
|
||||
{ _.collect_second_glyphs (digest, input_classes, lookahead_classes); })
|
||||
;
|
||||
}
|
||||
|
||||
@@ -5489,32 +5510,52 @@ struct ChainContextFormat2_5
|
||||
{
|
||||
(this+coverage).collect_coverage (c->input);
|
||||
|
||||
const ClassDef &backtrack_class_def = this+backtrackClassDef;
|
||||
const ClassDef &input_class_def = this+inputClassDef;
|
||||
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
|
||||
/* Rules repeat classes, and can be shared. Collect the class values
|
||||
* first, such that each ClassDef is walked once instead of per value. */
|
||||
hb_set_t backtrack_classes, input_classes, lookahead_classes;
|
||||
hb_set_t *before = c->before;
|
||||
hb_set_t *input = c->input;
|
||||
hb_set_t *after = c->after;
|
||||
c->before = &backtrack_classes;
|
||||
c->input = &input_classes;
|
||||
c->after = &lookahead_classes;
|
||||
|
||||
struct ChainContextCollectGlyphsLookupContext lookup_context = {
|
||||
{collect_class},
|
||||
{&backtrack_class_def,
|
||||
&input_class_def,
|
||||
&lookahead_class_def}
|
||||
{collect_glyph},
|
||||
{nullptr, nullptr, nullptr}
|
||||
};
|
||||
|
||||
+ hb_iter (ruleSet)
|
||||
| hb_map (hb_add (this))
|
||||
| hb_apply ([&] (const ChainRuleSet &_) { _.collect_glyphs (c, lookup_context); })
|
||||
;
|
||||
|
||||
c->before = before;
|
||||
c->input = input;
|
||||
c->after = after;
|
||||
|
||||
collect_classes (c->before, backtrack_classes, this+backtrackClassDef);
|
||||
collect_classes (c->input, input_classes, this+inputClassDef);
|
||||
collect_classes (c->after, lookahead_classes, this+lookaheadClassDef);
|
||||
}
|
||||
|
||||
void collect_second_glyphs (hb_set_digest_t *digest) const
|
||||
{
|
||||
const ClassDef &input_class_def = this+inputClassDef;
|
||||
const ClassDef &lookahead_class_def = this+lookaheadClassDef;
|
||||
/* Ditto. */
|
||||
hb_set_t input_classes, lookahead_classes;
|
||||
+ hb_iter (ruleSet)
|
||||
| hb_map (hb_add (this))
|
||||
| hb_apply ([digest, &input_class_def, &lookahead_class_def] (const ChainRuleSet &_)
|
||||
{ _.collect_second_glyphs (digest, &input_class_def, &lookahead_class_def); })
|
||||
| hb_apply ([digest, &input_classes, &lookahead_classes] (const ChainRuleSet &_)
|
||||
{ _.collect_second_glyphs (digest, &input_classes, &lookahead_classes); })
|
||||
;
|
||||
|
||||
if (unlikely (input_classes.in_error () || lookahead_classes.in_error ()))
|
||||
{
|
||||
*digest = hb_set_digest_t::full ();
|
||||
return;
|
||||
}
|
||||
(this+inputClassDef).collect_classes (digest, input_classes);
|
||||
(this+lookaheadClassDef).collect_classes (digest, lookahead_classes);
|
||||
}
|
||||
|
||||
bool would_apply (hb_would_apply_context_t *c) const
|
||||
|
||||
@@ -481,6 +481,107 @@ test_ot_layout_lookup_collect_glyphs_qahiri (void)
|
||||
hb_set_destroy (output);
|
||||
}
|
||||
|
||||
static uint8_t *
|
||||
put16 (uint8_t *p, unsigned v)
|
||||
{
|
||||
*p++ = (uint8_t) (v >> 8);
|
||||
*p++ = (uint8_t) v;
|
||||
return p;
|
||||
}
|
||||
|
||||
static void
|
||||
test_ot_layout_lookup_collect_glyphs_shared_class_rules (void)
|
||||
{
|
||||
/* A ContextSubstFormat2 whose RuleSet offsets all point at one RuleSet,
|
||||
* whose Rule offsets all point at one Rule with 64 class values, over a
|
||||
* 65535-entry ClassDef. The ClassDef must be walked once per subtable,
|
||||
* not once per class value of every Rule visited; the latter takes
|
||||
* about a minute. */
|
||||
enum { RULESETS = 16, RULES = 1000, CLASSES = 64, GLYPHS = 65535 };
|
||||
unsigned rule_len = 4 + 2 * CLASSES;
|
||||
unsigned ruleset_len = 2 + 2 * RULES + rule_len;
|
||||
unsigned coverage_len = 6;
|
||||
unsigned classdef_len = 6 + 2 * GLYPHS;
|
||||
unsigned ruleset_offset = 8 + 2 * RULESETS;
|
||||
unsigned coverage_offset = ruleset_offset + ruleset_len;
|
||||
unsigned classdef_offset = coverage_offset + coverage_len;
|
||||
unsigned gsub_len = 10 + 2 + 2 + 4 + 8 + classdef_offset + classdef_len;
|
||||
uint8_t *gsub = g_malloc0 (gsub_len);
|
||||
uint8_t *p = gsub;
|
||||
|
||||
/* GSUB header, empty ScriptList and FeatureList, LookupList with one lookup. */
|
||||
p = put16 (p, 1); p = put16 (p, 0);
|
||||
p = put16 (p, 10); p = put16 (p, 12); p = put16 (p, 14);
|
||||
p = put16 (p, 0);
|
||||
p = put16 (p, 0);
|
||||
p = put16 (p, 1); p = put16 (p, 4);
|
||||
/* Lookup: type 5 (Context), one subtable right after. */
|
||||
p = put16 (p, 5); p = put16 (p, 0); p = put16 (p, 1); p = put16 (p, 8);
|
||||
/* ContextSubstFormat2 */
|
||||
p = put16 (p, 2); p = put16 (p, coverage_offset); p = put16 (p, classdef_offset);
|
||||
p = put16 (p, RULESETS);
|
||||
for (unsigned i = 0; i < RULESETS; i++)
|
||||
p = put16 (p, ruleset_offset);
|
||||
/* RuleSet */
|
||||
p = put16 (p, RULES);
|
||||
for (unsigned i = 0; i < RULES; i++)
|
||||
p = put16 (p, 2 + 2 * RULES);
|
||||
/* Rule: classes 1 and 2, alternating. */
|
||||
p = put16 (p, CLASSES + 1); p = put16 (p, 0);
|
||||
for (unsigned i = 0; i < CLASSES; i++)
|
||||
p = put16 (p, 1 + (i & 1));
|
||||
/* Coverage: glyph 1. */
|
||||
p = put16 (p, 1); p = put16 (p, 1); p = put16 (p, 1);
|
||||
/* ClassDefFormat1: glyph i has class i % 3. */
|
||||
p = put16 (p, 1); p = put16 (p, 0); p = put16 (p, GLYPHS);
|
||||
for (unsigned i = 0; i < GLYPHS; i++)
|
||||
p = put16 (p, i % 3);
|
||||
g_assert_cmpuint (p - gsub, ==, gsub_len);
|
||||
|
||||
hb_blob_t *blob = hb_blob_create_or_fail ((const char *) gsub, gsub_len,
|
||||
HB_MEMORY_MODE_WRITABLE, gsub, g_free);
|
||||
hb_face_t *builder = hb_face_builder_create ();
|
||||
hb_face_builder_add_table (builder, HB_OT_TAG_GSUB, blob);
|
||||
hb_blob_destroy (blob);
|
||||
blob = hb_face_reference_blob (builder);
|
||||
hb_face_destroy (builder);
|
||||
hb_face_t *face = hb_face_create (blob, 0);
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
hb_set_t *before = hb_set_create();
|
||||
hb_set_t *input = hb_set_create();
|
||||
hb_set_t *after = hb_set_create();
|
||||
hb_set_t *output = hb_set_create();
|
||||
hb_codepoint_t g = HB_SET_VALUE_INVALID;
|
||||
|
||||
g_assert_cmpuint (1, ==, hb_ot_layout_table_get_lookup_count (face, HB_OT_TAG_GSUB));
|
||||
|
||||
BEGIN(HB_OT_TAG_GSUB, 0);
|
||||
g_assert_cmpuint (0, ==, hb_set_get_population (before));
|
||||
|
||||
/* Glyph 1 from the coverage, and every glyph of class 1 or 2. */
|
||||
g_assert_cmpuint (2 * (GLYPHS / 3), ==, hb_set_get_population (input));
|
||||
g = HB_SET_VALUE_INVALID;
|
||||
g_assert_true (hb_set_next (input, &g));
|
||||
g_assert_cmpuint (1, ==, g);
|
||||
g_assert_true (hb_set_next (input, &g));
|
||||
g_assert_cmpuint (2, ==, g);
|
||||
g_assert_true (hb_set_next (input, &g));
|
||||
g_assert_cmpuint (4, ==, g);
|
||||
g_assert_false (hb_set_has (input, 0));
|
||||
g_assert_true (hb_set_has (input, GLYPHS - 1));
|
||||
|
||||
g_assert_cmpuint (0, ==, hb_set_get_population (after));
|
||||
g_assert_cmpuint (0, ==, hb_set_get_population (output));
|
||||
END();
|
||||
|
||||
hb_face_destroy (face);
|
||||
hb_set_destroy (before);
|
||||
hb_set_destroy (input);
|
||||
hb_set_destroy (after);
|
||||
hb_set_destroy (output);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -488,5 +589,6 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_ot_layout_lookup_collect_glyphs_source_sans);
|
||||
hb_test_add (test_ot_layout_lookup_collect_glyphs_noto_nastaliq);
|
||||
hb_test_add (test_ot_layout_lookup_collect_glyphs_qahiri);
|
||||
hb_test_add (test_ot_layout_lookup_collect_glyphs_shared_class_rules);
|
||||
return hb_test_run ();
|
||||
}
|
||||
Reference in new issue
Block a user