diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 27858b8ba..00ade4327 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -285,6 +285,22 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #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. + * These are flat ceilings on that one allocation: a proportional bound + * would reject legitimate optimizer output, where several VarData use + * disjoint region subsets so the dense matrix is much wider than any one + * VarData. Exceeding either limit fails instancing of that store. */ +#ifndef HB_SUBSET_MAX_VARSTORE_ROWS +#define HB_SUBSET_MAX_VARSTORE_ROWS (1u << 21) +#endif + +#ifndef HB_SUBSET_MAX_VARSTORE_DELTAS +#define HB_SUBSET_MAX_VARSTORE_DELTAS ((uint64_t) 1 << 26) +#endif + + #ifndef HB_REPACKER_MAX_ITERATIONS #define HB_REPACKER_MAX_ITERATIONS 500 #endif diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index a97430d48..150c0635e 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -2233,10 +2233,17 @@ struct item_variations_t for (unsigned major = 0; major < var_data_num_rows.length; major++) total_rows += var_data_num_rows[major]; + /* Every row is allocated as wide as the whole region list, while in the + * font a row only pays for the regions of its own VarData (possibly none). + * Bound the matrix before allocating it. */ + if (unlikely (total_rows > HB_SUBSET_MAX_VARSTORE_ROWS || + (uint64_t) total_rows * num_cols > HB_SUBSET_MAX_VARSTORE_DELTAS)) + return false; + if (!delta_rows.resize (total_rows)) return false; /* init all rows to [0]*num_cols */ for (unsigned i = 0; i < total_rows; i++) - if (!(delta_rows[i].resize (num_cols))) return false; + if (!(delta_rows[i].resize_exact (num_cols))) return false; /* old VarIdxes -> full encoding_row mapping */ hb_hashmap_t*> front_mapping; diff --git a/src/test-item-varstore.cc b/src/test-item-varstore.cc index 80497ef4a..0e8cb92c6 100644 --- a/src/test-item-varstore.cc +++ b/src/test-item-varstore.cc @@ -561,6 +561,43 @@ test_item_varstore_instancer_varidx_overflow () } +static void +test_delta_matrix_limits () +{ + const unsigned num_rows = 0xFFFFu; + + /* One row using every region plus many rows using only the first one: the + * dense matrix is rows x regions while the store only pays for the deltas. */ + const unsigned region_counts[] = {2, (unsigned) (HB_SUBSET_MAX_VARSTORE_DELTAS / num_rows) + 1}; + for (unsigned num_regions : region_counts) + { + OT::item_variations_t item_vars; + unsigned wide = item_vars.add_vardata (1); + for (unsigned i = 0; i < num_regions; i++) + { + hb_hashmap_t region; + region.set (HB_TAG ('T','E','S','T'), Triple (0, (i + 1.) / num_regions, 1)); + item_vars.add_tuple (wide, std::move (region), 0, 1, 1); + } + unsigned tall = item_vars.add_vardata (num_rows); + hb_hashmap_t region; + region.set (HB_TAG ('T','E','S','T'), Triple (0, 1. / num_regions, 1)); + item_vars.add_tuple (tall, std::move (region), 0, 1, num_rows); + hb_always_assert (item_vars.build_region_list ()); + hb_always_assert (item_vars.get_region_list ().length == num_regions); + hb_always_assert (item_vars.as_item_varstore () == (num_regions == 2)); + } + + /* Rows of a VarData without regions take no space in the font at all. */ + OT::item_variations_t item_vars; + unsigned outer = item_vars.add_vardata (1); + item_vars.add_tuple (outer, hb_hashmap_t (), 0, 1, 1); + for (unsigned i = 0; i < HB_SUBSET_MAX_VARSTORE_ROWS / num_rows + 1; i++) + item_vars.add_vardata (num_rows); + hb_always_assert (item_vars.build_region_list ()); + hb_always_assert (!item_vars.as_item_varstore ()); +} + int main (int argc, char **argv) { @@ -578,4 +615,5 @@ main (int argc, char **argv) test_multi_instancer_budget (); test_condition_budget (); test_repeated_region_index (); + test_delta_matrix_limits (); }