mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-21 23:11:04 +01:00
[instancer] Merge repeated VarData region indices into one tuple
create_from_item_var_data() deep-copied a region's axis hash map once per entry of VarData.regionIndexes, so memory grew with regionIndexCount x axisCount while the font pays only their sum, and region indices may repeat freely. A 100 KB font (2000 axes, one region, 16000 copies of region index 0) made one hb-subset call allocate about 2 GB. A region a VarData names again now adds its delta column to the tuple it already owns, exactly what merge_tuple_variations() computes later, so the instanced output is byte-identical. To also bound the sibling case of many dataSets offsets each pointing at one VarData, the region axes copied while converting one ItemVariationStore are summed across its VarData subtables and capped at a new HB_VARSTORE_MAX_TUPLE_AXES (1<<20); stores in the test fonts stay far below the cap. Exceeding the cap fails instancing of that store, the same path a failed allocation already takes. Adds a unit test (test_repeated_region_index) covering the merge and the cap. Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-1CGIYCMY. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
488e98dd8c
commit
3feb273c01
3 files changed
+84
-3
No files matched your search
@@ -107,6 +107,14 @@
|
||||
#define HB_VAR_COMPOSITE_MAX_AXES 4096
|
||||
#endif
|
||||
|
||||
/* Region axes copied into tuples when one ItemVariationStore is converted
|
||||
* for instancing, summed over its VarData subtables. Every region a
|
||||
* VarData references gets its own copy of that region's axes, and the
|
||||
* font pays a few bytes per reference however many axes the region has. */
|
||||
#ifndef HB_VARSTORE_MAX_TUPLE_AXES
|
||||
#define HB_VARSTORE_MAX_TUPLE_AXES (1u << 20)
|
||||
#endif
|
||||
|
||||
#ifndef HB_GLYF_MAX_POINTS
|
||||
#define HB_GLYF_MAX_POINTS 200000
|
||||
#endif
|
||||
|
||||
+28
-3
@@ -1088,6 +1088,7 @@ struct TupleVariationData
|
||||
const hb_vector_t<hb_hashmap_t<hb_tag_t, Triple>>& regions,
|
||||
const hb_map_t& axes_old_index_tag_map,
|
||||
unsigned& item_count,
|
||||
unsigned& num_tuple_axes,
|
||||
const hb_inc_bimap_t* inner_map = nullptr)
|
||||
{
|
||||
/* NULL offset, to keep original varidx valid, just return */
|
||||
@@ -1102,8 +1103,31 @@ struct TupleVariationData
|
||||
unsigned row_size = var_data.get_row_size ();
|
||||
const HBUINT8 *delta_bytes = var_data.get_delta_bytes ();
|
||||
|
||||
/* region index -> index of its tuple in tuple_vars */
|
||||
hb_map_t region_tuple_map;
|
||||
for (unsigned r = 0; r < num_regions; r++)
|
||||
{
|
||||
unsigned region_index = var_data.get_region_index (r);
|
||||
if (region_index >= regions.length) return false;
|
||||
|
||||
/* A region the VarData references again adds its deltas to the
|
||||
* tuple it already has, as merge_tuple_variations () would do,
|
||||
* instead of getting another copy of the region's axes. */
|
||||
unsigned *tuple_index;
|
||||
if (region_tuple_map.has (region_index, &tuple_index))
|
||||
{
|
||||
tuple_delta_t& tuple = tuple_vars.arrayZ[*tuple_index];
|
||||
for (unsigned i = 0; i < item_count; i++)
|
||||
tuple.deltas_x.arrayZ[i] += var_data.get_item_delta_fast (inner_map ? inner_map->backward (i) : i,
|
||||
r, delta_bytes, row_size);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Each tuple copies its region's axes; charge them before copying. */
|
||||
const hb_hashmap_t<hb_tag_t, Triple>& region = regions.arrayZ[region_index];
|
||||
num_tuple_axes += region.get_population ();
|
||||
if (unlikely (num_tuple_axes > HB_VARSTORE_MAX_TUPLE_AXES)) return false;
|
||||
|
||||
/* In VarData, deltas are organized in rows, convert them into
|
||||
* column(region) based tuples, resize deltas_x first */
|
||||
tuple_delta_t tuple;
|
||||
@@ -1118,10 +1142,9 @@ struct TupleVariationData
|
||||
r, delta_bytes, row_size);
|
||||
}
|
||||
|
||||
unsigned region_index = var_data.get_region_index (r);
|
||||
if (region_index >= regions.length) return false;
|
||||
tuple.axis_tuples = regions.arrayZ[region_index];
|
||||
tuple.axis_tuples = region;
|
||||
|
||||
if (unlikely (!region_tuple_map.set (region_index, tuple_vars.length))) return false;
|
||||
tuple_vars.push (std::move (tuple));
|
||||
}
|
||||
return !tuple_vars.in_error ();
|
||||
@@ -2005,6 +2028,7 @@ struct item_variations_t
|
||||
if (!vars.alloc (num_var_data) ||
|
||||
!var_data_num_rows.alloc (num_var_data)) return false;
|
||||
|
||||
unsigned num_tuple_axes = 0;
|
||||
for (unsigned i = 0; i < num_var_data; i++)
|
||||
{
|
||||
if (inner_maps && !inner_maps.arrayZ[i].get_population ())
|
||||
@@ -2015,6 +2039,7 @@ struct item_variations_t
|
||||
orig_region_list,
|
||||
axes_old_index_tag_map,
|
||||
item_count,
|
||||
num_tuple_axes,
|
||||
inner_maps ? &(inner_maps.arrayZ[i]) : nullptr))
|
||||
return false;
|
||||
|
||||
|
||||
@@ -457,6 +457,53 @@ test_condition_budget ()
|
||||
hb_always_assert (budget < 0);
|
||||
}
|
||||
|
||||
static void
|
||||
test_repeated_region_index ()
|
||||
{
|
||||
/* One axis, two regions, and a VarData whose region indices are {0, 1, 0}:
|
||||
* the repeated region's two delta columns must be summed into one tuple. */
|
||||
const char store_data[] =
|
||||
"\x00\x01\x00\x00\x00\x0c\x00\x01\x00\x00\x00\x1c" // ItemVariationStore
|
||||
"\x00\x01\x00\x02" // VarRegionList: 1 axis, 2 regions
|
||||
"\x00\x00\x40\x00\x40\x00" // region 0: (0, 1, 1)
|
||||
"\xc0\x00\xc0\x00\x00\x00" // region 1: (-1, -1, 0)
|
||||
"\x00\x02\x00\x00\x00\x03\x00\x00\x00\x01\x00\x00" // VarData: 2 items, regions {0, 1, 0}
|
||||
"\x01\x02\x03" // item 0
|
||||
"\x0a\x14\x1e"; // item 1
|
||||
const auto &store = *reinterpret_cast<const OT::ItemVariationStore *> (store_data);
|
||||
hb_map_t axis_map;
|
||||
axis_map.set (0, HB_TAG ('T','E','S','T'));
|
||||
|
||||
hb_vector_t<hb_hashmap_t<hb_tag_t, Triple>> regions;
|
||||
hb_always_assert (store.get_region_list ().get_var_regions (axis_map, regions));
|
||||
OT::tuple_variations_t tuples;
|
||||
unsigned item_count = 0, num_tuple_axes = 0;
|
||||
hb_always_assert (tuples.create_from_item_var_data (store.get_sub_table (0), regions, axis_map,
|
||||
item_count, num_tuple_axes));
|
||||
hb_always_assert (item_count == 2 && num_tuple_axes == 2);
|
||||
hb_always_assert (tuples.tuple_vars.length == 2);
|
||||
hb_always_assert (tuples.tuple_vars[0].deltas_x[0] == 4.f && tuples.tuple_vars[0].deltas_x[1] == 40.f);
|
||||
hb_always_assert (tuples.tuple_vars[1].deltas_x[0] == 2.f && tuples.tuple_vars[1].deltas_x[1] == 20.f);
|
||||
|
||||
/* The region axes copied for one store are capped. */
|
||||
OT::tuple_variations_t capped;
|
||||
num_tuple_axes = HB_VARSTORE_MAX_TUPLE_AXES - 1;
|
||||
hb_always_assert (!capped.create_from_item_var_data (store.get_sub_table (0), regions, axis_map,
|
||||
item_count, num_tuple_axes));
|
||||
|
||||
OT::item_variations_t item_vars;
|
||||
hb_always_assert (item_vars.create_from_item_varstore (store, axis_map));
|
||||
hb_hashmap_t<hb_tag_t, Triple> locations;
|
||||
hb_hashmap_t<hb_tag_t, TripleDistances> distances;
|
||||
hb_always_assert (item_vars.instantiate_tuple_vars (locations, distances));
|
||||
hb_always_assert (item_vars.as_item_varstore (false));
|
||||
hb_always_assert (item_vars.get_region_list ().length == 2);
|
||||
const auto &items = item_vars.get_vardata_encodings ()[0].items;
|
||||
hb_always_assert (items.length == 2);
|
||||
hb_always_assert ((*items[0])[0] == 4 && (*items[0])[1] == 2);
|
||||
hb_always_assert ((*items[1])[0] == 40 && (*items[1])[1] == 20);
|
||||
}
|
||||
|
||||
static void
|
||||
test_failed_encoding ()
|
||||
{
|
||||
@@ -530,4 +577,5 @@ main (int argc, char **argv)
|
||||
test_condition_delta_precision ();
|
||||
test_multi_instancer_budget ();
|
||||
test_condition_budget ();
|
||||
test_repeated_region_index ();
|
||||
}
|
||||
Reference in new issue
Block a user