Commit Graph
31 Commits
Author SHA1 Message Date
Behdad EsfahbodandClaude Opus 4.8 420bb6d0ba [subset/cff] Bound hint-drop/closure walks and share the budget across the whole subset (#6638)
* [subset] Bound the CFF hint-dropping and closure walks

drop_hints_in_str()/drop_hints_in_subr() and the closure walk
(collect_subr_refs_in_str/_in_subr) recursively walk the subroutine
calls recorded for each glyph with no depth limit and no work budget.
The records need not match what the interpreter executed: a string is
parsed on its first run only, global subr records are shared across
Font DICTs whose local subrs differ, and endchar does not end a walk.
So the recorded call graph can nest deeper than kMaxCallLimit or be
cyclic, and a binary-tree subr structure makes the hint-dropping walk
expand exponentially.  A 360-byte CID font crashed the subsetter with
a stack overflow under --no-hinting (or hb_subset_preprocess), and a
~1 KB font ran unboundedly.  PR #6428 rejected stack-supplied call
operands but added no depth, cycle or work limit to these walks, so
these fonts were still affected.

Hold both walks to the interpreter's call-depth limit (kMaxCallLimit),
which stops the cycle and the long chain before the stack runs out, and
charge the hint-dropping walk to the subsetting pass budget introduced
for the shared-subroutine work, so the exponential expansion is capped
too.  A walk that follows the interpreter's path stays within both, so
only fonts whose records diverge from their execution are affected;
over either limit the subset fails, as other CFF planning failures do.

Adds test_subset_cff1_subr_loop, which subsets the 360-byte cycle font
with --no-hinting and only requires that hb_subset_or_fail() returns.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-BCC7WHCA.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [subset] Stop CFF subr walks at the call-depth limit instead of failing

The hint-dropping and subr-closure walks enforce kMaxCallLimit
themselves, because the recorded call graph can nest deeper than the
interpreter allows or be cyclic.  Hitting the limit failed the whole
subset, and the hint-dropping walk signalled it by writing -1 into the
shared work budget, conflating "too deep" with "out of budget" and
clobbering the budget for later passes.

Make both walks best-effort at the limit: the closure walk keeps the
subr it reached but does not walk into it, and the hint-dropping walk
does not walk into the subr and keeps the call and its hints.  Only
running out of the work budget fails the subset now, so
drop_hints_param_t no longer needs set_error() and the budget is only
ever touched by hb_budget_spend().  Walks that follow the
interpreter's path never reach the limit, so real fonts are
unaffected; for fonts whose records diverge the output may keep a few
hints or call a subr whose callees were not retained, but no longer
fails.

test_subset_cff1_subr_loop now also asserts that the subset succeeds;
it failed before this change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [subset] Share one CFF charstring budget across a whole subset

Each CFF / CFF2 charstring pass of the subsetter (seac closure, CFF2
extents for instancing, CFF2 usage and instancing passes, subroutine
subsetting with hint dropping, and flattening) created its own budget
with CFF::get_subset_budget(), so one subset could spend that budget
several times over, and the two CFF2 instancing passes reset it
between them.

Keep the budget on hb_subset_plan_t instead: cff_budget() sizes it on
first use from the table, as before, and every pass draws on the same
counter, so the total work of one subset is bounded once.  Retries of
a table with a larger serialize buffer draw on it too.  cff1 depend()
is not part of a subset plan and keeps its own budget per call.

Real fonts use about 1.5 operations per table byte per pass, against
a budget of 32 per byte (at least 2^24).  Tested: full meson test and
the subset suite; hb-subset on SourceSerif4Variable-Roman.otf (CFF2),
Roboto-Regular.otf (CFF), NotoSansCJKhk-VF.otf (30 MB CFF2) with
--gids='*' and --unicodes='*', with and without --no-hinting,
--desubroutinize and partial / full instancing, and on every .otf in
/Library/Fonts with --gids='*', all succeed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* [subset] Detect CFF charstring budget exhaustion at zero, not below

The shared CFF charstring budget is only ever spent through the
cs_interpreter_t interpret() cap, which clamps each glyph's operator
count to the remaining budget and charges back exactly what it
consumed.  By construction it never overshoots, so an exhausted
budget lands at exactly 0 rather than going negative.

The exhaustion checks therefore compare budget <= 0 (cff1 depend()'s
seac walk) and budget > 0 (the subset planner's seac-closure and CFF2
get_extents verdicts), not < 0 / >= 0, which that path can never
reach.  With < 0, cff1 depend() never failed, so
hb_subset_depend_from_face_or_fail() returned a result for an
adversarial font that exhausts the budget even though the equivalent
subset fails; that is the test-subset-cff1 shared-subrs case on CI
builds with -Dexperimental_api=true.  Document this at each check.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-11 18:33:22 -06:00
Behdad EsfahbodandClaude Opus 4.8 a6beb11493 [subset/cff] Bound parsed subrs and share one charstring budget per pass (#6636)
* [subset/cff] Bound parsed subroutines by the table size before allocating

subr_subsetter_t::subset() resizes one parsed_cs_str_vec_t per Font DICT
to that DICT's localSubrs->count, assuming the sum is bounded by the
table.  It is not: any number of Font DICTs may point at the same
Private DICT and Subrs INDEX, so a 500 KB font with 16 Font DICTs all
sharing one 1000-subr INDEX cost about 1.6 GiB (measured: control 10
MiB, shared 1651 MiB; 2.8 GiB with hb_subset_preprocess).

Sum the global plus per-Font-DICT local subroutine counts in 64-bit
before any per-Font-DICT allocation, and fail the subset when the
records would exceed 256 bytes per byte of the source table, the same
ratio _hb_subset_table_try() uses to stop growing the output buffer.
A font whose Font DICTs do not share their Subrs needs at least one
table byte per subroutine, so it stays far below the limit; every
CFF/CFF2 font in the test corpus is at most a couple of bytes per table
byte.  Over the limit the subset fails, as any CFF planning failure has
since dde7da182e; the shared font now peaks at 7 MiB instead of 1.6 GiB.

The check sits before the allocations in the non-accelerator branch, so
it also covers the hb_subset_preprocess path and both CFF1 and CFF2
instantiations of the template; the --desubroutinize flattener path
does not allocate per subroutine and is unaffected.

Adds test_subset_cff1_shared_local_subrs: a 16-Font-DICT CID font with
1000 local subrs subsets when the subrs are unshared and fails when
every Font DICT shares them.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-HPFG1IAV.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

* [subset] Share one CFF charstring work budget across a subsetting pass

Every per-glyph charstring loop in the subsetter (subr_subsetter_t::
subset, subr_flattener_t::flatten, the two CFF2 partial-instancing
passes, the CFF2 instance-metrics loop, and the CFF1 seac closure in
the plan and in the depend builder) created a fresh interpreter bounded
only by HB_CFF_MAX_OPS per charstring.  Glyphs can share subroutines,
so a font where every glyph makes one cheap call into a deeply nested
shared subr cost num_glyphs x ~200,000 ops: a 121 KB, 30,000-glyph font
took 33 s to subset (control 0.05 s).

Give each pass one cumulative counter, max(HB_BUDGET_SUBSET_CFF = 2^24,
32 ops per table byte), threaded through cs_interpreter_t::interpret()'s
existing budget argument exactly as the outline and extents paths do.
When it runs out, interpret() fails as it does for a single over-limit
charstring, so the CFF/CFF2 table fails to subset (the subset fails, as
any CFF planning failure has since dde7da182e); the seac closure fails
the plan and the depend builder fails.  The 32-ops-per-byte allowance
was measured against the in-tree corpus, where real fonts interpret at
most a few operations per CFF byte, so they stay far below it and
produce byte-identical output; the attacker font now fails in 0.17 s.

Adds test_subset_cff1_shared_subrs: four shared-subr glyphs subset
fine, while a hundred of them exhaust the budget and the subset (and
the dependency builder) fail.

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-Z5NLRFA6.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-11 16:58:48 -06:00
Behdad EsfahbodandClaude Opus 4.8 a61e59716f [subset/cff1] Remap Encoding supplement SIDs through sidmap
cff1_subset_plan renumbers every custom SID it emits through sidmap
(charset via plan_subset_charset, Top DICT names via reassignSIDs, FD
names via sidmap[]), and the output String INDEX is serialized from
that map.  plan_subset_encoding was the one emitter that pushed the
source font's SID unchanged into subset_enc_supp_codes, so once an
earlier custom-named glyph was dropped a supplement code named the
wrong glyph, or a string past the end of the renumbered String INDEX.

Look the SID up in sidmap where the supplement entry is recorded.  The
source SID is still used for get_supplement_codes(), which searches the
source Encoding.  This is safe on every path that reaches it:
subset_encoding implies a non-CID font, so plan_subset_charset has
already added each visited glyph's name (sidmap.add is idempotent), and
when the charset is predefined and plan_subset_charset is skipped all
glyph SIDs are standard strings, which sidmap[] returns unchanged.

Adds test_subset_cff1_encoding_supplement with a generated CFF font
(custom names, format-0 Encoding with supplement 98 -> 'second') and
its expected B,C subset; the test fails before this change.

Reported by Anthropic's OSS Scanner as ordinary bug ANT-2026-JORVXUQZ.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-11-09 13:18:05 -06:00
Behdad Esfahbod eb9dc91893 [subset] Bound CFF1 encoding lookups by the glyph count (#6543)
An explicit glyph map can create empty output glyphs beyond the source
font's glyph count. When preserving codes for gaps in the mapping, the
subsetter queries those nonexistent source glyphs. Excess entries in a
malformed custom encoding can then give the new empty glyphs codes.

Return CFF_UNDEF_CODE for glyphs outside the source font before reading
the encoding. Add format 0 and format 1 regression cases comparing
ordinary and explicitly remapped subsets against bounded encodings,
with a generator for the small synthetic fonts.

Both new cases fail before the fix. All 275 tests pass with
meson test -C build --print-errorlogs --num-processes 12.

Related: https://github.com/googlefonts/fontations/pull/2398
Assisted-by: Codex
2026-11-01 14:14:09 -07:00
Behdad Esfahbod e8f8ee612f [subset] Ignore seac component lookups in CID-keyed CFF fonts (#6506)
CID-keyed CFF charsets contain CIDs, not the standard SIDs needed by seac.
Interpreting the invalid operator can therefore retain unrelated glyphs
when their CIDs collide with the component SIDs from StandardEncoding.

Reject CID-keyed fonts in get_seac_components so both subset closure and
dependency collection skip these invalid lookups. Add a synthetic CID
font with colliding CIDs and check that only .notdef and the requested
glyph enter the subset plan.

The regression fails before the fix with four retained glyphs instead of
two. All 294 Meson tests pass, including the subset and API suites.

Assisted-by: OpenAI Codex
2026-10-30 16:10:52 -06:00
Behdad Esfahbod 4aebec5702 [subset/cff1] Bound preprocessing map lookups for output gaps (#6481)
A custom glyph mapping can leave unassigned output glyph IDs beyond
the source glyph count. The preprocessing glyph-to-SID map only covers
source glyphs, so use the existing charset lookup for those gaps and
skip the map-based range shortcut there.

Compare plain and preprocessed subsets of existing name-keyed and
CID-keyed fonts with output gaps. The new regression fails under ASan
before the fix. The complete ASan/UBSan Meson suite passes afterward
(293 tests, including 23 CFF1 API subtests).

Discovered by Anthropic's OSS Scanner, as vulnerability ANT-2026-42ZOI3TR.

Assisted-by: OpenAI Codex
2026-10-29 20:57:13 -06:00
Behdad Esfahbod 1e6a7f38ee [cff] Reject INDEX item counts that do not fit (#6465)
CFF1 INDEX counts are 16-bit. Serializing a larger iterator truncated
its count before reserving the offset array, then wrote offsets for
every item. Check the count before allocating or writing the offsets.

Test the CFF1 boundary and the corresponding valid CFF2 counts, and
reject a subset glyph mapping that overflows the CharStrings INDEX.
Both regressions fail before this change. The full Meson suite passes
(293 tests), including the subset and API suites.

Discovered by Anthropic's OSS Scanner, as vulnerability
ANT-2026-8ROS1VQ9.

Assisted-by: OpenAI Codex
2026-10-29 18:31:46 -06:00
Behdad Esfahbod a6214eefde [subset] Propagate CFF1 encoding range overflow errors
Returning false without setting a serializer error treats the CFF
subset as empty and silently drops its outlines. Record an error when
format 1 needs more than 255 encoding ranges so subsetting fails.

Reuse the many-codes fixture to check that 255 ranges still succeed
and 256 ranges fail. The regression fails before the fix.

Testing: meson test -C build (273 passed).

Assisted-by: OpenAI Codex
2026-10-29 15:35:51 -07:00
Beleniuc Ilie 4868fd314f [subset] Use CFF1 Encoding format 1 for more than 255 codes (#6460)
A format 1 Encoding can give codes to more than 255 glyphs, since
codes may repeat across ranges. When the subset keeps more than 255
of those glyphs, as with retain-gids, plan_subset_encoding() hit
assert (subset_enc_num_codes <= 0xFF).

Choose format 0 only when it can hold the codes, and fail cleanly in
the format 1 serializer instead of truncating nRanges past 255.

Add a test font whose format 1 Encoding covers 768 glyphs, and the
reporter's font to the fuzzing corpus.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/6031
Assisted-by: Claude
Assisted-by: OpenAI Codex
2026-10-29 16:35:22 -06:00
Behdad Esfahbod c305720f01 [subset] Handle seac operands returned by a subroutine (#6463)
A subroutine can push all four seac operands, return, and leave the
glyph's own endchar to consume them. Treat callsubr and callgsubr
immediately before endchar as possible seac suffixes even when no
subroutine contains endchar.

Add a synthetic CFF1 regression font for the operand-only subroutine
case.

Assisted-by: OpenAI Codex
2026-10-29 16:25:35 -06:00
Behdad Esfahbod 2c0df50ce1 [subset] Make CFF1 seac precheck conservative (#6458)
A subroutine can terminate a glyph with endchar, and dotsection preserves
live operands. Account for both cases when deciding whether a CFF1
charstring can form a seac composite.

Cache whether any subroutine may contain endchar so ordinary fonts keep
the fast trailing-byte check. Skip component closure when all glyphs are
already retained, except when computing reusable accelerator metadata.

Add regressions for endchar in a local subroutine and operands preserved
across dotsection.

Assisted-by: OpenAI Codex
2026-10-27 18:05:47 -06:00
Behdad Esfahbod dde7da182e [subset] Reject CFF calls without an immediate subroutine number
Retained subroutine remapping assumes the number preceding a call is
in the same program. Caller-supplied targets violate that assumption
and can silently change glyph outlines when unused subroutines are
removed. Enforce the existing support boundary in CFF1 and CFF2.

Report plan failures as serialization errors, rather than silently
dropping the CFF table. Explicit desubroutinization still handles the
regression fonts and preserves both outlines.

Validation: full Meson suite, 272 passed and one skipped.

Assisted-by: OpenAI Codex
2026-10-22 17:06:10 +03:00
Khaled Hosny f3dea1e602 [subset] Add HB_SUBSET_FLAGS_CFF_IDENTITY_CHARSET
When set and subsetting a CID-keyed CFF font, the output charset is
written as identity: CID = new GID.
2026-07-27 22:12:16 +03:00
Garret Rieger c6884377ec [subset] Add HB_SUBSET_FLAGS_IFTB_REQUIREMENTS flag.
When enabled the output subset conforms to the requirements for a base font to be patched by IFTB patches. IFTB is a proposed incremental font transfer patch. This new flag is set as experimental. Currently, setting this flag causes the output subset to also use long offsets for outline data in loca/glyf, gvar, CFF, and CFF2.

This change is a version of https://github.com/skef/harfbuzz/commit/3ae2fe2084d96f61d8ad1bbe42a6ef4e62ec830a rebased onto head w/ tests added.
2023-11-06 16:49:09 -07:00
Garret Rieger f9d8e4a976 [subset] switch ..._set_flags to not take a mask. 2021-07-29 18:30:27 -07:00
Garret Rieger 3d534b146c [subset] convert subset input flags into bit flags.
Store the flags in a bit set. Updates the public api to work with the bit set directly.
2021-07-29 18:02:34 -07:00
Garret Rieger 942636ae13 [subset] Remove hb_subset_input_get/set_retain_gids. 2021-07-22 12:12:36 -07:00
Garret Rieger 77b4a1cd96 [subset] Remove hb_subset_input_get/set_desubroutinize. 2021-07-22 12:12:36 -07:00
Garret Rieger 8bf5d4d4f7 [subset] Remove hb_subset_input_get/set_drop_hints. 2021-07-22 12:12:36 -07:00
Michiharu Ariza f2908b4d8f Implement subset --regain-gids option with CFF1/2
along with api tests & expected results
2019-01-31 14:16:37 -08:00
fanc999 3ee4ea9456 Fix build on older Visual Studio versions (#1499)
* src/hb-cff-interp-dict-common.hh: Use ull for unsigned int64_t

The llu suffix does not work for older Visual Studio versions
(pre-2013), but ull works for all the compilers that we attempt to
support.

* test/api: Fix build on pre-C99 compilers

Ensure variables are declared at the top of the block.

* src/hb-dsalgs.hh: Add specialization for hb_is_signed<> for __int8

Pre-Visual Studio 2010 does not consider __int8 (which is typedef'ed to
int8_t) to be equivilant to signed char, so the compiler cannot find the
corresponding hb_is_signed<> specialization that is needed.

The interesting thing is unsigned __int8 is considered to be equivilant
to unsigned char, so as the other types (short, int, long) that we look
for here, so only the specialization for __int8 is added here.

This will fix builds on Visual Studio 2008 at least.
2018-12-19 22:26:54 -05:00
Michiharu Ariza b67a7c731f drop dotsection as hint along with test case 2018-11-16 12:28:24 -08:00
Michiharu Ariza 3787c07856 Implemented seac for extents & subset along with API tests 2018-11-14 13:38:03 -08:00
Michiharu Ariza 0dfa584cb4 changed Adobe company name 2018-11-12 08:47:07 -08:00
Michiharu Ariza 1bc710a8c9 added CFF1 desubr api test cases & bug fixes 2018-11-02 15:28:01 -07:00
Michiharu Ariza ba055a8d53 fix build attempt ^3 2018-10-22 16:20:04 -07:00
Michiharu Ariza 3d3df5575c fix build attempt 2018-10-22 15:23:39 -07:00
Michiharu Ariza 9228db06f4 added CFF1 Expert Encoding subset test & bug fixes 2018-10-13 17:25:09 -07:00
Michiharu Ariza a6b6faf2b8 Replaced CFF1 J subset font for better coverage
in api test-ot-extents-cff
2018-10-11 10:53:44 -07:00
Michiharu Ariza dac70f252a Added CFF1 J font subset to api test
Also fixed FDSelect3_4::get_fd
Fixed test_subset_cff1_strip_hints (wrong font names!)
2018-09-26 16:30:26 -07:00
Michiharu Ariza 258b456566 Added CFF1 & CFF2 api tests 2018-09-24 16:50:47 -07:00