Files
harfbuzz/src/hb-subset.hh
T
Behdad EsfahbodandClaude Opus 4.8 fc302bdfe5 [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>
2026-10-31 22:00:12 -06:00

77 lines
2.6 KiB
C++

/*
* Copyright © 2018 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Garret Rieger, Roderick Sheeter
*/
#ifndef HB_SUBSET_HH
#define HB_SUBSET_HH
#include "hb.hh"
#include "hb-subset.h"
#include "hb-machinery.hh"
#include "hb-serialize.hh"
#include "hb-subset-input.hh"
#include "hb-subset-plan.hh"
struct hb_subset_context_t :
hb_dispatch_context_t<hb_subset_context_t, bool, HB_DEBUG_SUBSET>
{
const char *get_name () { return "SUBSET"; }
static return_t default_return_value () { return true; }
private:
template <typename T, typename ...Ts> auto
_dispatch (const T &obj, hb_priority<1>, Ts&&... ds) HB_AUTO_RETURN
( obj.subset (this, std::forward<Ts> (ds)...) )
template <typename T, typename ...Ts> auto
_dispatch (const T &obj, hb_priority<0>, Ts&&... ds) HB_AUTO_RETURN
( obj.dispatch (this, std::forward<Ts> (ds)...) )
public:
template <typename T, typename ...Ts> auto
dispatch (const T &obj, Ts&&... ds) HB_AUTO_RETURN
( _dispatch (obj, hb_prioritize, std::forward<Ts> (ds)...) )
hb_blob_t *source_blob;
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_,
hb_serialize_context_t *serializer_,
hb_tag_t table_tag_) :
source_blob (source_blob_),
plan (plan_),
serializer (serializer_),
table_tag (table_tag_) {}
};
#endif /* HB_SUBSET_HH */