mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-20 06:21:16 +01:00
[subset] Share one work budget across all glyphs when instancing glyf
hb-limits.hh states that a session which drives another subsystem carries one cumulative budget, initialized once per top-level entry and only ever decremented. When instancing, glyf::subset() drives Glyph::get_points() once per retained glyph to recompute bounds and metrics, but passed no budget, so each glyph restarted from a fresh HB_BUDGET_GLYPH and the table as a whole was bounded only by 2^24 times the glyph count. A 59 KB font of 2,000 nested composites over a shared 64-level chain made one hb_subset_or_fail() instancing call spend 34 s of CPU, since the shared chain was re-walked (and re-charged only against a fresh budget) for every top-level glyph. Add HB_BUDGET_SUBSET_GLYF and keep the live counter in hb_subset_context_t, which is constructed once and reused across the ran-out-of-room retries in _hb_subset_table_try(), so a retry does not reset it. Thread it through _populate_subset_glyphs() and both compile_bytes_with_deltas() into get_points(). Exhausting it fails get_points(), which the existing loop already turns into dropping glyf from the instance -- the same outcome as any other glyph-load failure there. This is one cumulative counter, not a per-glyph budget multiplied by the glyph count. It is sized at 2^28; measured consumption of the largest glyf variable font under test/ (MPLUS1-Variable) is ~1.2M units, about 220x below the cap, and even a font at the 65,535-glyph maximum stays well under, so real fonts instance unchanged (outputs byte-identical). One behaviour change: a single glyph is now bounded by what is left of the table budget rather than by HB_BUDGET_GLYPH, so one glyph may spend between 2^24 and 2^28 units; the session total is still bounded. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-5BY15VDS. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
be8103f334
commit
fc302bdfe5
6 files changed
+150
-8
No files matched your search
@@ -240,7 +240,8 @@ struct Glyph
|
||||
hb_font_t *font,
|
||||
const glyf_accelerator_t &glyf,
|
||||
hb_bytes_t &dest_start, /* IN/OUT */
|
||||
hb_bytes_t &dest_end /* OUT */)
|
||||
hb_bytes_t &dest_end, /* OUT */
|
||||
int64_t *budget /* IN/OUT */)
|
||||
{
|
||||
contour_point_vector_t all_points, points_with_deltas;
|
||||
unsigned composite_contours = 0;
|
||||
@@ -257,7 +258,8 @@ struct Glyph
|
||||
}
|
||||
|
||||
hb_glyf_scratch_t scratch;
|
||||
if (!get_points (font, glyf, all_points, scratch, &points_with_deltas, head_maxp_info_p, composite_contours_p, false, false))
|
||||
if (!get_points (font, glyf, all_points, scratch, &points_with_deltas, head_maxp_info_p, composite_contours_p, false, false,
|
||||
false, hb_array_t<const int> (), nullptr, 0, nullptr, budget))
|
||||
return false;
|
||||
|
||||
// .notdef, set type to empty so we only update metrics and don't compile bytes for
|
||||
|
||||
@@ -114,9 +114,10 @@ struct SubsetGlyph
|
||||
|
||||
bool compile_bytes_with_deltas (const hb_subset_plan_t *plan,
|
||||
hb_font_t *font,
|
||||
const glyf_accelerator_t &glyf)
|
||||
const glyf_accelerator_t &glyf,
|
||||
int64_t *budget /* IN/OUT */)
|
||||
{
|
||||
allocated = source_glyph.compile_bytes_with_deltas (plan, font, glyf, dest_start, dest_end);
|
||||
allocated = source_glyph.compile_bytes_with_deltas (plan, font, glyf, dest_start, dest_end, budget);
|
||||
return allocated;
|
||||
}
|
||||
|
||||
|
||||
+6
-4
@@ -125,7 +125,7 @@ struct glyf
|
||||
hb_vector_t<glyf_impl::SubsetGlyph> glyphs;
|
||||
/* SubsetGlyphs borrow bytes from this blob until they are serialized. */
|
||||
hb_blob_t *source_glyf = nullptr;
|
||||
if (!_populate_subset_glyphs (c->plan, font, glyphs, &source_glyf))
|
||||
if (!_populate_subset_glyphs (c->plan, font, glyphs, &source_glyf, &c->budget_remaining))
|
||||
{
|
||||
hb_blob_destroy (source_glyf);
|
||||
hb_font_destroy (font);
|
||||
@@ -173,7 +173,8 @@ struct glyf
|
||||
_populate_subset_glyphs (const hb_subset_plan_t *plan,
|
||||
hb_font_t *font,
|
||||
hb_vector_t<glyf_impl::SubsetGlyph>& glyphs /* OUT */,
|
||||
hb_blob_t **source_glyf /* OUT */) const;
|
||||
hb_blob_t **source_glyf /* OUT */,
|
||||
int64_t *budget /* IN/OUT */) const;
|
||||
|
||||
hb_font_t *
|
||||
_create_font_for_instancing (const hb_subset_plan_t *plan) const;
|
||||
@@ -667,7 +668,8 @@ inline bool
|
||||
glyf::_populate_subset_glyphs (const hb_subset_plan_t *plan,
|
||||
hb_font_t *font,
|
||||
hb_vector_t<glyf_impl::SubsetGlyph>& glyphs /* OUT */,
|
||||
hb_blob_t **source_glyf /* OUT */) const
|
||||
hb_blob_t **source_glyf /* OUT */,
|
||||
int64_t *budget /* IN/OUT */) const
|
||||
{
|
||||
OT::glyf_accelerator_t glyf (plan->source);
|
||||
if (!glyphs.alloc_exact (plan->new_to_old_gid_list.length)) return false;
|
||||
@@ -699,7 +701,7 @@ glyf::_populate_subset_glyphs (const hb_subset_plan_t *plan,
|
||||
|
||||
if (font)
|
||||
{
|
||||
if (unlikely (!subset_glyph.compile_bytes_with_deltas (plan, font, glyf)))
|
||||
if (unlikely (!subset_glyph.compile_bytes_with_deltas (plan, font, glyf, budget)))
|
||||
{
|
||||
_free_compiled_subset_glyphs (glyphs);
|
||||
return false;
|
||||
|
||||
@@ -231,6 +231,19 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
|
||||
return budget >= 0;
|
||||
}
|
||||
|
||||
/* One glyf instancing session in the subsetter. It loads the outline of
|
||||
* every retained glyph, and composite components are loaded again for each
|
||||
* glyph that uses them, so the per-glyph budget (HB_BUDGET_GLYPH) would
|
||||
* multiply by the glyph count. This is one cumulative budget for the whole
|
||||
* table instead; exhausting it fails the table. Sized well above what real
|
||||
* variable fonts spend -- the largest glyf variable font under test/
|
||||
* (MPLUS1-Variable, 6,434 glyphs retained) uses ~1.2M units, and even a
|
||||
* font at the 65,535-glyph maximum stays far below -- while keeping the
|
||||
* adversarial worst case bounded. */
|
||||
#ifndef HB_BUDGET_SUBSET_GLYF
|
||||
#define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28)
|
||||
#endif
|
||||
|
||||
/* The flat pixel-work floor for one raster draw or paint session. Pixel and
|
||||
* outline work use separate live counters, so keep this independent of
|
||||
* the common outline budget above. */
|
||||
|
||||
@@ -59,6 +59,9 @@ struct hb_subset_context_t :
|
||||
hb_subset_plan_t *plan;
|
||||
hb_serialize_context_t *serializer;
|
||||
hb_tag_t table_tag;
|
||||
/* Outline work left for instancing this table. Lives here rather than in
|
||||
* the table's subset() so retries with a larger buffer share it. */
|
||||
int64_t budget_remaining = HB_BUDGET_SUBSET_GLYF;
|
||||
|
||||
hb_subset_context_t (hb_blob_t *source_blob_,
|
||||
hb_subset_plan_t *plan_,
|
||||
|
||||
@@ -668,6 +668,126 @@ test_subset_glyf_aliased_loca (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static unsigned
|
||||
add_composite_glyph (uint8_t *p, unsigned count, unsigned component)
|
||||
{
|
||||
memset (p, 0, 10);
|
||||
p[0] = p[1] = 0xFF; /* numberOfContours -1 */
|
||||
for (unsigned i = 0; i < count; i++)
|
||||
{
|
||||
uint8_t *r = p + 10 + i * 6;
|
||||
r[0] = 0;
|
||||
r[1] = 0x02 | (i + 1 < count ? 0x20 : 0); /* ARGS_ARE_XY_VALUES [| MORE_COMPONENTS] */
|
||||
r[2] = component >> 8;
|
||||
r[3] = component & 0xFF;
|
||||
r[4] = r[5] = 1;
|
||||
}
|
||||
return 10 + count * 6;
|
||||
}
|
||||
|
||||
static void
|
||||
test_subset_glyf_instancing_budget (void)
|
||||
{
|
||||
/* gid 1 is a simple glyph of 65536 points (or 4 for the control), gid 2
|
||||
* uses it three times, gid 3..64 each wrap the previous glyph, and 100
|
||||
* top-level glyphs wrap gid 64. No glyph exceeds a per-glyph limit, but
|
||||
* instancing loads the shared chain again for every top-level glyph, so
|
||||
* the table as a whole must be bounded by one work budget. */
|
||||
static const char maxp_data[] =
|
||||
"\x00\x00\x50\x00" /* version */
|
||||
"\x00\xA5" /* numGlyphs */
|
||||
;
|
||||
static const char head_data[54] =
|
||||
"\x00\x01\x00\x00" /* version */
|
||||
"\x00\x00\x00\x00" /* fontRevision */
|
||||
"\x00\x00\x00\x00" /* checkSumAdjustment */
|
||||
"\x5F\x0F\x3C\xF5" /* magicNumber */
|
||||
"\x00\x00" /* flags */
|
||||
"\x03\xE8" /* unitsPerEm */
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00" /* created */
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00" /* modified */
|
||||
"\x00\x00\x00\x00\x00\x00\x00\x00" /* bounds */
|
||||
"\x00\x00" /* macStyle */
|
||||
"\x00\x00" /* lowestRecPPEM */
|
||||
"\x00\x00" /* fontDirectionHint */
|
||||
"\x00\x01" /* indexToLocFormat */
|
||||
"\x00\x00" /* glyphDataFormat */
|
||||
;
|
||||
static const char fvar_data[] =
|
||||
"\x00\x01\x00\x00" /* version */
|
||||
"\x00\x10" /* axesArrayOffset */
|
||||
"\x00\x02" /* reserved */
|
||||
"\x00\x01" /* axisCount */
|
||||
"\x00\x14" /* axisSize */
|
||||
"\x00\x00" /* instanceCount */
|
||||
"\x00\x08" /* instanceSize */
|
||||
"wght" "\x00\x64\x00\x00" "\x01\x90\x00\x00" "\x03\x84\x00\x00" "\x00\x00" "\x01\x00"
|
||||
;
|
||||
const unsigned num_glyphs = 165;
|
||||
|
||||
for (unsigned heavy = 0; heavy < 2; heavy++)
|
||||
{
|
||||
unsigned flag_pairs = heavy ? 256 : 1;
|
||||
uint8_t *glyf_data = calloc (14 + 2 * flag_pairs + 28 + 62 * 16 + 100 * 16, 1);
|
||||
uint8_t *loca_data = calloc (4 * (num_glyphs + 1), 1);
|
||||
unsigned offset = 0;
|
||||
for (unsigned gid = 0; gid < num_glyphs; gid++)
|
||||
{
|
||||
uint8_t *p = glyf_data + offset;
|
||||
loca_data[gid * 4 + 2] = offset >> 8;
|
||||
loca_data[gid * 4 + 3] = offset & 0xFF;
|
||||
if (gid == 1)
|
||||
{
|
||||
p[1] = 1; /* numberOfContours */
|
||||
p[10] = (flag_pairs * 256 - 1) >> 8; /* endPtsOfContours[0] */
|
||||
p[11] = heavy ? 0xFF : 0x03;
|
||||
for (unsigned i = 0; i < flag_pairs; i++)
|
||||
{
|
||||
p[14 + i * 2] = 0x39; /* ON_CURVE | X_SAME | Y_SAME | REPEAT */
|
||||
p[15 + i * 2] = heavy ? 0xFF : 0x03;
|
||||
}
|
||||
offset += 14 + 2 * flag_pairs;
|
||||
}
|
||||
else if (gid == 2)
|
||||
offset += add_composite_glyph (p, 3, 1);
|
||||
else if (gid >= 3)
|
||||
offset += add_composite_glyph (p, 1, gid <= 64 ? gid - 1 : 64);
|
||||
}
|
||||
loca_data[num_glyphs * 4 + 1] = offset >> 16;
|
||||
loca_data[num_glyphs * 4 + 2] = offset >> 8;
|
||||
loca_data[num_glyphs * 4 + 3] = offset & 0xFF;
|
||||
|
||||
hb_face_t *face = hb_face_builder_create ();
|
||||
HB_FACE_ADD_TABLE (face, "maxp", maxp_data);
|
||||
HB_FACE_ADD_TABLE (face, "head", head_data);
|
||||
HB_FACE_ADD_TABLE (face, "fvar", fvar_data);
|
||||
hb_blob_t *blob = hb_blob_create_or_fail ((const char *) glyf_data, offset,
|
||||
HB_MEMORY_MODE_WRITABLE, glyf_data, free);
|
||||
hb_face_builder_add_table (face, HB_TAG ('g','l','y','f'), blob);
|
||||
hb_blob_destroy (blob);
|
||||
blob = hb_blob_create_or_fail ((const char *) loca_data, 4 * (num_glyphs + 1),
|
||||
HB_MEMORY_MODE_WRITABLE, loca_data, free);
|
||||
hb_face_builder_add_table (face, HB_TAG ('l','o','c','a'), blob);
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
hb_subset_input_t *input = hb_subset_input_create_or_fail ();
|
||||
hb_set_add_range (hb_subset_input_glyph_set (input), 0, num_glyphs - 1);
|
||||
g_assert_true (hb_subset_input_pin_axis_location (input, face, HB_TAG ('w','g','h','t'), 500.f));
|
||||
hb_face_t *subset = hb_subset_test_create_subset (face, input);
|
||||
|
||||
/* Exhausting the budget drops glyf from the instance. */
|
||||
blob = hb_face_reference_table (subset, HB_TAG ('g','l','y','f'));
|
||||
if (heavy)
|
||||
g_assert_cmpuint (hb_blob_get_length (blob), ==, 0);
|
||||
else
|
||||
g_assert_cmpuint (hb_blob_get_length (blob), >, 0);
|
||||
hb_blob_destroy (blob);
|
||||
|
||||
hb_face_destroy (subset);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -692,6 +812,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_subset_glyf_zero_contour_instructions);
|
||||
hb_test_add (test_subset_glyf_recompute_default_instance_bounds);
|
||||
hb_test_add (test_subset_glyf_aliased_loca);
|
||||
hb_test_add (test_subset_glyf_instancing_budget);
|
||||
|
||||
#ifdef HB_EXPERIMENTAL_API
|
||||
hb_test_add (test_subset_glyf_iftb_requirements);
|
||||
|
||||
Reference in new issue
Block a user