diff --git a/src/hb-limits.hh b/src/hb-limits.hh index 217a973ac..08d5bb179 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -275,6 +275,21 @@ hb_budget_spend (int64_t &budget, unsigned int cost, unsigned int mult = 1) #define HB_BUDGET_SUBSET_GLYF ((int64_t) 1 << 28) #endif +/* The flat floor for one pass of the subsetter over the charstrings of + * a CFF or CFF2 table, in operators and operands interpreted. Glyphs + * can share subroutines, so neither HB_CFF_MAX_OPS per glyph nor the + * table size bounds that work. Running out fails the charstring being + * interpreted, as exceeding HB_CFF_MAX_OPS does. */ +#ifndef HB_BUDGET_SUBSET_CFF +#define HB_BUDGET_SUBSET_CFF ((int64_t) 1 << 24) +#endif + +/* Very large CFF and CFF2 tables still get several operations per byte; + * subroutinized fonts interpret a few operations per table byte. */ +#ifndef HB_BUDGET_SUBSET_CFF_PER_BYTE +#define HB_BUDGET_SUBSET_CFF_PER_BYTE 32 +#endif + /* Partially instancing one CFF2 table in the subsetter, in cells of the * old-region to new-region matrices built for its VarDatas. A VarData * with k regions costs k * k cells for 2 * k bytes of font, and many diff --git a/src/hb-ot-cff-common.hh b/src/hb-ot-cff-common.hh index 28b227f97..6c0081262 100644 --- a/src/hb-ot-cff-common.hh +++ b/src/hb-ot-cff-common.hh @@ -56,6 +56,14 @@ static inline const Type& StructAtOffsetOrNull (const void *P, int offset, hb_sa return obj; } +/* Work budget of one pass over the charstrings of the table in blob. */ +static inline int64_t +get_subset_budget (const hb_blob_t *blob) +{ + return hb_max ((int64_t) HB_BUDGET_SUBSET_CFF, + (int64_t) HB_BUDGET_SUBSET_CFF_PER_BYTE * blob->length); +} + struct code_pair_t { diff --git a/src/hb-ot-cff1-table.cc b/src/hb-ot-cff1-table.cc index 4cbd79b78..1a1178baa 100644 --- a/src/hb-ot-cff1-table.cc +++ b/src/hb-ot-cff1-table.cc @@ -662,7 +662,7 @@ _cff1_charstring_may_end_in_seac (const hb_ubytes_t &str, return end >= 2 && str[end - 2] == OpCode_escape; } -bool OT::cff1::accelerator_subset_t::get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent) const +bool OT::cff1::accelerator_subset_t::get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent, int64_t *budget) const { if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false; /* seac uses StandardEncoding names and is not defined in CID-keyed fonts. */ @@ -674,7 +674,7 @@ bool OT::cff1::accelerator_subset_t::get_seac_components (hb_codepoint_t glyph, cff1_cs_interp_env_t env (str, *this, fd); cff1_cs_interpreter_t interp (env); get_seac_param_t param (this); - if (unlikely (!interp.interpret (param))) return false; + if (unlikely (!interp.interpret (param, budget))) return false; if (param.has_seac ()) { diff --git a/src/hb-ot-cff1-table.hh b/src/hb-ot-cff1-table.hh index d6aa3d02c..e7c2f26da 100644 --- a/src/hb-ot-cff1-table.hh +++ b/src/hb-ot-cff1-table.hh @@ -1579,7 +1579,7 @@ struct cff1 HB_INTERNAL bool subset (hb_subset_context_t *c) const; HB_INTERNAL bool serialize (hb_serialize_context_t *c, struct cff1_subset_plan &plan) const; - HB_INTERNAL bool get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent) const; + HB_INTERNAL bool get_seac_components (hb_codepoint_t glyph, hb_codepoint_t *base, hb_codepoint_t *accent, int64_t *budget = nullptr) const; mutable CFF::cff_subset_accelerator_t* cff_accelerator = nullptr; bool subrs_may_end_in_seac = false; @@ -1615,15 +1615,18 @@ struct cff1_subset_accelerator_t : cff1::accelerator_subset_t { void depend (hb_depend_data_builder_t *builder) const { if (!is_valid ()) return; + int64_t budget = CFF::get_subset_budget (blob); for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++) { hb_codepoint_t base_gid, accent_gid; - if (get_seac_components (gid, &base_gid, &accent_gid)) + if (get_seac_components (gid, &base_gid, &accent_gid, &budget)) { builder->add_depend (gid, HB_TAG('C','F','F',' '), base_gid); builder->add_depend (gid, HB_TAG('C','F','F',' '), accent_gid); } } + if (unlikely (budget <= 0)) + builder->fail (); } }; diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 5cbd2774e..0b1679d32 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -416,6 +416,7 @@ struct subr_flattener_t unsigned count = plan->num_output_glyphs (); if (!flat_charstrings.resize_exact (count)) return false; + int64_t budget = get_subset_budget (acc.get_blob ()); for (unsigned int i = 0; i < count; i++) { hb_codepoint_t glyph; @@ -440,7 +441,7 @@ struct subr_flattener_t plan, command_capture ? &(*command_capture)[i] : nullptr }; - if (unlikely (!interp.interpret (param))) + if (unlikely (!interp.interpret (param, &budget))) return false; } return true; @@ -892,6 +893,19 @@ struct subr_subsetter_t parsed_global_subrs = &cff_accelerator->parsed_global_subrs; parsed_local_subrs = &cff_accelerator->parsed_local_subrs; } else { + /* Font DICTs can share a Private DICT and its Subrs, so the size of + * the table does not bound the sum of their counts. Every subroutine + * of a font that does not share them takes at least one byte of the + * table; _hb_subset_table_try() would not serialize more than this + * anyway. Over the limit the subset fails, as any CFF planning + * failure has since dde7da182e. */ + uint64_t num_subrs = acc.globalSubrs->count; + for (unsigned int i = 0; i < acc.fdCount; i++) + num_subrs += acc.privateDicts[i].localSubrs->count; + if (unlikely (num_subrs * sizeof (parsed_cs_str_t) > + (uint64_t) acc.get_blob ()->length * 256)) + return false; + parsed_charstrings.resize_exact (plan->num_output_glyphs ()); parsed_global_subrs_storage.resize_exact (acc.globalSubrs->count); @@ -922,6 +936,8 @@ struct subr_subsetter_t bool coalesce = !(plan->flags & HB_SUBSET_FLAGS_NO_HINTING) && !plan->inprogress_accelerator; + int64_t budget = get_subset_budget (acc.get_blob ()); + /* phase 1 & 2 */ for (auto _ : plan->new_to_old_gid_list) { @@ -957,7 +973,7 @@ struct subr_subsetter_t plan->flags & HB_SUBSET_FLAGS_NO_HINTING, coalesce); - if (unlikely (!interp.interpret (param))) + if (unlikely (!interp.interpret (param, &budget))) return false; /* complete parsed string esp. copy CFF1 width or CFF2 vsindex to the parsed charstring for encoding */ diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 97e326f5c..a87b0e094 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -1221,6 +1221,7 @@ struct cff2_subset_plan hb_vector_t round_charstrings; if (unlikely (!round_charstrings.resize (num_glyphs))) return false; hb_set_t used_fds; + int64_t budget = CFF::get_subset_budget (acc.get_blob ()); for (unsigned int i = 0; i < num_glyphs; i++) { hb_codepoint_t glyph; @@ -1238,7 +1239,7 @@ struct cff2_subset_plan cff2_usage_param_t param; param.instancing = partial_instancing ? &instancing : nullptr; param.drop_hints = drop_hints; - if (unlikely (!interp.interpret (param))) + if (unlikely (!interp.interpret (param, &budget))) return false; round_charstrings.arrayZ[i] = param.has_blend && !param.has_flex1; } @@ -1270,6 +1271,7 @@ struct cff2_subset_plan return false; if (capture_commands && !charstring_commands.resize_exact (num_glyphs)) return false; + budget = CFF::get_subset_budget (acc.get_blob ()); for (unsigned int i = 0; i < num_glyphs; i++) { hb_codepoint_t glyph; @@ -1294,7 +1296,7 @@ struct cff2_subset_plan cff2_cs_rounding_t rounding; if (round_charstrings.arrayZ[i]) param.rounding = &rounding; param.instancer = partial_instancing ? &instancing : nullptr; - if (unlikely (!interp.interpret (param))) + if (unlikely (!interp.interpret (param, &budget))) return false; } } diff --git a/src/hb-subset-plan-var.cc b/src/hb-subset-plan-var.cc index 72216e2c0..1a1cd7276 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -732,11 +732,15 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan) if (_vmtx.has_data () && _vmtx.var_table.get_length ()) vvar_store_cache = _vmtx.var_table->get_var_store ().create_cache (); + int64_t budget = CFF::get_subset_budget (cff2.get_blob ()); for (auto p : *plan->glyph_map) { hb_codepoint_t old_gid = p.first; hb_codepoint_t new_gid = p.second; - if (!cff2.get_extents (font, old_gid, &extents)) continue; + if (!cff2.get_extents_at (font, old_gid, &extents, + hb_array (font->coords, font->num_coords), + &budget)) + continue; bool has_bounds_info = true; if (extents.x_bearing == 0 && extents.width == 0 && extents.height == 0 && extents.y_bearing == 0) @@ -795,6 +799,7 @@ update_instance_metrics_map_from_cff2 (hb_subset_plan_t *plan) plan->bounds_height_vec[new_gid] = hb_saturate_neg (extents.height); } } + plan->check_success (budget > 0); /* No glyph contributed bounds: do not serialize the accumulator's * inverted sentinels into head (or a downgraded CFF FontBBox). */ if (plan->head_maxp_info.xMin > plan->head_maxp_info.xMax) diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 8f661cb18..34a8a716d 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -63,10 +63,11 @@ hb_subset_accelerator_t::~hb_subset_accelerator_t () static inline bool _add_cff_seac_components (const OT::cff1::accelerator_subset_t &cff, hb_codepoint_t gid, - hb_set_t *gids_to_retain) + hb_set_t *gids_to_retain, + int64_t *budget) { hb_codepoint_t base_gid, accent_gid; - if (cff.get_seac_components (gid, &base_gid, &accent_gid)) + if (cff.get_seac_components (gid, &base_gid, &accent_gid, budget)) { gids_to_retain->add (base_gid); gids_to_retain->add (accent_gid); @@ -531,9 +532,13 @@ _populate_gids_to_retain (hb_subset_plan_t* plan, { bool has_seac = false; if (cff->is_valid ()) + { + int64_t budget = CFF::get_subset_budget (cff->get_blob ()); for (hb_codepoint_t gid : cur_glyphset) - if (_add_cff_seac_components (*cff, gid, &plan->_glyphset)) + if (_add_cff_seac_components (*cff, gid, &plan->_glyphset, &budget)) has_seac = true; + plan->check_success (budget > 0); + } plan->has_seac = has_seac; } #endif diff --git a/test/api/test-subset-cff1.c b/test/api/test-subset-cff1.c index 6d836e152..e945f79c7 100644 --- a/test/api/test-subset-cff1.c +++ b/test/api/test-subset-cff1.c @@ -543,6 +543,209 @@ test_subset_cff1_j_identity_charset (void) hb_face_destroy (face_41_4c2e); } +/* Subsets a CID-keyed font of 16 Font DICTs, 1000 empty local subroutines and + * two empty glyphs, and returns whether the CFF table was subsetted. */ +static hb_bool_t +subset_cff1_many_font_dicts (hb_bool_t share_private_dict) +{ + static const unsigned char maxp[] = {0x00, 0x00, 0x50, 0x00, 0x00, 0x02}; + static const unsigned char prefix[] = { + 0x01, 0x00, 0x04, 0x04, /* 0: header */ + 0x00, 0x01, 0x01, 0x01, 0x02, 'X', /* 4: Name INDEX */ + 0x00, 0x01, 0x01, 0x01, 0x18, /* 10: Top DICT INDEX */ + 0x8b, 0x8b, 0x8b, 0x0c, 0x1e, /* ROS */ + 0x1c, 0x00, 0x2a, 0x0f, /* charset */ + 0x1c, 0x00, 0x32, 0x11, /* CharStrings */ + 0x1c, 0x00, 0x3e, 0x0c, 0x24, /* FDArray */ + 0x1c, 0x00, 0x2f, 0x0c, 0x25, /* FDSelect */ + 0x00, 0x00, /* 38: String INDEX */ + 0x00, 0x00, /* 40: Global Subr INDEX */ + 0x02, 0x00, 0x01, 0x00, 0x00, /* 42: charset */ + 0x00, 0x00, 0x00, /* 47: FDSelect */ + 0x00, 0x02, 0x01, 0x01, 0x02, 0x03, 0x0e, 0x0e, /* 50: CharStrings INDEX */ + 0x1c, 0x00, 0x68, 0x13, /* 58: Private DICT: Subrs */ + 0x00, 0x10, 0x01, /* 62: FDArray INDEX */ + }; + unsigned char cff[1166], *p = cff; + + memcpy (p, prefix, sizeof (prefix)); + p += sizeof (prefix); + for (unsigned i = 0; i <= 16; i++) + *p++ = 1 + 5 * i; + for (unsigned i = 0; i < 16; i++) + { + /* Private: size and offset. */ + *p++ = !i || share_private_dict ? 0x8f : 0x8b; + *p++ = 0x1c; *p++ = 0x00; *p++ = 0x3a; + *p++ = 0x12; + } + /* 162: Local Subr INDEX */ + *p++ = 0x03; *p++ = 0xe8; *p++ = 0x01; + memset (p, 0x01, 1001); + p += 1001; + g_assert_true (p == cff + sizeof (cff)); + + hb_face_t *face = hb_face_builder_create (); + hb_blob_t *blob = hb_blob_create ((const char *) maxp, sizeof (maxp), + HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (face, HB_TAG ('m','a','x','p'), blob); + hb_blob_destroy (blob); + blob = hb_blob_create ((const char *) cff, sizeof (cff), + HB_MEMORY_MODE_READONLY, NULL, NULL); + hb_face_builder_add_table (face, HB_TAG ('C','F','F',' '), blob); + hb_blob_destroy (blob); + + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add_range (hb_subset_input_glyph_set (input), 0, 1); + hb_face_t *subset = hb_subset_or_fail (face, input); + hb_subset_input_destroy (input); + + /* Whether the subset was produced with a (non-empty) CFF table. */ + hb_bool_t ret = 0; + if (subset) + { + blob = hb_face_reference_table (subset, HB_TAG ('C','F','F',' ')); + ret = hb_blob_get_length (blob) != 0; + hb_blob_destroy (blob); + hb_face_destroy (subset); + } + + hb_face_destroy (face); + return ret; +} + +static void +test_subset_cff1_shared_local_subrs (void) +{ + /* Subroutines bounded by the table subset fine. */ + g_assert_true (subset_cff1_many_font_dicts (false)); + /* With every Font DICT sharing the one Subrs INDEX, each would get its + * own parsed copy, out of proportion to the table; the subset fails + * instead (a planning failure, as for other unsupported CFF input). */ + g_assert_true (!subset_cff1_many_font_dicts (true)); +} + +static hb_face_t * +create_shared_subrs_face (unsigned num_glyphs, hb_bool_t endchar_in_subr) +{ + /* Global subrs 1, 2 and 3 call the previous one ten times each, so a call + * of subr 3 executes a little under HB_CFF_MAX_OPS operators and operands. + * Every glyph is such a call. */ + static const unsigned char prefix[] = { + 1, 0, 4, 4, /* header */ + 0, 1, 1, 1, 2, 'A', /* Name INDEX */ + 0, 1, 1, 1, 24, /* Top DICT INDEX */ + 29, 0, 0, 1, 62, 15, /* charset */ + 29, 0, 0, 1, 67, 17, /* CharStrings */ + 29, 0, 0, 0, 0, 29, 0, 0, 0, 4, 18, /* Private, empty */ + 0, 0, /* String INDEX */ + 0, 5, 2, 0, 1, 0, 198, 0, 219, 0, 240, 1, 5, 1, 8, /* Global Subr INDEX */ + }; + const unsigned char rlineto = 5, return_ = 11, endchar = 14, callgsubr = 29, bias = 139 - 107; + unsigned charstring_length = endchar_in_subr ? 2 : 3; + unsigned length = sizeof (prefix) + 263 + 5 + 3 + 2 * (num_glyphs + 1) + charstring_length * num_glyphs; + unsigned char *cff = (unsigned char *) calloc (length, 1); + g_assert_nonnull (cff); + unsigned char *p = cff; + + memcpy (p, prefix, sizeof (prefix)); + p += sizeof (prefix); + for (unsigned i = 0; i < 4; i++) + { + memset (p, 140, 48); + p += 48; + *p++ = rlineto; + } + *p++ = return_; + for (unsigned subr = 1; subr <= 3; subr++) + { + for (unsigned i = 0; i < 10; i++) + { + *p++ = bias + subr - 1; + *p++ = callgsubr; + } + *p++ = return_; + } + *p++ = bias + 3; + *p++ = callgsubr; + *p++ = endchar; + + /* charset */ + *p++ = 2; + *p++ = 0; + *p++ = 1; + *p++ = (num_glyphs - 2) >> 8; + *p++ = (num_glyphs - 2) & 0xFF; + + /* CharStrings INDEX */ + *p++ = num_glyphs >> 8; + *p++ = num_glyphs & 0xFF; + *p++ = 2; + for (unsigned i = 0; i <= num_glyphs; i++) + { + *p++ = (1 + charstring_length * i) >> 8; + *p++ = (1 + charstring_length * i) & 0xFF; + } + for (unsigned i = 0; i < num_glyphs; i++) + { + *p++ = bias + (endchar_in_subr ? 4 : 3); + *p++ = callgsubr; + if (!endchar_in_subr) + *p++ = endchar; + } + g_assert_cmpuint (p - cff, ==, length); + + char maxp[] = {0, 0, 0x50, 0, (char) (num_glyphs >> 8), (char) (num_glyphs & 0xFF)}; + hb_face_t *builder = hb_face_builder_create (); + HB_FACE_ADD_TABLE (builder, "maxp", maxp); + hb_blob_t *blob = hb_blob_create_or_fail ((const char *) cff, length, + HB_MEMORY_MODE_READONLY, cff, free); + g_assert_nonnull (blob); + hb_face_builder_add_table (builder, HB_TAG ('C','F','F',' '), blob); + hb_blob_destroy (blob); + + /* maxp does not outlive this function. */ + blob = hb_face_reference_blob (builder); + hb_face_destroy (builder); + hb_face_t *face = hb_face_create (blob, 0); + hb_blob_destroy (blob); + return face; +} + +static void +test_subset_cff1_shared_subrs (void) +{ + /* The work is limited over all glyphs, not only per glyph. With endchar + * in the glyph the seac closure interprets the charstrings as well. */ + for (unsigned endchar_in_subr = 0; endchar_in_subr < 2; endchar_in_subr++) + { + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_subset_input_keep_everything (input); + + hb_face_t *face = create_shared_subrs_face (4, endchar_in_subr); + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_nonnull (subset); + hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('C','F','F',' ')); + g_assert_cmpuint (hb_blob_get_length (blob), >, 0); + hb_blob_destroy (blob); + hb_face_destroy (subset); + hb_face_destroy (face); + + /* Enough of them exhaust the shared work budget; the subset fails + * instead of interpreting num_glyphs x HB_CFF_MAX_OPS, and so does the + * dependency builder that walks the same seac closure. */ + face = create_shared_subrs_face (100, endchar_in_subr); + subset = hb_subset_or_fail (face, input); + g_assert_null (subset); +#ifndef HB_NO_SUBSET_DEPEND + g_assert_null (hb_subset_depend_from_face_or_fail (face)); +#endif + hb_face_destroy (face); + + hb_subset_input_destroy (input); + } +} + static void test_subset_cff1_preprocess_glyph_map_holes (void) { @@ -678,6 +881,8 @@ main (int argc, char **argv) hb_test_add (test_subset_cff1_encoding_range_overflow); hb_test_add (test_subset_cff1_identity_charset); hb_test_add (test_subset_cff1_j_identity_charset); + hb_test_add (test_subset_cff1_shared_local_subrs); + hb_test_add (test_subset_cff1_shared_subrs); hb_test_add (test_subset_cff1_preprocess_glyph_map_holes); #ifdef HB_EXPERIMENTAL_API