Merge pull request #6505 from harfbuzz/scanner/repacker

[repacker] Fix an OOB write, an overflow-error crash, and six repacker DoS issues
This commit is contained in:
Behdad Esfahbod authored and GitHub committed 2026-11-02 11:48:40 -06:00
commit 5cb6cac5f7
10 files changed
+561 -144

No files matched your search

+17 -15
View File
@@ -193,6 +193,7 @@ struct class_def_size_estimator_t
included_glyphs.clear();
included_class_def_glyphs.clear();
included_classes.clear();
coverage_range_count = 0;
class_def_range_count = 0;
class_max = 0;
coverage_size_val = compute_coverage_size ();
@@ -210,12 +211,23 @@ struct class_def_size_estimator_t
if (!included_classes.has(klass)) {
hb_set_t* glyphs = nullptr;
if (glyphs_per_class.has(klass, &glyphs)) {
unsigned num_glyphs = included_glyphs.get_population();
// Each glyph belongs to exactly one class, so none of these glyphs are
// included yet. Keep the coverage range count incremental (recounting
// the ranges of included_glyphs for every class is quadratic): each new
// range adds one, and merges with each already included neighbour.
hb_codepoint_t start = HB_SET_VALUE_INVALID;
hb_codepoint_t end = HB_SET_VALUE_INVALID;
while (glyphs->next_range (&start, &end))
{
coverage_range_count++;
if (start && included_glyphs.has (start - 1)) coverage_range_count--;
if (included_glyphs.has (end + 1)) coverage_range_count--;
}
included_glyphs.union_(*glyphs);
if (klass)
included_class_def_glyphs.union_(*glyphs);
if (num_glyphs != included_glyphs.get_population())
coverage_size_val = compute_coverage_size ();
coverage_size_val = compute_coverage_size ();
}
if (klass)
@@ -230,17 +242,6 @@ struct class_def_size_estimator_t
return class_def_size ();
}
unsigned num_glyph_ranges (const hb_set_t &glyphs) const {
hb_codepoint_t start = HB_SET_VALUE_INVALID;
hb_codepoint_t end = HB_SET_VALUE_INVALID;
unsigned count = 0;
while (glyphs.next_range (&start, &end)) {
count++;
}
return count;
}
private:
graph_result_t<void> to_result() const
{
@@ -266,7 +267,7 @@ struct class_def_size_estimator_t
unsigned coverage_base_size = OT::HBUINT16::static_size + glyph_size;
unsigned range_size = 3 * glyph_size;
unsigned format1_size = coverage_base_size + glyph_size * included_glyphs.get_population();
unsigned format2_size = coverage_base_size + range_size * num_glyph_ranges (included_glyphs);
unsigned format2_size = coverage_base_size + range_size * coverage_range_count;
return hb_min(format1_size, format2_size);
}
@@ -306,6 +307,7 @@ struct class_def_size_estimator_t
hb_set_t included_classes;
hb_set_t included_glyphs;
hb_set_t included_class_def_glyphs;
unsigned coverage_range_count;
unsigned class_def_range_count;
unsigned class_max;
unsigned coverage_size_val;
+32 -16
View File
@@ -185,7 +185,8 @@ struct graph_t
return Err(INVALID_ARGUMENT);
}
if (unlikely (end >= table_size ()))
// end < start means start + width wrapped around.
if (unlikely (end < start || end >= table_size ()))
{
DEBUG_MSG (SUBSET_REPACK, nullptr,
"Invalid graph. Link position is out of bounds.");
@@ -1015,12 +1016,7 @@ public:
// incoming edges to root_idx should be all 32 bit in length so we don't need to de-dup these
// set the subgraph incoming edge count to match all of root_idx's incoming edges
hb_set_t parents;
for (unsigned root_idx : roots)
{
if (unlikely (root_idx >= vertices_.length))
return Err(OUT_OF_BOUNDS);
subgraph.set (root_idx, wide_parents (root_idx, parents));
}
TRY (wide_parents (roots, subgraph, parents));
for (unsigned root_idx : roots)
{
TRY (find_subgraph (root_idx, subgraph));
@@ -1337,6 +1333,7 @@ public:
index_map.set (child, *r);
return true;
}));
TRY (result);
return graph_result_t<void>::from(index_map, ALLOCATION_FAILURE);
}
@@ -1769,26 +1766,45 @@ public:
}
/*
* Returns the numbers of incoming edges that are 24 or 32 bits wide.
* For each node in nodes, sets counts[node] to the number of incoming edges that
* are 24 or 32 bits wide. The sources of those edges are added to parents.
* counts is expected to be empty.
*/
unsigned wide_parents (unsigned node_idx, hb_set_t& parents) const
graph_result_t<void> wide_parents (const hb_set_t& nodes,
hb_map_t& counts,
hb_set_t& parents) const
{
unsigned count = 0;
for (unsigned p : vertices_[node_idx].parents_iter ())
// A single parent may link to many of the nodes (eg. a ConditionSet with
// thousands of 32 bit offsets), so collect the parents first and walk the
// links of each one only once.
hb_set_t all_parents;
for (unsigned node_idx : nodes)
{
if (unlikely (node_idx >= vertices_.length))
return Err(OUT_OF_BOUNDS);
counts.set (node_idx, 0);
for (unsigned p : vertices_[node_idx].parents_iter ())
all_parents.add (p);
}
TRY (graph_result_t<void>::from (all_parents, ALLOCATION_FAILURE));
TRY (graph_result_t<void>::from (counts, ALLOCATION_FAILURE));
for (unsigned p : all_parents)
{
// Only real links can be wide
for (const auto& l : vertices_[p].obj().real_links)
{
if (l.objidx == node_idx
&& (l.width == 3 || l.width == 4)
&& !l.is_signed)
hb_codepoint_t *count;
if ((l.width == 3 || l.width == 4)
&& !l.is_signed
&& counts.has (l.objidx, &count))
{
count++;
(*count)++;
parents.add (p);
}
}
}
return count;
return graph_result_t<void>::from (parents, ALLOCATION_FAILURE);
}
public:
+24 -6
View File
@@ -201,16 +201,34 @@ struct MarkArray : public OT::Layout::GPOS_impl::MarkArray<SmallTypes>
unsigned offset_pos = (char*) &((*this)[mark].markAnchor) - (char*) this;
unsigned* anchor_index;
if (pos_to_index.has (offset_pos, &anchor_index))
TRY (c.graph.move_child (this_index,
&((*this)[mark].markAnchor),
prime_id,
&((*prime)[i].markAnchor)));
TRY (c.graph.add_link (&((*prime)[i].markAnchor),
prime_id,
*anchor_index));
i++;
}
// Restore correct ordering of the vertex links which are disturbed by move_child.
c.graph.vertices_[this_index].sort_real_links ();
// Finding the link for a single mark is O(n) so to avoid O(n^2), remove
// the old links of all of the moved marks in one pass.
auto& v = c.graph.vertices_[this_index];
const auto& o = v.obj ();
int num_links = o.real_links.length;
for (int j = 0; j < num_links; j++)
{
const auto& link = o.real_links[j];
unsigned mark = (link.position - MarkArray::min_size) /
OT::Layout::GPOS_impl::MarkRecord<SmallTypes>::static_size;
if (!marks.has (mark)) continue;
c.graph.vertices_[link.objidx].remove_parent (this_index);
v.remove_real_link_unordered (j);
num_links--;
j--;
}
// Restore correct ordering after the above removes, which mess up link ordering.
v.sort_real_links ();
return Ok(prime_id);
}
+31 -78
View File
@@ -61,11 +61,18 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3<SmallType
unsigned partial_coverage_size = 4;
unsigned accumulated = base_size;
hb_vector_t<unsigned> split_points;
// Finding the object id associated with an array index is O(n)
// so to avoid O(n^2), precompute the mapping.
TRY_ASSIGN (auto pair_sets, c.graph.vertices_[this_index].position_to_index_map ());
for (unsigned i = 0; i < pairSet.len; i++)
{
TRY_ASSIGN (unsigned pair_set_index, pair_set_graph_index (c, this_index, i));
unsigned pair_set_position = (const char*) &pairSet[i] - (const char*) this;
unsigned* pair_set_index;
if (unlikely (!pair_sets.has (pair_set_position, &pair_set_index)))
return Err(INVALID_ARGUMENT);
unsigned accumulated_delta =
c.graph.find_subgraph_size (pair_set_index, visited).value_or (0) +
c.graph.find_subgraph_size (*pair_set_index, visited).value_or (0) +
SmallTypes::size; // for PairSet offset.
partial_coverage_size += OT::HBUINT16::static_size;
@@ -164,17 +171,18 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3<SmallType
pair_pos_prime->valueFormat[1] = this->valueFormat[1];
pair_pos_prime->pairSet.len = num_pair_sets;
for (unsigned i = start; i < end; i++)
{
TRY (c.graph.move_child<> (this_index,
&pairSet[i],
pair_pos_prime_id,
&pair_pos_prime->pairSet[i - start]));
}
// Restore correct ordering of the vertex links which are disturbed by move_child.
// Moving the PairSets one at a time is O(n) per PairSet, so move the whole
// range in one pass. move_children keeps the links in the order it finds
// them, so sort by position first to keep the PairSets in array order.
c.graph.vertices_[this_index].sort_real_links ();
const unsigned pair_sets_position = (const char*) pairSet.arrayZ - (const char*) this;
TRY (c.graph.move_children<OT::Offset16> (this_index,
pair_sets_position + start * SmallTypes::size,
pair_sets_position + end * SmallTypes::size,
pair_pos_prime_id,
pair_sets_position));
TRY_ASSIGN (unsigned coverage_id, c.graph.index_for_offset (this_index, &coverage));
TRY (Coverage::clone_coverage (c,
coverage_id,
@@ -184,13 +192,6 @@ struct PairPosFormat1 : public OT::Layout::GPOS_impl::PairPosFormat1_3<SmallType
return Ok(pair_pos_prime_id);
}
graph_result_t<unsigned> pair_set_graph_index (gsubgpos_graph_context_t& c, unsigned this_index, unsigned i) const
{
return c.graph.index_for_offset (this_index, &pairSet[i]);
}
};
struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallTypes>
@@ -296,12 +297,8 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
this,
this_index,
class1_record_size,
total_value_len,
value_1_len,
value_2_len,
max_coverage_size,
max_class_def_1_size,
device_tables,
format1_device_table_indices,
format2_device_table_indices
};
@@ -316,13 +313,9 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
PairPosFormat2* thiz;
unsigned this_index;
unsigned class1_record_size;
unsigned value_record_len;
unsigned value1_record_len;
unsigned value2_record_len;
unsigned max_coverage_size;
unsigned max_class_def_size;
const hb_hashmap_t<unsigned, unsigned>& device_tables;
const hb_vector_t<unsigned>& format1_device_table_indices;
const hb_vector_t<unsigned>& format2_device_table_indices;
@@ -444,59 +437,19 @@ struct PairPosFormat2 : public OT::Layout::GPOS_impl::PairPosFormat2_4<SmallType
// No device tables to move over.
return Ok();
unsigned class2_count = class2Count;
for (unsigned i = start; i < end; i++)
{
for (unsigned j = 0; j < class2_count; j++)
{
unsigned value1_index = split_context.value_record_len * (class2_count * i + j);
unsigned value2_index = value1_index + split_context.value1_record_len;
// The only links in the class1 records are the ones to device tables, so
// rather than moving them one at a time (O(n) each) move the whole range
// in one pass. move_children keeps the links in the order it finds them,
// so sort by position first to keep the device tables in record order.
graph_t& graph = split_context.c.graph;
graph.vertices_[split_context.this_index].sort_real_links ();
unsigned new_value1_index = split_context.value_record_len * (class2_count * (i - start) + j);
unsigned new_value2_index = new_value1_index + split_context.value1_record_len;
TRY (transfer_device_tables (split_context,
pair_pos_prime_id,
split_context.format1_device_table_indices,
value1_index,
new_value1_index));
TRY (transfer_device_tables (split_context,
pair_pos_prime_id,
split_context.format2_device_table_indices,
value2_index,
new_value2_index));
}
}
return Ok();
}
graph_result_t<void> transfer_device_tables (split_context_t& split_context,
unsigned pair_pos_prime_id,
const hb_vector_t<unsigned>& device_table_indices,
unsigned old_value_record_index,
unsigned new_value_record_index) const
{
PairPosFormat2* pair_pos_prime =
(PairPosFormat2*) split_context.c.graph.object (pair_pos_prime_id).head;
for (unsigned i : device_table_indices)
{
OT::Offset16* record = (OT::Offset16*) &values[old_value_record_index + i];
unsigned record_position = ((char*) record) - ((char*) this);
if (!split_context.device_tables.has (record_position)) continue;
TRY (split_context.c.graph.move_child (
split_context.this_index,
record,
pair_pos_prime_id,
(OT::Offset16*) &pair_pos_prime->values[new_value_record_index + i]));
}
// Restore correct ordering of the vertex links which are disturbed by move_child.
split_context.c.graph.vertices_[split_context.this_index].sort_real_links ();
return Ok();
const unsigned values_position = (const char*) &values[0] - (const char*) this;
return graph.move_children<OT::Offset16> (split_context.this_index,
values_position + start * split_context.class1_record_size,
values_position + end * split_context.class1_record_size,
pair_pos_prime_id,
values_position);
}
graph_result_t<void> shrink (split_context_t& split_context,
+11 -22
View File
@@ -35,23 +35,6 @@ struct overflow_record_t
{
unsigned parent;
unsigned child;
bool operator != (const overflow_record_t o) const
{ return !(*this == o); }
inline bool operator == (const overflow_record_t& o) const
{
return parent == o.parent &&
child == o.child;
}
inline uint32_t hash () const
{
uint32_t current = 0;
current = current * 31 + hb_hash (parent);
current = current * 31 + hb_hash (child);
return current;
}
};
inline
@@ -113,10 +96,15 @@ will_overflow (graph_t& graph,
if (overflows) overflows->clear ();
graph.update_positions ();
hb_hashmap_t<overflow_record_t, bool> record_set;
// Each parent is visited once (ordering_ is a permutation of the vertices),
// so duplicate records can only come from two links of the same parent to the
// same child. A per-parent set of children avoids hashing (parent, child)
// records, whose hash a font can make collide.
hb_set_t overflowing_children;
const auto& vertices = graph.vertices_;
for (unsigned parent_idx : graph.ordering_)
{
overflowing_children.clear ();
// Don't need to check virtual links for overflow
for (const auto& link : vertices.arrayZ[parent_idx].obj ().real_links)
{
@@ -129,14 +117,14 @@ will_overflow (graph_t& graph,
overflow_record_t r;
r.parent = parent_idx;
r.child = link.objidx;
if (record_set.has(r)) continue; // don't keep duplicate overflows.
if (overflowing_children.has (r.child)) continue; // don't keep duplicate overflows.
overflows->push (r);
record_set.set (r, true);
overflowing_children.add (r.child);
}
}
TRY (graph_result_t<void>::from (record_set, ALLOCATION_FAILURE));
TRY (graph_result_t<void>::from (overflowing_children, ALLOCATION_FAILURE));
if (overflows)
TRY (graph_result_t<void>::from (*overflows, ALLOCATION_FAILURE));
@@ -188,7 +176,8 @@ serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link,
hb_serialize_context_t* c)
{
// A link must never write outside the bounds of its parent object.
if (unlikely (link.position + link.width > size))
// Compare without summing so a large position can't wrap around.
if (unlikely (link.position > size || size - link.position < link.width))
{
c->err (HB_SERIALIZE_ERROR_OTHER);
return;
+26
View File
@@ -327,6 +327,31 @@ static void test_running_class_size_estimates_with_locally_consecutive_glyphs ()
hb_always_assert(check_add_class_def_size(estimator, map, 3, {2, 3}));
}
static void test_running_coverage_size_estimates_with_interleaved_glyphs () {
gid_and_class_list_t map = {
{0, 2},
{1, 1},
{2, 1},
{3, 1},
{4, 2},
{5, 1},
{6, 1},
{7, 1},
{8, 2},
{10, 3},
};
// Class 2 extends and joins the coverage ranges of class 1.
graph::class_def_size_estimator_t estimator = *graph::class_def_size_estimator_t::create (map.iter());
hb_always_assert(check_add_class_def_size(estimator, map, 1, {1}));
hb_always_assert(check_add_class_def_size(estimator, map, 2, {1, 2}));
hb_always_assert(check_add_class_def_size(estimator, map, 3, {1, 2, 3}));
estimator.reset();
hb_always_assert(check_add_class_def_size(estimator, map, 2, {2}));
hb_always_assert(check_add_class_def_size(estimator, map, 1, {1, 2}));
}
static void
test_class_def_intersects_class ()
{
@@ -371,6 +396,7 @@ main (int argc, char **argv)
test_class_and_coverage_size_estimates ();
test_running_class_and_coverage_size_estimates ();
test_running_class_size_estimates_with_locally_consecutive_glyphs ();
test_running_coverage_size_estimates_with_interleaved_glyphs ();
#ifndef HB_NO_BEYOND_64K
test_class_def_format_3_values ();
#endif
+13 -5
View File
@@ -262,7 +262,7 @@ graph_result_t<bool> _resolve_shared_overflow(const hb_vector_t<graph::overflow_
}
auto result = sorted_graph.duplicate(&parents, r.child);
if (!result.is_ok () && parents.get_population() > 2) {
if (result.is_err () && result.error () == graph::INVALID_ARGUMENT && parents.get_population() > 2) {
// All links to the child are overflowing, so we can't include all
// in the duplication. Remove one parent from the duplication.
// Remove the lowest index parent, which will be the closest to the child.
@@ -270,7 +270,14 @@ graph_result_t<bool> _resolve_shared_overflow(const hb_vector_t<graph::overflow_
result = sorted_graph.duplicate(&parents, r.child);
}
if (!result.is_ok ()) return Ok(false);
if (result.is_err ()) {
if (result.error () == graph::INVALID_ARGUMENT)
// The child can't be duplicated (eg. all links are from the same parent),
// the caller can try other resolution options.
return Ok(false);
// All other errors (eg. vertex limit exceeded, allocation failure) are propagated.
return Err(result.error ());
}
if (parents.get_population() > 1) {
// If the duplicated node has more than one parent pre-emptively raise it's priority to the maximum.
@@ -327,10 +334,11 @@ graph_result_t<bool> _process_overflows (const hb_vector_t<graph::overflow_recor
// Since in that case moving the parent will cause a smaller increase in
// the length of other offsets.
TRY_ASSIGN (bool raised, sorted_graph.raise_childrens_priority (r.parent));
if (raised) {
priority_bumped_parents.add (r.parent);
// Record the parent even if nothing was raised: another attempt in this round can't
// do any better, and would walk all of the parent's links again for each overflow.
priority_bumped_parents.add (r.parent);
if (raised)
resolution_attempted = true;
}
continue;
}
+4 -2
View File
@@ -332,11 +332,13 @@ struct hb_serialize_context_t
__attribute__((returns_nonnull))
Type *push ()
{
if (unlikely (in_error ())) return start_embed<Type> ();
// Keep pushing when we've error'd out on int overflows, since pop_discard()
// and pop_pack() keep popping; otherwise they would pop the parent object.
if (unlikely (in_error () && !only_overflow ())) return start_embed<Type> ();
object_t *obj = object_pool.alloc ();
if (unlikely (!obj))
check_success (false);
err (HB_SERIALIZE_ERROR_OTHER);
else
{
obj->head = head;
+377
View File
@@ -743,6 +743,42 @@ populate_serializer_with_multiple_dedup_overflow (hb_serialize_context_t* c)
c->end_serialize();
}
static void
populate_serializer_with_unresolvable_priority_overflow (hb_serialize_context_t* c)
{
// One parent larger than 64kb with thousands of distinct leaf children, a few of which
// are shared with a small parent that packs before it. No 16 bit offset from the large
// parent can reach past it, so this can't be packed; but it shouldn't take long to find
// that out.
std::string large_string(70000, 'a');
c->start_serialize<char> ();
constexpr unsigned num_leaves = 10000;
constexpr unsigned num_shared = 40;
hb_vector_t<unsigned> leaves;
for (unsigned i = 0; i < num_leaves; i++) {
char leaf[3] = {'l', (char) (i >> 8), (char) i};
leaves.push (add_object (leaf, 3, c));
}
start_object ("s", 1, c);
for (unsigned i = 0; i < num_shared; i++)
add_offset (leaves[i], c);
unsigned small_parent = c->pop_pack (false);
start_object (large_string.c_str (), 70000, c);
for (unsigned i = 0; i < num_leaves; i++)
add_offset (leaves[i], c);
unsigned large_parent = c->pop_pack (false);
start_object ("r", 1, c);
add_offset (small_parent, c);
add_offset (large_parent, c);
c->pop_pack (false);
c->end_serialize();
}
static void
populate_serializer_with_isolation_overflow (hb_serialize_context_t* c)
{
@@ -1552,6 +1588,56 @@ populate_serializer_with_large_pair_pos_1 (hb_serialize_context_t* c,
c->end_serialize();
}
// One PairPosFormat1 with num_pair_set PairSet offsets which all point at one
// of num_shared small PairSets. PairSet i points at shared PairSet i % num_shared.
template<int num_pair_set, int num_shared>
static void
populate_serializer_with_shared_pair_pos_1 (hb_serialize_context_t* c)
{
c->start_serialize<char> ();
unsigned pair_set[num_shared];
for (int i = num_shared - 1; i >= 0; i--)
{
char pair_set_table[] = {
0, 1, // count
0, (char) i, // second glyph
};
pair_set[i] = add_object (pair_set_table, 4, c);
}
unsigned coverage = add_coverage (0, num_pair_set - 1, c);
char format[] = {
0, 1
};
start_object (format, 2, c);
add_offset (coverage, c);
char value_format[] = {
0, 0,
0, 0,
(char) ((num_pair_set >> 8) & 0xFF),
(char) (num_pair_set & 0xFF),
};
extend (value_format, 6, c);
for (int i = 0; i < num_pair_set; i++)
add_offset (pair_set[i % num_shared], c);
unsigned pair_pos_1 = c->pop_pack (false);
start_lookup (2, 1, c);
add_offset (pair_pos_1, c);
unsigned lookup = finish_lookup (c);
unsigned lookup_list = add_lookup_list (&lookup, 1, c);
add_gsubgpos_header (lookup_list, c);
c->end_serialize();
}
template<int num_pair_pos_2, int num_class_1, int num_class_2>
static void
populate_serializer_with_large_pair_pos_2 (hb_serialize_context_t* c,
@@ -2187,6 +2273,48 @@ static void test_will_overflow_3 ()
free (buffer);
}
static void test_will_overflow_records ()
{
size_t buffer_size = 160000;
void* buffer = malloc (buffer_size);
hb_serialize_context_t c (buffer, buffer_size);
std::string large_string(70000, 'a');
c.start_serialize<char> ();
unsigned obj_leaf = add_object ("d", 1, &c);
unsigned obj_large = add_object (large_string.c_str(), 70000, &c);
start_object ("c", 1, &c);
add_offset (obj_leaf, &c);
add_offset (obj_leaf, &c);
unsigned obj_c = c.pop_pack (false);
start_object ("b", 1, &c);
add_offset (obj_leaf, &c);
add_offset (obj_leaf, &c);
unsigned obj_b = c.pop_pack (false);
start_object ("a", 1, &c);
add_offset (obj_b, &c);
add_offset (obj_c, &c);
add_offset (obj_large, &c);
c.pop_pack (false);
c.end_serialize();
graph_t graph = graph_t::create (c.object_graph ()).value ();
// One record per overflowing (parent, child) pair, even if linked more than once.
hb_vector_t<graph::overflow_record_t> overflows;
hb_always_assert (graph::will_overflow (graph, &overflows).value ());
hb_always_assert (overflows.length == 2);
hb_always_assert (overflows[0].parent != overflows[1].parent);
hb_always_assert (overflows[0].child == overflows[1].child);
free (buffer);
}
static void test_resolve_overflows_via_sort ()
{
size_t buffer_size = 160000;
@@ -2538,6 +2666,69 @@ static void test_resolve_with_extension_pair_pos_1_split ()
free (expected_buffer);
}
static void test_resolve_with_shared_pair_set_pair_pos_1_split ()
{
// A PairPosFormat1 whose offset array alone is over the 16 bit limit, so it
// has to be split, and whose PairSets are all shared. Splitting must keep
// the PairSets in order and must not be quadratic in the number of PairSets.
constexpr int num_pair_set = 40000;
constexpr int num_shared = 4;
size_t buffer_size = 200000;
void* buffer = malloc (buffer_size);
hb_always_assert (buffer);
hb_serialize_context_t c (buffer, buffer_size);
populate_serializer_with_shared_pair_pos_1 <num_pair_set, num_shared>(&c);
graph_t graph = graph_t::create (c.object_graph ()).value ();
auto context = graph::gsubgpos_graph_context_t::create (HB_TAG ('G', 'P', 'O', 'S'),
graph).value ();
hb_always_assert (context.lookups.get_population () == 1);
unsigned lookup_index = *context.lookups.keys ();
graph::Lookup* lookup = context.lookups.get (lookup_index);
hb_always_assert (lookup->split_subtables_if_needed (context, lookup_index).is_ok ());
const auto& lookup_vertex = graph.vertices_[lookup_index];
const char* lookup_table = lookup_vertex.obj ().head;
unsigned subtable_count = *(const OT::HBUINT16*) (lookup_table + 4);
hb_always_assert (subtable_count > 1);
hb_always_assert (lookup_vertex.obj ().real_links.length == subtable_count);
auto subtables = lookup_vertex.position_to_index_map ().value ();
unsigned pair_set_count = 0;
for (unsigned i = 0; i < subtable_count; i++)
{
unsigned subtable_position = 6 + 2 * i;
hb_always_assert (subtables.has (subtable_position));
unsigned subtable_index = subtables.get (subtable_position);
const auto& subtable_vertex = graph.vertices_[subtable_index];
const char* subtable = subtable_vertex.obj ().head;
hb_always_assert (subtable_vertex.table_size () >= 10);
hb_always_assert (*(const OT::HBUINT16*) subtable == 1); // format
unsigned count = *(const OT::HBUINT16*) (subtable + 8); // pairSetCount
hb_always_assert (count > 0);
hb_always_assert (subtable_vertex.table_size () == 10 + 2 * count);
// coverage + one link per PairSet
hb_always_assert (subtable_vertex.obj ().real_links.length == count + 1);
auto pair_sets = subtable_vertex.position_to_index_map ().value ();
hb_always_assert (pair_sets.has (2)); // coverage
for (unsigned j = 0; j < count; j++)
{
unsigned pair_set_position = 10 + 2 * j;
hb_always_assert (pair_sets.has (pair_set_position));
const auto& pair_set = graph.object (pair_sets.get (pair_set_position));
hb_always_assert (pair_set.tail - pair_set.head == 4);
hb_always_assert (pair_set.head[3] == (char) ((pair_set_count + j) % num_shared));
}
pair_set_count += count;
}
hb_always_assert (pair_set_count == num_pair_set);
free (buffer);
}
static void test_resolve_with_basic_pair_pos_2_split ()
{
size_t buffer_size = 300000;
@@ -2827,6 +3018,21 @@ static void test_resolve_overflows_via_priority ()
free (expected_buffer);
}
static void test_unresolvable_priority_overflow ()
{
size_t buffer_size = 300000;
void* buffer = malloc (buffer_size);
hb_serialize_context_t c (buffer, buffer_size);
populate_serializer_with_unresolvable_priority_overflow (&c);
// Once every child of the large parent is at max priority further overflows
// from it must not re-walk all of its links, in every round.
hb_blob_t* out = hb_resolve_overflows (c.object_graph (), HB_TAG_NONE);
hb_always_assert (!out);
free (buffer);
}
static void test_virtual_link ()
{
@@ -2984,6 +3190,79 @@ test_deep_graph_traversal ()
free (buffer);
}
static void
test_duplicate_subgraph_vertex_limit ()
{
size_t buffer_size = 100;
void* buffer = malloc (buffer_size);
hb_serialize_context_t c (buffer, buffer_size);
c.start_serialize ();
unsigned leaf = add_object ("leaf", 4, &c);
start_object ("root", 4, &c);
add_offset (leaf, &c);
c.pop_pack (false);
c.end_serialize ();
graph_t graph = graph_t::create (c.object_graph ()).value ();
while (graph.vertices_.length < HB_REPACKER_MAX_VERTICES - 1)
hb_always_assert (graph.new_node (nullptr, nullptr).is_ok ());
// There's only room left for one of the two nodes, the failure must be reported.
hb_map_t index_map;
auto result = graph.duplicate_subgraph (graph.root_idx (), index_map);
hb_always_assert (result.is_err ());
hb_always_assert (result.error () == graph::LIMIT_EXCEEDED);
free (buffer);
}
static void
test_resolve_shared_overflow_vertex_limit ()
{
size_t buffer_size = 100;
void* buffer = malloc (buffer_size);
hb_serialize_context_t c (buffer, buffer_size);
c.start_serialize ();
unsigned leaf = add_object ("leaf", 4, &c);
start_object ("a", 4, &c);
add_offset (leaf, &c);
unsigned a = c.pop_pack (false);
start_object ("b", 4, &c);
add_offset (leaf, &c);
unsigned b = c.pop_pack (false);
start_object ("root", 4, &c);
add_offset (a, &c);
add_offset (b, &c);
c.pop_pack (false);
c.end_serialize ();
graph_t graph = graph_t::create (c.object_graph ()).value ();
while (graph.vertices_.length < HB_REPACKER_MAX_VERTICES)
hb_always_assert (graph.new_node (nullptr, nullptr).is_ok ());
// 'a' (1) is not shared so it can't be duplicated, other resolution options may be tried.
hb_vector_t<graph::overflow_record_t> overflows;
overflows.push (graph::overflow_record_t {3, 1});
auto result = _resolve_shared_overflow (overflows, 0, graph);
hb_always_assert (result.is_ok ());
hb_always_assert (!*result);
// 'leaf' (0) is shared by 'a' (1) and 'b' (2), but there's no room left to duplicate it.
// The limit must be reported instead of continuing the overflow resolution.
overflows.reset ();
overflows.push (graph::overflow_record_t {1, 0});
result = _resolve_shared_overflow (overflows, 0, graph);
hb_always_assert (result.is_err ());
hb_always_assert (result.error () == graph::LIMIT_EXCEEDED);
free (buffer);
}
static void
test_32bit_roots_traversal ()
{
@@ -3054,6 +3333,74 @@ test_32bit_roots_traversal ()
free (buffer);
}
static void
test_isolate_subgraph_shared_wide_parents ()
{
// Several parents each have 32 bit links to the same set of leaves, as
// FeatureVariations ConditionSets do. One leaf also has a 16 bit link from
// outside the subgraph, so isolating the leaves must duplicate that leaf only.
//
// root -16-> p1, p2, p3
// root -16-> l1
// p1, p2, p3 -32-> l1, l2, l3
constexpr unsigned num_parents = 3;
constexpr unsigned num_leaves = 3;
size_t buffer_size = 1000;
void* buffer = malloc (buffer_size);
hb_serialize_context_t c (buffer, buffer_size);
c.start_serialize ();
unsigned leaves[num_leaves];
for (unsigned i = 0; i < num_leaves; i++)
leaves[i] = add_object ("leaf", 4, &c);
unsigned parents[num_parents];
for (unsigned i = 0; i < num_parents; i++)
{
start_object ("parent", 6, &c);
for (unsigned j = 0; j < num_leaves; j++)
add_wide_offset (leaves[j], &c);
parents[i] = c.pop_pack (false);
}
start_object ("root", 4, &c);
for (unsigned i = 0; i < num_parents; i++)
add_offset (parents[i], &c);
add_offset (leaves[0], &c);
c.pop_pack (false);
c.end_serialize ();
graph_t graph = graph_t::create (c.object_graph ()).value ();
unsigned num_vertices = graph.vertices_.length;
hb_always_assert (num_vertices == 1 + num_parents + num_leaves);
hb_set_t roots;
for (unsigned i = 0; i < num_leaves; i++)
roots.add (leaves[i] - 1);
// leaves[0] has an incoming 16 bit edge from root, so it's duplicated.
hb_always_assert (graph.isolate_subgraph (roots).value ());
hb_always_assert (graph.vertices_.length == num_vertices + 1);
hb_always_assert (roots.get_population () == num_leaves);
hb_always_assert (!roots.has (leaves[0] - 1));
hb_always_assert (roots.has (num_vertices));
for (unsigned i = 1; i < num_leaves; i++)
hb_always_assert (roots.has (leaves[i] - 1));
hb_always_assert (graph.update_parents ().is_ok ());
hb_always_assert (graph.vertices_[leaves[0] - 1].incoming_edges () == 1);
hb_always_assert (graph.vertices_[num_vertices].incoming_edges () == num_parents);
for (unsigned i = 1; i < num_leaves; i++)
hb_always_assert (graph.vertices_[leaves[i] - 1].incoming_edges () == num_parents);
// Now that all incoming edges to the roots are 32 bit, nothing is left to isolate.
hb_always_assert (!graph.isolate_subgraph (roots).value ());
hb_always_assert (graph.vertices_.length == num_vertices + 1);
free (buffer);
}
static void test_invalid_link_objidx ()
{
size_t buffer_size = 100;
@@ -3150,6 +3497,29 @@ static void test_pair_pos_format_2_sanitize_size_overflow ()
hb_always_assert (!pair_pos->sanitize (vertex).is_ok ());
}
static void test_invalid_link_position ()
{
size_t buffer_size = 100;
void* buffer = malloc (buffer_size);
hb_serialize_context_t c (buffer, buffer_size);
populate_serializer_simple (&c);
graph_t graph = graph_t::create (c.object_graph ()).value ();
// Poison a link position so that position + width wraps around in
// 32 bit arithmetic. Serialization must reject it rather than write
// outside of the output buffer.
auto links = graph.vertices_[graph.root_idx ()].real_links_writer();
links->position = (unsigned) -1;
hb_always_assert (!graph::serialize (graph).is_ok ());
// The same poisoned link must be rejected when building the graph.
c.object_graph ().tail ()->real_links[0].position = (unsigned) -1;
hb_always_assert (!graph_t::create (c.object_graph ()).is_ok ());
free (buffer);
}
// TODO(garretrieger): update will_overflow tests to check the overflows array.
// TODO(garretrieger): add tests for priority raising.
@@ -3160,15 +3530,18 @@ main (int argc, char **argv)
test_pair_pos_format_2_sanitize_size_overflow ();
test_invalid_link_objidx ();
test_invalid_link_add_and_move ();
test_invalid_link_position ();
test_serialize ();
test_sort_shortest ();
test_will_overflow_1 ();
test_will_overflow_2 ();
test_will_overflow_3 ();
test_will_overflow_records ();
test_resolve_overflows_via_sort ();
test_resolve_overflows_via_duplication ();
test_resolve_overflows_via_multiple_duplications ();
test_resolve_overflows_via_priority ();
test_unresolvable_priority_overflow ();
test_resolve_overflows_via_space_assignment ();
test_resolve_overflows_via_isolation ();
test_resolve_overflows_via_isolation_with_recursive_duplication ();
@@ -3191,6 +3564,7 @@ main (int argc, char **argv)
test_resolve_with_repeated_extension_promotion ();
test_resolve_with_basic_pair_pos_1_split ();
test_resolve_with_extension_pair_pos_1_split ();
test_resolve_with_shared_pair_set_pair_pos_1_split ();
test_resolve_with_basic_pair_pos_2_split ();
test_resolve_with_pair_pos_2_split_with_device_tables ();
test_resolve_with_close_to_limit_pair_pos_2_split ();
@@ -3203,7 +3577,10 @@ main (int argc, char **argv)
test_resolve_with_liga_split_shared_table ();
test_resolve_with_liga_split_shared_coverage ();
test_deep_graph_traversal ();
test_duplicate_subgraph_vertex_limit ();
test_resolve_shared_overflow_vertex_limit ();
test_32bit_roots_traversal ();
test_isolate_subgraph_shared_wide_parents ();
// TODO(grieger): have run overflow tests compare graph equality not final packed binary.
// TODO(grieger): split test where multiple subtables in one lookup are split to test link ordering.
+26
View File
@@ -363,6 +363,31 @@ test_failed_class_mapping ()
hb_always_assert (glyph_and_klass[0].second == 1);
}
static void
test_push_pop_after_overflow ()
{
char buf[32];
hb_serialize_context_t s (buf, sizeof (buf));
s.start_serialize ();
s.push ();
auto snap = s.snapshot ();
s.err (HB_SERIALIZE_ERROR_INT_OVERFLOW);
/* Objects opened while only an overflow is pending must not pop their parent. */
s.push ();
s.pop_discard ();
hb_always_assert (s.snapshot ().current == snap.current);
s.push ();
hb_always_assert (!s.pop_pack ());
hb_always_assert (s.snapshot ().current == snap.current);
s.revert (snap);
hb_always_assert (!s.in_error ());
s.pop_discard ();
s.end_serialize ();
hb_always_assert (!s.in_error ());
}
int
main (int argc, char **argv)
{
@@ -374,6 +399,7 @@ main (int argc, char **argv)
test_medium_chain_rule ();
test_device_only_value ();
test_failed_class_mapping ();
test_push_pop_after_overflow ();
char buf[16384];