From ec4a38c4ab3adc0a3f35447b3e52d9b3c2ac2e45 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:05:20 -0600 Subject: [PATCH 01/10] [aat] Fix negative-state arithmetic in StateTable so 'kern'/'mort' tables with a non-first initial state are accepted AAT::StateTable numbers the rows before the initial 'kern'/'mort' row negatively (the Apple stateArray quirk the code documents), but the negative branch of sanitize() passed the negative int min_state to hb_unsigned_mul_overflows() and used it in unsigned index arithmetic, so the overflow check always fired and such tables were always rejected and silently dropped. get_entry() indexed the state array the same unsigned way and would read far past it had sanitize() let those tables through, so the two functions have to be fixed together. sanitize() now works with the magnitude of min_state (num_neg_states = -min_state) for the overflow check, the check_range() and the sweep pointer, and get_entry() computes its row index in signed int so negative states address the rows before the initial one. Negative states only arise for ObsoleteTypes, where nClasses and offsets are 16-bit so the products fit; for ExtendedTypes states are never negative and sanitize() already bounds (max_state+1)*nClasses by the blob size, so the (int) casts cannot change any sanitized index. Fonts whose 'kern' format 1 / 'mort' state tables use a non-first initial state now sanitize and apply instead of being dropped. Adds a regression test (kern-format1.tests plus its font) to test/shape/data/in-house. Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-FM7UTB3Y. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-common.hh | 12 +++++++----- .../a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf | Bin 0 -> 2208 bytes test/shape/data/in-house/meson.build | 1 + .../shape/data/in-house/tests/kern-format1.tests | 1 + 4 files changed, 9 insertions(+), 5 deletions(-) create mode 100644 test/shape/data/in-house/fonts/a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf create mode 100644 test/shape/data/in-house/tests/kern-format1.tests diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 251e9f7f4..9015371a8 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -948,7 +948,8 @@ struct StateTable const HBUSHORT *states = (this+stateArrayTable).arrayZ; const Entry *entries = (this+entryTable).arrayZ; - unsigned int entry = states[state * n_classes + klass]; + /* State can be negative; see sanitize(). Keep the index signed. */ + unsigned int entry = states[state * (int) n_classes + (int) klass]; DEBUG_MSG (APPLY, nullptr, "e%u", entry); return entries[entry]; @@ -997,16 +998,17 @@ struct StateTable if (min_state < state_neg) { /* Negative states. */ - if (unlikely (hb_unsigned_mul_overflows (min_state, num_classes))) + unsigned int num_neg_states = -min_state; + if (unlikely (hb_unsigned_mul_overflows (num_neg_states, num_classes))) return_trace (false); - if (unlikely (!c->check_range (&states[min_state * num_classes], - -min_state, + if (unlikely (!c->check_range (states - num_neg_states * num_classes, + num_neg_states, row_stride))) return_trace (false); if ((c->max_ops -= state_neg - min_state) <= 0) return_trace (false); { /* Sweep new states. */ - const HBUSHORT *stop = &states[min_state * num_classes]; + const HBUSHORT *stop = states - num_neg_states * num_classes; if (unlikely (stop > states)) return_trace (false); for (const HBUSHORT *p = states; stop < p; p--) diff --git a/test/shape/data/in-house/fonts/a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf b/test/shape/data/in-house/fonts/a5baedaf2c0f467d6a08701309a177dbd7e6156d.ttf new file mode 100644 index 0000000000000000000000000000000000000000..a7535a861ac8a2f3aeeae071c1f7163752f683fe GIT binary patch literal 2208 zcmZXVeQZ-z6u{4Y@3npXV0)e23af0dJl1`+Y;2|CTwunH4;Nun9wTkE34Da$n-)s!^5DCeZ4$nK)NQ(i>*yhZh@;*}$EWN9EGFOu7R0V?DrN1SMh zcuQmo4QdC^5dl?1~*L^j5p5-dWqA)?kTtEYaYk z1vM^ebkaf`OwMRN>6ar7>TnhAvwuj0{zi2e3iz8LuZ=2LtlU2&;ku36SeMhBw5oqd zh9)&y2-UV>DCBQedtLZcaQKdK_?)Dp7EIjYe-)NN4ivyMF>7IwIpbk!m8g6bcezHT z))HIPn!UiA1OY+F7zaQAgcxTPFeqkB4%AqJ;n8$L4aKVcJKJqDJNd+mIJ7uLt zDXL6i>0G-bU#rY=%yZ8!FQ4ajD6_PgireEUFUzu;E!NDuGS5Tv6gkt9#TQ71=RZ7t znUG)3gg5zjpXC;u~*&DPkaZ8Z#K~94i&>G9gwz2XuB+6sYVo9=76EXq!jiC)(B`Xj$7Dk!Nii zOl+VgDVj0W4&|k&bqY)85(}ry%GW98a^({GLInN7`YtMpBfI4$j`$s zzc^U8^31LQ&U`2S&7tjD82e;z{M-1L=Hn};;?7f7aQ9Z@j@`l?n+dfsDJK{IJhf92 z7A8%UEFy?lWfZ2WpdxFLN)oj&d4GXyeaq9ZNHEVEUv|Zj+yz{6Zh?P5JgwPsY2%Oa zzr&*gXa5jSiw8UQA340Ew`tAE0Ldqid+e|N??33+^TmjA{&RLn;PHpIsMiP@4W5We z;*^Z_@vCF&(kuUj86xw6i@%%au<<&nzkSy>c3od8b|dO14@=CV>x|>~M8EW|E&jN^ zG?|HoOk4uL4+26H1cmLePbh`e;~wn)%aXbV43G)g;6UO8fdwYdVuDuuF7yFp_Bj9& z_+?67+hCNqP-7VcI0#wLh^IOu_S9f3Hyckv#vHymcoGYGBoof1bdGsJJ+wd@^gt(` z>zk3fU_IVpyx9qkS7HPEmi+&sreX@k5F-*-Ok!q&a?o5+nrD+hEQUd{ Date: Fri, 30 Oct 2026 16:06:57 -0600 Subject: [PATCH 02/10] [aat] Index morx chain accelerator entries by subtable position hb_accelerate_subtables_context_t::dispatch() filled accelerator entries with a counter that advanced only for the subtable types ChainSubtable::dispatch() knows, while Chain::apply() reads entries by subtable position. A subtable of a reserved type (3, or 6 and up), which the sanitizer accepts and apply() steps over, shifts every later entry down by one, so each later subtable is gated by the wrong glyph set and the last one gets a zeroed entry and is never applied. Output then depends on where the unknown subtable sits in the chain. Add Chain::accelerate(), the same walk Chain::dispatch() does but setting the context's index to the subtable position before each dispatch, and stop the context's dispatch() from advancing the index itself; create() calls accelerate() instead of dispatch(). Unknown- type subtables keep a zeroed entry, which is what the calloc in create() already relies on, and apply() does nothing for them anyway. mort shares Chain/ChainSubtable so it is fixed too; the kerx accelerator already fills one entry per subtable and is untouched. Adds a regression font with subtables [reserved, 1->2, 3->1] to the in-house aat-morx shape tests. Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-OIN1GIHX. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-morx-table.hh | 22 +++++++++++++++--- ...b6608532fe39dd993eb3fc74e84256bd2d547e.ttf | Bin 0 -> 188 bytes test/shape/data/in-house/tests/aat-morx.tests | 2 ++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 test/shape/data/in-house/fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index aa6081131..9b1782faf 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -976,7 +976,7 @@ struct hb_accelerate_subtables_context_t : template return_t dispatch (const T &obj) { - hb_applicable_t *entry = &array[i++]; + hb_applicable_t *entry = &array[i]; entry->init (obj, num_glyphs); @@ -992,7 +992,7 @@ struct hb_accelerate_subtables_context_t : hb_applicable_t *array; unsigned num_glyphs; - unsigned i = 0; + unsigned i = 0; /* Index of the subtable being accelerated; set by Chain::accelerate(). */ }; struct hb_aat_layout_chain_accelerator_t @@ -1025,7 +1025,7 @@ struct hb_aat_layout_chain_accelerator_t thiz->count = count; hb_accelerate_subtables_context_t c_accelerate_subtables (thiz->subtables, num_glyphs); - chain.dispatch (&c_accelerate_subtables); + chain.accelerate (&c_accelerate_subtables); return thiz; } @@ -1260,6 +1260,22 @@ struct Chain size_t get_size () const { return length; } + /* Fills one accelerator entry per subtable, indexed by subtable position + * so that apply() can look them up by position. Subtables of unknown + * type are skipped by ChainSubtable::dispatch() and keep their zeroed + * entry, which is fine since apply() does not run them either. */ + void accelerate (hb_accelerate_subtables_context_t *c) const + { + const ChainSubtable *subtable = &StructAfter> (featureZ.as_array (featureCount)); + unsigned int count = subtableCount; + for (unsigned int i = 0; i < count; i++) + { + c->i = i; + subtable->dispatch (c); + subtable = &StructAfter> (*subtable); + } + } + template typename context_t::return_t dispatch (context_t *c, Ts&&... ds) const { diff --git a/test/shape/data/in-house/fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf b/test/shape/data/in-house/fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf new file mode 100644 index 0000000000000000000000000000000000000000..eb1503526003d49b4dcbcca9eaafdf738705e3db GIT binary patch literal 188 zcmZQzWME)m24Wyc&P^{wlV?$!3Yja literal 0 HcmV?d00001 diff --git a/test/shape/data/in-house/tests/aat-morx.tests b/test/shape/data/in-house/tests/aat-morx.tests index fffc7469b..279334b42 100644 --- a/test/shape/data/in-house/tests/aat-morx.tests +++ b/test/shape/data/in-house/tests/aat-morx.tests @@ -3,3 +3,5 @@ ../fonts/5f1d25b1f66fc4ef6d55b7c98f34b045b4adaa9d.ttf;;U+0041;[A=0+639|B=0+626|C=0+596|E=0+556] ../fonts/007e2c13e6baf28a7f1e8f981c2db0cb850b1f1f.ttf;--no-glyph-names;U+0061,U+0062,U+0063;[3=0+600|5=2+600] ../fonts/007e2c13e6baf28a7f1e8f981c2db0cb850b1f1f.ttf;--no-glyph-names;U+0061,U+0062,U+0063,U+0063;[3=0+600|5=2+600|4=3+600] +../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062;[2|2] +../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062,U+0063;[2|2|1] From 843b32d48c59c32b5be8fab73a5d59533eb72f50 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:14:19 -0600 Subject: [PATCH 03/10] [aat] Destroy the losing morx chain accelerator before freeing it hb_aat_layout_chain_accelerator_t owns one hb_bit_set_t (pages and page_map heap vectors) per subtable, filled by create() and released only by destroy(). mortmorx::accelerator_t::get_accel() builds a copy lazily and, when another thread already installed one, discarded its own copy with a bare hb_free(), leaking every subtable's glyph set. The lost-race branch is the only place the object was released without destroy() -- ~accelerator_t() calls destroy() first, and the sibling lazy-install patterns (the GSUB/GPOS accelerator and destroy_buffer_glyph_set()) already fini() before free -- so add accel->destroy() there to restore the invariant. mort and morx share the template, so one line covers both tables. No regression test: the leak is timing-dependent and none of the in-tree AAT fonts reaches the race window dependably in the threads suite. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-GNQC415D. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-morx-table.hh | 1 + 1 file changed, 1 insertion(+) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 9b1782faf..4ad80ca0b 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -1401,6 +1401,7 @@ struct mortmorx if (unlikely (!accels[chain_index].cmpexch (nullptr, accel))) { + accel->destroy (); hb_free (accel); goto retry; } From cf9889b73fe4d97a7c11dabe18467e011f40baef Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:16:06 -0600 Subject: [PATCH 04/10] [aat] Reject kern/kerx/mort/morx tables declaring too many subtables The kern/kerx accelerator keeps two hb_bit_set_t per subtable and mort/morx keeps one per subtable, all built eagerly when the shape plan is compiled and held for the life of the face. A 14-byte kern format-3 subtable fills both sets with every glyph, so heap use was about 2100x the font size with nothing bounding the subtable count; morx is worse because overlapping chains can claim each other's subtables. A ~560 KB kern font drove shaping one letter to 1.1 GiB. Add HB_AAT_MAX_SUBTABLES (1024) to hb-limits.hh and enforce it in sanitize, the way GSUB/GPOS already bound their subtables via visit_subtables (whose 0x4000 limit is too loose here -- 16383 kern subtables still peak at hundreds of MiB). KerxTable::sanitize() (shared by kerx, OpenType kern and Apple kern) fails when tableCount exceeds the limit, and mortmorx::sanitize() fails once the chains' subtable counts add up past it. Over-limit tables are dropped entirely (the font shapes without kerning/morx); real fonts under test/ declare at most three kern/kerx subtables and one morx chain. Regression test in test/api/test-aat-layout.c builds kern and morx faces with hb_face_builder and checks both sides of the limit. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-5N52Y0UN. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-kerx-table.hh | 3 + src/hb-aat-layout-morx-table.hh | 5 ++ src/hb-limits.hh | 7 ++ test/api/test-aat-layout.c | 133 ++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) diff --git a/src/hb-aat-layout-kerx-table.hh b/src/hb-aat-layout-kerx-table.hh index c53d6e353..7887cba35 100644 --- a/src/hb-aat-layout-kerx-table.hh +++ b/src/hb-aat-layout-kerx-table.hh @@ -1141,6 +1141,9 @@ struct KerxTable const SubTable *st = &thiz()->firstSubTable; unsigned int count = thiz()->tableCount; + /* The accelerator keeps two glyph sets per subtable. */ + if (unlikely (count > HB_AAT_MAX_SUBTABLES)) + return_trace (false); for (unsigned int i = 0; i < count; i++) { if (unlikely (!st->u.header.sanitize (c))) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 4ad80ca0b..7832eb873 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -1471,11 +1471,16 @@ struct mortmorx const Chain *chain = &firstChain; unsigned int count = chainCount; + unsigned int subtable_count = 0; for (unsigned int i = 0; i < count; i++) { if (!chain->sanitize (c, version)) return_trace (false); hb_barrier (); + /* The accelerator keeps a glyph set per subtable. */ + subtable_count += chain->get_subtable_count (); + if (unlikely (subtable_count > HB_AAT_MAX_SUBTABLES)) + return_trace (false); chain = &StructAfter> (*chain); } diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 1e1cebd9f..fd9c398d4 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -92,6 +92,13 @@ #define HB_MAX_LOOKUP_VISIT_COUNT 35000 #endif +/* Subtables of one kern, kerx, mort, or morx table; the table is rejected + * when it declares more. The table's accelerator keeps glyph sets for + * each, which can take far more memory than the subtable has bytes. */ +#ifndef HB_AAT_MAX_SUBTABLES +#define HB_AAT_MAX_SUBTABLES 1024 +#endif + #ifndef HB_MAX_GRAPH_EDGE_COUNT #define HB_MAX_GRAPH_EDGE_COUNT 16384 #endif diff --git a/test/api/test-aat-layout.c b/test/api/test-aat-layout.c index e4e85001b..88e9a18ce 100644 --- a/test/api/test-aat-layout.c +++ b/test/api/test-aat-layout.c @@ -116,6 +116,137 @@ test_aat_has (void) hb_face_destroy (trak); } + +static hb_bool_t +count_started_subtables (hb_buffer_t *buffer HB_UNUSED, + hb_font_t *font HB_UNUSED, + const char *message, + void *user_data) +{ + if (!strncmp (message, "start subtable ", 15) || + !strncmp (message, "start chainsubtable ", 20)) + (*(unsigned *) user_data)++; + return true; +} + +/* Shapes "aa" with a two-glyph font that has the table, which is + * freed; returns the number of its subtables that were applied. + * A table with more than HB_AAT_MAX_SUBTABLES subtables is rejected + * by sanitize, so none of them is. */ +static unsigned +count_applied_subtables (hb_tag_t tag, char *table, unsigned length) +{ + static const char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x02}; + static const char cmap[] = {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 *builder = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create (cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create (table, length, HB_MEMORY_MODE_READONLY, table, free); + hb_face_builder_add_table (builder, tag, blob); + hb_blob_destroy (blob); + blob = hb_face_reference_blob (builder); + hb_face_destroy (builder); + hb_face_t *limit_face = hb_face_create (blob, 0); + hb_blob_destroy (blob); + hb_font_t *font = hb_font_create (limit_face); + hb_face_destroy (limit_face); + + unsigned started = 0; + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_buffer_set_message_func (buffer, count_started_subtables, &started, NULL); + hb_shape (font, buffer, NULL, 0); + + hb_buffer_destroy (buffer); + hb_font_destroy (font); + + return started; +} + +/* A kern table of format-3 subtables. */ +static char * +make_kern_table (unsigned num_subtables, unsigned *length) +{ + *length = 4 + 14 * num_subtables; + char *kern = (char *) calloc (*length, 1); + kern[2] = (char) (num_subtables >> 8); + kern[3] = (char) (num_subtables & 0xFF); + for (unsigned i = 0; i < num_subtables; i++) + { + char *subtable = kern + 4 + 14 * i; + subtable[3] = 14; /* length */ + subtable[4] = 3; /* format */ + subtable[5] = 1; /* coverage: horizontal */ + subtable[7] = 1; /* glyphCount */ + } + return kern; +} + +static void +test_aat_kern_subtable_limit (void) +{ + unsigned length; + char *kern; + + kern = make_kern_table (1024, &length); + g_assert_cmpuint (1024, ==, count_applied_subtables (HB_TAG ('k','e','r','n'), kern, length)); + + kern = make_kern_table (1025, &length); + g_assert_cmpuint (0, ==, count_applied_subtables (HB_TAG ('k','e','r','n'), kern, length)); +} + +/* A morx table of chains of five noncontextual subtables each. */ +static char * +make_morx_table (unsigned num_chains, unsigned *length) +{ + *length = 8 + (16 + 5 * 20) * num_chains; + char *morx = (char *) calloc (*length, 1); + morx[1] = 2; /* version */ + morx[6] = (char) (num_chains >> 8); + morx[7] = (char) (num_chains & 0xFF); + for (unsigned i = 0; i < num_chains; i++) + { + char *chain = morx + 8 + (16 + 5 * 20) * i; + chain[3] = 1; /* defaultFlags */ + chain[7] = 16 + 5 * 20; /* length */ + chain[15] = 5; /* subtableCount */ + for (unsigned j = 0; j < 5; j++) + { + char *subtable = chain + 16 + 20 * j; + subtable[3] = 20; /* length */ + subtable[4] = 0x20; /* coverage: all directions */ + subtable[7] = 4; /* type */ + subtable[11] = 1; /* subFeatureFlags */ + subtable[13] = 8; /* lookup format */ + subtable[15] = 1; /* firstGlyph */ + subtable[17] = 1; /* glyphCount */ + subtable[19] = 1; /* value */ + } + } + return morx; +} + +static void +test_aat_morx_subtable_limit (void) +{ + unsigned length; + char *morx; + + morx = make_morx_table (204, &length); /* 1020 subtables */ + g_assert_cmpuint (1020, ==, count_applied_subtables (HB_TAG ('m','o','r','x'), morx, length)); + + morx = make_morx_table (205, &length); /* 1025 subtables */ + g_assert_cmpuint (0, ==, count_applied_subtables (HB_TAG ('m','o','r','x'), morx, length)); +} + #endif int @@ -128,6 +259,8 @@ main (int argc, char **argv) hb_test_add (test_aat_get_feature_types); hb_test_add (test_aat_get_feature_selectors); hb_test_add (test_aat_has); + hb_test_add (test_aat_kern_subtable_limit); + hb_test_add (test_aat_morx_subtable_limit); #endif face = hb_test_open_font_file ("fonts/aat-feat.ttf"); From 663a004d7cea1cfd896563fd61a31337d8a8545d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:21:26 -0600 Subject: [PATCH 05/10] [aat] Charge each applied kerx/kern/morx subtable pass to max_ops KerxTable::apply() (shared by kerx and both kern flavours) and morx Chain::apply() run one full pass over the buffer per subtable, but a pass that changes nothing never touched buffer->max_ops, so a font could ask for an unbounded number of passes -- and morx may first grow the buffer up to 256x, so each pass is over a much larger buffer than the input. The kerx loop also ignored buffer->successful. Add hb_aat_apply_context_t::charge_buffer_pass(), which debits buffer->len from max_ops using the same decrement/check/ successful=false idiom as morx glyph insertion and hb_buffer_t, and call it in both subtable loops right before a subtable is applied; skipped subtables (wrong direction, no glyph intersection) stay free. Placing the charge in the shared apply context covers kerx, OT kern, Apple kern and morx with one helper. Also stop the kerx loop once the buffer is no longer successful, as the morx loop already does. This complements the subtable-count cap: that bounds the per-face accelerator memory, while this bounds the per-shape pass work, including passes over a buffer that morx has grown. Real fonts apply a few subtables over a buffer far shorter than the budget, so they are unaffected. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KCOERIUT. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-common.hh | 13 +++++++++++++ src/hb-aat-layout-kerx-table.hh | 5 +++++ src/hb-aat-layout-morx-table.hh | 3 +++ 3 files changed, 21 insertions(+) diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 9015371a8..23cbb0384 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -166,6 +166,19 @@ struct hb_aat_apply_context_t : buffer_is_reversed = !buffer_is_reversed; } + /* Applying a subtable walks the whole buffer. The font controls the + * number of subtables, so charge each pass to the buffer's work budget. */ + HB_NODISCARD bool charge_buffer_pass () + { + buffer->max_ops -= buffer->len; + if (unlikely (buffer->max_ops < 0)) + { + buffer->successful = false; + return false; + } + return true; + } + void setup_buffer_glyph_set () { using_buffer_glyph_set = buffer->len >= 4 && buffer_glyph_set; diff --git a/src/hb-aat-layout-kerx-table.hh b/src/hb-aat-layout-kerx-table.hh index 7887cba35..f8e2987e7 100644 --- a/src/hb-aat-layout-kerx-table.hh +++ b/src/hb-aat-layout-kerx-table.hh @@ -1087,6 +1087,9 @@ struct KerxTable reverse = bool (st->u.header.coverage & st->u.header.Backwards) != HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction); + if (unlikely (!c->charge_buffer_pass ())) + break; + if (!c->buffer->message (c->font, "start subtable %u", c->lookup_index)) goto skip; @@ -1118,6 +1121,8 @@ struct KerxTable (void) c->buffer->message (c->font, "end subtable %u", c->lookup_index); + if (unlikely (!c->buffer->successful)) break; + skip: st = &StructAfter (*st); c->set_lookup_index (c->lookup_index + 1); diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 7832eb873..a85ad6d4d 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -1238,6 +1238,9 @@ struct Chain bool (coverage & ChainSubtable::Backwards) != HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction); + if (unlikely (!c->charge_buffer_pass ())) + break; + if (!c->buffer->message (c->font, "start chainsubtable %u", c->lookup_index)) goto skip; From 5ef8ef9281ba6923ed08bb7f44224ff0bec42d06 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:24:33 -0600 Subject: [PATCH 06/10] [kerx] Charge format-4 control-point actions' contour-point lookups to max_ops A kerx format-4 control-point action asks the font for two glyph contour points, which the font functions may satisfy by loading the whole glyph outline (two full FT_Load_Glyph calls with hb-ft). The state-machine driver charges a non-advancing transition only one op, so a looping entry turned the whole work budget into glyph loads: two letters and a 680-byte font stalled hb_shape() for seconds. Charge 2 * HB_BUDGET_256 to buffer->max_ops at the action, before the callbacks, using the same decrement-and-return idiom morx insertion uses; once the budget is spent the action is skipped and the driver's existing max_ops check forces the machine forward. The sibling GPOS AnchorFormat2 contour-point path has the same uncharged callback but lives in the layout subsystem and is left for a separate change. GDEF CaretValueFormat2 also calls it but only from hb_ot_layout_get_ligature_carets, bounded per API call. Regression test in test/api/test-shape.c uses a counting glyph_contour_point callback on a builder-made looping kerx font (131074 calls before, <= 256 after). Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-EE451WE1. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-kerx-table.hh | 4 ++ test/api/test-shape.c | 85 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/hb-aat-layout-kerx-table.hh b/src/hb-aat-layout-kerx-table.hh index f8e2987e7..7b22098f2 100644 --- a/src/hb-aat-layout-kerx-table.hh +++ b/src/hb-aat-layout-kerx-table.hh @@ -613,6 +613,10 @@ struct KerxSubTableFormat4 const HBUINT16 *data = &ankrData[entry.data.ankrActionIndex * 2]; if (!c->sanitizer.check_array (data, 2)) return; hb_barrier (); + /* Font functions may load the whole glyph outline to find a contour + * point, and a non-advancing transition can repeat that at will. + * Charge the two lookups to the buffer's work budget. */ + if (unlikely ((buffer->max_ops -= 2 * HB_BUDGET_256) <= 0)) return; unsigned int markControlPoint = *data++; unsigned int currControlPoint = *data++; hb_position_t markX = 0; diff --git a/test/api/test-shape.c b/test/api/test-shape.c index 37571e18e..a7e706dba 100644 --- a/test/api/test-shape.c +++ b/test/api/test-shape.c @@ -242,6 +242,88 @@ test_shape_overlong_cluster_remerge (void) } + +#ifndef HB_NO_AAT +static hb_bool_t +count_contour_point_calls (hb_font_t *font HB_UNUSED, + void *font_data, + hb_codepoint_t glyph HB_UNUSED, + unsigned int point_index HB_UNUSED, + hb_position_t *x, + hb_position_t *y, + void *user_data HB_UNUSED) +{ + (*(unsigned *) font_data)++; + *x = *y = 0; + return TRUE; +} + +static void +test_shape_kerx_control_point_actions_are_charged (void) +{ + /* A two-glyph font whose kerx format-4 subtable loops on the letter 'a' + * in state 1 without advancing, performing a control-point action on + * every transition. Each action asks the font for two contour points, + * which may load a whole glyph outline, so shaping must charge them to + * the buffer's work budget. */ + static const char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x02}; + static const char cmap[] = {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}; + static const unsigned char kerx[] = { + 0, 2, 0, 0, 0, 0, 0, 1, /* version 2, one subtable */ + 0, 0, 0, 82, 0, 0, 0, 4, 0, 0, 0, 0, /* length, format 4, tupleCount */ + 0, 0, 0, 5, 0, 0, 0, 20, 0, 0, 0, 28, 0, 0, 0, 48, /* nClasses, class, state, entry offsets */ + 0, 0, 0, 66, /* action type 0, control points at 66 */ + 0, 8, 0, 1, 0, 1, 0, 4, /* class lookup format 8: glyph 1 is class 4 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, /* state 0: class 4 takes entry 1 */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, /* state 1: class 4 takes entry 2 */ + 0, 0, 0x00, 0, 0xFF, 0xFF, /* entry 0: to state 0 */ + 0, 1, 0xC0, 0, 0xFF, 0xFF, /* entry 1: to state 1, Mark, DontAdvance */ + 0, 1, 0x40, 0, 0, 0, /* entry 2: to state 1, DontAdvance, action 0 */ + 0, 0, 0, 0 /* action 0: mark point 0, current point 0 */ + }; + static const char *shapers[] = {"ot", NULL}; + + hb_face_t *builder = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create (maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create (cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) kerx, sizeof (kerx), HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (builder, HB_TAG ('k','e','r','x'), 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); + + unsigned calls = 0; + hb_font_t *parent = hb_font_create (face); + hb_font_t *font = hb_font_create_sub_font (parent); + hb_font_funcs_t *funcs = hb_font_funcs_create (); + hb_font_funcs_set_glyph_contour_point_func (funcs, count_contour_point_calls, NULL, NULL); + hb_font_set_funcs (font, funcs, &calls, NULL); + hb_font_funcs_destroy (funcs); + + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape_full (font, buffer, NULL, 0, shapers); + + /* 131074 before the budget was charged. */ + g_assert_cmpuint (calls, >=, 2); + g_assert_cmpuint (calls, <=, 256); + + hb_buffer_destroy (buffer); + hb_font_destroy (font); + hb_font_destroy (parent); + hb_face_destroy (face); +} +#endif + static void test_shape_harfrust_budget (void) { @@ -315,6 +397,9 @@ main (int argc, char **argv) hb_test_add (test_shape_overlong_mark_cluster); hb_test_add (test_shape_overlong_cluster_remerge); hb_test_add (test_shape_harfrust_budget); +#ifndef HB_NO_AAT + hb_test_add (test_shape_kerx_control_point_actions_are_charged); +#endif /* TODO test fallback shaper */ /* TODO test shaper_full */ hb_test_add (test_shape_list); From 77be62a733b62804318d108a04cc383c5b94fde8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:28:30 -0600 Subject: [PATCH 07/10] [aat] Collect LookupFormat2 glyphs in one pass over the segments The per-subtable glyph sets built by the morx/kerx chain accelerator assume LookupFormat2 segments are sorted and disjoint, but the sanitizer does not check that, so a malformed table can repeat a segment spanning the whole glyph range tens of thousands of times, and every subtable pointing at it pays a full bit-set pass per segment -- 300 subtables sharing one 65000-segment lookup stalled shaping two letters for over 20 seconds. Make LookupFormat2::collect_glyphs{,_filtered} track the end of the last range added and, at the first segment that is out of order, fold every remaining matching segment into a single add_range. For well-formed fonts the result is identical; for malformed ones it is a superset, which is safe because the set is only an intersects() pre-filter. collect_glyphs() now shares the filtered implementation with an always-true filter. Formats 4/6/8/10 cannot amplify this way and are untouched. Adds a morx font with unsorted format-2 segments to the in-house aat-morx shape tests, checking the subtable still applies. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-A0RYWLIT. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-common.hh | 38 +++++++++++++++--- ...03b8963831c2381809884633f483b7361baa97.ttf | Bin 0 -> 340 bytes test/shape/data/in-house/tests/aat-morx.tests | 1 + 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 test/shape/data/in-house/fonts/3503b8963831c2381809884633f483b7361baa97.ttf diff --git a/src/hb-aat-layout-common.hh b/src/hb-aat-layout-common.hh index 23cbb0384..42a5a76fe 100644 --- a/src/hb-aat-layout-common.hh +++ b/src/hb-aat-layout-common.hh @@ -382,16 +382,44 @@ struct LookupFormat2 template void collect_glyphs (set_t &glyphs) const { - unsigned count = segments.get_length (); - for (unsigned int i = 0; i < count; i++) - segments[i].collect_glyphs (glyphs); + collect_glyphs_filtered (glyphs, [] (const T &) { return true; }); } template void collect_glyphs_filtered (set_t &glyphs, const filter_t &filter) const { + /* Segments are supposed to be sorted and disjoint, in which case adding + * them one by one touches each page of the set at most once. The + * sanitizer does not check that, and a malformed table can repeat a + * segment spanning the whole glyph range thousands of times, each + * costing a full pass over the set. So once a segment is out of order, + * add the rest as a single range; that is a superset of the glyphs, + * which is fine for a set that is only used as a pre-filter. */ unsigned count = segments.get_length (); - for (unsigned int i = 0; i < count; i++) - segments[i].collect_glyphs_filtered (glyphs, filter); + hb_codepoint_t next = 0; /* One past the last glyph added. */ + unsigned i; + for (i = 0; i < count; i++) + { + const auto &segment = segments[i]; + if (segment.first < next) break; /* Not sorted and disjoint. */ + if (segment.first == DELETED_GLYPH || segment.first > segment.last || + !filter (segment.value)) + continue; + glyphs.add_range (segment.first, segment.last); + next = segment.last + 1; + } + + hb_codepoint_t first = HB_CODEPOINT_INVALID, last = 0; + for (; i < count; i++) + { + const auto &segment = segments[i]; + if (segment.first == DELETED_GLYPH || segment.first > segment.last || + !filter (segment.value)) + continue; + first = hb_min (first, (hb_codepoint_t) segment.first); + last = hb_max (last, (hb_codepoint_t) segment.last); + } + if (first <= last) + glyphs.add_range (first, last); } bool sanitize (hb_sanitize_context_t *c) const diff --git a/test/shape/data/in-house/fonts/3503b8963831c2381809884633f483b7361baa97.ttf b/test/shape/data/in-house/fonts/3503b8963831c2381809884633f483b7361baa97.ttf new file mode 100644 index 0000000000000000000000000000000000000000..b14d64be7b3062a27c1a4ea9ece22e98964d51f4 GIT binary patch literal 340 zcmYjMF%H5o47@Z^rb_UH4vY*;h=GBTksqL_AY~u|A%;GsBTwQPeE`hi986V5m-E?| z#DM|e2}Ya3?U&&~UdD4csqV?^$b#w@c@fj2Q~f5-Q|R=(anwFt@7gyt^`0=W1W?1F zAv-S7ZO+_7&iPriILA27wjaO_V_}lmq0}uZE%Kag^5*YncXCtlIKyeD01h$<^bYY0 bc7%lFD?%KG9$zvF{zZNxOWv Date: Fri, 30 Oct 2026 16:32:10 -0600 Subject: [PATCH 08/10] [aat] Match morx language-tag features against ltag without interning the font's tags Chain::compile_flags() resolved every type-39 (language tag) feature record by interning the font's ltag string with hb_language_from_string(), whose process-wide language list is a linear, never-freed linked list meant for the handful of languages an application passes in. A font controls both the number of records and the number of distinct tags, so shape-plan creation became quadratic (about a minute for a 125 KB font) and the tags stayed in the list for the life of the process. Add ltag::matches_language(i, specific), which compares the tag bytes directly against the buffer language, applying the same canonicalization hb_language_from_string() does (at most 63 bytes, lowercase, '_' to '-', stop at the first other non-alphanumeric) and the same prefix rule as hb_language_matches(); each record now costs at most the length of the buffer's language and nothing is added to the global list. get_language() is kept for the name table, which genuinely needs an hb_language_t. Behaviour is unchanged for every real tag; only a degenerate tag with no valid leading byte, which used to become the empty/invalid language, now matches nothing. Adds a regression font to the in-house aat-morx tests whose type-39 features gate substitutions by tags 'sr' and 'Zh_Hant', exercising case folding, '_' canonicalization, subtag prefix matching and non-matches. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-KYZIZMO6. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-morx-table.hh | 2 +- src/hb-aat-ltag-table.hh | 26 ++++++++++++++++++ ...2a555639995ed746c7575416acb689ecb9549d.ttf | Bin 0 -> 924 bytes test/shape/data/in-house/tests/aat-morx.tests | 9 ++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index a85ad6d4d..5279d91d8 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -1164,7 +1164,7 @@ struct Chain #ifndef HB_NO_AAT else if (type == HB_AAT_LAYOUT_FEATURE_TYPE_LANGUAGE_TAG_TYPE && setting && /* TODO: Rudimentary language matching. */ - hb_language_matches (map->face->table.ltag->get_language (setting - 1), map->props.language)) + map->face->table.ltag->matches_language (setting - 1, map->props.language)) { flags &= feature.disableFlags; flags |= feature.enableFlags; diff --git a/src/hb-aat-ltag-table.hh b/src/hb-aat-ltag-table.hh index c974025d4..6dc69b0e8 100644 --- a/src/hb-aat-ltag-table.hh +++ b/src/hb-aat-ltag-table.hh @@ -71,6 +71,32 @@ struct ltag range.length); } + /* Equivalent to hb_language_matches (get_language (i), specific), but + * without interning the font's tag: hb_language_from_string() appends + * every new string to a process-wide, linearly-searched, never-freed list, + * so a hostile font with many tags and many feature records referencing + * them could make that quadratic. */ + bool matches_language (unsigned int i, hb_language_t specific) const + { + const char *s = hb_language_to_string (specific); + if (!s) return false; + + const FTStringRange &range = tagRanges[i]; + const HBUINT8 *tag = (this+range.tag).arrayZ; + /* Canonicalize the same way hb_language_from_string() does: at most 63 + * bytes, lowercase, '_' to '-', stop at the first other non-alphanumeric. */ + unsigned len = hb_min ((unsigned) range.length, 63u); + unsigned l = 0; + for (; l < len; l++) + { + unsigned char c = TOLOWER (tag[l]); + if (c == '_') c = '-'; + if (!ISALNUM (c) && c != '-') break; + if (c != (unsigned char) s[l]) return false; + } + return l && (s[l] == '\0' || s[l] == '-'); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); diff --git a/test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf b/test/shape/data/in-house/fonts/882a555639995ed746c7575416acb689ecb9549d.ttf new file mode 100644 index 0000000000000000000000000000000000000000..c3f0d5311b3c4d7ab8882b5300bcf3858630e097 GIT binary patch literal 924 zcmb_aO-oxr6g@NZ5@Q=o5Q#x3yhXuHMF=8YxQNOp8*$fz4YJtHsHgX6eSIRF@#5y*OKYUI-9~Vrz8a2>giG9Zu z&*>3anxB~LOU%&MW0$Ut*fr}F**ZxC-Qf(%DB^$%zYncc<00=y1b83pT->3bC~M1UpMwk_NXfjlJ)pS zRij$TL?$OU(cp^I5MOj1YDgn?F^7S?FN&ic&79oIBpxZ6aoocyE0i})MY+=mYs`>E r*uw(yF-{T}u#Fm8Xrt*+9q+hPOFoJFf6XjC$V9J(3lGoa4gYC(tto5R literal 0 HcmV?d00001 diff --git a/test/shape/data/in-house/tests/aat-morx.tests b/test/shape/data/in-house/tests/aat-morx.tests index 2673c9627..100c646e4 100644 --- a/test/shape/data/in-house/tests/aat-morx.tests +++ b/test/shape/data/in-house/tests/aat-morx.tests @@ -6,3 +6,12 @@ ../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062;[2|2] ../fonts/7fb6608532fe39dd993eb3fc74e84256bd2d547e.ttf;--font-funcs=ot --no-glyph-names --no-positions --no-clusters;U+0061,U+0062,U+0063;[2|2|1] ../fonts/3503b8963831c2381809884633f483b7361baa97.ttf;--font-funcs=ot;U+0062,U+0062,U+0062,U+0062;[gid3=0+500|gid3=1+500|gid3=2+500|gid3=3+500] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=sr;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=SR;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=sr-Latn-RS;U+0041;[gid2=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=srb;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=s;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=en;U+0041;[gid1=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=zh-hant;U+0041;[gid3=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=ZH_HANT;U+0041;[gid3=0+600] +../fonts/882a555639995ed746c7575416acb689ecb9549d.ttf;--language=zh;U+0041;[gid1=0+600] From 370a0da5a11d139c626978cc0737834d83078049 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:37:09 -0600 Subject: [PATCH 09/10] [aat] Merge equal-flag feature ranges per chain and budget AAT map compilation hb_aat_map_t stored one range_flags_t per morx/mort chain for every feature range, so memory and time were the product of a font- controlled count (chains) and a caller-controlled count (ranges), with no limit on either: a 304 KB font with 19000 chains and 5000 feature ranges peaked at 1.4 GiB. mortmorx::compile_flags() now extends a chain's previous range entry when the new range compiles to the same flags. Ranges arrive in order and contiguous and the consumers only look at flags per cluster, so this is output-equivalent; it collapses every chain that does not react to the features (and most do not) to a single entry. A chain that does react still gets one entry per range, so also add HB_BUDGET_AAT_MAP (in the existing hb_budget_spend() style) to hb_aat_map_builder_t, charged per chain, per chain feature record visited, and more heavily per stored range; hb_aat_map_builder_t:: compile() stops after the range that exhausts it and the existing fix-up extends the last compiled ranges to the end of the text (best-effort degradation, as other budgets do). Only inputs whose chains x ranges x features exceed ~16M, far beyond any real font/document, lose their trailing ranges. The budget member is mutable because Chain::compile_flags receives the builder as const. Adds two regression tests to test/api/test-aat-layout.c with a hand-built feat+morx face. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-U4GKZAGM. Co-Authored-By: Claude Opus 4.8 --- src/hb-aat-layout-morx-table.hh | 22 +++++- src/hb-aat-map.cc | 5 ++ src/hb-aat-map.hh | 1 + src/hb-limits.hh | 11 +++ test/api/test-aat-layout.c | 117 ++++++++++++++++++++++++++++++++ 5 files changed, 153 insertions(+), 3 deletions(-) diff --git a/src/hb-aat-layout-morx-table.hh b/src/hb-aat-layout-morx-table.hh index 5279d91d8..fdd2776a8 100644 --- a/src/hb-aat-layout-morx-table.hh +++ b/src/hb-aat-layout-morx-table.hh @@ -1140,6 +1140,9 @@ struct Chain hb_mask_t flags = defaultFlags; { unsigned int count = featureCount; + /* Charge the chain and its feature records; the map builder stops + * compiling further ranges once its budget is exhausted. */ + hb_budget_spend (map->budget, HB_BUDGET_1, 1 + count); for (unsigned i = 0; i < count; i++) { const Feature &feature = featureZ[i]; @@ -1429,9 +1432,22 @@ struct mortmorx return; for (unsigned int i = 0; i < count; i++) { - map->chain_flags[i].push (hb_aat_map_t::range_flags_t {chain->compile_flags (mapper), - mapper->range_first, - mapper->range_last}); + hb_mask_t flags = chain->compile_flags (mapper); + auto &ranges = map->chain_flags[i]; + /* Ranges are compiled in order and are contiguous. Extend the last + * one instead of starting a new one if this chain does not tell the + * two apart; the map would otherwise hold one entry per chain per + * range, a product the font and the caller's features do not bound. */ + if (ranges.length && ranges.tail ().flags == flags) + ranges.tail ().cluster_last = mapper->range_last; + else + { + /* Stored ranges cost memory; charge them more than visited ones. */ + hb_budget_spend (mapper->budget, HB_BUDGET_16); + ranges.push (hb_aat_map_t::range_flags_t {flags, + mapper->range_first, + mapper->range_last}); + } chain = &StructAfter> (*chain); } } diff --git a/src/hb-aat-map.cc b/src/hb-aat-map.cc index 0b85c901e..33e2b7cc1 100644 --- a/src/hb-aat-map.cc +++ b/src/hb-aat-map.cc @@ -156,6 +156,11 @@ hb_aat_map_builder_t::compile (hb_aat_map_t &m) hb_aat_layout_compile_map (this, &m); + /* Out of budget? Stop here; the adjustment below extends the ranges + * compiled so far to the end of the text. */ + if (unlikely (!hb_budget_spend (budget, HB_BUDGET_1, active_features.length))) + break; + last_index = event->index; } diff --git a/src/hb-aat-map.hh b/src/hb-aat-map.hh index cb22ffee4..33a94ca96 100644 --- a/src/hb-aat-map.hh +++ b/src/hb-aat-map.hh @@ -117,6 +117,7 @@ struct hb_aat_map_builder_t hb_sorted_vector_t current_features; unsigned range_first = HB_FEATURE_GLOBAL_START; unsigned range_last = HB_FEATURE_GLOBAL_END; + mutable int64_t budget = HB_BUDGET_AAT_MAP; }; diff --git a/src/hb-limits.hh b/src/hb-limits.hh index fd9c398d4..49fe45c29 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -268,6 +268,17 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_RASTER_MAX_DRAW_EDGES ((int64_t) 1 << 20) #endif +/* Compiling one AAT feature map: morx/mort chains and chain feature + * records visited for each range of the text with its own set of + * features, plus extra for each range a chain keeps rather than merges + * into the previous one, since those are what the map stores. The + * ranges come from the caller's features and the chains from the font, + * so neither input bounds their product. Once exhausted, the last + * range compiled extends to the end of the text. */ +#ifndef HB_BUDGET_AAT_MAP +#define HB_BUDGET_AAT_MAP ((int64_t) 1 << 24) +#endif + #ifndef HB_REPACKER_MAX_ITERATIONS #define HB_REPACKER_MAX_ITERATIONS 500 diff --git a/test/api/test-aat-layout.c b/test/api/test-aat-layout.c index 88e9a18ce..899c28ffe 100644 --- a/test/api/test-aat-layout.c +++ b/test/api/test-aat-layout.c @@ -247,6 +247,121 @@ test_aat_morx_subtable_limit (void) g_assert_cmpuint (0, ==, count_applied_subtables (HB_TAG ('m','o','r','x'), morx, length)); } + +/* A font mapping 'a' and 'b' to glyphs 1 and 2. Its first morx chain turns + * glyph 1 into glyph 2 where common ligatures are on; they are off by default. + * That chain is followed by empty_chains chains that have nothing in them. */ +static hb_face_t * +create_morx_face (unsigned int empty_chains) +{ + static const unsigned char cmap[] = { + 0x00,0x00, 0x00,0x01, 0x00,0x03, 0x00,0x0A, 0x00,0x00,0x00,0x0C, + 0x00,0x0C, 0x00,0x00, 0x00,0x00,0x00,0x1C, 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x01, + 0x00,0x00,0x00,0x61, 0x00,0x00,0x00,0x62, 0x00,0x00,0x00,0x01, + }; + static const unsigned char maxp[] = {0x00,0x00,0x50,0x00, 0x00,0x03}; + static const unsigned char feat[] = { + 0x00,0x01,0x00,0x00, 0x00,0x01, 0x00,0x00, 0x00,0x00,0x00,0x00, + 0x00,0x01, 0x00,0x02, 0x00,0x00,0x00,0x18, 0x00,0x00, 0x01,0x00, + 0x00,0x02, 0x01,0x00, 0x00,0x03, 0x01,0x00, + }; + static const unsigned char chain[] = { + 0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x3C, 0x00,0x00,0x00,0x02, 0x00,0x00,0x00,0x01, + 0x00,0x01, 0x00,0x02, 0x00,0x00,0x00,0x01, 0xFF,0xFF,0xFF,0xFF, + 0x00,0x01, 0x00,0x03, 0x00,0x00,0x00,0x00, 0xFF,0xFF,0xFF,0xFE, + 0x00,0x00,0x00,0x14, 0x20,0x00,0x00,0x04, 0x00,0x00,0x00,0x01, + 0x00,0x08, 0x00,0x01, 0x00,0x01, 0x00,0x02, + }; + unsigned int chain_count = 1 + empty_chains; + unsigned int morx_length = 8 + sizeof (chain) + 16 * empty_chains; + unsigned char *morx = (unsigned char *) calloc (1, morx_length); + hb_face_t *builder = hb_face_builder_create (); + hb_blob_t *blob; + + g_assert_nonnull (morx); + morx[1] = 2; + morx[4] = chain_count >> 24; + morx[5] = chain_count >> 16; + morx[6] = chain_count >> 8; + morx[7] = chain_count; + memcpy (morx + 8, chain, sizeof (chain)); + for (unsigned int i = 0; i < empty_chains; i++) + morx[8 + sizeof (chain) + 16 * i + 7] = 16; + + blob = hb_blob_create ((const char *) cmap, sizeof (cmap), HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('c','m','a','p'), blob)); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) maxp, sizeof (maxp), HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('m','a','x','p'), blob)); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) feat, sizeof (feat), HB_MEMORY_MODE_READONLY, NULL, NULL); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('f','e','a','t'), blob)); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) morx, morx_length, HB_MEMORY_MODE_READONLY, morx, free); + g_assert_true (hb_face_builder_add_table (builder, HB_TAG ('m','o','r','x'), blob)); + hb_blob_destroy (blob); + + return builder; +} + +/* Shapes a run of 'a' that has ligatures on in every character whose index + * is not a multiple of period, each character having its own feature range. */ +static hb_buffer_t * +shape_ranged_features (hb_face_t *morx_face, unsigned int length, unsigned int period) +{ + hb_font_t *font = hb_font_create (morx_face); + hb_buffer_t *buffer = hb_buffer_create (); + hb_feature_t *features = (hb_feature_t *) calloc (length, sizeof (hb_feature_t)); + + g_assert_nonnull (features); + for (unsigned int i = 0; i < length; i++) + { + hb_buffer_add (buffer, 'a', i); + features[i].tag = HB_TAG ('l','i','g','a'); + features[i].value = i % period != 0; + features[i].start = i; + features[i].end = i + 1; + } + hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); + hb_buffer_guess_segment_properties (buffer); + hb_shape (font, buffer, features, length); + g_assert_cmpuint (length, ==, hb_buffer_get_length (buffer)); + + free (features); + hb_font_destroy (font); + return buffer; +} + +static void +test_aat_ranged_features (void) +{ + hb_face_t *morx_face = create_morx_face (1); + hb_buffer_t *buffer = shape_ranged_features (morx_face, 100, 3); + hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); + + for (unsigned int i = 0; i < 100; i++) + g_assert_cmpuint (i % 3 ? 2 : 1, ==, info[i].codepoint); + + hb_buffer_destroy (buffer); + hb_face_destroy (morx_face); +} + +static void +test_aat_ranged_features_many_chains (void) +{ + /* The number of chains times the number of ranges is what must not + * be paid for, in time or memory. The first ranges still work. */ + hb_face_t *morx_face = create_morx_face (20000); + hb_buffer_t *buffer = shape_ranged_features (morx_face, 5000, 2); + hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL); + + for (unsigned int i = 0; i < 100; i++) + g_assert_cmpuint (i % 2 ? 2 : 1, ==, info[i].codepoint); + + hb_buffer_destroy (buffer); + hb_face_destroy (morx_face); +} + #endif int @@ -261,6 +376,8 @@ main (int argc, char **argv) hb_test_add (test_aat_has); hb_test_add (test_aat_kern_subtable_limit); hb_test_add (test_aat_morx_subtable_limit); + hb_test_add (test_aat_ranged_features); + hb_test_add (test_aat_ranged_features_many_chains); #endif face = hb_test_open_font_file ("fonts/aat-feat.ttf"); From bcdf9c02a1c2dce6ce7730887208d3ac59f22467 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 17:09:09 -0600 Subject: [PATCH 10/10] [test] Force the ot shaper in the AAT subtable-limit tests CI re-runs the api suite under HarfRust (HB_SHAPER_LIST=harfrust), which does not implement AAT, so test-aat-layout's new tests would shape with no kern/morx applied and their subtable-count and substitution assertions would fail. The tests are inherently about the OT/AAT shaper, so pin it via hb_shape_full (font, buffer, ..., {"ot", NULL}), as the AAT additions to test-shape.c already do. Co-Authored-By: Claude Opus 4.8 --- test/api/test-aat-layout.c | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/test/api/test-aat-layout.c b/test/api/test-aat-layout.c index 899c28ffe..26e4755b6 100644 --- a/test/api/test-aat-layout.c +++ b/test/api/test-aat-layout.c @@ -158,12 +158,15 @@ count_applied_subtables (hb_tag_t tag, char *table, unsigned length) hb_font_t *font = hb_font_create (limit_face); hb_face_destroy (limit_face); + /* This exercises the OT/AAT shaper; other shapers (e.g. HarfRust) do not + * implement AAT, so pin it. */ + static const char *shaper_list[] = {"ot", NULL}; unsigned started = 0; hb_buffer_t *buffer = hb_buffer_create (); hb_buffer_add_utf8 (buffer, "aa", -1, 0, -1); hb_buffer_guess_segment_properties (buffer); hb_buffer_set_message_func (buffer, count_started_subtables, &started, NULL); - hb_shape (font, buffer, NULL, 0); + hb_shape_full (font, buffer, NULL, 0, shaper_list); hb_buffer_destroy (buffer); hb_font_destroy (font); @@ -324,7 +327,10 @@ shape_ranged_features (hb_face_t *morx_face, unsigned int length, unsigned int p } hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE); hb_buffer_guess_segment_properties (buffer); - hb_shape (font, buffer, features, length); + /* AAT morx is the OT/AAT shaper only; other shapers (e.g. HarfRust) do not + * implement it, so pin it. */ + static const char *shaper_list[] = {"ot", NULL}; + hb_shape_full (font, buffer, features, length, shaper_list); g_assert_cmpuint (length, ==, hb_buffer_get_length (buffer)); free (features);