mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-30 18:13:53 +01:00
[GPOS] Charge out-of-order MarkBasePos/MarkLigPos base rescans to max_ops
MarkBasePosFormat1 and MarkLigPosFormat1 find a mark's base with a cache (c->last_base / last_base_until) that is only amortized while buffer->idx moves forward. A (Chain)Context lookup that recurses into the mark lookup at input positions out of order resets the cache, so each application rescans the glyphs from last_base_until to idx from scratch; on a long run of marks with no base this is quadratic and was uncharged. Charge the rescanned span (idx - j) to buffer->max_ops after the backward search in both lookups, failing the shape when the budget runs out. A real mark-heavy font pays a negligible amount. Adds a regression test shaping a Context lookup that recurses to MarkBasePos at positions 1,0,1,0,... Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-FJEXEDJ4. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
339aa688fa
commit
0aa7da1772
3 files changed
+147
No files matched your search
@@ -495,6 +495,132 @@ test_ot_layout_collect_lookup_variations (void)
|
||||
}
|
||||
#endif
|
||||
|
||||
/* A Context lookup that recurses to a MarkBasePos lookup at input positions
|
||||
* 1,0,1,0,... makes the latter drop its cached backward base search every
|
||||
* other time and rescan from scratch. On a run of marks with no base glyph
|
||||
* each rescan walks the whole buffer, so it must be charged to the buffer's
|
||||
* work budget: shaping must give up rather than take quadratic time. */
|
||||
static void
|
||||
test_ot_layout_gpos_mark_base_rescan_budget (void)
|
||||
{
|
||||
#define LOOKUP_RECORDS 20
|
||||
#define MARKS 10000
|
||||
static const char gdef[] = {
|
||||
0x00, 0x01, 0x00, 0x00, /* version 1.0 */
|
||||
0x00, 0x0C, /* glyphClassDefOffset */
|
||||
0x00, 0x00, /* attachListOffset */
|
||||
0x00, 0x00, /* ligCaretListOffset */
|
||||
0x00, 0x00, /* markAttachClassDefOffset */
|
||||
0x00, 0x01, 0x00, 0x02, /* ClassDef format 1, glyph 2 */
|
||||
0x00, 0x01, 0x00, 0x03, /* one glyph in class 3 (mark) */
|
||||
};
|
||||
static const char cmap[] = {
|
||||
0x00, 0x00, 0x00, 0x01, /* version 0, one encoding record */
|
||||
0x00, 0x03, 0x00, 0x0A, 0x00, 0x00, 0x00, 0x0C, /* (3,10) at 12 */
|
||||
0x00, 0x0C, 0x00, 0x00, /* format 12 */
|
||||
0x00, 0x00, 0x00, 0x1C, /* length */
|
||||
0x00, 0x00, 0x00, 0x00, /* language */
|
||||
0x00, 0x00, 0x00, 0x01, /* one group: */
|
||||
0x00, 0x00, 0x00, 0x6D, 0x00, 0x00, 0x00, 0x6D, 0x00, 0x00, 0x00, 0x02, /* U+006D -> glyph 2 */
|
||||
};
|
||||
static const char maxp[] = {
|
||||
0x00, 0x00, 0x50, 0x00, /* version 0.5 */
|
||||
0x00, 0x03, /* numGlyphs */
|
||||
};
|
||||
/* GPOS up to the LookupRecords of the Context lookup. */
|
||||
static const char gpos_head[] = {
|
||||
0x00, 0x01, 0x00, 0x00, /* version 1.0 */
|
||||
0x00, 0x0A, /* scriptListOffset */
|
||||
0x00, 0x1E, /* featureListOffset */
|
||||
0x00, 0x2C, /* lookupListOffset */
|
||||
/* ScriptList: DFLT with a default LangSys using feature 0 */
|
||||
0x00, 0x01, 'D', 'F', 'L', 'T', 0x00, 0x08,
|
||||
0x00, 0x04, 0x00, 0x00,
|
||||
0x00, 0x00, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0x00,
|
||||
/* FeatureList: 'mark' using lookup 0 */
|
||||
0x00, 0x01, 'm', 'a', 'r', 'k', 0x00, 0x08,
|
||||
0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
|
||||
/* LookupList: two lookups */
|
||||
0x00, 0x02, 0x00, 0x06, 0x00, 0x6E,
|
||||
/* Lookup 0: ContextPos, one subtable */
|
||||
0x00, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08,
|
||||
/* ContextPos format 3: input [glyph 2] [glyph 2] */
|
||||
0x00, 0x03, 0x00, 0x02, 0x00, LOOKUP_RECORDS, 0x00, 0x5A, 0x00, 0x5A,
|
||||
/* LookupRecords go here */
|
||||
};
|
||||
static const char gpos_tail[] = {
|
||||
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, /* Coverage: glyph 2 */
|
||||
/* Lookup 1: MarkBasePos, one subtable */
|
||||
0x00, 0x04, 0x00, 0x00, 0x00, 0x01, 0x00, 0x08,
|
||||
/* MarkBasePos format 1: mark [glyph 2], base [glyph 1], one class */
|
||||
0x00, 0x01, 0x00, 0x0C, 0x00, 0x12, 0x00, 0x01, 0x00, 0x18, 0x00, 0x24,
|
||||
0x00, 0x01, 0x00, 0x01, 0x00, 0x02, /* markCoverage */
|
||||
0x00, 0x01, 0x00, 0x01, 0x00, 0x01, /* baseCoverage */
|
||||
0x00, 0x01, 0x00, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, /* MarkArray */
|
||||
0x00, 0x01, 0x00, 0x04, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, /* BaseArray */
|
||||
};
|
||||
char gpos[sizeof (gpos_head) + 4 * LOOKUP_RECORDS + sizeof (gpos_tail)];
|
||||
char *records = gpos + sizeof (gpos_head);
|
||||
unsigned int out_of_order, i;
|
||||
|
||||
memcpy (gpos, gpos_head, sizeof (gpos_head));
|
||||
memcpy (records + 4 * LOOKUP_RECORDS, gpos_tail, sizeof (gpos_tail));
|
||||
|
||||
for (out_of_order = 0; out_of_order < 2; out_of_order++)
|
||||
{
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
hb_font_t *font;
|
||||
hb_buffer_t *buffer = hb_buffer_create ();
|
||||
hb_blob_t *blob;
|
||||
|
||||
/* All records recurse to lookup 1, either at input positions
|
||||
* 0,...,0,1,...,1 (forward only) or at 1,0,1,0,... */
|
||||
for (i = 0; i < LOOKUP_RECORDS; i++)
|
||||
{
|
||||
records[4 * i + 0] = 0;
|
||||
records[4 * i + 1] = out_of_order ? i & 1 : i >= LOOKUP_RECORDS / 2;
|
||||
records[4 * i + 2] = 0;
|
||||
records[4 * i + 3] = 1;
|
||||
}
|
||||
|
||||
blob = hb_blob_create (gdef, sizeof (gdef), HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
g_assert_true (hb_face_builder_add_table (face, HB_OT_TAG_GDEF, blob));
|
||||
hb_blob_destroy (blob);
|
||||
blob = hb_blob_create (gpos, sizeof (gpos), HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
g_assert_true (hb_face_builder_add_table (face, HB_OT_TAG_GPOS, blob));
|
||||
hb_blob_destroy (blob);
|
||||
blob = hb_blob_create (cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
g_assert_true (hb_face_builder_add_table (face, HB_TAG ('c','m','a','p'), blob));
|
||||
hb_blob_destroy (blob);
|
||||
blob = hb_blob_create (maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL);
|
||||
g_assert_true (hb_face_builder_add_table (face, HB_TAG ('m','a','x','p'), blob));
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
font = hb_font_create (face);
|
||||
|
||||
for (i = 0; i < MARKS; i++)
|
||||
hb_buffer_add (buffer, 0x006Du, i);
|
||||
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
|
||||
hb_buffer_guess_segment_properties (buffer);
|
||||
|
||||
hb_shape (font, buffer, NULL, 0);
|
||||
|
||||
if (out_of_order)
|
||||
g_assert_true (!hb_buffer_allocation_successful (buffer));
|
||||
else
|
||||
{
|
||||
g_assert_true (hb_buffer_allocation_successful (buffer));
|
||||
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, MARKS);
|
||||
}
|
||||
|
||||
hb_buffer_destroy (buffer);
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
#undef MARKS
|
||||
#undef LOOKUP_RECORDS
|
||||
}
|
||||
|
||||
static char *
|
||||
write_be16 (char *p, unsigned v)
|
||||
{
|
||||
@@ -599,6 +725,7 @@ main (int argc, char **argv)
|
||||
#ifndef HB_NO_VAR
|
||||
hb_test_add (test_ot_layout_collect_lookup_variations);
|
||||
#endif
|
||||
hb_test_add (test_ot_layout_gpos_mark_base_rescan_budget);
|
||||
hb_test_add (test_ot_layout_lookups_substitute_closure_large_coverage);
|
||||
return hb_test_run ();
|
||||
}
|
||||
Reference in new issue
Block a user