From 69680c7b4cd31f7cf097556f0107d0920b4dee34 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 30 Oct 2026 16:14:50 -0600 Subject: [PATCH] [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 --- src/hb-limits.hh | 10 ++++++++++ src/hb-subset-plan-var.cc | 19 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/hb-limits.hh b/src/hb-limits.hh index d39b9cb3e..71523accf 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -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. diff --git a/src/hb-subset-plan-var.cc b/src/hb-subset-plan-var.cc index 4b13b97ff..b05a97ab3 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -144,6 +144,7 @@ hb_hashmap_t grid_pos; hb_vector_t> 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>> 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> ()); 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 */