Merge pull request #6514 from harfbuzz/scanner/cmap

[subset] Bound adversarial cmap work in format 4, per-record collection, and DMAP closure
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-11-09 13:52:51 -06:00
commit b457e5d2a1
4 files changed
+348 -18

No files matched your search

+90 -17
View File
@@ -615,10 +615,16 @@ struct CmapSubtableFormat4
unsigned int count = this->segCount;
if (count && this->startCount[count - 1] == 0xFFFFu)
count--; /* Skip sentinel segment. */
hb_codepoint_t next = 0;
for (unsigned int i = 0; i < count; i++)
{
hb_codepoint_t start = this->startCount[i];
/* Segments must be sorted and disjoint; never walk a codepoint twice,
* so that the total work is bounded regardless of segment count. */
hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next);
hb_codepoint_t end = this->endCount[i];
if (unlikely (start > end))
continue;
next = end + 1;
unsigned int rangeOffset = this->idRangeOffset[i];
out->add_range(start, end);
if (rangeOffset == 0)
@@ -656,10 +662,16 @@ struct CmapSubtableFormat4
unsigned count = this->segCount;
if (count && this->startCount[count - 1] == 0xFFFFu)
count--; /* Skip sentinel segment. */
hb_codepoint_t next = 0;
for (unsigned i = 0; i < count; i++)
{
hb_codepoint_t start = this->startCount[i];
/* Segments must be sorted and disjoint; never walk a codepoint twice,
* so that the total work is bounded regardless of segment count. */
hb_codepoint_t start = hb_max ((hb_codepoint_t) this->startCount[i], next);
hb_codepoint_t end = this->endCount[i];
if (unlikely (start > end))
continue;
next = end + 1;
unsigned rangeOffset = this->idRangeOffset[i];
if (rangeOffset == 0)
{
@@ -1318,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
@@ -1405,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,
@@ -1586,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) {
@@ -1801,11 +1831,14 @@ struct SubtableUnicodesCache {
hb_blob_ptr_t<cmap> base_blob;
const char* base;
unsigned int table_length;
unsigned int num_glyphs;
/* Keyed by subtable offset: several encoding records may share one subtable. */
hb_hashmap_t<unsigned, hb::unique_ptr<hb_set_t>> cached_unicodes;
public:
static SubtableUnicodesCache* create (hb_blob_ptr_t<cmap> source_table)
static SubtableUnicodesCache* create (hb_blob_ptr_t<cmap> source_table,
unsigned int num_glyphs)
{
SubtableUnicodesCache* cache =
(SubtableUnicodesCache*) hb_malloc (sizeof(SubtableUnicodesCache));
@@ -1813,7 +1846,7 @@ struct SubtableUnicodesCache {
if (unlikely (!cache))
return nullptr;
new (cache) SubtableUnicodesCache (source_table);
new (cache) SubtableUnicodesCache (source_table, num_glyphs);
return cache;
}
@@ -1826,17 +1859,21 @@ struct SubtableUnicodesCache {
}
SubtableUnicodesCache(const void* cmap_base,
unsigned int table_length_ = 0)
unsigned int table_length_ = 0,
unsigned int num_glyphs_ = UINT_MAX)
: base_blob(),
base ((const char*) cmap_base),
table_length (table_length_),
num_glyphs (num_glyphs_),
cached_unicodes ()
{}
SubtableUnicodesCache(hb_blob_ptr_t<cmap> base_blob_)
SubtableUnicodesCache(hb_blob_ptr_t<cmap> base_blob_,
unsigned int num_glyphs_ = UINT_MAX)
: base_blob(base_blob_),
base ((const char *) base_blob.get()),
table_length (base_blob.get_length ()),
num_glyphs (num_glyphs_),
cached_unicodes ()
{}
@@ -1853,15 +1890,15 @@ struct SubtableUnicodesCache {
const hb_set_t* set_for (const EncodingRecord* record,
SubtableUnicodesCache& mutable_cache) const
{
if (cached_unicodes.has ((unsigned) ((const char *) record - base)))
return cached_unicodes.get ((unsigned) ((const char *) record - base));
if (cached_unicodes.has ((unsigned) record->subtable))
return cached_unicodes.get ((unsigned) record->subtable);
return mutable_cache.set_for (record);
}
const hb_set_t* set_for (const EncodingRecord* record)
{
if (!cached_unicodes.has ((unsigned) ((const char *) record - base)))
if (!cached_unicodes.has ((unsigned) record->subtable))
{
hb_set_t *s = hb_set_create ();
if (unlikely (s->in_error ()))
@@ -1870,14 +1907,14 @@ struct SubtableUnicodesCache {
unsigned int subtable_data_size = record->subtable < table_length
? table_length - (unsigned int) record->subtable
: 0;
(base+record->subtable).collect_unicodes (s, UINT_MAX, subtable_data_size);
(base+record->subtable).collect_unicodes (s, num_glyphs, subtable_data_size);
if (unlikely (!cached_unicodes.set ((unsigned) ((const char *) record - base), hb::unique_ptr<hb_set_t> {s})))
if (unlikely (!cached_unicodes.set ((unsigned) record->subtable, hb::unique_ptr<hb_set_t> {s})))
return hb_set_get_empty ();
return s;
}
return cached_unicodes.get ((unsigned) ((const char *) record - base));
return cached_unicodes.get ((unsigned) record->subtable);
}
};
@@ -1902,7 +1939,8 @@ struct cmap
static constexpr hb_tag_t tableTag = HB_OT_TAG_cmap;
static SubtableUnicodesCache* create_filled_cache(hb_blob_ptr_t<cmap> source_table) {
static SubtableUnicodesCache* create_filled_cache(hb_blob_ptr_t<cmap> source_table,
unsigned int num_glyphs) {
const cmap* cmap = source_table.get();
auto it =
+ hb_iter (cmap->encodingRecord)
@@ -1911,13 +1949,15 @@ struct cmap
})
;
SubtableUnicodesCache* cache = SubtableUnicodesCache::create(source_table);
SubtableUnicodesCache* cache = SubtableUnicodesCache::create(source_table, num_glyphs);
if (unlikely (!cache))
return nullptr;
hb_set_t seen_encodings;
for (const EncodingRecord& _ : it)
cache->set_for(&_); // populate the cache for this encoding record.
if (cmap::is_first_record_for_encoding (cmap, _, seen_encodings))
cache->set_for(&_); // populate the cache for this encoding record.
return cache;
}
@@ -1941,7 +1981,8 @@ struct cmap
#endif
auto snap = c->snapshot ();
SubtableUnicodesCache local_unicodes_cache (base, source_table_length);
SubtableUnicodesCache local_unicodes_cache (base, source_table_length,
plan->source->get_num_glyphs ());
const SubtableUnicodesCache* unicodes_cache = &local_unicodes_cache;
if (plan->accelerator &&
@@ -1949,6 +1990,7 @@ struct cmap
plan->accelerator->cmap_cache->same_base (base))
unicodes_cache = plan->accelerator->cmap_cache;
hb_set_t seen_encodings;
for (const EncodingRecord& _ : encodingrec_iter)
{
if (c->in_error ())
@@ -1961,6 +2003,8 @@ struct cmap
#endif
) continue;
if (!is_first_record_for_encoding (base, _, seen_encodings)) continue;
const hb_set_t* unicodes_set = unicodes_cache->set_for (&_, local_unicodes_cache);
if (!drop_format_4 && format == 4)
@@ -2370,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,
@@ -2514,6 +2561,26 @@ struct cmap
(cmap + _.subtable).is_variation_selector ();
}
/* Only the first encoding record of each (platformID, encodingID) pair is
* used: lookup binary-searches the records and sees one per pair, and
* EncodingRecord::copy already links later records of a format to the first
* serialized subtable. Collapsing them here bounds the number of per-record
* unicode sets the subsetter builds (and the _can_drop walk) regardless of
* how many records a malformed font repeats. Variation-selector records are
* exempt, so a font with both a format 14 and a format 15 subtable keeps
* both. */
static bool is_first_record_for_encoding (const void *base,
const EncodingRecord& _,
hb_set_t& seen_encodings)
{
if ((base+_.subtable).is_variation_selector ()) return true;
hb_codepoint_t encoding = ((unsigned) _.platformID << 16) | _.encodingID;
if (seen_encodings.has (encoding)) return false;
seen_encodings.add (encoding);
return true;
}
protected:
HBUINT16 version; /* Table version number (0). */
SortedArray16Of<EncodingRecord>
@@ -2628,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))
+1 -1
View File
@@ -382,7 +382,7 @@ static void _attach_accelerator_data (hb_subset_plan_t* plan,
// Populate caches that need access to the final tables.
hb_blob_ptr_t<OT::cmap> cmap_ptr (hb_sanitize_context_t ().reference_table<OT::cmap> (face));
accel->cmap_cache = OT::cmap::create_filled_cache (cmap_ptr);
accel->cmap_cache = OT::cmap::create_filled_cache (cmap_ptr, face->get_num_glyphs ());
accel->destroy_cmap_cache = OT::SubtableUnicodesCache::destroy;
if (!hb_face_set_user_data(face,
+66
View File
@@ -107,6 +107,71 @@ test_collect_unicodes_format4_with_range_offsets_and_deltas (void)
hb_face_destroy (face);
}
static void
test_collect_unicodes_format4_overlapping_segments (void)
{
/* Segments overlap and one lies entirely inside an earlier one; each
* codepoint must be collected once, from the first segment covering it. */
const char cmap_data[60] = {
0, 0, 0, 1, /* cmap header */
0, 0, 0, 3, 0, 0, 0, 12, /* encoding records */
/* cmap 4 */
0, 4, /* format */
0, 48, /* length */
0, 0, /* lang */
0, 8, /* segcountx2 */
0, 0, /* search range */
0, 0, /* entry selector */
0, 0, /* range shift */
0, 0x45, /* end code [0] */
0, 0x47, /* end code [1] */
0, 0x47, /* end code [2] */
-1, -1, /* end code [3] */
0, 0, /* pad */
0, 0x41, /* start code [0] */
0, 0x43, /* start code [1] */
0, 0x40, /* start code [2] */
-1, -1, /* start code [3] */
0, 0, /* id delta [0] */
1, 0, /* id delta [1] */
2, 0, /* id delta [2] */
0, 1, /* id delta [3] */
0, 0, /* id range offset [0] */
0, 0, /* id range offset [1] */
0, 0, /* id range offset [2] */
0, 0, /* id range offset [3] */
};
hb_blob_t* cmap = hb_blob_create(cmap_data, 60, HB_MEMORY_MODE_READONLY, 0, 0);
hb_face_t* face = hb_face_builder_create();
hb_face_builder_add_table(face, HB_TAG('c', 'm', 'a', 'p'), cmap);
hb_blob_destroy(cmap);
hb_set_t *codepoints = hb_set_create();
hb_map_t *mapping = hb_map_create();
hb_codepoint_t cp;
hb_face_collect_unicodes (face, codepoints);
g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7);
g_assert_cmpuint (hb_set_get_min (codepoints), ==, 0x41);
g_assert_cmpuint (hb_set_get_max (codepoints), ==, 0x47);
hb_set_clear(codepoints);
hb_face_collect_nominal_glyph_mapping(face, mapping, codepoints);
g_assert_cmpuint (hb_set_get_population (codepoints), ==, 7);
g_assert_cmpuint (hb_map_get_population (mapping), ==, 7);
for (cp = 0x41; cp <= 0x45; cp++)
g_assert_cmpuint (hb_map_get(mapping, cp), ==, cp);
g_assert_cmpuint (hb_map_get(mapping, 0x46), ==, 0x146);
g_assert_cmpuint (hb_map_get(mapping, 0x47), ==, 0x147);
hb_map_destroy(mapping);
hb_set_destroy (codepoints);
hb_face_destroy (face);
}
static void
test_collect_unicodes_format12_notdef (void)
{
@@ -275,6 +340,7 @@ main (int argc, char **argv)
hb_test_add (test_collect_unicodes_format12_clamped);
hb_test_add (test_collect_unicodes_format13);
hb_test_add (test_collect_unicodes_format4_with_range_offsets_and_deltas);
hb_test_add (test_collect_unicodes_format4_overlapping_segments);
return hb_test_run();
}
+191
View File
@@ -352,6 +352,194 @@ test_subset_DMAP (void)
hb_face_destroy (face);
}
/* Many encoding records sharing one format 12 subtable whose group spans
* all of Unicode: the subsetter must collect the subtable once, and only
* up to the font's glyph count, instead of once per record. */
static void
test_subset_cmap_shared_subtable_many_records (void)
{
const unsigned num_records = 4000;
const char maxp_data[] = {
0, 0, 0x50, 0, 0, 3,
};
unsigned cmap_len = 4 + 8 * num_records + 16 + 12;
char *cmap_data = calloc (1, cmap_len);
g_assert_nonnull (cmap_data);
char *p = cmap_data;
*p++ = 0; *p++ = 0; /* version */
*p++ = num_records >> 8; *p++ = num_records & 0xFF; /* numTables */
unsigned subtable_offset = 4 + 8 * num_records;
for (unsigned i = 0; i < num_records; i++)
{
*p++ = 0; *p++ = 3; *p++ = 0; *p++ = 10; /* platform 3, encoding 10 */
*p++ = subtable_offset >> 24; *p++ = subtable_offset >> 16;
*p++ = subtable_offset >> 8; *p++ = subtable_offset;
}
const char subtable[] = {
0, 12, 0, 0, 0, 0, 0, 28, /* format 12 header */
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1, /* U+0041..U+10FFFF -> 1... */
};
memcpy (p, subtable, sizeof (subtable));
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
hb_blob_t *cmap_blob = hb_blob_create_or_fail (cmap_data, cmap_len,
HB_MEMORY_MODE_WRITABLE,
cmap_data, free);
hb_face_builder_add_table (face, HB_TAG ('c','m','a','p'), cmap_blob);
hb_blob_destroy (cmap_blob);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, 'A');
hb_face_t *subset = hb_subset_test_create_subset (face,
hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_font_t *font = hb_font_create (subset);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph));
g_assert_cmpuint (glyph, ==, 1);
g_assert_false (hb_font_get_nominal_glyph (font, 'B', &glyph));
hb_font_destroy (font);
hb_face_destroy (subset);
hb_face_destroy (face);
}
/* Several encoding records that repeat one (platformID, encodingID) pair,
* two sharing a subtable and one with its own copy: the subset must keep
* only the first record of the pair, so the output cmap has a single
* encoding record and still maps the retained code point. */
static void
test_subset_cmap_repeated_encoding_records (void)
{
const char maxp_data[] = {
0, 0, 0x50, 0, 0, 3,
};
const char cmap_data[] = {
0, 0, 0, 3, /* version, numTables = 3 */
0, 3, 0, 10, 0, 0, 0, 28, /* (3,10) -> subtable A */
0, 3, 0, 10, 0, 0, 0, 28, /* (3,10) -> subtable A */
0, 3, 0, 10, 0, 0, 0, 56, /* (3,10) -> subtable B */
/* subtable A at 28 */
0, 12, 0, 0, 0, 0, 0, 28,
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1,
/* subtable B at 56 */
0, 12, 0, 0, 0, 0, 0, 28,
0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 'A', 0, 0x10, 0xFF, 0xFF, 0, 0, 0, 1,
};
hb_face_t *face = hb_face_builder_create ();
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
HB_FACE_ADD_TABLE (face, "cmap", cmap_data);
hb_set_t *codepoints = hb_set_create ();
hb_set_add (codepoints, 'A');
hb_face_t *subset = hb_subset_test_create_subset (face,
hb_subset_test_create_input (codepoints));
hb_set_destroy (codepoints);
hb_blob_t *out = hb_face_reference_table (subset, HB_TAG ('c','m','a','p'));
unsigned out_len = 0;
const char *out_data = hb_blob_get_data (out, &out_len);
g_assert_cmpuint (out_len, >=, 4);
unsigned num_tables = ((unsigned char) out_data[2] << 8) | (unsigned char) out_data[3];
g_assert_cmpuint (num_tables, ==, 1);
hb_blob_destroy (out);
hb_font_t *font = hb_font_create (subset);
hb_codepoint_t glyph;
g_assert_true (hb_font_get_nominal_glyph (font, 'A', &glyph));
g_assert_cmpuint (glyph, ==, 1);
hb_font_destroy (font);
hb_face_destroy (subset);
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)
@@ -424,6 +612,9 @@ main (int argc, char **argv)
hb_test_add (test_subset_cmap_noto_color_emoji_noop);
hb_test_add (test_subset_cmap_noto_color_emoji_non_consecutive_glyphs);
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);