mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-19 14:01:10 +01:00
on a stream-backed FT_Face
hb_ft_font_set_funcs() creates its own internal FT_Face from the
font's face blob, and hooks that blob to the FT_Face's generic
finalizer (_release_blob), with the shared static FT_Library hooked
to the blob's user-data destructor (destroy_ft_library).
On hb_font_destroy(), _hb_ft_face_destroy_static() locks
static_ft_library_mutex and calls FT_Done_Face(), which synchronously
runs the finalizer chain: _release_blob() -> hb_blob_destroy(). For a
face created from a custom FreeType stream (FT_OPEN_STREAM), each
hb_face_reference_blob() call synthesizes a brand-new, unshared blob
(via _hb_ft_reference_table()), so this hb_blob_destroy() call drops
it straight to zero, which triggers destroy_ft_library(), which tries
to lock the same non-recursive static_ft_library_mutex again on the
same thread. Self-deadlock.
This didn't reproduce for a file-path or memory-blob face, because
there the blob handed to the internal FT_Face is the same blob shared
by the outer hb_face_t, so the drop inside the lock only decrements
its refcount without reaching zero; the final unref (and
destroy_ft_library() call) happens later, outside the lock, when the
outer face itself is destroyed.
Fix by holding an extra reference on the blob across the locked
FT_Done_Face() call, so it can only reach a zero refcount (and invoke
destroy_ft_library()) after static_ft_library_mutex has been
released. FT_Done_Face() itself remains fully serialized, as intended
by 46644f885d.
Fixes: https://github.com/harfbuzz/harfbuzz/issues/6276
Tests: meson test -C build (260/260 passed); added
test_native_ft_set_funcs_stream_face_no_deadlock to test/api/test-ft.c,
which hangs on unfixed code and passes with the fix. Also verified
against the reporter's original repro.c.
Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>