mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-22 15:31:05 +01:00
[instancer] Budget tuple splitting when narrowing axes
Narrowing an axis makes change_tuple_variations_axis_limits() replace every tuple whose region uses that axis with up to six tuples (rebase_tent solutions), axis after axis with no merging, so one region spanning N narrowed axes becomes up to 6^N tuples, each with its own copy of the axis map and deltas. A 364-byte font with a region on 8 narrowed axes drove one hb_subset_or_fail() call to 9 s of CPU and 1.7 GB. The only existing limit, the subset output buffer cap, is checked after the whole instantiation has run. Charge the copies to a cumulative budget held in optimize_scratch_t, which every caller (gvar, cvar, CFF2, and item_variations_t for GDEF/HVAR/MVAR/BASE/COLR) already keeps for one whole table. The budget starts at HB_BUDGET_INSTANCER_MIN and earns HB_BUDGET_INSTANCER_FACTOR times the cost of each source tuple; each split spends out[0].get_cost() x (out.length - 1) through the existing hb_budget_spend() helper, and the function returns false when the budget is spent, which all callers already treat as failure. The cost (32 + 8 per axis + 1 per row/point) scales with what a copy allocates for both ItemVariationStore and gvar tuples, so the budget also bounds a store with many VarData and a gvar with many glyphs. An instancing request whose splits exceed the budget (roughly more than 64 copies per source tuple beyond a 4M-unit floor) now fails instead of producing a store that could not be serialized anyway. Normal fonts stay far below the floor, so the subset suite's partial-instance tests are byte-identical. Adds a unit test (test_tuple_split_budget) with a tent on 16 axes. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-X7UB9BVO. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
52152cbf97
commit
171a4fa59f
3 files changed
+48
-1
No files matched your search
@@ -285,6 +285,19 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
|
||||
#endif
|
||||
|
||||
|
||||
/* One instancing session: all the tuple variations of one table.
|
||||
* Limiting an axis splits every tuple that uses it into up to six, so
|
||||
* tuples multiply with each limited axis. The copies are charged to a
|
||||
* budget that starts at MIN and earns FACTOR times each source tuple,
|
||||
* in tuple_delta_t::get_cost () units of roughly eight bytes. */
|
||||
#ifndef HB_BUDGET_INSTANCER_FACTOR
|
||||
#define HB_BUDGET_INSTANCER_FACTOR 64
|
||||
#endif
|
||||
#ifndef HB_BUDGET_INSTANCER_MIN
|
||||
#define HB_BUDGET_INSTANCER_MIN ((int64_t) 1 << 22)
|
||||
#endif
|
||||
|
||||
|
||||
/* The instancer rebuilds an ItemVariationStore from a dense delta matrix,
|
||||
* one int per (row, region) pair. In the font a row only pays for the
|
||||
* regions of its own VarData, so a small font can ask for a huge matrix.
|
||||
|
||||
+16
-1
@@ -271,6 +271,9 @@ struct optimize_scratch_t
|
||||
hb_vector_t<unsigned char> point_data;
|
||||
hb_vector_t<unsigned char> deltas_data;
|
||||
hb_vector_t<int> rounded_deltas;
|
||||
|
||||
/* What splitting tuples may still cost; see HB_BUDGET_INSTANCER_*. */
|
||||
int64_t budget = HB_BUDGET_INSTANCER_MIN;
|
||||
};
|
||||
|
||||
struct tuple_delta_t
|
||||
@@ -333,6 +336,10 @@ struct tuple_delta_t
|
||||
compiled_interm_coords.duplicate_vector_from_pool (pool, o.compiled_interm_coords);
|
||||
}
|
||||
|
||||
/* Budget cost of a copy of this tuple. */
|
||||
unsigned get_cost () const
|
||||
{ return HB_BUDGET_32 + HB_BUDGET_8 * axis_tuples.get_population () + HB_BUDGET_1 * indices.length; }
|
||||
|
||||
void remove_axis (hb_tag_t axis_tag)
|
||||
{ axis_tuples.del (axis_tag); }
|
||||
|
||||
@@ -1153,8 +1160,13 @@ struct TupleVariationData
|
||||
private:
|
||||
bool change_tuple_variations_axis_limits (const hb_hashmap_t<hb_tag_t, Triple>& normalized_axes_location,
|
||||
const hb_hashmap_t<hb_tag_t, TripleDistances>& axes_triple_distances,
|
||||
int64_t &budget,
|
||||
hb_alloc_pool_t *pool = nullptr)
|
||||
{
|
||||
/* Splitting below may grow the tuples this much. */
|
||||
for (const tuple_delta_t& var : tuple_vars)
|
||||
budget += (int64_t) var.get_cost () * HB_BUDGET_INSTANCER_FACTOR;
|
||||
|
||||
/* sort axis_tag/axis_limits, make result deterministic */
|
||||
hb_vector_t<hb_tag_t> axis_tags;
|
||||
if (!axis_tags.alloc (normalized_axes_location.get_population ()))
|
||||
@@ -1184,6 +1196,9 @@ struct TupleVariationData
|
||||
if (unlikely (out.in_error ())) return false;
|
||||
if (!out) continue;
|
||||
|
||||
if (unlikely (!hb_budget_spend (budget, out.arrayZ[0].get_cost (), out.length - 1)))
|
||||
return false;
|
||||
|
||||
unsigned new_len = new_vars.length + out.length;
|
||||
|
||||
if (unlikely (!new_vars.alloc (new_len, false)))
|
||||
@@ -1343,7 +1358,7 @@ struct TupleVariationData
|
||||
bool optimize = false)
|
||||
{
|
||||
if (!tuple_vars) return true;
|
||||
if (!change_tuple_variations_axis_limits (normalized_axes_location, axes_triple_distances, pool))
|
||||
if (!change_tuple_variations_axis_limits (normalized_axes_location, axes_triple_distances, scratch.budget, pool))
|
||||
return false;
|
||||
/* compute inferred deltas only for gvar */
|
||||
if (contour_points)
|
||||
|
||||
@@ -598,6 +598,24 @@ test_delta_matrix_limits ()
|
||||
hb_always_assert (!item_vars.as_item_varstore ());
|
||||
}
|
||||
|
||||
static void
|
||||
test_tuple_split_budget ()
|
||||
{
|
||||
/* A tent inside the new range with the new default on its slope splits
|
||||
* into six, so the region below would become 6^16 tuples. */
|
||||
hb_hashmap_t<hb_tag_t, Triple> region, locations;
|
||||
hb_hashmap_t<hb_tag_t, TripleDistances> distances;
|
||||
for (unsigned i = 0; i < 16; i++)
|
||||
{
|
||||
region.set (HB_TAG ('A','A','A','A') + i, Triple (0.3, 0.5, 0.7));
|
||||
locations.set (HB_TAG ('A','A','A','A') + i, Triple (0.2, 0.45, 0.8));
|
||||
}
|
||||
OT::item_variations_t item_vars;
|
||||
unsigned outer = item_vars.add_vardata (1);
|
||||
item_vars.add_tuple (outer, std::move (region), 0, 100, 1);
|
||||
hb_always_assert (!item_vars.instantiate_tuple_vars (locations, distances));
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
@@ -616,4 +634,5 @@ main (int argc, char **argv)
|
||||
test_condition_budget ();
|
||||
test_repeated_region_index ();
|
||||
test_delta_matrix_limits ();
|
||||
test_tuple_split_budget ();
|
||||
}
|
||||
Reference in new issue
Block a user