Address code review feedback for depend API

This commit addresses multiple code review comments:

1. Switch RNG to std::mt19937 for deterministic tests
   - Replace std::rand/srand with std::mt19937 in parity checker
   - Use std::uniform_int_distribution for all random number generation
   - Ensures test determinism across platforms for non-error-injection tests

2. Convert debug fprintf to DEBUG_MSG_LEVEL
   - Convert GSUB lookup processing prints to DEBUG_MSG_LEVEL(DEPEND, nullptr, 1, 0, ...)
   - Visible at HB_DEBUG_DEPEND=2 or higher for detailed per-lookup debugging

3. Rename function for consistency
   - hb_depend_from_face → hb_depend_from_face_or_fail
   - Follows HarfBuzz convention for functions returning nullptr on failure
   - Update all call sites, documentation, and code comments

4. Adopt established set deduplication pattern
   - Change from hb_vector_t<hb_set_t> to hb_vector_t<hb::unique_ptr<hb_set_t>>
   - Use pointer-based hashmap: hb_hashmap_t<const hb_set_t*, hb_codepoint_t>
   - Leverages hb_hash's content-based deduplication via pointer dereferencing
   - Follows pattern established elsewhere in HarfBuzz codebase

5. Separate ligature and context set semantics
   - Rename free_set → free_ligature_set (no hashmap manipulation)
   - Rename new_set → new_ligature_set (no hashmap manipulation)
   - Rename find_or_create_set → find_or_create_context_set (only function using hashmap)
   - Makes it explicit which sets are deduplicated (context) vs unique (ligature)
   - Update all call sites in Ligature.hh, ReverseChainSingleSubstFormat1.hh, hb-ot-layout-gsubgpos.hh

6. Code cleanup
   - Remove unused add_to_ligature_set function (dead code)
   - Refactor new_ligature_set(cp) to delegate to new_ligature_set(set)
   - Move set_to_index declaration near sets/free_set_list for better organization

All tests pass.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Skef IterumandClaude committed 2026-02-05 20:58:13 -08:00
1 parent c32a6ca5bd
commit 7ad4fa1e6b
12 files changed
+180 -184

No files matched your search

