[CFF] Bound charstring operators by the live work budget

The per-operator budget spend added a check to the charstring
interpreter's innermost loop.  The loop already counts operators through
max_ops, so cap max_ops by the caller's remaining budget and charge the
consumed operators back after the loop.  A single counter then enforces
both the structural HB_CFF_MAX_OPS limit and the live work budget, with
no budget check in the hot loop, and the operator count can never
overshoot the remaining budget.

This bounds the real quantity a COLR or VARC driver multiplies: executed
operator count, which a small charstring can drive up to HB_CFF_MAX_OPS
via subroutine reuse regardless of its byte length -- so charging the
charstring's length instead would badly undercount.  CFF2 blend still
charges its proportional work in place.

Tests: meson test -C build (275/275 passed); the CFF/COLR gpu-fuzzer
regression seed completes in 0.45s.

Assisted-by: Claude <noreply@anthropic.com>
This commit is contained in:
Behdad Esfahbod committed 2026-08-27 16:34:19 -06:00
1 parent 148fcb3c79
commit 83eafa8ca6
1 file changed
+29 -30
+29 -30
View File
@@ -264,19 +264,11 @@ struct cs_opset_t : opset_t<ARG>
break;
case OpCode_callsubr:
/* The interpret loop already charged one unit for this operator;
* charge the remaining call weight before entering the subroutine. */
if (unlikely (!env.spend_budget (HB_BUDGET_8 - HB_BUDGET_1)))
env.set_error ();
else
env.call_subr (env.localSubrs, CSType_LocalSubr);
env.call_subr (env.localSubrs, CSType_LocalSubr);
break;
case OpCode_callgsubr:
if (unlikely (!env.spend_budget (HB_BUDGET_8 - HB_BUDGET_1)))
env.set_error ();
else
env.call_subr (env.globalSubrs, CSType_GlobalSubr);
env.call_subr (env.globalSubrs, CSType_GlobalSubr);
break;
case OpCode_hstem:
@@ -897,30 +889,37 @@ struct cs_interpreter_t : interpreter_t<ENV>
SUPER::env.set_endchar (false);
SUPER::env.set_budget (budget);
bool ret = true;
unsigned max_ops = HB_CFF_MAX_OPS;
for (;;) {
op_code_t op = SUPER::env.fetch_op ();
/* Charge one unit per operator here; call_subr charges the extra
* weight in its own handler so this hot loop stays branch-free. */
if (unlikely (!SUPER::env.spend_budget (HB_BUDGET_1)))
{
SUPER::env.set_error ();
ret = false;
break;
}
/* Cap the operator count by the caller's remaining budget so the single
* max_ops counter enforces both the structural limit and the live work
* budget, with no budget check in the hot loop. The consumed operators
* are charged back after the loop, and since the cap never exceeds the
* remaining budget the charstring cannot overshoot it. CFF2 blend
* charges its proportional work separately, in place. */
int64_t remaining = budget ? *budget : (int64_t) HB_CFF_MAX_OPS;
unsigned max_ops = (unsigned) hb_clamp (remaining, (int64_t) 0, (int64_t) HB_CFF_MAX_OPS);
unsigned start_ops = max_ops;
OPSET::process_op (op, SUPER::env, param);
if (unlikely (SUPER::env.in_error () || !--max_ops))
{
SUPER::env.set_error ();
ret = false;
break;
bool ret = true;
if (likely (max_ops))
for (;;) {
OPSET::process_op (SUPER::env.fetch_op (), SUPER::env, param);
if (unlikely (SUPER::env.in_error () || !--max_ops))
{
SUPER::env.set_error ();
ret = false;
break;
}
if (SUPER::env.is_endchar ())
break;
}
if (SUPER::env.is_endchar ())
break;
else
{
SUPER::env.set_error ();
ret = false;
}
if (budget) *budget -= (int64_t) (start_ops - max_ops);
return ret;
}