diff --git a/src/OT/Var/VARC/VARC-subset.hh b/src/OT/Var/VARC/VARC-subset.hh index 9012c86d4..592c604a7 100644 --- a/src/OT/Var/VARC/VARC-subset.hh +++ b/src/OT/Var/VARC/VARC-subset.hh @@ -26,6 +26,11 @@ struct varc_subset_plan_t bool collect_glyphs () { const hb_set_t &glyphset = subset_context->plan->_glyphset_varced; + /* The records of a well-formed font do not overlap, so together they + * cannot outgrow their INDEX. As in VARC::closure_glyphs(). This also + * keeps the running total exact: a single saturating check after the + * loop can be bypassed by the sum truncating into unsigned data_size. */ + size_t records_size = source_records.get_size (); for (auto _ : + hb_zip (source_coverage, hb_range ((unsigned) source_records.count))) { @@ -39,13 +44,15 @@ struct varc_subset_plan_t return false; old_indices.push (index); new_gids.push (new_gid); - data_size = hb_unsigned_add_saturate (data_size, - source_records[index].length); + unsigned length = source_records[index].length; + if (unlikely (length > records_size)) + return false; + records_size -= length; + data_size += length; } } - return !old_indices.in_error () && !new_gids.in_error () && - data_size != UINT_MAX; + return !old_indices.in_error () && !new_gids.in_error (); } bool collect_indices () diff --git a/src/OT/Var/VARC/VARC.cc b/src/OT/Var/VARC/VARC.cc index c6a274c1e..53f105256 100644 --- a/src/OT/Var/VARC/VARC.cc +++ b/src/OT/Var/VARC/VARC.cc @@ -168,6 +168,13 @@ VARC::closure_glyphs (hb_set_t *glyphset) const if (unlikely (pending.in_error ())) return false; + /* Each glyph is visited once, and the records of a well-formed font do + * not overlap, so together they cannot outgrow their INDEX. A font with + * overlapping glyphRecords offsets, or Coverage ranges sharing an index, + * can otherwise make many gids resolve to one large record. */ + const CFF2Index &records = this+glyphRecords; + size_t records_size = records.get_size (); + while (pending) { hb_codepoint_t gid = pending.pop (); @@ -175,7 +182,10 @@ VARC::closure_glyphs (hb_set_t *glyphset) const unsigned index = (this+coverage).get_coverage (gid); if (index == NOT_COVERED) continue; - hb_ubytes_t record = (this+glyphRecords)[index]; + hb_ubytes_t record = records[index]; + if (unlikely (record.length > records_size)) + return false; + records_size -= record.length; while (record) { VarComponent::record_t component; @@ -201,11 +211,18 @@ void VARC::depend (hb_depend_data_builder_t *depend_data) const { const CFF2Index &records = this+glyphRecords; + size_t records_size = records.get_size (); /* As in closure_glyphs(). */ for (auto _ : + hb_zip (this+coverage, hb_range ((unsigned) records.count))) { hb_codepoint_t gid = _.first; hb_ubytes_t record = records[_.second]; + if (unlikely (record.length > records_size)) + { + depend_data->fail (); + return; + } + records_size -= record.length; while (record) { VarComponent::record_t component; @@ -379,8 +396,10 @@ VarComponent::get_path_at (const hb_varc_context_t &c, if (flags & (unsigned) flags_t::HAVE_CONDITION) { const auto &condition = (&VARC+VARC.conditionList)[component.condition_index]; - auto instancer = MultiItemVarStoreInstancer(&varStore, nullptr, coords, cache); + auto instancer = MultiItemVarStoreInstancer(&varStore, nullptr, coords, cache, &c.budget); show = condition.evaluate (coords.arrayZ, coords.length, &instancer); + if (unlikely (c.budget < 0)) + return hb_ubytes_t (); } // Axis values @@ -389,11 +408,10 @@ VarComponent::get_path_at (const hb_varc_context_t &c, if ((flags & (unsigned) flags_t::AXIS_VALUES_HAVE_VARIATION) && show && !axisValues.in_error ()) { - if (unlikely (!hb_budget_spend (c.budget, axisValues.length, - coords.length))) - return hb_ubytes_t (); varStore.get_delta (component.axis_values_var_idx, coords, - axisValues.as_array (), cache); + axisValues.as_array (), cache, &c.budget); + if (unlikely (c.budget < 0)) + return hb_ubytes_t (); } auto component_coords = coords; @@ -429,10 +447,9 @@ VarComponent::get_path_at (const hb_varc_context_t &c, transformValues[numTransformValues++] = (double) transform.name; VARC_PROCESS_TRANSFORM_COMPONENTS; #undef PROCESS_TRANSFORM_COMPONENT - if (unlikely (!hb_budget_spend (c.budget, numTransformValues, - coords.length))) + varStore.get_delta (transformVarIdx, coords, hb_array (transformValues, numTransformValues), cache, &c.budget); + if (unlikely (c.budget < 0)) return hb_ubytes_t (); - varStore.get_delta (transformVarIdx, coords, hb_array (transformValues, numTransformValues), cache); numTransformValues = 0; #define PROCESS_TRANSFORM_COMPONENT(shift, type, flag, name) \ if (flags & (unsigned) flags_t::flag) \ diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 19e0f4e43..ba274a7f3 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2953,10 +2953,15 @@ _hb_avar2_region_is_dead (const hb_hashmap_t &axis_tuples, struct SparseVariationRegion { - float evaluate (const int *coords, unsigned int coord_len) const + float evaluate (const int *coords, unsigned int coord_len, + int64_t *budget = nullptr) const { float v = 1.f; unsigned int count = axes.len; + /* Each region is walked per uncached delta; charge its axis records. */ + if (budget && + unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, count))) + return 0.; for (unsigned int i = 0; i < count; i++) { float factor = (this+axes[i]).evaluate (coords, coord_len); @@ -2997,7 +3002,8 @@ struct SparseVarRegionList HB_ALWAYS_INLINE float evaluate (unsigned int region_index, const int *coords, unsigned int coord_len, - hb_scalar_cache_t *cache = nullptr) const + hb_scalar_cache_t *cache = nullptr, + int64_t *budget = nullptr) const { if (unlikely (region_index >= regions.len)) return 0.; @@ -3008,7 +3014,7 @@ struct SparseVarRegionList const SparseVariationRegion ®ion = this+regions[region_index]; - v = region.evaluate (coords, coord_len); + v = region.evaluate (coords, coord_len, budget); if (cache) cache->set (region_index, v); @@ -3452,18 +3458,24 @@ struct MultiVarData const int *coords, unsigned int coord_count, const SparseVarRegionList ®ions, hb_array_t out, - hb_scalar_cache_t *cache = nullptr) const + hb_scalar_cache_t *cache = nullptr, + int64_t *budget = nullptr) const { auto &deltaSets = get_delta_sets (); auto values_iter = deltaSets.fetcher (inner); unsigned regionCount = regionIndices.len; + /* Every listed region is visited, and can add to every value. */ + if (budget && + unlikely (!hb_budget_spend (*budget, HB_BUDGET_1 + out.length, + regionCount))) + return; unsigned skip = 0; for (unsigned regionIndex = 0; regionIndex < regionCount; regionIndex++) { float scalar = regions.evaluate (regionIndices.arrayZ[regionIndex], coords, coord_count, - cache); + cache, budget); // We skip lazily. Helps with the tail end. if (scalar == 0.0f) skip += out.length; @@ -3878,7 +3890,8 @@ struct MultiItemVariationStore void get_delta (unsigned int outer, unsigned int inner, const int *coords, unsigned int coord_count, hb_array_t out, - hb_scalar_cache_t *cache = nullptr) const + hb_scalar_cache_t *cache = nullptr, + int64_t *budget = nullptr) const { #ifdef HB_NO_VAR return; @@ -3891,28 +3904,32 @@ struct MultiItemVariationStore coords, coord_count, this+regions, out, - cache); + cache, budget); } public: + /* If BUDGET is given, the work is charged to it; once it is exhausted + * (negative), OUT is unspecified and the caller should bail out. */ void get_delta (unsigned int index, const int *coords, unsigned int coord_count, hb_array_t out, - hb_scalar_cache_t *cache = nullptr) const + hb_scalar_cache_t *cache = nullptr, + int64_t *budget = nullptr) const { unsigned int outer = index >> 16; unsigned int inner = index & 0xFFFF; - get_delta (outer, inner, coords, coord_count, out, cache); + get_delta (outer, inner, coords, coord_count, out, cache, budget); } void get_delta (unsigned int index, hb_array_t coords, hb_array_t out, - hb_scalar_cache_t *cache = nullptr) const + hb_scalar_cache_t *cache = nullptr, + int64_t *budget = nullptr) const { return get_delta (index, coords.arrayZ, coords.length, out, - cache); + cache, budget); } bool sanitize (hb_sanitize_context_t *c) const @@ -4233,6 +4250,10 @@ struct ItemVarStoreInstancer double get_condition_delta (uint32_t varIdx) const { return get_delta (varIdx); } + /* FeatureVariations conditions are evaluated once while building a shape + * or subset plan, not under a rendering work budget. */ + bool charge_condition () const { return true; } + template Float get_delta (uint32_t varIdx, unsigned short offset = 0) const { @@ -4261,8 +4282,10 @@ struct MultiItemVarStoreInstancer MultiItemVarStoreInstancer (const MultiItemVariationStore *varStore, const DeltaSetIndexMap *varIdxMap, hb_array_t coords, - hb_scalar_cache_t *cache = nullptr) : - varStore (varStore), varIdxMap (varIdxMap), coords (coords), cache (cache) + hb_scalar_cache_t *cache = nullptr, + int64_t *budget = nullptr) : + varStore (varStore), varIdxMap (varIdxMap), coords (coords), cache (cache), + budget (budget) { if (!varStore) varStore = &Null(MultiItemVariationStore); @@ -4273,6 +4296,11 @@ struct MultiItemVarStoreInstancer double get_condition_delta (uint32_t varIdx) const { return (double) (*this)[varIdx]; } + /* Condition trees are evaluated per VARC component visit, so they draw + * on the renderer's live work budget when one is attached. */ + bool charge_condition () const + { return !budget || hb_budget_spend (*budget, HB_BUDGET_1); } + double operator[] (uint32_t varIdx) const { double v = 0; @@ -4288,7 +4316,7 @@ struct MultiItemVarStoreInstancer varIdx += offset; if (varIdxMap) varIdx = varIdxMap->map (varIdx); - varStore->get_delta (varIdx, coords, out, cache); + varStore->get_delta (varIdx, coords, out, cache, budget); } else for (unsigned i = 0; i < out.length; i++) @@ -4299,6 +4327,7 @@ struct MultiItemVarStoreInstancer const DeltaSetIndexMap *varIdxMap; hb_array_t coords; hb_scalar_cache_t *cache; + int64_t *budget; }; @@ -4680,6 +4709,11 @@ struct Condition bool evaluate (const int *coords, unsigned int coord_len, Instancer *instancer) const { + /* And/Or children are offsets that may all share one subtree, so the + * evaluated size of a condition tree is not tied to its encoded size. + * Charge every node visited; the guard keeps a null instancer safe. */ + if (unlikely (instancer && !instancer->charge_condition ())) return false; + switch (u.format.v) { /* A null condition offset resolves to the nil Condition, whose reserved * format 0 represents the True condition required by the specification. */ diff --git a/src/test-item-varstore.cc b/src/test-item-varstore.cc index 19a79b62d..7e0ca2eed 100644 --- a/src/test-item-varstore.cc +++ b/src/test-item-varstore.cc @@ -395,6 +395,68 @@ test_condition_delta_precision () bytes.fini (); } +static void +test_multi_instancer_budget () +{ + const char store_data[] = + "\x00\x01" "\x00\x00\x00\x0c" "\x00\x01" "\x00\x00\x00\x22" // Store + "\x00\x00\x00\x01" "\x00\x00\x00\x08" // SparseVarRegionList + "\x00\x01" "\x00\x00\x00\x06" // SparseVariationRegion + "\x00\x00" "\x00\x00" "\x40\x00" "\x40\x00" // SparseVarRegionAxis + "\x01" "\x00\x02" "\x00\x00" "\x00\x00" "\x00\x00\x00\x0b" // MultiVarData + "\x00\x00\x00\x01" "\x01" "\x01\x04" "\x01\x01\x02"; // TupleList + const auto &store = *reinterpret_cast (store_data); + int coords[] = {16384}; + OT::hb_scalar_cache_t static_cache; + auto *cache = store.create_cache (&static_cache); + hb_always_assert (cache == &static_cache); + int64_t budget = 9; + OT::MultiItemVarStoreInstancer instancer (&store, nullptr, hb_array (coords), cache, &budget); + // Each delta is charged per region listed by its MultiVarData, for the + // visit and for each value; and per axis of each region not cached yet. + hb_always_assert (instancer[0] == 3.); + hb_always_assert (budget == 4); + hb_always_assert (instancer[OT::VarIdx::NO_VARIATION] == 0.); + hb_always_assert (budget == 4); + hb_always_assert (instancer[0] == 3.); + hb_always_assert (budget == 0); + hb_always_assert (instancer[0] == 0.); + hb_always_assert (budget < 0); + OT::MultiItemVariationStore::destroy_cache (cache, &static_cache); +} + +static void +test_condition_budget () +{ + static const unsigned char store_data[] = { + 0, 1, 0, 0, 0, 12, 0, 1, 0, 0, 0, 26, /* MultiItemVariationStore */ + 0, 0, 0, 2, 0, 0, 0, 12, 0, 0, 0, 12, /* SparseVarRegionList */ + 0, 0, /* SparseVariationRegion */ + 1, 0, 2, 0, 0, 0, 1, 0, 0, 0, 11, /* MultiVarData */ + 0, 0, 0, 1, 1, 1, 4, 1, 1, 2, /* TupleList */ + }; + static const unsigned char condition_data[] = { + 0, 3, 3, 0, 0, 12, 0, 0, 12, 0, 0, 12, /* ConditionAnd */ + 0, 2, 0xFF, 0xFE, 0, 0, 0, 0, /* ConditionValue */ + }; + const auto &store = *reinterpret_cast (store_data); + const auto &condition = *reinterpret_cast (condition_data); + + OT::MultiItemVarStoreInstancer unbudgeted (&store, nullptr, hb_array_t ()); + hb_always_assert (unbudgeted.get_condition_delta (0) == 3.); + hb_always_assert (condition.evaluate (nullptr, 0, &unbudgeted)); + + // Every node visited is charged one unit, even when the And's three + // children share one subtree; each value leaf fetches a delta charged per + // region its MultiVarData sums over ((1 + out.length) x 2 regions). + int64_t budget = 1 + 3 * (1 + (1 + 1) * 2); + OT::MultiItemVarStoreInstancer instancer (&store, nullptr, hb_array_t (), nullptr, &budget); + hb_always_assert (condition.evaluate (nullptr, 0, &instancer)); + hb_always_assert (budget == 0); + hb_always_assert (!condition.evaluate (nullptr, 0, &instancer)); + hb_always_assert (budget < 0); +} + static void test_failed_encoding () { @@ -466,4 +528,6 @@ main (int argc, char **argv) test_failed_encoding (); test_accumulated_delta_widths (); test_condition_delta_precision (); + test_multi_instancer_budget (); + test_condition_budget (); } diff --git a/test/api/test-subset-varc.c b/test/api/test-subset-varc.c index 2e4ec37cc..ced903485 100644 --- a/test/api/test-subset-varc.c +++ b/test/api/test-subset-varc.c @@ -524,6 +524,46 @@ test_subset_varc_passthrough_requires_retained_gids (void) hb_face_destroy (face); } +static void +test_subset_varc_overlapping_records (void) +{ + /* Three glyphs whose VARC records share bytes. Walking every record a + * glyph resolves to must not cost more than the glyphRecords INDEX. */ + static const char maxp_data[] = {0, 0, 0x50, 0, 0, 3}; + static const char coverages[][10] = { + /* Format 2, glyphs 0..2 -> records 0..2: caught by the closure. */ + {0, 2, 0, 1, 0, 0, 0, 2, 0, 0}, + /* Format 1, glyph 0 three times: caught by the table subsetter. */ + {0, 1, 0, 3, 0, 0, 0, 0, 0, 0}, + }; + char varc_data[] = { + 0, 1, 0, 0, /* version */ + 0, 0, 0, 24, /* coverage */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* varStore, conditionList, axisIndicesList */ + 0, 0, 0, 34, /* glyphRecords */ + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* coverage, filled in below */ + 0, 0, 0, 3, 1, /* INDEX: count 3, offSize 1 */ + 1, 31, 1, 31, /* records 0 and 2 are the same 30 bytes */ + 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, + 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, 0, 0, 2, + }; + + for (unsigned i = 0; i < G_N_ELEMENTS (coverages); i++) + { + memcpy (varc_data + 24, coverages[i], sizeof (coverages[i])); + hb_face_t *face = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face, "VARC", varc_data); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_glyph_set (input), 0, 2); + g_assert_null (hb_subset_or_fail (face, input)); + + hb_subset_input_destroy (input); + hb_face_destroy (face); + } +} + int main (int argc, char **argv) { @@ -540,5 +580,6 @@ main (int argc, char **argv) hb_test_add (test_subset_varc_instance_without_varc_glyphs); hb_test_add (test_subset_varc_can_be_explicitly_dropped_when_instancing); hb_test_add (test_subset_varc_passthrough_requires_retained_gids); + hb_test_add (test_subset_varc_overlapping_records); return hb_test_run (); }