+5 -1
View File
@@ -32,7 +32,11 @@ hb_blob_t *blob = hb_blob_create_from_file("font.ttf");
hb_face_t *face = hb_face_create(blob, 0);
// Extract dependency graph
hb_depend_t *depend = hb_depend_from_face(face);
hb_depend_t *depend = hb_depend_from_face_or_fail(face);
if (!depend) {
// Handle error
return;
}
// Query dependencies for a specific glyph
hb_codepoint_t gid = 42; // Glyph to query
+1 -1
View File
@@ -134,7 +134,7 @@ struct hb_depend_data_t {
The depend graph is immutable after construction:
1. **Construction**: `hb_depend_from_face()` walks all relevant OpenType tables once to build the complete graph
1. **Construction**: `hb_depend_from_face_or_fail()` walks all relevant OpenType tables once to build the complete graph
2. **Temporary structures freed**: After graph extraction, temporary structures used only during construction are freed:
- `edge_hashes` - deduplication hash table
- `unicodes` - Unicode codepoint set
+2 -2
View File
@@ -47,7 +47,7 @@ struct Ligature
for (const auto &source : component)
complete_ligset.add(source);
hb_codepoint_t ligset = c->depend_data->new_set(complete_ligset);
hb_codepoint_t ligset = c->depend_data->new_ligature_set(complete_ligset);
// Track whether any edge using this ligset was actually added
bool any_added = false;
@@ -65,7 +65,7 @@ struct Ligature
// If no edges were added, the ligset is unused - free it for reuse
if (!any_added)
c->depend_data->free_set(ligset);
c->depend_data->free_ligature_set(ligset);
}
#endif
@@ -90,7 +90,7 @@ struct ReverseChainSingleSubstFormat1
context_glyphs.add (back_glyphs.get_min ());
else if (back_glyphs.get_population () > 1)
{
hb_codepoint_t set_idx = c->depend_data->find_or_create_set (back_glyphs);
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (back_glyphs);
context_glyphs.add (0x80000000 | set_idx);
}
}
@@ -105,7 +105,7 @@ struct ReverseChainSingleSubstFormat1
context_glyphs.add (look_glyphs.get_min ());
else if (look_glyphs.get_population () > 1)
{
hb_codepoint_t set_idx = c->depend_data->find_or_create_set (look_glyphs);
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (look_glyphs);
context_glyphs.add (0x80000000 | set_idx);
}
}
@@ -113,7 +113,7 @@ struct ReverseChainSingleSubstFormat1
/* Allocate context set and save/restore around edge creation */
hb_codepoint_t context_set_idx = context_glyphs.is_empty ()
? HB_CODEPOINT_INVALID
: c->depend_data->find_or_create_set (context_glyphs);
: c->depend_data->find_or_create_context_set (context_glyphs);
hb_codepoint_t saved_context = c->depend_data->current_context_set_index;
c->depend_data->current_context_set_index = context_set_idx;
+75 -95
View File
@@ -144,7 +144,7 @@ struct hb_lookup_feature_record_t {
* - nominal_glyphs: Codepoint to nominal glyph mapping
* - lookup_features: Lookup index to feature tag mapping
* - edge_hashes: Hash-based edge deduplication table
* - set_hashes: Hash-based set deduplication table
* - set_to_index: Content-based context set deduplication map
* - free_set_list: Indices of freed sets available for reuse
*
* Extraction state (used during graph construction):
@@ -165,7 +165,10 @@ struct hb_depend_data_t
~hb_depend_data_t () {}
void free_set (hb_codepoint_t set_index)
/* Free an unused ligature set for reuse.
* Only called for ligature sets that were allocated but had no edges added.
* Ligature sets are never in set_to_index, so no hashmap manipulation needed. */
void free_ligature_set (hb_codepoint_t set_index)
{
if (set_index >= sets.length)
{
@@ -174,98 +177,71 @@ struct hb_depend_data_t
return;
}
/* Clear the set to save memory */
sets[set_index]->clear ();
/* Add to free list for reuse */
free_set_list.push (set_index);
/* Clear the set to save memory */
sets[set_index].clear ();
}
hb_codepoint_t new_set (hb_codepoint_t cp)
/* Allocate a new ligature set (no deduplication).
* Used for ligature component sets - each must be unique per API contract. */
hb_codepoint_t new_ligature_set (hb_codepoint_t cp)
{
hb_codepoint_t set_index;
if (free_set_list.length > 0)
{
/* Reuse freed set */
set_index = free_set_list[free_set_list.length - 1];
free_set_list.resize (free_set_list.length - 1);
sets[set_index].clear ();
sets[set_index].add (cp);
}
else
{
/* Allocate new set */
set_index = sets.length;
sets.push (std::initializer_list<hb_codepoint_t>({cp}));
}
return set_index;
}
hb_codepoint_t new_set (hb_set_t &set)
{
hb_codepoint_t set_index;
if (free_set_list.length > 0)
{
/* Reuse freed set */
set_index = free_set_list[free_set_list.length - 1];
free_set_list.resize (free_set_list.length - 1);
sets[set_index].set (set);
}
else
{
/* Allocate new set */
set_index = sets.length;
sets.push (set);
}
return set_index;
}
hb_codepoint_t add_to_set (hb_codepoint_t set_index, hb_codepoint_t cp) {
if (set_index == HB_CODEPOINT_INVALID)
set_index = new_set(cp);
else if (set_index < sets.length)
sets[set_index].add(cp);
// else error
return set_index;
}
/* Compute hash of a set's contents (for deduplication) */
uint64_t compute_set_hash (const hb_set_t &set) const
{
uint64_t hash = 0;
hb_codepoint_t cp = HB_SET_VALUE_INVALID;
/* Need to use non-const iteration since next() modifies internal state */
hb_set_t temp_set;
temp_set.set (set);
while (temp_set.next (&cp))
hash = hash * 31 + hb_hash (cp);
return hash;
temp_set.add (cp);
return new_ligature_set (temp_set);
}
/* Find existing set with same contents, or create new one (with deduplication).
* Use this for context sets. For ligature sets, call new_set() directly. */
hb_codepoint_t find_or_create_set (const hb_set_t &set)
hb_codepoint_t new_ligature_set (hb_set_t &set)
{
uint64_t hash = compute_set_hash (set);
hb_codepoint_t set_index;
/* Check if we already have this set */
if (set_hashes.has (hash))
if (free_set_list.length > 0)
{
hb_codepoint_t existing_idx = set_hashes.get (hash);
if (sets[existing_idx] == set)
return existing_idx; /* Reuse existing set */
/* Hash collision - different set, same hash */
/* Fall through to create new set */
/* Reuse freed set */
set_index = free_set_list[free_set_list.length - 1];
free_set_list.resize (free_set_list.length - 1);
sets[set_index]->set (set);
}
else
{
/* Allocate new set */
set_index = sets.length;
hb_set_t *new_set = hb_set_create ();
if (unlikely (!new_set))
return HB_CODEPOINT_INVALID;
new_set->set (set);
sets.push (hb::unique_ptr<hb_set_t> {new_set});
}
/* Create new set and store in hash table */
hb_codepoint_t new_idx = new_set (const_cast<hb_set_t&>(set));
set_hashes.set (hash, new_idx);
return set_index;
}
/* Find existing context set with same contents, or create new one (with deduplication).
* Only for context sets. For ligature sets, call new_ligature_set() directly.
*
* Uses content-based deduplication via hb_hashmap_t with pointer keys:
* hb_hash dereferences pointers and hashes content, so identical context sets
* are found regardless of pointer address.
*
* Note: Context sets may reuse indices from ligature sets if the contents match.
* This is acceptable since the API only guarantees uniqueness for ligature sets. */
hb_codepoint_t find_or_create_context_set (const hb_set_t &set)
{
/* Check if we already have this set (content-based lookup via pointer hash) */
hb_codepoint_t *existing_idx = nullptr;
if (set_to_index.has (&set, &existing_idx))
return *existing_idx; /* Reuse existing set */
/* Create new set (allocated as if it were a ligature set, but will be deduplicated) */
hb_codepoint_t new_idx = new_ligature_set (const_cast<hb_set_t&>(set));
if (unlikely (new_idx == HB_CODEPOINT_INVALID))
return HB_CODEPOINT_INVALID;
/* Add to deduplication map for future context set lookups */
set_to_index.set (sets[new_idx].get (), new_idx);
return new_idx;
}
@@ -329,7 +305,7 @@ struct hb_depend_data_t
if (!filtered_set.is_empty ())
{
hb_codepoint_t set_idx = find_or_create_set (filtered_set);
hb_codepoint_t set_idx = find_or_create_context_set (filtered_set);
context_elements.add (0x80000000 | set_idx);
}
}
@@ -349,7 +325,7 @@ struct hb_depend_data_t
if (!filtered_set.is_empty ())
{
hb_codepoint_t set_idx = find_or_create_set (filtered_set);
hb_codepoint_t set_idx = find_or_create_context_set (filtered_set);
context_elements.add (0x80000000 | set_idx);
}
}
@@ -363,7 +339,7 @@ struct hb_depend_data_t
return HB_CODEPOINT_INVALID;
/* Store context elements with deduplication */
return find_or_create_set (context_elements);
return find_or_create_context_set (context_elements);
}
#endif
@@ -407,7 +383,7 @@ struct hb_depend_data_t
hb_codepoint_t cp = HB_SET_VALUE_INVALID;
/* Need temp copy because next() modifies iterator state */
hb_set_t temp_set;
temp_set.set (sets[lig_set_index]);
temp_set.set (*sets[lig_set_index]);
while (temp_set.next(&cp)) {
components.push(cp);
}
@@ -418,7 +394,7 @@ struct hb_depend_data_t
hb_codepoint_t cp = HB_SET_VALUE_INVALID;
/* Need temp copy because next() modifies iterator state */
hb_set_t temp_set;
temp_set.set (sets[context_set_index]);
temp_set.set (*sets[context_set_index]);
while (temp_set.next(&cp)) {
components.push(cp);
}
@@ -445,7 +421,7 @@ struct hb_depend_data_t
hb_codepoint_t cp = HB_SET_VALUE_INVALID;
/* Need temp copy because next() modifies iterator state */
hb_set_t temp_set;
temp_set.set (sets[lig_set_index]);
temp_set.set (*sets[lig_set_index]);
while (temp_set.next(&cp)) {
hash = hash * 31 + hb_hash(cp);
}
@@ -456,7 +432,7 @@ struct hb_depend_data_t
hb_codepoint_t cp = HB_SET_VALUE_INVALID;
/* Need temp copy because next() modifies iterator state */
hb_set_t temp_set;
temp_set.set (sets[context_set_index]);
temp_set.set (*sets[context_set_index]);
while (temp_set.next(&cp)) {
hash = hash * 31 + hb_hash(cp);
}
@@ -484,7 +460,7 @@ struct hb_depend_data_t
{
if (existing.ligature_set == HB_CODEPOINT_INVALID || lig_set == HB_CODEPOINT_INVALID)
return false;
if (!(sets[existing.ligature_set] == sets[lig_set]))
if (!(*sets[existing.ligature_set] == *sets[lig_set]))
return false;
}
@@ -493,7 +469,7 @@ struct hb_depend_data_t
{
if (existing.context_set == HB_CODEPOINT_INVALID || context_set == HB_CODEPOINT_INVALID)
return false;
if (!(sets[existing.context_set] == sets[context_set]))
if (!(*sets[existing.context_set] == *sets[context_set]))
return false;
}
@@ -522,7 +498,7 @@ struct hb_depend_data_t
bool get_set_from_index(hb_codepoint_t index, hb_set_t *out)
{
if (index < sets.length) {
out->set(sets[index]);
out->set(*sets[index]);
return true;
}
return false;
@@ -622,8 +598,15 @@ struct hb_depend_data_t
hb_set_t unicodes;
hb_map_t nominal_glyphs;
hb_vector_t<hb_set_t> sets;
/* Set storage: vector of heap-allocated sets (both ligature and context sets)
* Using unique_ptr follows HarfBuzz pattern and provides stable pointers. */
hb_vector_t<hb::unique_ptr<hb_set_t>> sets;
hb_vector_t<hb_codepoint_t> free_set_list; /* Indices of freed sets for reuse */
/* Context set deduplication: set pointer (content-hashed) -> set_index.
* Uses HarfBuzz's established pattern where hb_hash dereferences pointers
* to hash contents, enabling content-based deduplication.
* Only context sets are added to this map; ligature sets remain unique. */
hb_hashmap_t<const hb_set_t*, hb_codepoint_t> set_to_index;
hb_vector_t<hb_glyph_depend_record_t> glyph_dependencies;
hb_vector_t<hb_set_t> lookup_features;
@@ -631,9 +614,6 @@ struct hb_depend_data_t
/* Hash-based deduplication: edge hash -> component vector for collision detection */
hb_hashmap_t<uint64_t, hb_vector_t<uint32_t>> edge_hashes;
/* Set deduplication: set_hash -> set_index (for context sets) */
hb_hashmap_t<uint64_t, hb_codepoint_t> set_hashes;
#ifdef HB_DEPEND_API
/* Temporary: Context set index for the current rule.
* Built once per rule by context_depend_lookup/chain_context_depend_lookup,
+16 -19
View File
@@ -85,7 +85,7 @@ hb_depend_t::hb_depend_t (hb_face_t *f)
/* Free temporary structures - no longer needed after graph extraction */
data.edge_hashes.fini();
data.set_hashes.fini();
data.set_to_index.fini();
data.unicodes.fini();
data.nominal_glyphs.fini();
for (auto &fs : data.lookup_features)
@@ -179,24 +179,18 @@ void hb_depend_t::get_gsub_dependencies() {
// During graph extraction, all lookups see ALL glyphs, allowing us to record all possible edges.
OT::hb_depend_context_t c (&data, face, &all_glyphs);
const char *debug_depend = getenv ("HB_DEPEND_DEBUG_LOOKUPS");
bool debug_lookups = debug_depend && atoi(debug_depend);
/* Debug output at level 1: per-lookup processing details.
* Use HB_DEBUG_DEPEND=2 or higher to see these messages. */
int i = -1;
for (auto &feature_set : data.lookup_features) {
i++;
if (feature_set.is_empty ()) {
if (debug_lookups)
fprintf (stderr, "[DEPEND] Skipping lookup %d (no features)\n", i);
DEBUG_MSG_LEVEL (DEPEND, nullptr, 1, 0,
"Skipping lookup %d (no features)", i);
continue;
}
if (debug_lookups) {
fprintf (stderr, "[DEPEND] Processing lookup %d with features:", i);
hb_codepoint_t f = HB_SET_VALUE_INVALID;
while (hb_set_next (&feature_set, &f))
fprintf (stderr, " %c%c%c%c", HB_UNTAG((hb_tag_t)f));
fprintf (stderr, "\n");
}
DEBUG_MSG_LEVEL (DEPEND, nullptr, 1, 0,
"Processing lookup %d with features:", i);
c.lookup_index = i;
c.lookups_seen.clear ();
table->get_lookup (i).depend (&c);
@@ -288,7 +282,7 @@ hb_depend_t::~hb_depend_t ()
/**
* hb_depend_from_face:
* hb_depend_from_face_or_fail:
* @face: font face to collect dependencies from
*
* Calculates the dependencies between glyphs in the supplied face.
@@ -297,19 +291,22 @@ hb_depend_t::~hb_depend_t ()
*
* Example:
* ```c
* hb_depend_t *depend = hb_depend_from_face (face);
* hb_depend_t *depend = hb_depend_from_face_or_fail (face);
* if (!depend) {
* // Handle error (OOM or invalid face)
* return;
* }
* // ... use depend ...
* hb_depend_destroy (depend);
* ```
*
* Return value: (transfer full): New depend object. Destroy with
* hb_depend_destroy(). If there is a failure creating the depend
* object nullptr will be returned.
* Return value: (transfer full): New depend object, or `nullptr` if creation
* failed (out of memory or invalid face). Destroy with hb_depend_destroy().
*
* Since: REPLACEME
**/
hb_depend_t *
hb_depend_from_face (hb_face_t *face)
hb_depend_from_face_or_fail (hb_face_t *face)
{
hb_depend_t *depend;
if (unlikely (!(depend = hb_object_create<hb_depend_t> (face))))
+1 -1
View File
@@ -49,7 +49,7 @@ HB_BEGIN_DECLS
typedef struct hb_depend_t hb_depend_t;
HB_EXTERN hb_depend_t *
hb_depend_from_face (hb_face_t *face);
hb_depend_from_face_or_fail (hb_face_t *face);
HB_EXTERN hb_bool_t
hb_depend_get_glyph_entry(hb_depend_t *depend,
+1 -1
View File
@@ -41,7 +41,7 @@
* Contains the dependency data (data), the source font face (face),
* and the set of features referenced by dependencies (features).
*
* Initialized via hb_depend_from_face() which computes the dependency
* Initialized via hb_depend_from_face_or_fail() which computes the dependency
* graph once. The graph remains immutable for the lifetime of the object.
*/
struct hb_depend_t
+4 -4
View File
@@ -1929,7 +1929,7 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c,
if (filtered == (*input_position_glyphs)[j]) {
/* No overlap with position_context - add full requirement */
hb_codepoint_t idx = c->depend_data->find_or_create_set ((*input_position_glyphs)[j]);
hb_codepoint_t idx = c->depend_data->find_or_create_context_set ((*input_position_glyphs)[j]);
filtered_disjunctive_indices.add (0x80000000 | idx);
}
/* Otherwise: position_context intersects, requirement satisfied */
@@ -1942,7 +1942,7 @@ static void context_depend_recurse_lookups (hb_depend_context_t *c,
/* Allocate final context_set */
hb_codepoint_t context_set_idx = position_context.is_empty ()
? HB_CODEPOINT_INVALID
: c->depend_data->find_or_create_set (position_context);
: c->depend_data->find_or_create_context_set (position_context);
/* Save outer context to restore after this lookup. When a contextual lookup
* calls another contextual lookup, we want each lookup in this rule to see
@@ -3665,7 +3665,7 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
if (back_set.get_population () == 1) {
preliminary_context.add (back_set.get_min ()); // Direct glyph
} else if (back_set.get_population () > 1) {
hb_codepoint_t set_idx = c->depend_data->find_or_create_set (back_set);
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (back_set);
preliminary_context.add (0x80000000 | set_idx); // Encoded set reference
}
}
@@ -3674,7 +3674,7 @@ static inline void chain_context_depend_lookup (hb_depend_context_t *c,
if (look_set.get_population () == 1) {
preliminary_context.add (look_set.get_min ()); // Direct glyph
} else if (look_set.get_population () > 1) {
hb_codepoint_t set_idx = c->depend_data->find_or_create_set (look_set);
hb_codepoint_t set_idx = c->depend_data->find_or_create_context_set (look_set);
preliminary_context.add (0x80000000 | set_idx); // Encoded set reference
}
}
+11 -11
View File
@@ -303,7 +303,7 @@ test_depend_cmap (void)
* This creates dependency: GID 7 → GID 10 with layout_tag=0xE01E5 */
hb_face_t *face = hb_test_open_font_file ("fonts/base.ttf");
hb_depend_t *depend = hb_depend_from_face (face);
hb_depend_t *depend = hb_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
g_test_message ("Testing cmap NonDefault UVS: cid00007 (7) → cid00010 (10) with selector 0xE01E5");
@@ -321,7 +321,7 @@ test_depend_glyf (void)
{
/* NotoSans-Bold.ttf has composite glyphs */
hb_face_t *face = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf");
hb_depend_t *depend = hb_depend_from_face (face);
hb_depend_t *depend = hb_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
/* Composite glyph "Aacute" (131) is composed of two components:
@@ -347,7 +347,7 @@ test_depend_cff (void)
{
/* cff1_seac.C0.otf has a SEAC composite glyph */
hb_face_t *face = hb_test_open_font_file ("fonts/cff1_seac.C0.otf");
hb_depend_t *depend = hb_depend_from_face (face);
hb_depend_t *depend = hb_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
/* Glyph 2 is a SEAC composite defined as base (glyph 1) + accent (glyph 3).
@@ -371,7 +371,7 @@ test_depend_colr (void)
{
/* COLRv0: base glyph with layer glyphs */
hb_face_t *face = hb_test_open_font_file ("fonts/COLRv0.extents.ttf");
hb_depend_t *depend = hb_depend_from_face (face);
hb_depend_t *depend = hb_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
g_test_message ("Testing COLRv0: glyph00013 (13) → glyph00010 (10)");
@@ -382,7 +382,7 @@ test_depend_colr (void)
/* COLRv1: Paint graph dependencies */
face = hb_test_open_font_file ("fonts/test_glyphs-glyf_colr_1.ttf");
depend = hb_depend_from_face (face);
depend = hb_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
g_test_message ("Testing COLRv1 v0 layer: glyph 168 → glyph 176");
@@ -404,7 +404,7 @@ test_depend_math (void)
{
/* MathTestFontFull.otf has full MATH table with variants and assemblies */
hb_face_t *face = hb_test_open_font_file ("fonts/MathTestFontFull.otf");
hb_depend_t *depend = hb_depend_from_face (face);
hb_depend_t *depend = hb_depend_from_face_or_fail (face);
g_assert_nonnull (depend);
/* Base glyph "arrowup" (31) has:
@@ -439,7 +439,7 @@ test_depend_gsub_formats (void)
/* SourceSansPro-Regular.otf */
hb_face_t *face_source = hb_test_open_font_file ("fonts/SourceSansPro-Regular.otf");
hb_depend_t *depend_source = hb_depend_from_face (face_source);
hb_depend_t *depend_source = hb_depend_from_face_or_fail (face_source);
g_assert_nonnull (depend_source);
/* AlternateSubst: A → A.sc (tests AlternateSet with multiple alternates) */
@@ -459,7 +459,7 @@ test_depend_gsub_formats (void)
/* Qahiri-Regular.ttf */
hb_face_t *face_qahiri = hb_test_open_font_file ("fonts/Qahiri-Regular.ttf");
hb_depend_t *depend_qahiri = hb_depend_from_face (face_qahiri);
hb_depend_t *depend_qahiri = hb_depend_from_face_or_fail (face_qahiri);
g_assert_nonnull (depend_qahiri);
/* ReverseChainSingleSubst */
@@ -512,7 +512,7 @@ test_depend_gsub_formats (void)
/* NotoSans-Bold.ttf has ContextSubst Format 1, Format 2, and ChainContextSubst Format 2 */
hb_face_t *face_notosans = hb_test_open_font_file ("fonts/NotoSans-Bold.ttf");
hb_depend_t *depend_notosans = hb_depend_from_face (face_notosans);
hb_depend_t *depend_notosans = hb_depend_from_face_or_fail (face_notosans);
g_assert_nonnull (depend_notosans);
/* ContextSubst Format 1 → LigatureSubst
@@ -572,7 +572,7 @@ test_depend_gsub_formats (void)
* - Lookup 25, 119: ChainContextSubst Format 2 → SingleSubst test
* Test runtime: 4+ seconds → 0.02 seconds (200× faster) */
hb_face_t *face_context = hb_test_open_font_file ("fonts/ContextFormat2-Depend-Test.ttf");
hb_depend_t *depend_context = hb_depend_from_face (face_context);
hb_depend_t *depend_context = hb_depend_from_face_or_fail (face_context);
g_assert_nonnull (depend_context);
/* ContextSubst Format 2 → MultipleSubst
@@ -611,7 +611,7 @@ test_depend_gsub_formats (void)
/* ChainContextSubst Format 3 → MultipleSubst + feature tagging */
face_source = hb_test_open_font_file ("fonts/SourceSansPro-Regular.otf");
depend_source = hb_depend_from_face (face_source);
depend_source = hb_depend_from_face_or_fail (face_source);
g_assert_nonnull (depend_source);
/* Lookup 8: Format 3 chain context invokes Lookup 7 (MultipleSubst)
+60 -45
View File
@@ -8,6 +8,7 @@
#include "hb-subset.h"
#include <random>
#include <vector>
#ifdef HB_DEPEND_API
@@ -80,9 +81,6 @@ init_parity_config ()
config_initialized = true;
/* Initialize RNG with seed (already set by main or defaulted to random) */
srand (_hb_depend_fuzzer_seed);
/* Check for debug glyph ID (only used when HB_DEBUG_DEPEND > 0) */
const char *debug_gid_str = getenv ("HB_DEPEND_PARITY_DEBUG_GID");
if (debug_gid_str)
@@ -94,18 +92,19 @@ init_parity_config ()
trace_closure = true;
}
/* Simple RNG helpers */
/* Simple RNG helpers - use std::mt19937 for deterministic cross-platform behavior */
static unsigned
rng_range (unsigned min, unsigned max)
rng_range (std::mt19937 &rng, unsigned min, unsigned max)
{
if (max <= min)
return min;
return min + (rand () % (max - min + 1));
std::uniform_int_distribution<unsigned> dist (min, max);
return dist (rng);
}
/* Select n random distinct values from range [0, max) */
static void
rng_select_distinct (hb_set_t *out, unsigned n, unsigned max)
rng_select_distinct (std::mt19937 &rng, hb_set_t *out, unsigned n, unsigned max)
{
if (n >= max)
{
@@ -118,14 +117,14 @@ rng_select_distinct (hb_set_t *out, unsigned n, unsigned max)
/* Simple approach: keep trying random values until we have enough distinct ones */
while (hb_set_get_population (out) < n)
{
unsigned val = rng_range (0, max - 1);
unsigned val = rng_range (rng, 0, max - 1);
hb_set_add (out, val);
}
}
/* Select n random distinct values from an existing set */
static void
rng_select_from_set (hb_set_t *out, unsigned n, const hb_set_t *source)
rng_select_from_set (std::mt19937 &rng, hb_set_t *out, unsigned n, const hb_set_t *source)
{
unsigned source_size = hb_set_get_population (source);
if (n == 0 || source_size == 0)
@@ -148,7 +147,7 @@ rng_select_from_set (hb_set_t *out, unsigned n, const hb_set_t *source)
/* Select n random indices */
while (hb_set_get_population (out) < n)
{
unsigned random_idx = rng_range (0, source_size - 1);
unsigned random_idx = rng_range (rng, 0, source_size - 1);
hb_set_add (out, array[random_idx]);
}
@@ -443,7 +442,7 @@ generate_test_from_explicit (hb_face_t *face, hb_font_t *font)
* The seed determines:
* - Test type (GSUB vs non-GSUB, size category) via seed % 100
* - Target size within range via middle bits
* - Specific element selection via high bits
* - Specific element selection via separate RNG seeded from high bits
* - For GSUB tests: which features to activate (from available GSUB features)
*
* Test type distribution (percentages of total tests):
@@ -549,9 +548,10 @@ generate_test_from_seed (uint64_t seed, hb_face_t *face, hb_font_t *font)
return params;
}
/* Step 3: Select specific elements using full seed with mixing */
/* Step 3: Select specific elements using full seed with mixing
* Create a separate RNG for element selection, seeded from high bits */
unsigned selection_seed = (unsigned)((seed >> 32) ^ seed);
srand (selection_seed);
std::mt19937 selection_rng (selection_seed);
if (params.is_gsub)
{
@@ -561,7 +561,7 @@ generate_test_from_seed (uint64_t seed, hb_face_t *face, hb_font_t *font)
/* Select random codepoints by index */
hb_set_t *cp_indices = hb_set_create ();
rng_select_distinct (cp_indices, params.target_size, num_codepoints);
rng_select_distinct (selection_rng, cp_indices, params.target_size, num_codepoints);
/* Convert indices to actual codepoints */
hb_codepoint_t idx = HB_SET_VALUE_INVALID;
@@ -592,17 +592,18 @@ generate_test_from_seed (uint64_t seed, hb_face_t *face, hb_font_t *font)
if (num_features > 0)
{
unsigned feature_selector = rand () % 100;
std::uniform_int_distribution<unsigned> feat_dist (0, 99);
unsigned feature_selector = feat_dist (selection_rng);
unsigned num_to_select;
if (feature_selector < 30)
num_to_select = 1; /* Single (30%) */
else if (feature_selector < 65)
num_to_select = rng_range (2, 5); /* Small (35%) */
num_to_select = rng_range (selection_rng, 2, 5); /* Small (35%) */
else if (feature_selector < 85)
num_to_select = rng_range (6, 15); /* Medium (20%) */
num_to_select = rng_range (selection_rng, 6, 15); /* Medium (20%) */
else if (feature_selector < 95)
num_to_select = rng_range (16, 50); /* Large (10%) */
num_to_select = rng_range (selection_rng, 16, 50); /* Large (10%) */
else
num_to_select = num_features; /* All (5%) */
@@ -612,7 +613,7 @@ generate_test_from_seed (uint64_t seed, hb_face_t *face, hb_font_t *font)
/* Select specific features */
params.features = hb_set_create ();
rng_select_from_set (params.features, num_to_select, available_features);
rng_select_from_set (selection_rng, params.features, num_to_select, available_features);
}
hb_set_destroy (available_features);
@@ -621,7 +622,7 @@ generate_test_from_seed (uint64_t seed, hb_face_t *face, hb_font_t *font)
{
/* Non-GSUB test: select glyphs directly */
params.glyphs = hb_set_create ();
rng_select_distinct (params.glyphs, params.target_size, num_glyphs);
rng_select_distinct (selection_rng, params.glyphs, params.target_size, num_glyphs);
}
hb_set_destroy (all_codepoints);
@@ -632,7 +633,7 @@ generate_test_from_seed (uint64_t seed, hb_face_t *face, hb_font_t *font)
* Depend Parity Checker - Validates depend API correctness by comparing closures
*
* For each font, this parity checker:
* 1. Extracts the dependency graph using hb_depend_from_face()
* 1. Extracts the dependency graph using hb_depend_from_face_or_fail()
* 2. For various starting glyphs, computes closure via:
* a) Following the depend API dependency graph
* b) Using HarfBuzz's subset plan closure calculation
@@ -721,6 +722,7 @@ check_context_satisfied (hb_depend_t *depend,
/* Helper: Process edges for specific table(s)
* table_filter: if non-NULL, only process edges from these tables
* hit_flagged_edge: if non-NULL, set to true if any edge with FROM_CONTEXT_POSITION flag is traversed
* injection_rng: if non-NULL, use for error injection
* Returns: true if any new glyphs were added */
static bool
process_edges_for_tables (hb_depend_t *depend,
@@ -730,7 +732,8 @@ process_edges_for_tables (hb_depend_t *depend,
hb_set_t *table_filter,
bool skip_gsub,
hb_set_t *active_features,
bool *hit_flagged_edge)
bool *hit_flagged_edge,
std::mt19937 *injection_rng)
{
bool added_any = false;
@@ -779,8 +782,9 @@ process_edges_for_tables (hb_depend_t *depend,
{
/* Over-approximation injection: sometimes follow inactive feature edges */
bool skip_feature_check = false;
if (_hb_depend_fuzzer_inject_over_approx > 0.0f) {
float r = (float)rand() / (float)RAND_MAX;
if (injection_rng && _hb_depend_fuzzer_inject_over_approx > 0.0f) {
std::uniform_real_distribution<float> dist (0.0f, 1.0f);
float r = dist (*injection_rng);
if (r < _hb_depend_fuzzer_inject_over_approx) {
skip_feature_check = true;
}
@@ -803,8 +807,9 @@ process_edges_for_tables (hb_depend_t *depend,
{
/* Over-approximation injection: sometimes follow edges with unsatisfied context */
bool skip_context_check = false;
if (_hb_depend_fuzzer_inject_over_approx > 0.0f) {
float r = (float)rand() / (float)RAND_MAX;
if (injection_rng && _hb_depend_fuzzer_inject_over_approx > 0.0f) {
std::uniform_real_distribution<float> dist (0.0f, 1.0f);
float r = dist (*injection_rng);
if (r < _hb_depend_fuzzer_inject_over_approx) {
skip_context_check = true;
}
@@ -833,8 +838,9 @@ process_edges_for_tables (hb_depend_t *depend,
else
{
/* Under-approximation injection: randomly skip following this edge */
if (_hb_depend_fuzzer_inject_under_approx > 0.0f) {
float r = (float)rand() / (float)RAND_MAX;
if (injection_rng && _hb_depend_fuzzer_inject_under_approx > 0.0f) {
std::uniform_real_distribution<float> dist (0.0f, 1.0f);
float r = dist (*injection_rng);
if (r < _hb_depend_fuzzer_inject_under_approx) {
/* Skip this edge to inject under-approximation */
continue;
@@ -870,10 +876,12 @@ process_edges_for_tables (hb_depend_t *depend,
* skip_gsub: if true, skip GSUB dependencies to match subset behavior
* (subset doesn't follow GSUB when starting with glyph IDs)
* active_features: if non-NULL, only follow GSUB dependencies from these features
* hit_flagged_edge: if non-NULL, set to true if any edge with FROM_CONTEXT_POSITION flag is traversed */
* hit_flagged_edge: if non-NULL, set to true if any edge with FROM_CONTEXT_POSITION flag is traversed
* injection_rng: if non-NULL, use for error injection */
static void
compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub,
hb_set_t *active_features, bool *hit_flagged_edge)
hb_set_t *active_features, bool *hit_flagged_edge,
std::mt19937 *injection_rng)
{
if (debug_gid != HB_CODEPOINT_INVALID)
{
@@ -923,7 +931,7 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub,
/* Process non-ligature GSUB edges */
hb_set_union (to_process, glyphs);
process_edges_for_tables (depend, glyphs, to_process, &deferred_ligatures,
gsub_filter, skip_gsub, active_features, hit_flagged_edge);
gsub_filter, skip_gsub, active_features, hit_flagged_edge, injection_rng);
/* Process deferred ligatures (GSUB ligatures only)
* Only add a ligature when ALL glyphs in its ligature set are in closure.
@@ -945,8 +953,9 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub,
bool set_satisfied = hb_set_is_subset (ligature_set_glyphs, glyphs);
/* Over-approximation injection: sometimes add ligature even when components NOT satisfied */
if (!set_satisfied && _hb_depend_fuzzer_inject_over_approx > 0.0f) {
float r = (float)rand() / (float)RAND_MAX;
if (!set_satisfied && injection_rng && _hb_depend_fuzzer_inject_over_approx > 0.0f) {
std::uniform_real_distribution<float> dist (0.0f, 1.0f);
float r = dist (*injection_rng);
if (r < _hb_depend_fuzzer_inject_over_approx) {
set_satisfied = true; /* Pretend it's satisfied */
}
@@ -956,8 +965,9 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub,
{
/* Under-approximation injection: randomly skip adding ligature */
bool skip_ligature = false;
if (_hb_depend_fuzzer_inject_under_approx > 0.0f) {
float r = (float)rand() / (float)RAND_MAX;
if (injection_rng && _hb_depend_fuzzer_inject_under_approx > 0.0f) {
std::uniform_real_distribution<float> dist (0.0f, 1.0f);
float r = dist (*injection_rng);
if (r < _hb_depend_fuzzer_inject_under_approx) {
skip_ligature = true;
}
@@ -1005,7 +1015,7 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub,
hb_set_union (to_process, glyphs);
process_edges_for_tables (depend, glyphs, to_process, &deferred_ligatures,
math_filter, skip_gsub, active_features, hit_flagged_edge);
math_filter, skip_gsub, active_features, hit_flagged_edge, injection_rng);
hb_set_destroy (math_filter);
if (trace_closure)
@@ -1020,7 +1030,7 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub,
hb_set_union (to_process, glyphs);
process_edges_for_tables (depend, glyphs, to_process, &deferred_ligatures,
colr_filter, skip_gsub, active_features, hit_flagged_edge);
colr_filter, skip_gsub, active_features, hit_flagged_edge, injection_rng);
hb_set_destroy (colr_filter);
if (trace_closure)
@@ -1039,7 +1049,7 @@ compute_depend_closure (hb_depend_t *depend, hb_set_t *glyphs, bool skip_gsub,
hb_set_union (to_process, glyphs);
process_edges_for_tables (depend, glyphs, to_process, &deferred_ligatures,
glyf_filter, skip_gsub, active_features, NULL);
glyf_filter, skip_gsub, active_features, NULL, injection_rng);
hb_set_destroy (glyf_filter);
if (trace_closure)
@@ -1274,16 +1284,19 @@ compare_closures (hb_face_t *face, hb_depend_t *depend,
/* Seed RNG for error injection based on test seed to ensure reproducibility.
* For a given test seed and injection probability, the same edges will be
* skipped/added every time. */
* skipped/added every time. Use std::mt19937 for deterministic cross-platform behavior. */
std::mt19937 *injection_rng = nullptr;
std::mt19937 injection_rng_storage;
if (_hb_depend_fuzzer_inject_over_approx > 0.0f ||
_hb_depend_fuzzer_inject_under_approx > 0.0f)
{
srand ((unsigned) seed);
injection_rng_storage.seed ((unsigned) seed);
injection_rng = &injection_rng_storage;
}
bool hit_flagged_edge = false;
compute_depend_closure (depend, depend_closure, skip_gsub,
skip_gsub ? NULL : active_features, &hit_flagged_edge);
skip_gsub ? NULL : active_features, &hit_flagged_edge, injection_rng);
hb_set_destroy (actual_start_glyphs);
@@ -1433,7 +1446,7 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
hb_face_t *face = hb_face_create (blob, 0);
/* Extract dependency graph */
hb_depend_t *depend = hb_depend_from_face (face);
hb_depend_t *depend = hb_depend_from_face_or_fail (face);
if (!depend)
{
hb_face_destroy (face);
@@ -1575,17 +1588,19 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
{
/* Generate T pseudo-random 64-bit test seeds from the parity checker seed
* Each seed deterministically generates a test with specific parameters
* Distribution follows research-backed proportions (see generate_test_from_seed) */
* Distribution follows research-backed proportions (see generate_test_from_seed)
* Use std::mt19937 for deterministic cross-platform behavior */
unsigned T = _hb_depend_fuzzer_num_tests;
/* Initialize RNG with parity checker seed to generate test seeds */
srand (_hb_depend_fuzzer_seed);
std::mt19937 test_rng (_hb_depend_fuzzer_seed);
std::uniform_int_distribution<uint32_t> uint32_dist;
for (unsigned i = 0; i < T; i++)
{
/* Generate pseudo-random 64-bit test seed */
uint64_t test_seed = ((uint64_t)rand() << 32) | (uint64_t)rand();
uint64_t test_seed = ((uint64_t)uint32_dist (test_rng) << 32) | (uint64_t)uint32_dist (test_rng);
/* Generate test parameters from seed */
test_params params = generate_test_from_seed (test_seed, face, font);
+1 -1
View File
@@ -14,7 +14,7 @@ extern "C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size)
HB_MEMORY_MODE_READONLY, nullptr, nullptr);
hb_face_t *face = hb_face_create (blob, 0);
hb_depend_t *depend = hb_depend_from_face (face);
hb_depend_t *depend = hb_depend_from_face_or_fail (face);
if (depend)
{