mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-23 16:01:07 +01:00
[layout] Charge GSUB would_apply() walks to max_ops during shaping
hb_would_apply_context_t walked every subtable of a lookup and every ligature/rule of a set without charging any work limit, while the Indic shaper issues eight such probes per consonant (plus rphf/pref probes per syllable). A GSUB whose offsets alias one subtable and one ligature makes each probe cost millions of tests, and the sanitizer budget only bounds the font once while shaping pays it per glyph, so a 131 KB font makes hb_shape() spend ~17 ms per consonant with max_ops never consulted. Give the context an optional hb_buffer_t and a charge_ops() that spends buffer->max_ops and clears buffer->successful, matching hb_ot_apply_context_t::charge_ops(); charge it in SubstLookup::would_apply (1 + subtable count, after the digest reject), LigatureSet::would_apply (ligature count) and RuleSet/ChainRuleSet::would_apply (rule count). An HB_INTERNAL hb_ot_layout_lookup_would_substitute_with_buffer() carries the buffer from the Indic shaper (all five probe sites); the public hb_ot_layout_lookup_would_substitute() passes nullptr, so its API, ABI and behaviour are unchanged. When the budget runs out the remaining probes answer no and hb_buffer_allocation_successful() returns false, the same outcome max_ops exhaustion already has during lookup application. The attack font drops from ~25 s to ~0.03 s for a 1,500-consonant line; real Indic fonts spend a handful of ops per consonant against a budget of 4096 per character. Verified Indic output byte-identical on a real Devanagari font and across the shape suite. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-Y2YGXGS0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
6202af118b
commit
b74fee5e87
6 files changed
+70
-18
No files matched your search
@@ -81,6 +81,7 @@ struct LigatureSet
|
||||
|
||||
bool would_apply (hb_would_apply_context_t *c) const
|
||||
{
|
||||
if (unlikely (!c->charge_ops (ligature.len))) return false;
|
||||
return
|
||||
+ hb_iter (ligature)
|
||||
| hb_map (hb_add (this))
|
||||
|
||||
@@ -101,6 +101,7 @@ struct SubstLookup : Lookup
|
||||
{
|
||||
if (unlikely (!c->len)) return false;
|
||||
if (!accel->may_have (c->glyphs[0])) return false;
|
||||
if (unlikely (!c->charge_ops (1 + get_subtable_count ()))) return false;
|
||||
return dispatch (c);
|
||||
}
|
||||
|
||||
|
||||
@@ -291,19 +291,39 @@ struct hb_would_apply_context_t :
|
||||
static return_t default_return_value () { return false; }
|
||||
bool stop_sublookup_iteration (return_t r) const { return r; }
|
||||
|
||||
/* Subtable, rule and ligature offsets can alias each other, so a font can
|
||||
* declare far more of them than it has bytes. When running as part of
|
||||
* shaping, charge walking them to the buffer's work budget, the way
|
||||
* hb_ot_apply_context_t does. */
|
||||
bool charge_ops (unsigned int count)
|
||||
{
|
||||
if (!buffer) return true;
|
||||
if (unlikely (buffer->max_ops < (int) count))
|
||||
{
|
||||
buffer->max_ops = -1;
|
||||
buffer->successful = false;
|
||||
return false;
|
||||
}
|
||||
buffer->max_ops -= (int) count;
|
||||
return true;
|
||||
}
|
||||
|
||||
hb_face_t *face;
|
||||
const hb_codepoint_t *glyphs;
|
||||
unsigned int len;
|
||||
bool zero_context;
|
||||
hb_buffer_t *buffer; /* nullptr when called from the public API. */
|
||||
|
||||
hb_would_apply_context_t (hb_face_t *face_,
|
||||
const hb_codepoint_t *glyphs_,
|
||||
unsigned int len_,
|
||||
bool zero_context_) :
|
||||
bool zero_context_,
|
||||
hb_buffer_t *buffer_ = nullptr) :
|
||||
face (face_),
|
||||
glyphs (glyphs_),
|
||||
len (len_),
|
||||
zero_context (zero_context_) {}
|
||||
zero_context (zero_context_),
|
||||
buffer (buffer_) {}
|
||||
};
|
||||
|
||||
struct hb_collect_glyphs_context_t :
|
||||
@@ -3315,6 +3335,7 @@ struct RuleSetOf
|
||||
bool would_apply (hb_would_apply_context_t *c,
|
||||
const ContextApplyLookupContext &lookup_context) const
|
||||
{
|
||||
if (unlikely (!c->charge_ops (rule.len))) return false;
|
||||
return
|
||||
+ hb_iter (rule)
|
||||
| hb_map (hb_add (this))
|
||||
@@ -4918,6 +4939,7 @@ struct ChainRuleSetOf
|
||||
bool would_apply (hb_would_apply_context_t *c,
|
||||
const ChainContextApplyLookupContext &lookup_context) const
|
||||
{
|
||||
if (unlikely (!c->charge_ops (rule.len))) return false;
|
||||
return
|
||||
+ hb_iter (rule)
|
||||
| hb_map (hb_add (this))
|
||||
|
||||
+16
-1
@@ -1585,10 +1585,25 @@ hb_ot_layout_lookup_would_substitute (hb_face_t *face,
|
||||
const hb_codepoint_t *glyphs,
|
||||
unsigned int glyphs_length,
|
||||
hb_bool_t zero_context)
|
||||
{
|
||||
return hb_ot_layout_lookup_would_substitute_with_buffer (face, lookup_index,
|
||||
glyphs, glyphs_length,
|
||||
(bool) zero_context,
|
||||
nullptr);
|
||||
}
|
||||
|
||||
/* Same, but charges the work done to buffer's max_ops when buffer is not nullptr. */
|
||||
bool
|
||||
hb_ot_layout_lookup_would_substitute_with_buffer (hb_face_t *face,
|
||||
unsigned int lookup_index,
|
||||
const hb_codepoint_t *glyphs,
|
||||
unsigned int glyphs_length,
|
||||
bool zero_context,
|
||||
hb_buffer_t *buffer)
|
||||
{
|
||||
auto &gsub = face->table.GSUB;
|
||||
if (unlikely (lookup_index >= gsub->lookup_count)) return false;
|
||||
OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, (bool) zero_context);
|
||||
OT::hb_would_apply_context_t c (face, glyphs, glyphs_length, zero_context, buffer);
|
||||
|
||||
const OT::SubstLookup& l = gsub->table->get_lookup (lookup_index);
|
||||
auto *accel = gsub->get_accel (lookup_index);
|
||||
|
||||
@@ -68,6 +68,14 @@ hb_ot_layout_table_find_feature (hb_face_t *face,
|
||||
hb_tag_t feature_tag,
|
||||
unsigned int *feature_index);
|
||||
|
||||
HB_INTERNAL bool
|
||||
hb_ot_layout_lookup_would_substitute_with_buffer (hb_face_t *face,
|
||||
unsigned int lookup_index,
|
||||
const hb_codepoint_t *glyphs,
|
||||
unsigned int glyphs_length,
|
||||
bool zero_context,
|
||||
hb_buffer_t *buffer);
|
||||
|
||||
|
||||
/*
|
||||
* GDEF
|
||||
|
||||
+20
-15
@@ -98,10 +98,14 @@ struct hb_indic_would_substitute_feature_t
|
||||
|
||||
bool would_substitute (const hb_codepoint_t *glyphs,
|
||||
unsigned int glyphs_count,
|
||||
hb_face_t *face) const
|
||||
hb_face_t *face,
|
||||
hb_buffer_t *buffer) const
|
||||
{
|
||||
if (unlikely (!buffer->successful)) return false;
|
||||
for (const auto &lookup : lookups)
|
||||
if (hb_ot_layout_lookup_would_substitute (face, lookup.index, glyphs, glyphs_count, zero_context))
|
||||
if (hb_ot_layout_lookup_would_substitute_with_buffer (face, lookup.index,
|
||||
glyphs, glyphs_count,
|
||||
zero_context, buffer))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
@@ -357,7 +361,8 @@ static indic_position_t
|
||||
consonant_position_from_face (const indic_shape_plan_t *indic_plan,
|
||||
const hb_codepoint_t consonant,
|
||||
const hb_codepoint_t virama,
|
||||
hb_face_t *face)
|
||||
hb_face_t *face,
|
||||
hb_buffer_t *buffer)
|
||||
{
|
||||
/* For old-spec, the order of glyphs is Consonant,Virama,
|
||||
* whereas for new-spec, it's Virama,Consonant. However,
|
||||
@@ -374,16 +379,16 @@ consonant_position_from_face (const indic_shape_plan_t *indic_plan,
|
||||
* https://github.com/harfbuzz/harfbuzz/issues/1587
|
||||
*/
|
||||
hb_codepoint_t glyphs[3] = {virama, consonant, virama};
|
||||
if (indic_plan->blwf.would_substitute (glyphs , 2, face) ||
|
||||
indic_plan->blwf.would_substitute (glyphs+1, 2, face) ||
|
||||
indic_plan->vatu.would_substitute (glyphs , 2, face) ||
|
||||
indic_plan->vatu.would_substitute (glyphs+1, 2, face))
|
||||
if (indic_plan->blwf.would_substitute (glyphs , 2, face, buffer) ||
|
||||
indic_plan->blwf.would_substitute (glyphs+1, 2, face, buffer) ||
|
||||
indic_plan->vatu.would_substitute (glyphs , 2, face, buffer) ||
|
||||
indic_plan->vatu.would_substitute (glyphs+1, 2, face, buffer))
|
||||
return POS_BELOW_C;
|
||||
if (indic_plan->pstf.would_substitute (glyphs , 2, face) ||
|
||||
indic_plan->pstf.would_substitute (glyphs+1, 2, face))
|
||||
if (indic_plan->pstf.would_substitute (glyphs , 2, face, buffer) ||
|
||||
indic_plan->pstf.would_substitute (glyphs+1, 2, face, buffer))
|
||||
return POS_POST_C;
|
||||
if (indic_plan->pref.would_substitute (glyphs , 2, face) ||
|
||||
indic_plan->pref.would_substitute (glyphs+1, 2, face))
|
||||
if (indic_plan->pref.would_substitute (glyphs , 2, face, buffer) ||
|
||||
indic_plan->pref.would_substitute (glyphs+1, 2, face, buffer))
|
||||
return POS_POST_C;
|
||||
return POS_BASE_C;
|
||||
}
|
||||
@@ -445,7 +450,7 @@ update_consonant_positions_indic (const hb_ot_shape_plan_t *plan,
|
||||
if (info[i].indic_position() == POS_BASE_C)
|
||||
{
|
||||
hb_codepoint_t consonant = info[i].codepoint;
|
||||
info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face);
|
||||
info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face, buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -511,9 +516,9 @@ initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
|
||||
info[start + 1].codepoint,
|
||||
indic_plan->config->reph_mode == REPH_MODE_EXPLICIT ?
|
||||
info[start + 2].codepoint : 0};
|
||||
if (indic_plan->rphf.would_substitute (glyphs, 2, face) ||
|
||||
if (indic_plan->rphf.would_substitute (glyphs, 2, face, buffer) ||
|
||||
(indic_plan->config->reph_mode == REPH_MODE_EXPLICIT &&
|
||||
indic_plan->rphf.would_substitute (glyphs, 3, face)))
|
||||
indic_plan->rphf.would_substitute (glyphs, 3, face, buffer)))
|
||||
{
|
||||
limit += 2;
|
||||
while (limit < end && is_joiner (info[limit]))
|
||||
@@ -898,7 +903,7 @@ initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
|
||||
hb_codepoint_t glyphs[2];
|
||||
for (unsigned int j = 0; j < pref_len; j++)
|
||||
glyphs[j] = info[i + j].codepoint;
|
||||
if (indic_plan->pref.would_substitute (glyphs, pref_len, face))
|
||||
if (indic_plan->pref.would_substitute (glyphs, pref_len, face, buffer))
|
||||
{
|
||||
for (unsigned int j = 0; j < pref_len; j++)
|
||||
info[i++].mask |= indic_plan->mask_array[INDIC_PREF];
|
||||
|
||||
Reference in new issue
Block a user