mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-28 09:09:13 +01:00
[subset] Only visit non-default UVS mappings in the DMAP cmap closure
cmap_accelerator_t::closure_glyphs() in the DMAP path expanded, for every retained variation selector, all of the selector's Default UVS ranges and resolved each code point with get_variation_glyph(). Any number of 11-byte selector records may share one Default UVS table and a 4-byte range covers 256 code points, so the work was bounded by selectors x covered code points rather than by the table size: a 223 KB font with 20000 selectors on one 168-range table drove ~860M lookups and ~49 s of CPU in hb_subset_plan_create_or_fail(). A default mapping can only ever resolve to the nominal glyph, which the subset plan already retains with the unicode itself, so the closure only needs the code points that carry a Non-Default UVS mapping in cmap or DMAP; get_variation_glyph() still decides which table wins for each. This is exactly what the non-DMAP path (NonDefaultUVS::closure_glyphs) already does. A collect_variation_non_default_unicodes() accessor is added alongside the existing collect_variation_unicodes() chain (VariationSelectorRecord, CmapSubtableFormat14_15, CmapSubtable, cmap::accelerator_t) and used from the DMAP closure, so the work is one lookup per 5-byte mapping visited. No public API or ABI change; hb_face_collect_variation_unicodes() keeps expanding Default UVS ranges as before, and fonts without DMAP take the unchanged first branch. The reproducer's plan drops from ~49 s to 0.01 s with the same 257-glyph result. The separate, older per-record DefaultUVS::copy() serialization cost (~5.2 s for this font, independent of DMAP) is left in place. Adds test_subset_DMAP_shared_default_uvs to test/api/test-subset-cmap.c. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-8RP452C0. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
60bd82dcb4
commit
c3fb304549
2 files changed
+108
-1
No files matched your search
+28
-1
@@ -1330,6 +1330,9 @@ struct VariationSelectorRecord
|
||||
(base+nonDefaultUVS).collect_unicodes (out);
|
||||
}
|
||||
|
||||
void collect_non_default_unicodes (hb_set_t *out, const void *base) const
|
||||
{ (base+nonDefaultUVS).collect_unicodes (out); }
|
||||
|
||||
void collect_mapping (const void *base,
|
||||
hb_set_t *unicodes, /* OUT */
|
||||
hb_map_t *mapping /* OUT */) const
|
||||
@@ -1417,6 +1420,9 @@ struct CmapSubtableFormat14_15
|
||||
void collect_variation_unicodes (hb_codepoint_t variation_selector,
|
||||
hb_set_t *out) const
|
||||
{ record.bsearch (variation_selector).collect_unicodes (out, this); }
|
||||
void collect_variation_non_default_unicodes (hb_codepoint_t variation_selector,
|
||||
hb_set_t *out) const
|
||||
{ record.bsearch (variation_selector).collect_non_default_unicodes (out, this); }
|
||||
|
||||
void serialize (hb_serialize_context_t *c,
|
||||
const hb_set_t *unicodes,
|
||||
@@ -1598,6 +1604,18 @@ struct CmapSubtable
|
||||
}
|
||||
}
|
||||
|
||||
void collect_variation_non_default_unicodes (hb_codepoint_t variation_selector,
|
||||
hb_set_t *out) const
|
||||
{
|
||||
switch (u.format.v) {
|
||||
case 14: hb_barrier (); u.format14.collect_variation_non_default_unicodes (variation_selector, out); return;
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
case 15: hb_barrier (); u.format15.collect_variation_non_default_unicodes (variation_selector, out); return;
|
||||
#endif
|
||||
default: return;
|
||||
}
|
||||
}
|
||||
|
||||
void closure_glyphs (const hb_set_t *unicodes, hb_set_t *glyphset) const
|
||||
{
|
||||
switch (u.format.v) {
|
||||
@@ -2396,6 +2414,9 @@ struct cmap
|
||||
void collect_variation_unicodes (hb_codepoint_t variation_selector,
|
||||
hb_set_t *out) const
|
||||
{ subtable_uvs->collect_variation_unicodes (variation_selector, out); }
|
||||
void collect_variation_non_default_unicodes (hb_codepoint_t variation_selector,
|
||||
hb_set_t *out) const
|
||||
{ subtable_uvs->collect_variation_non_default_unicodes (variation_selector, out); }
|
||||
|
||||
protected:
|
||||
typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj,
|
||||
@@ -2674,8 +2695,14 @@ struct cmap_accelerator_t
|
||||
if (!unicodes->has (variation_selector))
|
||||
continue;
|
||||
|
||||
/* As in CmapSubtableFormat14_15::closure_glyphs(), only non-default
|
||||
* mappings can add glyphs: a default mapping resolves to the nominal
|
||||
* glyph, which the caller retains with the unicode itself. Expanding
|
||||
* the Default UVS ranges instead would cost work proportional to the
|
||||
* codepoints they cover, for every selector that shares them. */
|
||||
hb_set_t variation_unicodes;
|
||||
collect_variation_unicodes (variation_selector, &variation_unicodes);
|
||||
base.collect_variation_non_default_unicodes (variation_selector, &variation_unicodes);
|
||||
dmap.collect_variation_non_default_unicodes (variation_selector, &variation_unicodes);
|
||||
for (hb_codepoint_t unicode : variation_unicodes)
|
||||
{
|
||||
if (!unicodes->has (unicode))
|
||||
|
||||
@@ -461,6 +461,85 @@ test_subset_cmap_repeated_encoding_records (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
write_be (unsigned char *p, unsigned int value, unsigned int size)
|
||||
{
|
||||
while (size--)
|
||||
{
|
||||
p[size] = value & 0xFF;
|
||||
value >>= 8;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_DMAP_shared_default_uvs (void)
|
||||
{
|
||||
/* 20000 variation selectors sharing one Default UVS table whose ranges
|
||||
* cover U+0000..U+A7FF, plus an empty DMAP so the subsetter takes the
|
||||
* DMAP closure path. Only the first selector has a non-default mapping.
|
||||
* The glyph closure must not resolve every covered codepoint for every
|
||||
* selector; that used to take tens of seconds. */
|
||||
static const char maxp_data[] = {0, 0, 0x50, 0, 0, 3};
|
||||
static const char dmap_data[] = {0, 0, 0, 0};
|
||||
static const unsigned char cmap_start[] = {
|
||||
0, 0, 0, 2, /* cmap header */
|
||||
0, 0, 0, 5, 0, 0, 0, 48, /* UVS encoding record */
|
||||
0, 3, 0, 10, 0, 0, 0, 20, /* nominal encoding record */
|
||||
0, 13, 0, 0, 0, 0, 0, 28, /* format 13 header */
|
||||
0, 0, 0, 0, 0, 0, 0, 1,
|
||||
0, 0, 0, 0, 0, 0, 0xA8, 0, 0, 0, 0, 1, /* U+0000..U+A800 -> glyph 1 */
|
||||
};
|
||||
const unsigned int num_selectors = 20000;
|
||||
const unsigned int num_ranges = 0xA8;
|
||||
const unsigned int default_uvs = 10 + 11 * num_selectors;
|
||||
const unsigned int non_default_uvs = default_uvs + 4 + 4 * num_ranges;
|
||||
const unsigned int format14_length = non_default_uvs + 4 + 5;
|
||||
const unsigned int cmap_length = sizeof (cmap_start) + format14_length;
|
||||
unsigned char *cmap_data = g_malloc0 (cmap_length);
|
||||
unsigned char *format14 = cmap_data + sizeof (cmap_start);
|
||||
memcpy (cmap_data, cmap_start, sizeof (cmap_start));
|
||||
|
||||
write_be (format14 + 0, 14, 2); /* format 14 header */
|
||||
write_be (format14 + 2, format14_length, 4);
|
||||
write_be (format14 + 6, num_selectors, 4);
|
||||
for (unsigned int i = 0; i < num_selectors; i++)
|
||||
{
|
||||
unsigned char *record = format14 + 10 + 11 * i; /* variation selector */
|
||||
write_be (record + 0, 0xE0100 + i, 3);
|
||||
write_be (record + 3, default_uvs, 4);
|
||||
write_be (record + 7, i ? 0 : non_default_uvs, 4);
|
||||
}
|
||||
write_be (format14 + default_uvs, num_ranges, 4); /* default UVS */
|
||||
for (unsigned int i = 0; i < num_ranges; i++)
|
||||
{
|
||||
write_be (format14 + default_uvs + 4 + 4 * i, i << 8, 3);
|
||||
write_be (format14 + default_uvs + 4 + 4 * i + 3, 0xFF, 1);
|
||||
}
|
||||
write_be (format14 + non_default_uvs, 1, 4); /* non-default UVS */
|
||||
write_be (format14 + non_default_uvs + 4, 0xA800, 3);
|
||||
write_be (format14 + non_default_uvs + 7, 2, 2);
|
||||
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
|
||||
HB_FACE_ADD_TABLE (face, "DMAP", dmap_data);
|
||||
hb_blob_t *blob = hb_blob_create ((const char *) cmap_data, cmap_length,
|
||||
HB_MEMORY_MODE_WRITABLE, cmap_data, g_free);
|
||||
g_assert_true (hb_face_builder_add_table (face, HB_TAG ('c','m','a','p'), blob));
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_invert (hb_subset_input_unicode_set (input));
|
||||
hb_subset_plan_t *plan = hb_subset_plan_create_or_fail (face, input);
|
||||
g_assert_nonnull (plan);
|
||||
/* notdef, the nominal glyph and the one non-default variant. */
|
||||
g_assert_cmpuint (hb_map_get_population (hb_subset_plan_old_to_new_glyph_mapping (plan)), ==, 3);
|
||||
g_assert_true (hb_map_has (hb_subset_plan_old_to_new_glyph_mapping (plan), 2));
|
||||
hb_subset_plan_destroy (plan);
|
||||
hb_subset_input_destroy (input);
|
||||
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
static void
|
||||
test_subset_cmap_format15 (void)
|
||||
@@ -535,6 +614,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_subset_DMAP);
|
||||
hb_test_add (test_subset_cmap_shared_subtable_many_records);
|
||||
hb_test_add (test_subset_cmap_repeated_encoding_records);
|
||||
hb_test_add (test_subset_DMAP_shared_default_uvs);
|
||||
#ifndef HB_NO_BEYOND_64K
|
||||
hb_test_add (test_subset_cmap_format15);
|
||||
hb_test_add (test_subset_cmap15_default_uvs_range_limit);
|
||||
|
||||
Reference in new issue
Block a user