From ea668aaa34e8325e6a3928032678c47c805ef737 Mon Sep 17 00:00:00 2001 From: Skef Iterum Date: Fri, 22 May 2026 11:39:36 -0700 Subject: [PATCH] [depend] Detect GSUB self-feeding chains in parity checker MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the parity checker finds unexpected over-approximation (depend closure has extra glyphs without flagged edges), check whether the font has self-feeding GSUB lookups — lookups whose output coverage overlaps their input coverage. These create transitive chains that the depend closure follows to completion while the subsetter's iteration-capped closure truncates. No shaper re-applies a lookup to its own output within a single feature application, so the extra glyphs are harmless. The detection traces extra glyphs through self-feeding feature edges from the subset closure; if all are explained, the test passes. Detection runs only on the failure path and is O(edges), so well-formed fonts (which don't have self-feeding lookups) pay no cost. Also document MATH single-pass closure and GSUB self-feeding chains in depend-for-closure.md and depend-implementation.md. Assisted-by: Claude Opus 4.6 --- docs/depend-for-closure.md | 36 +++++++ docs/depend-implementation.md | 21 +++- test/fuzzing/hb-depend-closure-parity.cc | 124 ++++++++++++++++++++--- 3 files changed, 161 insertions(+), 20 deletions(-) diff --git a/docs/depend-for-closure.md b/docs/depend-for-closure.md index 579789651..e971f1929 100644 --- a/docs/depend-for-closure.md +++ b/docs/depend-for-closure.md @@ -85,6 +85,42 @@ if (!hb_set_has(glyphs, entry.dependent)) { } ``` +## Table-Specific Closure Behavior + +### MATH Closure + +MATH edges should be followed in a **single pass**: follow MATH edges for glyphs +that are in the set before the MATH stage, but do NOT follow MATH edges for +glyphs that the MATH stage itself added. This matches the subsetter's behavior, +which retains MATH-closure glyphs (their outlines are needed for drawing assembly +parts) but strips their own MATH constructions from the subset. + +Math layout engines do not recursively stretch assembly parts, so the +second-hop constructions are not functionally needed. + +### GSUB Self-Feeding Chains + +The depend graph captures all GSUB substitution edges. When computing transitive +closure, a lookup's output glyph may itself be in the same lookup's coverage, +creating a chain: A → B → C → ... through the same lookup. No shaper actually +re-applies a lookup to its own output within a single feature application, so +these chains are over-approximation relative to real shaping behavior. + +The subsetter's iterative closure (`HB_CLOSURE_MAX_STAGES`) truncates these +chains. The depend closure, following edges to a fixed point, may go further. + +To detect this condition (only relevant when unexpected over-approximation is +found without flagged edges): + +1. For each active GSUB feature, collect source glyphs and dependent glyphs +2. If those sets intersect for any feature, the font has **self-feeding potential** +3. Trace extra glyphs through self-feeding feature edges from the subset closure +4. If all extra glyphs are reachable through self-feeding chains, the + over-approximation is a known artifact of self-feeding, not a bug + +This detection only runs on the failure path and is O(edges), so real fonts pay +no cost. In practice, well-formed fonts do not have self-feeding lookups. + ## Edge Flags and Over-Approximation Detection Each dependency edge has an optional `flags` field that indicates potential over-approximation: diff --git a/docs/depend-implementation.md b/docs/depend-implementation.md index dcb94afc5..6380817bf 100644 --- a/docs/depend-implementation.md +++ b/docs/depend-implementation.md @@ -84,11 +84,24 @@ if (nested_contextual) { Closure implementations should track whether any flagged edges contributed to the result. If so, any extra glyphs compared to subset's closure are "expected" over-approximation. -### Graph Completeness +### Graph Completeness and Known Divergences -The depend graph captures all edges that exist in the font structure. Recent testing -with proper ligature_set, context_set, and feature filtering has not generated -over-approximation cases when computing closures. +The depend graph captures all edges that exist in the font structure. With proper +ligature_set, context_set, and feature filtering, closures computed via the depend +graph closely match the subsetter's closure. Two known sources of divergence remain: + +**MATH closure (single-pass):** The subsetter retains MATH-closure glyphs but strips +their own MATH constructions from the subset. The depend graph records all MATH edges, +including from closure-only glyphs. Closure implementations should follow MATH edges +in a single pass (not transitively) to match the subsetter. + +**GSUB self-feeding chains:** A lookup whose output coverage overlaps its input coverage +creates a transitive chain when following depend edges. The subsetter's iteration-capped +closure truncates these; the depend closure follows them to completion. This is harmless +over-approximation — no shaper re-applies a lookup to its own output within a single +feature application. The parity checker detects this condition post-hoc by checking +whether the font has self-feeding features and tracing extra glyphs through those +features' edges. ### Using the Graph for Closure Computation diff --git a/test/fuzzing/hb-depend-closure-parity.cc b/test/fuzzing/hb-depend-closure-parity.cc index 1cdd067e7..a12a7d14e 100644 --- a/test/fuzzing/hb-depend-closure-parity.cc +++ b/test/fuzzing/hb-depend-closure-parity.cc @@ -813,26 +813,118 @@ compare_closures (hb_face_t *face, hb_subset_depend_t *depend, } else { - /* Unexpected: no flagged edge, but depend still has extra glyphs */ - if (_hb_depend_fuzzer_report_under_approximation || - _hb_depend_fuzzer_report_over_approximation || - _hb_depend_fuzzer_verbose) + /* Unexpected: no flagged edge, but depend still has extra glyphs. + * + * Before asserting, check whether this is a known self-feeding chain: + * a GSUB lookup whose output coverage overlaps its input coverage, + * causing the transitive depend closure to follow the chain further + * than the subsetter's iteration-capped closure. This is harmless + * over-approximation — no shaper re-applies a lookup to its own + * output within a single feature application. */ + bool explained_by_self_feeding = false; + if (!skip_gsub && active_features) { - fprintf (stderr, "Test 0x%016llx: UNEXPECTED OVER-APPROX - Depend found %u extra glyphs WITHOUT hitting flagged edge:", - (unsigned long long) seed, hb_set_get_population (in_depend_not_subset)); - hb_codepoint_t gid = HB_SET_VALUE_INVALID; - unsigned count = 0; - while (hb_set_next (in_depend_not_subset, &gid) && count < 20) + /* Check each active feature for self-feeding: a feature where any + * dependent glyph also appears as a source glyph. */ + hb_set_t *self_feeding_features = hb_set_create (); + unsigned num_glyphs = hb_face_get_glyph_count (face); + hb_codepoint_t feat_tag = HB_SET_VALUE_INVALID; + while (hb_set_next (active_features, &feat_tag)) { - fprintf (stderr, " %u", gid); - count++; + hb_set_t *sources = hb_set_create (); + hb_set_t *dependents = hb_set_create (); + for (hb_codepoint_t gid = 0; gid < num_glyphs; gid++) + { + unsigned total = hb_subset_depend_lookup_glyph (depend, gid, 0, nullptr, nullptr); + for (unsigned j = 0; j < total; j++) + { + unsigned cnt = 1; + hb_subset_depend_entry_t entry; + hb_subset_depend_lookup_glyph (depend, gid, j, &cnt, &entry); + if (entry.table_tag != HB_OT_TAG_GSUB) continue; + if (entry.layout_tag != feat_tag) continue; + hb_set_add (sources, gid); + hb_set_add (dependents, entry.dependent); + } + } + hb_set_intersect (sources, dependents); + if (!hb_set_is_empty (sources)) + hb_set_add (self_feeding_features, feat_tag); + hb_set_destroy (sources); + hb_set_destroy (dependents); } - if (count == 20) - fprintf (stderr, " ..."); - fprintf (stderr, "\n"); + + if (!hb_set_is_empty (self_feeding_features)) + { + /* Check that every extra glyph is reachable from the subset closure + * through a chain of edges within a self-feeding feature. */ + hb_set_t *reachable = hb_set_create (); + hb_set_union (reachable, subset_closure); + bool changed; + do + { + changed = false; + hb_codepoint_t gid = HB_SET_VALUE_INVALID; + while (hb_set_next (reachable, &gid)) + { + unsigned total = hb_subset_depend_lookup_glyph (depend, gid, 0, nullptr, nullptr); + for (unsigned j = 0; j < total; j++) + { + unsigned cnt = 1; + hb_subset_depend_entry_t e; + hb_subset_depend_lookup_glyph (depend, gid, j, &cnt, &e); + if (e.table_tag != HB_OT_TAG_GSUB) continue; + if (!hb_set_has (self_feeding_features, e.layout_tag)) continue; + if (!hb_set_has (reachable, e.dependent)) + { + hb_set_add (reachable, e.dependent); + changed = true; + } + } + } + } while (changed); + + /* If every extra glyph is explained by self-feeding chains, + * this is known over-approximation. */ + hb_set_t *unexplained = hb_set_create (); + hb_set_union (unexplained, in_depend_not_subset); + hb_set_subtract (unexplained, reachable); + if (hb_set_is_empty (unexplained)) + explained_by_self_feeding = true; + hb_set_destroy (unexplained); + hb_set_destroy (reachable); + } + hb_set_destroy (self_feeding_features); + } + + if (explained_by_self_feeding) + { + if (_hb_depend_fuzzer_verbose || _hb_depend_fuzzer_report_over_approximation) + fprintf (stderr, "Test 0x%016llx: Depend found %u extra glyphs (explained by self-feeding GSUB chain)\n", + (unsigned long long) seed, hb_set_get_population (in_depend_not_subset)); + } + else + { + if (_hb_depend_fuzzer_report_under_approximation || + _hb_depend_fuzzer_report_over_approximation || + _hb_depend_fuzzer_verbose) + { + fprintf (stderr, "Test 0x%016llx: UNEXPECTED OVER-APPROX - Depend found %u extra glyphs WITHOUT hitting flagged edge:", + (unsigned long long) seed, hb_set_get_population (in_depend_not_subset)); + hb_codepoint_t gid = HB_SET_VALUE_INVALID; + unsigned count = 0; + while (hb_set_next (in_depend_not_subset, &gid) && count < 20) + { + fprintf (stderr, " %u", gid); + count++; + } + if (count == 20) + fprintf (stderr, " ..."); + fprintf (stderr, "\n"); + } + if (!_hb_depend_fuzzer_report_under_approximation) + assert (0 && "Depend API has unexpected over-approximation without flagged edges"); } - if (!_hb_depend_fuzzer_report_under_approximation) - assert (0 && "Depend API has unexpected over-approximation without flagged edges"); } }