Files
harfbuzz/src/graph/serialize.hh
T
Behdad EsfahbodandClaude Opus 4.8 7e28f358ae [repacker] De-duplicate overflow records without an overflow_record_t hash map
will_overflow() de-duplicated (parent, child) overflow records through
an hb_hashmap_t keyed by overflow_record_t, whose hash() collapses to
2654435761 * (31 * parent + child).  With dense vertex ids, a graph
where many consecutive vertices link the same children puts all records
into a handful of equal-hash chains, so the map degenerates into
quadratic probing and repeated over-growth in every resolution round: a
crafted 60 KB GDEF font with a LigCaretList of overlapping LigGlyphs
took ~65 s of CPU and ~520 MiB before failing.

graph.ordering_ is a permutation of the vertices, so every parent is
visited exactly once and a duplicate record can only come from two
links of the same parent to the same child.  A per-parent hb_set_t of
the children already recorded, cleared when moving to the next parent,
yields exactly the same records in the same order with no hashing of
records at all.  The now-unused operator==/!=/hash() on
overflow_record_t are removed (the struct is only used in vectors
elsewhere).

A regression test, test_will_overflow_records(), asserts one record per
overflowing (parent, child) pair when a parent links the same child
twice.  The attack font drops to ~1.3 s CPU and 133 MiB, the remaining
cost being the 1.8 million links the sanitizer budget admits; no public
API/ABI change.

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

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-10-30 15:37:38 -06:00

303 lines
9.2 KiB
C++

