Files
harfbuzz/test/api/test-graphite2.c
T
Behdad EsfahbodandClaude Opus 4.8 757cd85427 [shaper] Cache a failed face/font data create() so it is not retried
The per-shaper lazy loaders (hb_shaper_lazy_loader_t) declare nullptr
as their null object, so hb_lazy_loader_t::get_stored() cannot tell
"create() failed" from "not created yet" and re-runs create() on
every access.  hb_shape_plan_key_t::init() tests face->data.graphite2
before the plan cache is consulted, so a font whose Silf table
Graphite2 rejects paid a full gr_make_face_with_ops() on every
hb_shape() call for the life of the face -- CPU cost unbounded by
max_ops/max_len, which apply only once the buffer is processed.

The loader already has the mechanism for this: tag_null stores a
failed create() as get_null() with bit 0 set, and get_stored()/
operator bool()/do_destroy() mask or recognize it by that bit.
Opt the shaper loaders into tag_null, fixing every shaper's face and
font data at once, as the table and accelerator loaders already do.
HB_SHAPER_DATA_SUCCEEDED moves from (void *) +1 to (void *) +2 so the
success sentinel no longer collides with the tagged null (nullptr | 1
== (void *) 1); its no-op destroy functions never dereference it.

hb_font_t's data.fini() on font changes still resets the font-data
loaders, so a retry happens there.  A font with no Silf table is now
also asked for it once per face instead of once per call.

Adds test_graphite2_failed_face_load_is_remembered, which counts Silf
table requests across three hb_shape() calls; it fails on the
unpatched tree (8 != 4) and passes with the fix.

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

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

260 lines
9.6 KiB
C

