From fd3fe6c54f4106cd0903490e00664d444f49008e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 19 Aug 2026 13:39:49 -0600 Subject: [PATCH] [vector/raster] Bound output-session work VARC's traversal budget still allowed one glyph to amplify into large serialized paths or millions of raster edges. Vector paint also charged outline bytes only after a whole glyph, allowing an unbounded single-operation overshoot. Charge complete SVG/PDF path commands against cumulative draw and paint context budgets, reset on clear. Cap each raster draw session's stored edges and stop processing outline callbacks after exhaustion. The pathological VARC seed now keeps raster chunk 7 below 0.8 seconds and vector chunk 7 below 1 second in the full suite. SVG and PDF outputs remain syntactically valid at the budget boundary. Tested with the vector, raster, fuzzing, and full Meson test suites. Assisted-by: OpenAI Codex --- src/hb-limits.hh | 11 ++++++ src/hb-raster-draw.cc | 13 ++++++- src/hb-raster.hh | 5 +-- src/hb-vector-draw.cc | 73 +++++++++++++++++++++++++++++++++----- src/hb-vector-draw.hh | 21 +++++++++++ src/hb-vector-paint-pdf.cc | 7 ++-- src/hb-vector-paint-svg.cc | 11 +++--- src/hb-vector-paint.hh | 2 +- src/hb-vector-path.cc | 27 ++++++++++++++ src/hb-vector-path.hh | 19 ++++++++++ 10 files changed, 168 insertions(+), 21 deletions(-) diff --git a/src/hb-limits.hh b/src/hb-limits.hh index b305bd7c2..dcc624e88 100644 --- a/src/hb-limits.hh +++ b/src/hb-limits.hh @@ -176,6 +176,12 @@ #define HB_GPU_PAINT_MAX_WORK ((int64_t) 1 << 20) #endif +/* One vector (SVG/PDF) draw session, in bytes of generated outline + * path data. */ +#ifndef HB_VECTOR_MAX_DRAW_WORK +#define HB_VECTOR_MAX_DRAW_WORK ((int64_t) 16 << 20) +#endif + /* One vector (SVG/PDF) paint session, in bytes of generated outline * path and sweep-gradient patch data. */ #ifndef HB_VECTOR_MAX_PAINT_WORK @@ -204,6 +210,11 @@ #define HB_RASTER_MAX_DRAW_WORK ((int64_t) 1 << 24) #endif +/* One raster draw session, in accumulated non-horizontal edges. */ +#ifndef HB_RASTER_MAX_DRAW_EDGES +#define HB_RASTER_MAX_DRAW_EDGES ((int64_t) 1 << 20) +#endif + #ifndef HB_REPACKER_MAX_ITERATIONS #define HB_REPACKER_MAX_ITERATIONS 500 diff --git a/src/hb-raster-draw.cc b/src/hb-raster-draw.cc index 23ef964b6..b20ce0ddc 100644 --- a/src/hb-raster-draw.cc +++ b/src/hb-raster-draw.cc @@ -90,6 +90,7 @@ struct hb_raster_draw_t int64_t *external_work = nullptr; /* Accumulated geometry */ + int64_t edges_left = HB_RASTER_MAX_DRAW_EDGES; hb_vector_t edges; /* Scratch — reused across render() calls */ @@ -479,6 +480,7 @@ hb_raster_draw_clear (hb_raster_draw_t *draw) draw->flatten_clip_active = false; draw->flatten_work_left = HB_RASTER_MAX_DRAW_WORK; draw->external_work = nullptr; + draw->edges_left = HB_RASTER_MAX_DRAW_EDGES; draw->edges.clear (); draw->active_edges.clear (); } @@ -572,6 +574,9 @@ emit_segment (hb_raster_draw_t *draw, float x0, float y0, float x1, float y1) { + if (unlikely (draw->edges_left <= 0)) + return; + int32_t X0 = hb_clamp_to (roundf (x0 * HB_RASTER_ONE_PIXEL)); int32_t Y0 = hb_clamp_to (roundf (y0 * HB_RASTER_ONE_PIXEL)); int32_t X1 = hb_clamp_to (roundf (x1 * HB_RASTER_ONE_PIXEL)); @@ -588,7 +593,10 @@ emit_segment (hb_raster_draw_t *draw, e.slope = (((int64_t) e.xH - (int64_t) e.xL) * (int64_t) 65536) / ((int64_t) e.yH - (int64_t) e.yL); - draw->edges.push (e); + if (likely (draw->edges.push_or_fail (e))) + draw->edges_left--; + else + draw->edges_left = 0; } @@ -1028,6 +1036,7 @@ hb_raster_line_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *user_data HB_UNUSED) { hb_raster_draw_t *draw = (hb_raster_draw_t *) draw_data; + if (unlikely (draw->edges_left <= 0)) return; float tx0, ty0, tx1, ty1; transform_point (draw, st->current_x, st->current_y, tx0, ty0); @@ -1044,6 +1053,7 @@ hb_raster_quadratic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *user_data HB_UNUSED) { hb_raster_draw_t *draw = (hb_raster_draw_t *) draw_data; + if (unlikely (draw->edges_left <= 0)) return; float tx0, ty0, tx1, ty1, tx2, ty2; transform_point (draw, st->current_x, st->current_y, tx0, ty0); @@ -1062,6 +1072,7 @@ hb_raster_cubic_to (hb_draw_funcs_t *dfuncs HB_UNUSED, void *user_data HB_UNUSED) { hb_raster_draw_t *draw = (hb_raster_draw_t *) draw_data; + if (unlikely (draw->edges_left <= 0)) return; float tx0, ty0, tx1, ty1, tx2, ty2, tx3, ty3; transform_point (draw, st->current_x, st->current_y, tx0, ty0); diff --git a/src/hb-raster.hh b/src/hb-raster.hh index 68caf3890..a0c08a516 100644 --- a/src/hb-raster.hh +++ b/src/hb-raster.hh @@ -51,8 +51,9 @@ hb_raster_draw_set_clip_box (hb_raster_draw_t *draw, /* Points curve-flattening work charges at the caller's session budget * (one unit per Bézier subdivision) instead of the standalone - * per-session HB_RASTER_MAX_DRAW_WORK budget. Cleared by - * hb_raster_draw_clear() (and hence after every render). */ + * per-session HB_RASTER_MAX_DRAW_WORK budget. The rasterizer's local + * accumulated-edge limit still applies. Cleared by hb_raster_draw_clear() + * (and hence after every render). */ HB_INTERNAL void hb_raster_draw_set_external_work (hb_raster_draw_t *draw, int64_t *work_left); diff --git a/src/hb-vector-draw.cc b/src/hb-vector-draw.cc index 1a96cb003..5208fdcf5 100644 --- a/src/hb-vector-draw.cc +++ b/src/hb-vector-draw.cc @@ -49,36 +49,90 @@ hb_vector_svg_buffer_contains (const hb_vector_buf_t &buf, const char *needle) /* ---- SVG draw callbacks ---- */ static void hb_vector_draw_svg_move_to (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, float x, float y, void *) -{ auto *d = (hb_vector_draw_t *) dd; d->path.append_c ('M'); d->append_xy_svg (x, y); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->path.append_c ('M'); d->append_xy_svg (x, y); + d->end_path_command (before); +} static void hb_vector_draw_svg_line_to (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, float x, float y, void *) -{ auto *d = (hb_vector_draw_t *) dd; d->path.append_c ('L'); d->append_xy_svg (x, y); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->path.append_c ('L'); d->append_xy_svg (x, y); + d->end_path_command (before); +} static void hb_vector_draw_svg_quadratic_to (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, float cx, float cy, float x, float y, void *) -{ auto *d = (hb_vector_draw_t *) dd; d->path.append_c ('Q'); d->append_xy_svg (cx, cy); d->path.append_c (' '); d->append_xy_svg (x, y); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->path.append_c ('Q'); d->append_xy_svg (cx, cy); d->path.append_c (' '); d->append_xy_svg (x, y); + d->end_path_command (before); +} static void hb_vector_draw_svg_cubic_to (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, float c1x, float c1y, float c2x, float c2y, float x, float y, void *) -{ auto *d = (hb_vector_draw_t *) dd; d->path.append_c ('C'); d->append_xy_svg (c1x, c1y); d->path.append_c (' '); d->append_xy_svg (c2x, c2y); d->path.append_c (' '); d->append_xy_svg (x, y); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->path.append_c ('C'); d->append_xy_svg (c1x, c1y); d->path.append_c (' '); d->append_xy_svg (c2x, c2y); d->path.append_c (' '); d->append_xy_svg (x, y); + d->end_path_command (before); +} static void hb_vector_draw_svg_close_path (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, void *) -{ ((hb_vector_draw_t *) dd)->path.append_c ('Z'); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->path.append_c ('Z'); + d->end_path_command (before); +} /* ---- PDF draw callbacks ---- */ static void hb_vector_draw_pdf_move_to (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, float x, float y, void *) -{ auto *d = (hb_vector_draw_t *) dd; d->append_xy_pdf (x, y); d->path.append_str (" m\n"); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->append_xy_pdf (x, y); d->path.append_str (" m\n"); + d->end_path_command (before); +} static void hb_vector_draw_pdf_line_to (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, float x, float y, void *) -{ auto *d = (hb_vector_draw_t *) dd; d->append_xy_pdf (x, y); d->path.append_str (" l\n"); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->append_xy_pdf (x, y); d->path.append_str (" l\n"); + d->end_path_command (before); +} /* No quadratic_to — null fallback auto-promotes to cubic. */ static void hb_vector_draw_pdf_cubic_to (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, float c1x, float c1y, float c2x, float c2y, float x, float y, void *) -{ auto *d = (hb_vector_draw_t *) dd; d->append_xy_pdf (c1x, c1y); d->path.append_c (' '); d->append_xy_pdf (c2x, c2y); d->path.append_c (' '); d->append_xy_pdf (x, y); d->path.append_str (" c\n"); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->append_xy_pdf (c1x, c1y); d->path.append_c (' '); d->append_xy_pdf (c2x, c2y); d->path.append_c (' '); d->append_xy_pdf (x, y); d->path.append_str (" c\n"); + d->end_path_command (before); +} static void hb_vector_draw_pdf_close_path (hb_draw_funcs_t *, void *dd, hb_draw_state_t *, void *) -{ ((hb_vector_draw_t *) dd)->path.append_str ("h\n"); } +{ + auto *d = (hb_vector_draw_t *) dd; + unsigned before; + if (unlikely (!d->begin_path_command (&before))) return; + d->path.append_str ("h\n"); + d->end_path_command (before); +} /* ---- Lazy loaders ---- */ @@ -895,6 +949,7 @@ hb_vector_draw_clear (hb_vector_draw_t *draw) draw->defs.clear (); draw->body.clear (); draw->path.clear (); + draw->work_left = HB_VECTOR_MAX_DRAW_WORK; } /** diff --git a/src/hb-vector-draw.hh b/src/hb-vector-draw.hh index 0158cfd41..6f21a32bd 100644 --- a/src/hb-vector-draw.hh +++ b/src/hb-vector-draw.hh @@ -54,6 +54,27 @@ struct hb_vector_draw_t hb_vector_buf_t pdf_extgstate_dict; unsigned pdf_extgstate_count = 0; + /* Cumulative output budget for the current draw session; reset by + * hb_vector_draw_clear(). Charge complete path commands so budget + * exhaustion cannot leave a syntactically partial command. */ + int64_t work_left = HB_VECTOR_MAX_DRAW_WORK; + + bool begin_path_command (unsigned *before) + { + if (unlikely (work_left <= 0 || path.in_error ())) + return false; + *before = path.length; + return true; + } + + void end_path_command (unsigned before) + { + if (unlikely (path.in_error ())) + work_left = 0; + else + work_left -= path.length - before; + } + void set_precision (unsigned p) { p = hb_min (p, 12u); diff --git a/src/hb-vector-paint-pdf.cc b/src/hb-vector-paint-pdf.cc index e484a3383..69a522c86 100644 --- a/src/hb-vector-paint-pdf.cc +++ b/src/hb-vector-paint-pdf.cc @@ -345,11 +345,11 @@ hb_pdf_emit_glyph_path (hb_vector_paint_t *paint, if (likely (paint->work_left > 0)) { hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (), - paint->x_scale_factor, paint->y_scale_factor}; + paint->x_scale_factor, paint->y_scale_factor, + &paint->work_left}; hb_font_draw_glyph (font, glyph, hb_vector_pdf_path_draw_funcs_get (), &sink); - paint->work_left -= paint->path.length; } buf->append_len (paint->path.arrayZ, paint->path.length); paint->path.clear (); @@ -511,7 +511,8 @@ hb_pdf_paint_push_clip_path_start (hb_paint_funcs_t *, * scale_factor so they land in output space. */ paint->clip_path_sink = {&body, paint->get_precision (), paint->x_scale_factor, - paint->y_scale_factor}; + paint->y_scale_factor, + &paint->work_left}; *draw_data = &paint->clip_path_sink; return hb_vector_pdf_path_draw_funcs_get (); } diff --git a/src/hb-vector-paint-svg.cc b/src/hb-vector-paint-svg.cc index c41e4e789..e8b9f4456 100644 --- a/src/hb-vector-paint-svg.cc +++ b/src/hb-vector-paint-svg.cc @@ -443,9 +443,9 @@ hb_vector_paint_fill_glyph (hb_paint_funcs_t *, if (likely (paint->work_left > 0)) { hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (), - paint->x_scale_factor, paint->y_scale_factor}; + paint->x_scale_factor, paint->y_scale_factor, + &paint->work_left}; hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); - paint->work_left -= paint->path.length; } auto &body = paint->current_body (); @@ -476,9 +476,9 @@ hb_vector_paint_push_clip_glyph (hb_paint_funcs_t *, if (likely (paint->work_left > 0)) { hb_vector_path_sink_t sink = {&paint->path, paint->get_precision (), - paint->x_scale_factor, paint->y_scale_factor}; + paint->x_scale_factor, paint->y_scale_factor, + &paint->work_left}; hb_font_draw_glyph (font, glyph, hb_vector_svg_path_draw_funcs_get (), &sink); - paint->work_left -= paint->path.length; } unsigned def_id = paint->path_def_count++; @@ -557,7 +557,8 @@ hb_vector_paint_push_clip_path_start (hb_paint_funcs_t *, paint->path.clear (); paint->clip_path_sink = {&paint->path, paint->get_precision (), paint->x_scale_factor, - paint->y_scale_factor}; + paint->y_scale_factor, + &paint->work_left}; *draw_data = &paint->clip_path_sink; return hb_vector_svg_path_draw_funcs_get (); } diff --git a/src/hb-vector-paint.hh b/src/hb-vector-paint.hh index 45da57ccb..fecb4a0a3 100644 --- a/src/hb-vector-paint.hh +++ b/src/hb-vector-paint.hh @@ -72,7 +72,7 @@ struct hb_vector_paint_t * with the paint-graph traversal limits of the font tables driving * us (e.g. COLR). */ int64_t work_left = HB_VECTOR_MAX_PAINT_WORK; - hb_vector_path_sink_t clip_path_sink = {nullptr, 0, 1.f, 1.f}; + hb_vector_path_sink_t clip_path_sink = {nullptr, 0, 1.f, 1.f, nullptr}; unsigned gradient_counter = 0; unsigned color_glyph_depth = 0; unsigned path_def_count = 0; diff --git a/src/hb-vector-path.cc b/src/hb-vector-path.cc index e63726873..db66e12a7 100644 --- a/src/hb-vector-path.cc +++ b/src/hb-vector-path.cc @@ -48,8 +48,11 @@ hb_vector_svg_path_move_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t float to_x, float to_y, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; s->path->append_c ('M'); hb_vector_svg_path_append_xy (s, to_x, to_y); + s->end_command (before); } static void @@ -57,8 +60,11 @@ hb_vector_svg_path_line_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t float to_x, float to_y, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; s->path->append_c ('L'); hb_vector_svg_path_append_xy (s, to_x, to_y); + s->end_command (before); } static void @@ -66,10 +72,13 @@ hb_vector_svg_path_quadratic_to (hb_draw_funcs_t *, void *draw_data, hb_draw_sta float cx, float cy, float to_x, float to_y, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; s->path->append_c ('Q'); hb_vector_svg_path_append_xy (s, cx, cy); s->path->append_c (' '); hb_vector_svg_path_append_xy (s, to_x, to_y); + s->end_command (before); } static void @@ -78,19 +87,25 @@ hb_vector_svg_path_cubic_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t float to_x, float to_y, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; s->path->append_c ('C'); hb_vector_svg_path_append_xy (s, c1x, c1y); s->path->append_c (' '); hb_vector_svg_path_append_xy (s, c2x, c2y); s->path->append_c (' '); hb_vector_svg_path_append_xy (s, to_x, to_y); + s->end_command (before); } static void hb_vector_svg_path_close_path (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; s->path->append_c ('Z'); + s->end_command (before); } @@ -109,8 +124,11 @@ hb_vector_pdf_path_move_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t float to_x, float to_y, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; hb_vector_pdf_path_append_xy (s, to_x, to_y); s->path->append_str (" m\n"); + s->end_command (before); } static void @@ -118,8 +136,11 @@ hb_vector_pdf_path_line_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t float to_x, float to_y, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; hb_vector_pdf_path_append_xy (s, to_x, to_y); s->path->append_str (" l\n"); + s->end_command (before); } /* No quadratic_to — the null fallback auto-promotes to cubic. */ @@ -130,19 +151,25 @@ hb_vector_pdf_path_cubic_to (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t float to_x, float to_y, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; hb_vector_pdf_path_append_xy (s, c1x, c1y); s->path->append_c (' '); hb_vector_pdf_path_append_xy (s, c2x, c2y); s->path->append_c (' '); hb_vector_pdf_path_append_xy (s, to_x, to_y); s->path->append_str (" c\n"); + s->end_command (before); } static void hb_vector_pdf_path_close_path (hb_draw_funcs_t *, void *draw_data, hb_draw_state_t *, void *) { auto *s = (hb_vector_path_sink_t *) draw_data; + unsigned before; + if (unlikely (!s->begin_command (&before))) return; s->path->append_str ("h\n"); + s->end_command (before); } diff --git a/src/hb-vector-path.hh b/src/hb-vector-path.hh index 9936428d3..8618a5b82 100644 --- a/src/hb-vector-path.hh +++ b/src/hb-vector-path.hh @@ -44,6 +44,25 @@ struct hb_vector_path_sink_t unsigned precision; float x_scale; float y_scale; + int64_t *work_left; + + bool begin_command (unsigned *before) + { + if (unlikely (path->in_error () || + (work_left && *work_left <= 0))) + return false; + *before = path->length; + return true; + } + + void end_command (unsigned before) + { + if (!work_left) return; + if (unlikely (path->in_error ())) + *work_left = 0; + else + *work_left -= path->length - before; + } }; HB_INTERNAL hb_draw_funcs_t *