mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 06:21:16 +01:00
[layout] Charge each GSUB/GPOS lookup pass to max_ops
Lookups can share subtables, features can share lookups, and each shaping stage applies its lookups anew, so the number of passes over the buffer is not bounded by the font size. An earlier lookup can also grow the buffer far beyond the text the budget was sized from: a font that grows one character into a long buffer and then walks it with thousands of lookups (as in harfbuzz/harfrust#571) made hb_shape() spend CPU out of proportion to the text, and none of it was charged to buffer->max_ops. Charge each pass in apply_string() len / 16 for walking the buffer, even if it applies at no glyph (skipping a glyph is cheap, hence the division; applying at one is charged only through the buffer operations and nested lookups it does), and stop the stage loop in hb_ot_map_t::apply() once the buffer has failed. Nothing is charged in the matching hot loops (skipping iterator, RuleSet, ChainRuleSet, LigatureSet): charging there cost real fonts most of the budget in HarfRust (Gulzar spent about half of max_ops on ordinary Urdu prose, Noto Sans Duployan ran out on its own sample text). Aliased rules and ligatures stay bounded by the sanitizer, including the ChainRule array charge. Matches harfbuzz/harfrust#601. test_shape_lookup_budget grows one 'a' to 65025 glyphs before applying aliased lookups, and checks that the pass charge stops the shape. With the font from harfbuzz/harfrust#571 (one 'A' grown to 30,000 glyphs, then 2,000 lookups), hb_shape() takes about 10 ms instead of 0.6 s, stopping after about 35 passes with the glyphs kept. Gulzar and Noto Nastaliq Urdu spend under 0.5% of max_ops on Urdu and Persian prose. Noto Sans Duployan spends up to 45% on its sample text, as on main: nearly all of it is the _infos_set_glyph_flags() charge from #5522; the per-pass charge adds about 5%. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-6A9E6OEX. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-K2T2LOJH. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-DVG5XAU0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8f93b6d21b
commit
339aa688fa
2 files changed
+153
-1
No files matched your search
+14
-1
@@ -2035,6 +2035,19 @@ apply_string (OT::hb_ot_apply_context_t *c,
|
||||
if (unlikely (!buffer->len || !c->lookup_mask))
|
||||
return false;
|
||||
|
||||
/* Lookups can share subtables, features can share lookups, each stage
|
||||
* applies its lookups anew, and an earlier lookup can grow the buffer far
|
||||
* beyond the text the budget was sized for. Charge each pass for walking
|
||||
* the buffer, even if it applies at no glyph. Skipping a glyph is cheap,
|
||||
* hence the division; applying at one is charged only through the buffer
|
||||
* operations and nested lookups it does. */
|
||||
buffer->max_ops -= buffer->len / 16;
|
||||
if (unlikely (buffer->max_ops < 0))
|
||||
{
|
||||
buffer->successful = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ret = false;
|
||||
|
||||
c->set_lookup_props (lookup.get_props ());
|
||||
@@ -2080,7 +2093,7 @@ inline void hb_ot_map_t::apply (const Proxy &proxy,
|
||||
for (unsigned int stage_index = 0; stage_index < stages[table_index].length; stage_index++)
|
||||
{
|
||||
const stage_map_t *stage = &stages[table_index][stage_index];
|
||||
for (; i < stage->last_lookup; i++)
|
||||
for (; i < stage->last_lookup && buffer->successful; i++)
|
||||
{
|
||||
auto &lookup = lookups[table_index][i];
|
||||
|
||||
|
||||
@@ -374,6 +374,144 @@ test_shape_harfrust_budget (void)
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
put16 (char *p, unsigned v)
|
||||
{
|
||||
p[0] = (char) (v >> 8);
|
||||
p[1] = (char) v;
|
||||
}
|
||||
|
||||
static hb_face_t *
|
||||
create_aliased_lookups_face (unsigned lookup_count)
|
||||
{
|
||||
/* A GSUB whose one 'ccmp' feature lists two lookups that each replace
|
||||
* glyph 1 by 255 copies of it, growing one 'a' to 65025 glyphs, and then
|
||||
* lookup_count lookups, all of them the same 20-byte single substitution
|
||||
* of glyph 1 by itself. */
|
||||
unsigned total = 2 + lookup_count;
|
||||
unsigned feature_len = 2 + 6 + 4 + 2 * total;
|
||||
unsigned multiple_len = 8 + 8 + 6 + 2 + 2 * 255;
|
||||
unsigned lookup_list_len = 2 + 2 * total + 20 + multiple_len;
|
||||
unsigned gsub_len = 10 + 20 + feature_len + lookup_list_len;
|
||||
char *gsub = calloc (gsub_len, 1);
|
||||
char *p = gsub;
|
||||
unsigned i;
|
||||
|
||||
/* Header */
|
||||
put16 (p, 1);
|
||||
put16 (p + 4, 10);
|
||||
put16 (p + 6, 10 + 20);
|
||||
put16 (p + 8, 10 + 20 + feature_len);
|
||||
p += 10;
|
||||
/* ScriptList: 'DFLT' with a default LangSys using feature 0 */
|
||||
put16 (p, 1);
|
||||
memcpy (p + 2, "DFLT", 4);
|
||||
put16 (p + 6, 8);
|
||||
put16 (p + 8, 4);
|
||||
put16 (p + 10, 0);
|
||||
put16 (p + 12, 0);
|
||||
put16 (p + 14, 0xFFFF);
|
||||
put16 (p + 16, 1);
|
||||
put16 (p + 18, 0);
|
||||
p += 20;
|
||||
/* FeatureList: 'ccmp' listing every lookup */
|
||||
put16 (p, 1);
|
||||
memcpy (p + 2, "ccmp", 4);
|
||||
put16 (p + 6, 8);
|
||||
put16 (p + 8, 0);
|
||||
put16 (p + 10, total);
|
||||
for (i = 0; i < total; i++)
|
||||
put16 (p + 12 + 2 * i, i);
|
||||
p += feature_len;
|
||||
/* LookupList: lookups 0 and 1 point at the multiple substitution, the
|
||||
* others at the single substitution, both after the offsets. */
|
||||
unsigned single_offset = 2 + 2 * total;
|
||||
unsigned multiple_offset = single_offset + 20;
|
||||
put16 (p, total);
|
||||
for (i = 0; i < total; i++)
|
||||
put16 (p + 2 + 2 * i, i < 2 ? multiple_offset : single_offset);
|
||||
p += single_offset;
|
||||
put16 (p, 1); /* Lookup: type 1 */
|
||||
put16 (p + 2, 0); /* flag */
|
||||
put16 (p + 4, 1); /* one subtable */
|
||||
put16 (p + 6, 8);
|
||||
put16 (p + 8, 1); /* SingleSubstFormat1 */
|
||||
put16 (p + 10, 6); /* coverage */
|
||||
put16 (p + 12, 0); /* deltaGlyphID */
|
||||
put16 (p + 14, 1); /* CoverageFormat1 */
|
||||
put16 (p + 16, 1);
|
||||
put16 (p + 18, 1); /* glyph 1 */
|
||||
p += 20;
|
||||
put16 (p, 2); /* Lookup: type 2 */
|
||||
put16 (p + 2, 0); /* flag */
|
||||
put16 (p + 4, 1); /* one subtable */
|
||||
put16 (p + 6, 8);
|
||||
put16 (p + 8, 1); /* MultipleSubstFormat1 */
|
||||
put16 (p + 10, 8); /* coverage */
|
||||
put16 (p + 12, 1); /* one sequence */
|
||||
put16 (p + 14, 14);
|
||||
put16 (p + 16, 1); /* CoverageFormat1 */
|
||||
put16 (p + 18, 1);
|
||||
put16 (p + 20, 1); /* glyph 1 */
|
||||
put16 (p + 22, 255); /* Sequence: 255 times glyph 1 */
|
||||
for (i = 0; i < 255; i++)
|
||||
put16 (p + 24 + 2 * i, 1);
|
||||
|
||||
/* cmap: format 12, 'a' -> glyph 1 */
|
||||
static const char cmap_data[] = {0, 0, 0, 1, 0, 3, 0, 10, 0, 0, 0, 12,
|
||||
0, 12, 0, 0, 0, 0, 0, 28, 0, 0, 0, 0, 0, 0, 0, 1,
|
||||
0, 0, 0, 'a', 0, 0, 0, 'a', 0, 0, 0, 1};
|
||||
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
hb_blob_t *blob = hb_blob_create_or_fail (gsub, gsub_len, HB_MEMORY_MODE_WRITABLE, gsub, free);
|
||||
hb_face_builder_add_table (face, HB_TAG ('G','S','U','B'), blob);
|
||||
hb_blob_destroy (blob);
|
||||
HB_FACE_ADD_TABLE (face, "cmap", cmap_data);
|
||||
hb_face_set_glyph_count (face, 2);
|
||||
return face;
|
||||
}
|
||||
|
||||
static void
|
||||
test_shape_lookup_budget (void)
|
||||
{
|
||||
/* Lookups and features can alias the same bytes, and a lookup can grow
|
||||
* the buffer far beyond the text, so a small font can make the shaper
|
||||
* walk a long buffer thousands of times for one character. Those passes
|
||||
* must be charged to the buffer's work budget, which one character sizes
|
||||
* at 65536, and cut off when it runs out; each costs 65025 / 16. */
|
||||
hb_face_t *face;
|
||||
hb_font_t *font;
|
||||
hb_buffer_t *buffer;
|
||||
const char *shaper_list[] = {"ot", NULL};
|
||||
|
||||
face = create_aliased_lookups_face (4);
|
||||
font = hb_font_create (face);
|
||||
buffer = hb_buffer_create ();
|
||||
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
|
||||
hb_buffer_add (buffer, 'a', 0);
|
||||
hb_buffer_guess_segment_properties (buffer);
|
||||
hb_shape_full (font, buffer, NULL, 0, shaper_list);
|
||||
g_assert_true (hb_buffer_allocation_successful (buffer));
|
||||
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 65025);
|
||||
hb_buffer_destroy (buffer);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
|
||||
face = create_aliased_lookups_face (2000);
|
||||
font = hb_font_create (face);
|
||||
buffer = hb_buffer_create ();
|
||||
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
|
||||
hb_buffer_add (buffer, 'a', 0);
|
||||
hb_buffer_guess_segment_properties (buffer);
|
||||
hb_shape_full (font, buffer, NULL, 0, shaper_list);
|
||||
g_assert_false (hb_buffer_allocation_successful (buffer));
|
||||
/* The glyphs made so far are kept. */
|
||||
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 65025);
|
||||
hb_buffer_destroy (buffer);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_shape_list (void)
|
||||
{
|
||||
@@ -428,6 +566,7 @@ main (int argc, char **argv)
|
||||
#ifndef HB_NO_AAT
|
||||
hb_test_add (test_shape_kerx_control_point_actions_are_charged);
|
||||
#endif
|
||||
hb_test_add (test_shape_lookup_budget);
|
||||
/* TODO test fallback shaper */
|
||||
/* TODO test shaper_full */
|
||||
hb_test_add (test_shape_list);
|
||||
|
||||
Reference in new issue
Block a user