/*
* Copyright © 2026 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): Behdad Esfahbod
*/
#include "hb-test.h"
static void
test_graphite2_repeated_clusters (void)
{
const char *shapers[] = {"graphite2", NULL};
hb_face_t *face;
hb_font_t *font;
hb_buffer_t *buffer;
hb_glyph_info_t *info;
hb_glyph_position_t *position;
unsigned int length;
hb_position_t advance = 0;
face = hb_test_open_font_file ("../shape/data/in-house/fonts/2f652d30fd15eea6be23fd9bdc1099836975aa83.ttf");
font = hb_font_create (face);
hb_face_destroy (face);
buffer = hb_buffer_create ();
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
hb_buffer_set_direction (buffer, HB_DIRECTION_RTL);
hb_buffer_set_script (buffer, HB_SCRIPT_ARABIC);
hb_buffer_add (buffer, 0x0660, 0);
hb_buffer_add (buffer, 0x0600, 3);
hb_buffer_add (buffer, 0x0600, 3);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, shapers));
info = hb_buffer_get_glyph_infos (buffer, &length);
position = hb_buffer_get_glyph_positions (buffer, NULL);
g_assert_cmpuint (length, ==, 3);
for (unsigned int i = 0; i < length; i++)
{
g_assert_cmpuint (info[i].cluster, ==, 0);
advance += position[i].x_advance;
}
g_assert_cmpint (advance, ==, 3200);
hb_buffer_destroy (buffer);
hb_font_destroy (font);
}
static void
test_graphite2_inserted_glyph_masks (void)
{
const char *ot_shapers[] = {"ot", NULL};
const char *graphite2_shapers[] = {"graphite2", NULL};
hb_face_t *face;
hb_font_t *font;
hb_buffer_t *buffer;
hb_glyph_info_t *info;
unsigned int length;
face = hb_test_open_font_file ("../subset/data/fonts/Harmattan-Regular.ttf");
font = hb_font_create (face);
hb_face_destroy (face);
buffer = hb_buffer_create ();
/* Leave stale masks in the buffer storage beyond its length: the OpenType
* shaper sets every mask during shaping and only cleans the glyphs that
* survive, so a removed default-ignorable leaves its mask behind. */
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
hb_buffer_set_flags (buffer, HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES);
hb_buffer_add (buffer, 0x0628, 0);
hb_buffer_add (buffer, 0x200B, 1);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, ot_shapers));
info = hb_buffer_get_glyph_infos (buffer, &length);
g_assert_cmpuint (length, ==, 1);
/* Now let Graphite produce more glyphs than there were characters; the
* added glyphs must come back with nothing but glyph flags in their mask. */
hb_buffer_clear_contents (buffer);
hb_buffer_set_content_type (buffer, HB_BUFFER_CONTENT_TYPE_UNICODE);
hb_buffer_set_flags (buffer, HB_BUFFER_FLAG_DEFAULT);
hb_buffer_add (buffer, 0x0622, 0);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, graphite2_shapers));
info = hb_buffer_get_glyph_infos (buffer, &length);
g_assert_cmpuint (length, ==, 2);
for (unsigned int i = 0; i < length; i++)
g_assert_cmphex (info[i].mask & ~HB_GLYPH_FLAG_DEFINED, ==, 0);
hb_buffer_destroy (buffer);
hb_font_destroy (font);
}
/* Three glyphs, cmap for 'a' and 'b', and a Silf table with a single
* substitution rule that deletes the glyph of 'a'. */
static const unsigned char deletes_a_font[] = {
0x00,0x01,0x00,0x00,0x00,0x08,0x00,0x80,0x00,0x03,0x00,0x00,
0x47,0x6C,0x61,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x8C,
0x00,0x00,0x00,0x10,0x47,0x6C,0x6F,0x63,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x9C,0x00,0x00,0x00,0x10,0x53,0x69,0x6C,0x66,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAC,0x00,0x00,0x00,0x94,
0x63,0x6D,0x61,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x40,
0x00,0x00,0x00,0x2C,0x68,0x65,0x61,0x64,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0x6C,0x00,0x00,0x00,0x36,0x68,0x68,0x65,0x61,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xA4,0x00,0x00,0x00,0x24,
0x68,0x6D,0x74,0x78,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xC8,
0x00,0x00,0x00,0x0C,0x6D,0x61,0x78,0x70,0x00,0x00,0x00,0x00,
0x00,0x00,0x01,0xD4,0x00,0x00,0x00,0x20,0x00,0x01,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x01,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x04,0x00,0x08,
0x00,0x0C,0x00,0x10,0x00,0x02,0x00,0x00,0x00,0x01,0x00,0x00,
0x00,0x00,0x00,0x0C,0x00,0x02,0x00,0x00,0x00,0x00,0x01,0x00,
0x01,0x01,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0xFF,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x88,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,0x00,0x08,
0x00,0x0A,0x00,0x02,0x00,0x01,0x01,0x00,0x00,0x01,0x00,0x00,
0x00,0x00,0x00,0x85,0x00,0x00,0x00,0x85,0x00,0x00,0x00,0x85,
0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x01,0x00,0x01,0x00,0x01,
0x00,0x01,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x01,
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x03,0x00,0x01,0x00,0x20,0x19,0x31,0x00,0x00,0x00,0x01,
0x00,0x03,0x00,0x01,0x00,0x00,0x00,0x0C,0x00,0x04,0x00,0x20,
0x00,0x00,0x00,0x04,0x00,0x04,0x00,0x01,0x00,0x00,0x00,0x62,
0xFF,0xFF,0x00,0x00,0x00,0x61,0xFF,0xFF,0xFF,0xA0,0x00,0x01,
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x5F,0x0F,0x3C,0xF5,0x00,0x00,0x03,0xE8,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x08,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x00,0x00,0x03,0x20,0xFF,0x38,0x00,0x00,0x01,0xF4,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,
0x01,0xF4,0x00,0x00,0x01,0xF4,0x00,0x00,0x01,0xF4,0x00,0x00,
0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
static void
test_graphite2_all_glyphs_deleted (void)
{
const char *shapers[] = {"graphite2", NULL};
hb_blob_t *blob;
hb_face_t *face;
hb_font_t *font;
hb_buffer_t *buffer;
blob = hb_blob_create ((const char *) deletes_a_font, sizeof (deletes_a_font),
HB_MEMORY_MODE_READONLY, NULL, NULL);
face = hb_face_create (blob, 0);
hb_blob_destroy (blob);
font = hb_font_create (face);
hb_face_destroy (face);
buffer = hb_buffer_create ();
hb_buffer_add_utf8 (buffer, "aba", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, shapers));
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 1);
g_assert_true (hb_buffer_has_positions (buffer));
hb_buffer_clear_contents (buffer);
hb_buffer_add_utf8 (buffer, "aaa", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
g_assert_true (hb_shape_full (font, buffer, NULL, 0, shapers));
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 0);
g_assert_true (hb_buffer_has_positions (buffer));
hb_buffer_destroy (buffer);
hb_font_destroy (font);
}
static unsigned int silf_requests;
static hb_blob_t *
reference_table_with_rejected_silf (hb_face_t *face HB_UNUSED, hb_tag_t tag, void *user_data)
{
/* Version 1.0 is too old for Graphite2. */
static const char silf[20] = {0, 1, 0, 0};
if (tag != HB_TAG ('S','i','l','f'))
return hb_face_reference_table ((hb_face_t *) user_data, tag);
silf_requests++;
return hb_blob_create (silf, sizeof (silf), HB_MEMORY_MODE_READONLY, NULL, NULL);
}
static void
destroy_face (void *data)
{
hb_face_destroy ((hb_face_t *) data);
}
static void
test_graphite2_failed_face_load_is_remembered (void)
{
hb_face_t *source, *face;
hb_font_t *font;
unsigned int first_shape_requests = 0;
source = hb_test_open_font_file ("../shape/data/in-house/fonts/2f652d30fd15eea6be23fd9bdc1099836975aa83.ttf");
face = hb_face_create_for_tables (reference_table_with_rejected_silf, source, destroy_face);
font = hb_font_create (face);
hb_face_destroy (face);
for (unsigned int i = 0; i < 3; i++)
{
hb_buffer_t *buffer = hb_buffer_create ();
hb_buffer_add_utf8 (buffer, "1", -1, 0, -1);
hb_buffer_guess_segment_properties (buffer);
hb_shape (font, buffer, NULL, 0);
g_assert_cmpuint (hb_buffer_get_length (buffer), ==, 1);
hb_buffer_destroy (buffer);
if (!i)
first_shape_requests = silf_requests;
}
g_assert_cmpuint (first_shape_requests, >, 0);
g_assert_cmpuint (silf_requests, ==, first_shape_requests);
hb_font_destroy (font);
}
int
main (int argc, char **argv)
{
hb_test_init (&argc, &argv);
hb_test_add (test_graphite2_repeated_clusters);
hb_test_add (test_graphite2_inserted_glyph_masks);
hb_test_add (test_graphite2_all_glyphs_deleted);
hb_test_add (test_graphite2_failed_face_load_is_remembered);
return hb_test_run ();
}