diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index 83d3eaed0..1376d5386 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -314,7 +314,8 @@ struct Glyph hb_array_t coords = hb_array_t (), hb_scalar_cache_t *gvar_cache = nullptr, unsigned int depth = 0, - unsigned *edge_count = nullptr) const + unsigned *edge_count = nullptr, + int64_t *budget = nullptr) const { if (unlikely (depth > HB_MAX_NESTING_LEVEL)) return false; unsigned stack_edge_count = 0; @@ -322,6 +323,10 @@ struct Glyph if (unlikely (*edge_count > HB_MAX_GRAPH_EDGE_COUNT)) return false; (*edge_count)++; + int64_t stack_budget = HB_BUDGET_GLYPH; + if (!budget) budget = &stack_budget; + if (unlikely (!hb_budget_spend (*budget, HB_BUDGET_16))) return false; + if (head_maxp_info) { head_maxp_info->maxComponentDepth = hb_max (head_maxp_info->maxComponentDepth, depth); @@ -390,7 +395,8 @@ struct Glyph points.as_array ().sub_array (old_length), scratch, gvar_cache, - phantom_only && type == SIMPLE)) + phantom_only && type == SIMPLE, + budget)) return false; } else @@ -400,7 +406,8 @@ struct Glyph points.as_array ().sub_array (old_length), scratch, gvar_cache, - phantom_only && type == SIMPLE)) + phantom_only && type == SIMPLE, + budget)) return false; } #endif @@ -452,7 +459,8 @@ struct Glyph coords, gvar_cache, depth + 1, - edge_count))) + edge_count, + budget))) { points.resize (old_length); return false; diff --git a/src/OT/glyf/glyf.hh b/src/OT/glyf/glyf.hh index ce563977a..bb92d0f60 100644 --- a/src/OT/glyf/glyf.hh +++ b/src/OT/glyf/glyf.hh @@ -238,15 +238,22 @@ struct glyf_accelerator_t bool get_points (hb_font_t *font, hb_codepoint_t gid, T consumer, hb_array_t coords, hb_glyf_scratch_t &scratch, - hb_scalar_cache_t *gvar_cache = nullptr) const + hb_scalar_cache_t *gvar_cache = nullptr, + int64_t *budget = nullptr) const { if (gid >= num_glyphs) return false; + int64_t stack_budget = HB_BUDGET_GLYPH; + if (!budget) budget = &stack_budget; + auto &all_points = scratch.all_points; all_points.clear (); bool phantom_only = !consumer.is_consuming_contour_points (); - if (unlikely (!glyph_for_gid (gid).get_points (font, *this, all_points, scratch, nullptr, nullptr, nullptr, true, true, phantom_only, coords, gvar_cache))) + if (unlikely (!glyph_for_gid (gid).get_points (font, *this, all_points, scratch, nullptr, nullptr, nullptr, true, true, phantom_only, coords, gvar_cache, 0, nullptr, budget))) + return false; + + if (unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, all_points.length))) return false; unsigned count = all_points.length; @@ -470,8 +477,9 @@ struct glyf_accelerator_t gid, points_aggregator_t (font, extents, nullptr, true), coords, - *scratch); - if (budget) *budget -= scratch->all_points.length; + *scratch, + nullptr, + budget); release_scratch (scratch); return ret; } @@ -508,7 +516,9 @@ struct glyf_accelerator_t } bool - get_path (hb_font_t *font, hb_codepoint_t gid, hb_draw_session_t &draw_session, hb_scalar_cache_t *gvar_cache = nullptr) const + get_path (hb_font_t *font, hb_codepoint_t gid, hb_draw_session_t &draw_session, + hb_scalar_cache_t *gvar_cache = nullptr, + int64_t *budget = nullptr) const { if (!has_data ()) return false; @@ -519,7 +529,8 @@ struct glyf_accelerator_t hb_array (font->coords, font->has_nonzero_coords ? font->num_coords : 0), *scratch, - gvar_cache); + gvar_cache, + budget); release_scratch (scratch); @@ -537,8 +548,8 @@ struct glyf_accelerator_t bool ret = get_points (font, gid, glyf_impl::path_builder_t (font, draw_session), coords, scratch, - gvar_cache); - if (budget) *budget -= scratch.all_points.length; + gvar_cache, + budget); return ret; } diff --git a/src/hb-cff-interp-cs-common.hh b/src/hb-cff-interp-cs-common.hh index 66884d4a6..eb1d11229 100644 --- a/src/hb-cff-interp-cs-common.hh +++ b/src/hb-cff-interp-cs-common.hh @@ -121,6 +121,7 @@ struct cs_interp_env_t : interp_env_t hstem_count = 0; vstem_count = 0; hintmask_size = 0; + budget = nullptr; pt.set_int (0, 0); globalSubrs.init (globalSubrs_); localSubrs.init (localSubrs_); @@ -186,6 +187,11 @@ struct cs_interp_env_t : interp_env_t void set_endchar (bool endchar_flag_) { endchar_flag = endchar_flag_; } bool is_endchar () const { return endchar_flag; } + void set_budget (int64_t *budget_) { budget = budget_; } + int64_t *get_budget () const { return budget; } + bool spend_budget (int64_t cost, int64_t mult = 1) + { return !budget || hb_budget_spend (*budget, cost, mult); } + const number_t &get_x () const { return pt.x; } const number_t &get_y () const { return pt.y; } const point_t &get_pt () const { return pt; } @@ -201,6 +207,7 @@ struct cs_interp_env_t : interp_env_t unsigned int hstem_count; unsigned int vstem_count; unsigned int hintmask_size; + int64_t *budget; call_stack_t callStack; biased_subrs_t globalSubrs; biased_subrs_t localSubrs; @@ -880,11 +887,22 @@ struct cs_interpreter_t : interpreter_t bool interpret (PARAM& param, int64_t *budget = nullptr) { SUPER::env.set_endchar (false); + SUPER::env.set_budget (budget); bool ret = true; unsigned max_ops = HB_CFF_MAX_OPS; for (;;) { - OPSET::process_op (SUPER::env.fetch_op (), SUPER::env, param); + op_code_t op = SUPER::env.fetch_op (); + int64_t cost = op == OpCode_callsubr || op == OpCode_callgsubr ? + HB_BUDGET_8 : HB_BUDGET_1; + if (unlikely (!SUPER::env.spend_budget (cost))) + { + SUPER::env.set_error (); + ret = false; + break; + } + + OPSET::process_op (op, SUPER::env, param); if (unlikely (SUPER::env.in_error () || !--max_ops)) { SUPER::env.set_error (); @@ -895,7 +913,6 @@ struct cs_interpreter_t : interpreter_t break; } - if (budget) *budget -= (int64_t) (HB_CFF_MAX_OPS - max_ops); return ret; } diff --git a/src/hb-cff2-interp-cs.hh b/src/hb-cff2-interp-cs.hh index 770a4ae36..af810c362 100644 --- a/src/hb-cff2-interp-cs.hh +++ b/src/hb-cff2-interp-cs.hh @@ -305,6 +305,11 @@ struct cff2_cs_opset_t : cs_opset_t, PAR env.set_error (); return; } + if (unlikely (!env.spend_budget (HB_BUDGET_1, total - n))) + { + env.set_error (); + return; + } unsigned int start = count - total; for (unsigned int i = 0; i < n; i++) { diff --git a/src/hb-limits.hh b/src/hb-limits.hh index dcc624e88..854351f64 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -27,6 +27,8 @@ #include "hb.hh" +#include + #ifndef HB_BUFFER_MAX_LEN_FACTOR #define HB_BUFFER_MAX_LEN_FACTOR 256 @@ -159,10 +161,56 @@ * is skipped best-effort. */ +/* Fixed relative weights. Keep the value in the name so charge sites are + * easy to audit; tune the session caps below, not these values. */ +#define HB_BUDGET_1 ((int64_t) 1) +#define HB_BUDGET_2 ((int64_t) 2) +#define HB_BUDGET_4 ((int64_t) 4) +#define HB_BUDGET_8 ((int64_t) 8) +#define HB_BUDGET_16 ((int64_t) 16) +#define HB_BUDGET_32 ((int64_t) 32) +#define HB_BUDGET_64 ((int64_t) 64) +#define HB_BUDGET_128 ((int64_t) 128) +#define HB_BUDGET_256 ((int64_t) 256) +#define HB_BUDGET_512 ((int64_t) 512) +#define HB_BUDGET_1024 ((int64_t) 1024) + +/* A generous per-glyph fallback. Paint backends use their session cap so + * repeated outline traversals consume one aggregate budget. */ +#ifndef HB_BUDGET_GLYPH +#define HB_BUDGET_GLYPH ((int64_t) 1 << 26) +#endif + +/* Precharge COST * MULT without signed overflow. Exhaustion is latched at + * -1 so repeated calls remain safe and fail cheaply. */ +static inline bool +hb_budget_spend (int64_t &budget, int64_t cost, int64_t mult = 1) +{ + if (cost < 0 || mult < 0 || + (cost && mult > INT64_MAX / cost)) + { + budget = -1; + return false; + } + + int64_t charge = cost * mult; + if (budget < charge) + { + budget = -1; + return false; + } + + budget -= charge; + return true; +} + /* One VARC glyph (draw or extents), shared by all leaf glyphs loaded * from glyf/CFF/CFF2, in units of glyf points / CFF charstring ops. */ +#ifndef HB_BUDGET_VARC +#define HB_BUDGET_VARC ((int64_t) 1 << 20) +#endif #ifndef HB_VARC_MAX_WORK -#define HB_VARC_MAX_WORK ((int64_t) 1 << 20) +#define HB_VARC_MAX_WORK HB_BUDGET_VARC #endif /* One paint-extents session, in outline points consumed by diff --git a/src/hb-ot-cff1-table.cc b/src/hb-ot-cff1-table.cc index 25f71cb34..61d223600 100644 --- a/src/hb-ot-cff1-table.cc +++ b/src/hb-ot-cff1-table.cc @@ -379,8 +379,8 @@ struct cff1_cs_opset_extents_t : cff1_cs_opset_tstd_code_to_glyph (env.argStack[n-1].to_int ()); if (unlikely (!(!env.in_seac && base && accent - && _get_path (param.cff, param.font, base, *param.draw_session, true) - && _get_path (param.cff, param.font, accent, *param.draw_session, true, &delta)))) + && _get_path (param.cff, param.font, base, *param.draw_session, true, nullptr, env.get_budget ()) + && _get_path (param.cff, param.font, accent, *param.draw_session, true, &delta, env.get_budget ())))) env.set_error (); } }; @@ -568,6 +571,9 @@ bool OT::cff1::accelerator_t::get_path (hb_font_t *font, hb_codepoint_t glyph, h return true; #endif + int64_t stack_budget = HB_BUDGET_GLYPH; + if (!budget) budget = &stack_budget; + return _get_path (this, font, glyph, draw_session, false, nullptr, budget); } diff --git a/src/hb-ot-cff2-table.cc b/src/hb-ot-cff2-table.cc index e3c4af9fc..c1d08007e 100644 --- a/src/hb-ot-cff2-table.cc +++ b/src/hb-ot-cff2-table.cc @@ -123,6 +123,9 @@ bool OT::cff2::accelerator_t::get_extents_at (hb_font_t *font, if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false; + int64_t stack_budget = HB_BUDGET_GLYPH; + if (!budget) budget = &stack_budget; + unsigned int fd = fdSelect->get_fd (glyph); const hb_ubytes_t str = (*charStrings)[glyph]; cff2_cs_interp_env_t env (str, *this, fd, coords.arrayZ, coords.length); @@ -207,13 +210,19 @@ struct cff2_path_procs_path_t : path_procs_t {}; -bool OT::cff2::accelerator_t::get_path (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session) const +bool OT::cff2::accelerator_t::get_path (hb_font_t *font, hb_codepoint_t glyph, + hb_draw_session_t &draw_session, + int64_t *budget) const { + int64_t stack_budget = HB_BUDGET_GLYPH; + if (!budget) budget = &stack_budget; + return get_path_at (font, glyph, draw_session, hb_array (font->coords, - font->has_nonzero_coords ? font->num_coords : 0)); + font->has_nonzero_coords ? font->num_coords : 0), + budget); } bool OT::cff2::accelerator_t::get_path_at (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session, hb_array_t coords, int64_t *budget) const @@ -225,6 +234,9 @@ bool OT::cff2::accelerator_t::get_path_at (hb_font_t *font, hb_codepoint_t glyph if (unlikely (!is_valid () || (glyph >= num_glyphs))) return false; + int64_t stack_budget = HB_BUDGET_GLYPH; + if (!budget) budget = &stack_budget; + unsigned int fd = fdSelect->get_fd (glyph); const hb_ubytes_t str = (*charStrings)[glyph]; cff2_cs_interp_env_t env (str, *this, fd, coords.arrayZ, coords.length); diff --git a/src/hb-ot-cff2-table.hh b/src/hb-ot-cff2-table.hh index ddbf66b4f..375e5271d 100644 --- a/src/hb-ot-cff2-table.hh +++ b/src/hb-ot-cff2-table.hh @@ -539,7 +539,7 @@ struct cff2 hb_glyph_extents_t *extents, hb_array_t coords, int64_t *budget = nullptr) const; - HB_INTERNAL bool get_path (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session) const; + HB_INTERNAL bool get_path (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session, int64_t *budget = nullptr) const; HB_INTERNAL bool get_path_at (hb_font_t *font, hb_codepoint_t glyph, hb_draw_session_t &draw_session, hb_array_t coords, int64_t *budget = nullptr) const; }; diff --git a/src/hb-ot-var-gvar-table.hh b/src/hb-ot-var-gvar-table.hh index ec1de51df..f5ba7c9aa 100644 --- a/src/hb-ot-var-gvar-table.hh +++ b/src/hb-ot-var-gvar-table.hh @@ -757,7 +757,8 @@ struct gvar_GVAR const hb_array_t points, hb_glyf_scratch_t &scratch, hb_scalar_cache_t *gvar_cache = nullptr, - bool phantom_only = false) const + bool phantom_only = false, + int64_t *budget = nullptr) const { if (unlikely (glyph >= glyphCount)) return true; hb_scalar_cache_t *scalar_cache = gvar_cache ? @@ -806,6 +807,11 @@ struct gvar_GVAR if (scalar == 0.f) continue; + if (budget && + unlikely (!hb_budget_spend (*budget, HB_BUDGET_1, + phantom_only && count >= 4 ? 4 : count))) + return false; + if (!private_points_checked) { auto scan = iterator; diff --git a/src/test-algs.cc b/src/test-algs.cc index 3689d9f6f..102837857 100644 --- a/src/test-algs.cc +++ b/src/test-algs.cc @@ -106,6 +106,27 @@ main (int argc, char **argv) hb_always_assert (hb_clamp_to ((int64_t) INT_MAX + 1) == INT_MAX); hb_always_assert (hb_clamp_to ((int64_t) INT_MIN - 1) == INT_MIN); + int64_t budget = 10; + hb_always_assert (hb_budget_spend (budget, 4, 2)); + hb_always_assert (budget == 2); + hb_always_assert (!hb_budget_spend (budget, 3)); + hb_always_assert (budget == -1); + hb_always_assert (!hb_budget_spend (budget, 1)); + hb_always_assert (budget == -1); + + budget = 0; + hb_always_assert (hb_budget_spend (budget, 0)); + hb_always_assert (!hb_budget_spend (budget, 1)); + hb_always_assert (budget == -1); + + budget = INT64_MAX; + hb_always_assert (!hb_budget_spend (budget, INT64_MAX, 2)); + hb_always_assert (budget == -1); + + budget = INT64_MIN + 1; + hb_always_assert (!hb_budget_spend (budget, 1)); + hb_always_assert (budget == -1); + x = 1; hb_always_assert (++hb_inc (x) == 3); hb_always_assert (x == 3);