mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-25 00:41:09 +01:00
[subset] Budget the avar2 reachable-range computation
_compute_avar2_reachable_ranges() bounded only the breakpoint grid (MAX_GRID = 16384 vertices), while its real work is axes x vertices x active row columns x tents. A delta-set row may list the same region tens of thousands of times, and a clamped DeltaSetIndexMap lets every fvar axis share that one row, so a 50 KB font cost about two minutes of CPU per hb_subset_plan_create_or_fail(). Charge one shared allowance per plan, HB_BUDGET_SUBSET_AVAR2_RANGES, at the three places the work is created: scanning the row's columns into the active set, collecting each active region's tents, and the exact grid enumeration (charged up front as grid_size x (active + tents)). Degradation reuses states the surrounding code already handles: an axis whose exact enumeration does not fit takes the existing interval-arithmetic bounds (still conservative, so the instance stays correct), and once the allowance is gone the remaining axes are left unconstrained. The hostile fonts now finish in well under a second; real avar2 fonts use a small fraction of the budget and their output is unchanged. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-3VDGZ019. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
d1e8c1079d
commit
69680c7b4c
2 files changed
+29
No files matched your search
@@ -322,6 +322,16 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1)
|
||||
#endif
|
||||
|
||||
|
||||
/* One avar2 partial-instancing plan, in region tents visited to bound the
|
||||
* final coordinates each axis can reach. A delta-set row can list a region
|
||||
* any number of times and axes can share a row, so the table size does not
|
||||
* bound that work. An axis whose exact bounds do not fit what is left gets
|
||||
* the looser interval bounds; once exhausted, axes are left unconstrained. */
|
||||
#ifndef HB_BUDGET_SUBSET_AVAR2_RANGES
|
||||
#define HB_BUDGET_SUBSET_AVAR2_RANGES ((int64_t) 1 << 26)
|
||||
#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.
|
||||
|
||||
@@ -144,6 +144,7 @@
|
||||
|
||||
hb_hashmap_t<hb_tag_t, unsigned> grid_pos;
|
||||
hb_vector_t<hb_vector_t<double>> grid_points;
|
||||
int64_t budget = HB_BUDGET_SUBSET_AVAR2_RANGES;
|
||||
for (unsigned i = 0; i < axes.length; i++)
|
||||
{
|
||||
grid_pos.reset ();
|
||||
@@ -171,6 +172,9 @@
|
||||
const OT::HBUINT8 *delta_bytes = vd.get_delta_bytes ();
|
||||
unsigned row_size = vd.get_row_size ();
|
||||
unsigned region_count = vd.get_region_index_count ();
|
||||
/* Out of budget: the remaining axes are left unconstrained. */
|
||||
if (unlikely (!hb_budget_spend (budget, HB_BUDGET_1, region_count)))
|
||||
return true;
|
||||
for (unsigned r = 0; r < region_count; r++)
|
||||
{
|
||||
int delta = vd.get_item_delta_fast (inner, r, delta_bytes, row_size);
|
||||
@@ -217,8 +221,13 @@
|
||||
if (add_grid_axis (tag) < 0) return false;
|
||||
/* Per active region: (grid axis position, tent) for its valid tents. */
|
||||
hb_vector_t<hb_vector_t<hb_pair_t<unsigned, Triple>>> region_tents;
|
||||
unsigned num_tents = 0;
|
||||
for (const auto &ar : active)
|
||||
{
|
||||
/* Collecting a tent allocates, and adds breakpoints to sort. */
|
||||
if (unlikely (!hb_budget_spend (budget, HB_BUDGET_16,
|
||||
1 + regions[ar.second].get_population ())))
|
||||
return true;
|
||||
region_tents.push (hb_vector_t<hb_pair_t<unsigned, Triple>> ());
|
||||
if (region_tents.in_error ()) return false;
|
||||
auto &tents = region_tents[region_tents.length - 1];
|
||||
@@ -240,6 +249,7 @@
|
||||
tents.push (hb_pair ((unsigned) pos, tent));
|
||||
if (points.in_error () || tents.in_error ()) return false;
|
||||
}
|
||||
num_tents += tents.length;
|
||||
}
|
||||
|
||||
unsigned grid_size = 1;
|
||||
@@ -256,6 +266,15 @@
|
||||
grid_size *= points.length;
|
||||
}
|
||||
|
||||
/* Every vertex evaluates every tent of every active region. Fall back
|
||||
* to the interval bounds, without charging, when that does not fit. */
|
||||
if (exact)
|
||||
{
|
||||
int64_t cost = (int64_t) grid_size * (active.length + num_tents);
|
||||
if (cost <= budget) budget -= cost;
|
||||
else exact = false;
|
||||
}
|
||||
|
||||
if (exact)
|
||||
{
|
||||
unsigned target_pos = 0; /* the target axis was added first */
|
||||
|
||||
Reference in new issue
Block a user