/*
* Copyright © 2022 Google, Inc.
*
* This is part of HarfBuzz, a text shaping library.
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the
* above copyright notice and the following two paragraphs appear in
* all copies of this software.
*
* IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
* IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*
* THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
* ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
*
* Google Author(s): Garret Rieger
*/
#ifndef GRAPH_SERIALIZE_HH
#define GRAPH_SERIALIZE_HH
#include "graph-result.hh"
namespace graph {
struct overflow_record_t
{
unsigned parent;
unsigned child;
};
inline
int64_t compute_offset (
const graph_t& graph,
unsigned parent_idx,
const hb_serialize_context_t::object_t::link_t& link)
{
const auto& parent = graph.vertices_[parent_idx];
const auto& child = graph.vertices_[link.objidx];
int64_t offset = 0;
switch ((hb_serialize_context_t::whence_t) link.whence) {
case hb_serialize_context_t::whence_t::Head:
offset = child.start - parent.start; break;
case hb_serialize_context_t::whence_t::Tail:
offset = child.start - parent.end; break;
case hb_serialize_context_t::whence_t::Absolute:
offset = child.start; break;
}
assert (offset >= link.bias);
offset -= link.bias;
return offset;
}
inline
bool is_valid_offset (int64_t offset,
const hb_serialize_context_t::object_t::link_t& link)
{
if (unlikely (!link.width))
// Virtual links can't overflow.
return link.is_signed || offset >= 0;
if (link.is_signed)
{
if (link.width == 4)
return offset >= -((int64_t) 1 << 31) && offset < ((int64_t) 1 << 31);
else
return offset >= -(1 << 15) && offset < (1 << 15);
}
else
{
if (link.width == 4)
return offset >= 0 && offset < ((int64_t) 1 << 32);
else if (link.width == 3)
return offset >= 0 && offset < ((int32_t) 1 << 24);
else
return offset >= 0 && offset < (1 << 16);
}
}
/*
* Will any offsets overflow on graph when it's serialized?
*/
inline graph_result_t<bool>
will_overflow (graph_t& graph,
hb_vector_t<overflow_record_t>* overflows = nullptr)
{
if (overflows) overflows->clear ();
graph.update_positions ();
// 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)
{
int64_t offset = compute_offset (graph, parent_idx, link);
if (likely (is_valid_offset (offset, link)))
continue;
if (!overflows) return Ok(true);
overflow_record_t r;
r.parent = parent_idx;
r.child = link.objidx;
if (overflowing_children.has (r.child)) continue; // don't keep duplicate overflows.
overflows->push (r);
overflowing_children.add (r.child);
}
}
TRY (graph_result_t<void>::from (overflowing_children, ALLOCATION_FAILURE));
if (overflows)
TRY (graph_result_t<void>::from (*overflows, ALLOCATION_FAILURE));
return Ok((bool) (overflows && overflows->length));
}
inline
void print_overflows (graph_t& graph,
const hb_vector_t<overflow_record_t>& overflows)
{
if (!DEBUG_ENABLED(SUBSET_REPACK)) return;
auto r = graph.update_parents ();
if (r.is_err()) {
DEBUG_MSG (SUBSET_REPACK, nullptr, "Unable to print overflows due to error updating parent links: %s",
to_string(r.error()));
return;
}
int limit = 10;
for (const auto& o : overflows)
{
if (!limit--) break;
const auto& parent = graph.vertices_[o.parent];
const auto& child = graph.vertices_[o.child];
DEBUG_MSG (SUBSET_REPACK, nullptr,
" overflow from "
"%4u (%4u in, %4u out, space %2u) => "
"%4u (%4u in, %4u out, space %2u)",
o.parent,
parent.incoming_edges (),
parent.obj ().real_links.length + parent.obj ().virtual_links.length,
graph.space_for (o.parent),
o.child,
child.incoming_edges (),
child.obj ().real_links.length + child.obj ().virtual_links.length,
graph.space_for (o.child));
}
if (overflows.length > 10) {
DEBUG_MSG (SUBSET_REPACK, nullptr, " ... plus %u more overflows.", overflows.length - 10);
}
}
template <typename O> inline void
serialize_link_of_type (const hb_serialize_context_t::object_t::link_t& link,
char* head,
unsigned size,
const hb_vector_t<unsigned>& id_map,
hb_serialize_context_t* c)
{
// A link must never write outside the bounds of its parent object.
// 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;
}
OT::Offset<O>* offset = reinterpret_cast<OT::Offset<O>*> (head + link.position);
*offset = 0;
c->add_link (*offset,
id_map[link.objidx],
(hb_serialize_context_t::whence_t) link.whence,
link.bias);
}
inline
void serialize_link (const hb_serialize_context_t::object_t::link_t& link,
char* head,
unsigned size,
const hb_vector_t<unsigned>& id_map,
hb_serialize_context_t* c)
{
switch (link.width)
{
case 0:
// Virtual links aren't serialized.
return;
case 4:
if (link.is_signed)
{
serialize_link_of_type<OT::HBINT32> (link, head, size, id_map, c);
} else {
serialize_link_of_type<OT::HBUINT32> (link, head, size, id_map, c);
}
return;
case 2:
if (link.is_signed)
{
serialize_link_of_type<OT::HBINT16> (link, head, size, id_map, c);
} else {
serialize_link_of_type<OT::HBUINT16> (link, head, size, id_map, c);
}
return;
case 3:
serialize_link_of_type<OT::HBUINT24> (link, head, size, id_map, c);
return;
default:
// Unexpected link width.
assert (0);
}
}
/*
* serialize graph into the provided serialization buffer.
*/
inline graph_result_t<hb_blob_t*> serialize (const graph_t& graph)
{
hb_vector_t<char> buffer;
size_t size = graph.total_size_in_bytes ();
if (!size) return Ok(hb_blob_get_empty ());
// The output blob cannot represent a larger size; check before alloc () narrows it.
if (unlikely (size > INT_MAX || !buffer.alloc (size))) {
DEBUG_MSG (SUBSET_REPACK, nullptr, "Unable to allocate output buffer.");
return Err(ALLOCATION_FAILURE);
}
hb_serialize_context_t c((void *) buffer, size);
c.start_serialize<void> ();
const auto& vertices = graph.vertices_;
// Objects are placed in the serializer in reverse order since children need
// to be inserted before their parents.
// Maps from our obj id's to the id's used during this serialization.
hb_vector_t<unsigned> id_map;
if (unlikely (!id_map.resize(graph.ordering_.length))) {
DEBUG_MSG (SUBSET_REPACK, nullptr, "Unable to allocate id_map buffer.");
return Err(ALLOCATION_FAILURE);
}
for (int pos = graph.ordering_.length - 1; pos >= 0; pos--) {
unsigned i = graph.ordering_[pos];
c.push ();
auto& v = vertices[i];
size_t size = v.table_size ();
char* start = c.allocate_size <char> (size);
if (unlikely (!start)) {
DEBUG_MSG (SUBSET_REPACK, nullptr, "Buffer out of space.");
return Err(ALLOCATION_FAILURE);
}
hb_memcpy (start, v.obj ().head, size);
// Only real links needs to be serialized.
for (const auto& link : v.obj ().real_links)
serialize_link (link, start, size, id_map, &c);
// All duplications are already encoded in the graph, so don't
// enable sharing during packing.
id_map[i] = c.pop_pack (false);
}
c.end_serialize ();
if (unlikely (c.in_error ())) {
DEBUG_MSG (SUBSET_REPACK, nullptr, "Error during serialization. Err flag: %d",
c.errors);
if (c.errors & HB_SERIALIZE_ERROR_OFFSET_OVERFLOW)
return Err(OVERFLOW_RESOLUTION_FAILED);
return Err(ALLOCATION_FAILURE);
}
auto* blob = c.copy_blob ();
if (unlikely (!blob)) return Err(ALLOCATION_FAILURE);
return Ok(blob);
}
} // namespace graph
#endif // GRAPH_SERIALIZE_HH