Files
harfbuzz/src/OT/Layout/GSUB/SingleSubstFormat2.hh
T
Behdad EsfahbodandClaude Opus 4.8 70df3ff279 [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 <noreply@anthropic.com>
2026-11-10 16:42:16 -06:00

215 lines
6.1 KiB
C++

#ifndef OT_LAYOUT_GSUB_SINGLESUBSTFORMAT2_HH
#define OT_LAYOUT_GSUB_SINGLESUBSTFORMAT2_HH
#include "Common.hh"
namespace OT {
namespace Layout {
namespace GSUB_impl {
template <typename Types>
struct SingleSubstFormat2_4
{
protected:
HBUINT16 format; /* Format identifier--format = 2 */
typename Types::template LOffsetTo<Coverage>
coverage; /* Offset to Coverage table--from
* beginning of Substitution table */
typename Types::template ArrayOf<typename Types::HBGlyphID>
substitute; /* Array of substitute
* GlyphIDs--ordered by Coverage Index */
public:
DEFINE_SIZE_ARRAY (2 + Types::LOffset::static_size + Types::HBUINT::static_size, substitute);
bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
return_trace (coverage.sanitize (c, this) && substitute.sanitize (c));
}
bool intersects (const hb_set_t *glyphs) const
{ return (this+coverage).intersects (glyphs); }
bool may_have_non_1to1 () const
{ return false; }
void depend (hb_depend_context_t *c) const
{
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
// Filter by active glyphs like closure does
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]);
}
return;
}
+ 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); })
;
}
void closure (hb_closure_context_t *c) const
{
auto &cov = this+coverage;
auto &glyph_set = c->parent_active_glyphs ();
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);
if (i == NOT_COVERED || i >= substitute.len)
continue;
c->output->add (substitute.arrayZ[i]);
}
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)
| hb_sink (c->output)
;
}
void closure_lookups (hb_closure_lookups_context_t *c) const {}
void collect_glyphs (hb_collect_glyphs_context_t *c) const
{
if (unlikely (!(this+coverage).collect_coverage (c->input))) return;
+ hb_zip (this+coverage, substitute)
| hb_map (hb_second)
| hb_sink (c->output)
;
}
const Coverage &get_coverage () const { return this+coverage; }
bool would_apply (hb_would_apply_context_t *c) const
{ return c->len == 1 && (this+coverage).get_coverage (c->glyphs[0]) != NOT_COVERED; }
unsigned
get_glyph_alternates (hb_codepoint_t glyph_id,
unsigned start_offset,
unsigned *alternate_count /* IN/OUT. May be NULL. */,
hb_codepoint_t *alternate_glyphs /* OUT. May be NULL. */) const
{
unsigned int index = (this+coverage).get_coverage (glyph_id);
if (likely (index == NOT_COVERED))
{
if (alternate_count)
*alternate_count = 0;
return 0;
}
if (alternate_count && *alternate_count && alternate_glyphs)
{
glyph_id = substitute[index];
*alternate_glyphs = glyph_id;
*alternate_count = 1;
}
return 1;
}
void
collect_glyph_alternates (hb_map_t *alternate_count /* IN/OUT */,
hb_map_t *alternate_glyphs /* IN/OUT */) const
{
+ hb_zip (this+coverage, substitute)
| hb_apply ([&] (const hb_pair_t<hb_codepoint_t, hb_codepoint_t> &p) -> void
{ _hb_collect_glyph_alternates_add (p.first, p.second,
alternate_count, alternate_glyphs); })
;
}
bool apply (hb_ot_apply_context_t *c) const
{
TRACE_APPLY (this);
unsigned int index = (this+coverage).get_coverage (c->buffer->cur().codepoint);
if (index == NOT_COVERED) return_trace (false);
if (unlikely (index >= substitute.len)) return_trace (false);
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->sync_so_far ();
c->buffer->message (c->font,
"replacing glyph at %u (single substitution)",
c->buffer->idx);
}
c->replace_glyph (substitute[index]);
if (HB_BUFFER_MESSAGE_MORE && c->buffer->messaging ())
{
c->buffer->message (c->font,
"replaced glyph at %u (single substitution)",
c->buffer->idx - 1u);
}
return_trace (true);
}
template<typename Iterator,
hb_requires (hb_is_sorted_source_of (Iterator,
hb_codepoint_pair_t))>
bool serialize (hb_serialize_context_t *c,
Iterator it)
{
TRACE_SERIALIZE (this);
auto substitutes =
+ it
| hb_map (hb_second)
;
auto glyphs =
+ it
| hb_map_retains_sorting (hb_first)
;
if (unlikely (!c->extend_min (this))) return_trace (false);
if (unlikely (!substitute.serialize (c, substitutes))) return_trace (false);
if (unlikely (!coverage.serialize_serialize (c, glyphs))) return_trace (false);
return_trace (true);
}
bool subset (hb_subset_context_t *c) const
{
TRACE_SUBSET (this);
const hb_set_t &glyphset = *c->plan->glyphset_gsub ();
const hb_map_t &glyph_map = *c->plan->glyph_map;
auto it =
+ hb_zip (this+coverage, substitute)
| hb_filter (glyphset, hb_first)
| hb_filter (glyphset, hb_second)
| hb_map_retains_sorting ([&] (hb_pair_t<hb_codepoint_t, const typename Types::HBGlyphID &> p) -> hb_codepoint_pair_t
{ return hb_pair (glyph_map[p.first], glyph_map[p.second]); })
;
bool ret = bool (it);
SingleSubst_serialize (c->serializer, it);
return_trace (ret);
}
};
}
}
}
#endif /* OT_LAYOUT_GSUB_SINGLESUBSTFORMAT2_HH */