mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-25 08:51:07 +01:00
[limits] Add weighted outline work accounting
COLR can repeatedly request expensive glyf or CFF outlines, multiplying their per-glyph structural limits. Make both outline implementations consume a caller-provided weighted budget so a later paint-session counter can aggregate that work. Add checked precharge arithmetic with stable exhaustion, charge glyf loads, terminal points, and gvar tuples, and charge CFF operators, subroutine calls, CFF2 blends, and both CFF1 seac outlines. Keep a generous local fallback for callers that do not yet provide a session budget. Tests: meson test -C build Assisted-by: Codex <codex@openai.com>
This commit is contained in:
1 parent
3f84c51ede
commit
074b486d21
10 files changed
+157
-23
No files matched your search
+12
-4
@@ -314,7 +314,8 @@ struct Glyph
|
||||
hb_array_t<const int> coords = hb_array_t<const int> (),
|
||||
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;
|
||||
|
||||
+19
-8
@@ -238,15 +238,22 @@ struct glyf_accelerator_t
|
||||
bool get_points (hb_font_t *font, hb_codepoint_t gid, T consumer,
|
||||
hb_array_t<const int> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -121,6 +121,7 @@ struct cs_interp_env_t : interp_env_t<ARG>
|
||||
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<ARG>
|
||||
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<ARG>
|
||||
unsigned int hstem_count;
|
||||
unsigned int vstem_count;
|
||||
unsigned int hintmask_size;
|
||||
int64_t *budget;
|
||||
call_stack_t callStack;
|
||||
biased_subrs_t<SUBRS> globalSubrs;
|
||||
biased_subrs_t<SUBRS> localSubrs;
|
||||
@@ -880,11 +887,22 @@ struct cs_interpreter_t : interpreter_t<ENV>
|
||||
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<ENV>
|
||||
break;
|
||||
}
|
||||
|
||||
if (budget) *budget -= (int64_t) (HB_CFF_MAX_OPS - max_ops);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
@@ -305,6 +305,11 @@ struct cff2_cs_opset_t : cs_opset_t<ELEM, OPSET, cff2_cs_interp_env_t<ELEM>, 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++)
|
||||
{
|
||||
|
||||
+49
-1
@@ -27,6 +27,8 @@
|
||||
|
||||
#include "hb.hh"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
#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
|
||||
|
||||
+10
-4
@@ -379,8 +379,8 @@ struct cff1_cs_opset_extents_t : cff1_cs_opset_t<cff1_cs_opset_extents_t, cff1_e
|
||||
|
||||
bounds_t base_bounds, accent_bounds;
|
||||
if (likely (!env.in_seac && base && accent
|
||||
&& _get_bounds (param.cff, base, base_bounds, true)
|
||||
&& _get_bounds (param.cff, accent, accent_bounds, true)))
|
||||
&& _get_bounds (param.cff, base, base_bounds, true, env.get_budget ())
|
||||
&& _get_bounds (param.cff, accent, accent_bounds, true, env.get_budget ())))
|
||||
{
|
||||
param.bounds.merge (base_bounds);
|
||||
accent_bounds.offset (delta);
|
||||
@@ -416,6 +416,9 @@ bool OT::cff1::accelerator_t::get_extents (hb_font_t *font, hb_codepoint_t glyph
|
||||
|
||||
bounds_t bounds;
|
||||
|
||||
int64_t stack_budget = HB_BUDGET_GLYPH;
|
||||
if (!budget) budget = &stack_budget;
|
||||
|
||||
if (!_get_bounds (this, glyph, bounds, false, budget))
|
||||
return false;
|
||||
|
||||
@@ -535,8 +538,8 @@ struct cff1_cs_opset_path_t : cff1_cs_opset_t<cff1_cs_opset_path_t, cff1_path_pa
|
||||
hb_codepoint_t accent = param.cff->std_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);
|
||||
}
|
||||
|
||||
|
||||
+14
-2
@@ -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<number_t> env (str, *this, fd, coords.arrayZ, coords.length);
|
||||
@@ -207,13 +210,19 @@ struct cff2_path_procs_path_t : path_procs_t<cff2_path_procs_path_t, cff2_cs_int
|
||||
|
||||
struct cff2_cs_opset_path_t : cff2_cs_opset_t<cff2_cs_opset_path_t, cff2_path_param_t, number_t, cff2_path_procs_path_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<const int> 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<number_t> env (str, *this, fd, coords.arrayZ, coords.length);
|
||||
|
||||
@@ -539,7 +539,7 @@ struct cff2
|
||||
hb_glyph_extents_t *extents,
|
||||
hb_array_t<const int> 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<const int> coords, int64_t *budget = nullptr) const;
|
||||
};
|
||||
|
||||
|
||||
@@ -757,7 +757,8 @@ struct gvar_GVAR
|
||||
const hb_array_t<contour_point_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;
|
||||
|
||||
@@ -106,6 +106,27 @@ main (int argc, char **argv)
|
||||
hb_always_assert (hb_clamp_to<int> ((int64_t) INT_MAX + 1) == INT_MAX);
|
||||
hb_always_assert (hb_clamp_to<int> ((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);
|
||||
|
||||
Reference in new issue
Block a user