Commit Graph
2735 Commits
Author SHA1 Message Date
Behdad Esfahbod f587af3ec3 [gpu] Extract shared shader helpers into hb_gpu_shader_source() (#5921)
The draw-renderer fragment shaders defined utilities
(HB_GPU_UNITS_PER_EM, the hb_gpu_atlas uniform, hb_gpu_fetch) that
any future GPU renderer will also need.  The draw-renderer vertex
shader likewise contained only the generic hb_gpu_dilate helper.
If a paint renderer were added today and a caller linked both
fragment sources together, hb_gpu_fetch would be defined twice.

Move these shared helpers into a new family of sources and expose
them via a new public entry point:

  hb_gpu_shader_source (stage, lang);

The existing hb_gpu_draw_shader_source() now returns only the
draw-specific fragment code; it returns an empty string for the
vertex stage because all current vertex logic is shared.  Callers
concatenate the shared source first, then the draw-specific
source, then their own main():

  [#version, hb_gpu_shader_source (...), hb_gpu_draw_shader_source
  (...), main]

Additions:
 - src/hb-gpu-fragment.{glsl,msl,wgsl,hlsl} — shared fragment
 - src/hb-gpu-vertex.{glsl,msl,wgsl,hlsl}   — shared vertex,
   renamed from hb-gpu-draw-vertex.*
 - src/hb-gpu-shaders.cc — hb_gpu_shader_source() dispatcher

Removals:
 - src/hb-gpu-draw-vertex.{glsl,msl,wgsl,hlsl} — moved to common

Shared sources are Behdad-only copyright; the Eric Lengyel / Slug
attribution stays on the draw-specific fragment sources where the
Slug algorithm actually lives.

Updates: test/api/test-gpu.cc, the Metal/D3D11/WebGPU/GL demo
shaders, the hb-gpu SECTION documentation, meson.build,
CMakeLists.txt, gen-gpu-shader-artifacts.py decl_map,
harfbuzz-world.cc, and docs/harfbuzz-sections.txt.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 12:10:23 -06:00
Behdad Esfahbod f9de50ab52 [raster] Simplify hb_raster_paint_glyph; add set/get_palette
hb_raster_paint_glyph() took the palette index and foreground
color as per-call arguments, duplicating state that also lives on
the paint context (foreground via hb_raster_paint_set_foreground,
palette previously only available as a per-call arg).  This gave
callers two paths for configuring the same state.

Drop the per-call palette/foreground arguments and require callers
to configure them via setters before painting, matching the
already-established pattern in hb_vector_paint_glyph() and
elsewhere in the subsystem.

- Drop palette and foreground args from hb_raster_paint_glyph().
- Add hb_raster_paint_set_palette() / _get_palette() and a
  hb_raster_paint_get_foreground() companion.
- Store palette on hb_raster_paint_t (default 0).
- Update util/raster-output.hh, util/hb-raster-all.cc, and
  test/fuzzing/hb-raster-fuzzer.cc to the new API.
- Update the hb-raster section-doc example.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 03:35:47 -06:00
Behdad Esfahbod eeec9bd9fa [gpu] Unify shader source getters; rename for draw/paint split
Replace the two separate getters

  hb_gpu_shader_fragment_source (lang)
  hb_gpu_shader_vertex_source   (lang)

with a single

  hb_gpu_draw_shader_source (stage, lang)

where stage is the new hb_gpu_shader_stage_t enum (VERTEX or
FRAGMENT).  Parameter order is stage-first because "the vertex
shader in GLSL" reads more naturally than the reverse, and stage
is the primary axis (it determines which shader object the source
is compiled into).

The `_draw_` in the new name makes room for the incoming
hb_gpu_paint_shader_source() once the COLRv1 encoder lands —
that family will have its own (likely different) shader sources
per stage and language.

Updated the four in-tree callers: test/api/test-gpu.cc, and the
GPU demos under util/gpu/.  ABI break on a 14.0-only API; the
gpu library is still young enough that this is acceptable.

Marked new API with XSince: REPLACEME.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 02:26:21 -06:00
Behdad Esfahbod 9d52c16abb [fuzzing] hb-vector-fuzzer: also exercise PDF output format
Previously only HB_VECTOR_FORMAT_SVG was fuzzed.  Factor the
create/paint/render body into a helper and call it twice per
input — once for SVG, once for PDF — so the PDF-specific code
paths get the same coverage:

  - PDF content-stream encoder
  - Type 2/3 shading + stitching function builder
  - Type 6 Coons-patch sweep gradient encoder (hb_pdf_add_sweep_patch)
  - PNG IDAT passthrough
  - Indexed-PNG SMask builder (hb_pdf_build_indexed_smask, zlib inflate
    + PNG un-filter + palette-to-alpha mapping)

Input-fuzzed knobs (precision, flat-draw, flat-paint) are shared
between the two formats.

Smoke-tested on a handful of existing fuzzing/fonts corpus entries;
all pass clean.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 01:40:02 -06:00
Behdad Esfahbod 4650398a07 [gpu] encode() returns extents + auto-clears; drop get_extents() (#5910)
Bring hb_gpu_draw_encode into line with the rest of the render APIs:
auto-clear on success, and return the computed glyph extents to the
caller via a new out-parameter (nullable).

  HB_EXTERN hb_blob_t *
  hb_gpu_draw_encode (hb_gpu_draw_t      *draw,
                      hb_glyph_extents_t *extents);

Consumers typically need both the encoded blob and the glyph's
extents to size the quad they'll draw into in the vertex shader;
returning them together removes a redundant trip through the draw
state and lets encode() auto-clear like hb_vector_*_render() and
hb_raster_*_render() do.

Drop hb_gpu_draw_get_extents() from the public API — its only
role was reading extents out of the draw state post-encode, which
the new encode() signature makes redundant.  The computation
stays (moved to a static _hb_gpu_draw_get_extents helper) so
encode() can call it before auto-clear wipes the state.

ABI break for anyone who had already wired up against the 14.0.0
hb_gpu_draw_encode signature or hb_gpu_draw_get_extents, but the
library is new/experimental and the two in-tree consumers
(test/api/test-gpu.cc, util/gpu/demo-font.cc, util/hb-gpu-encode-all.cc,
test/fuzzing/hb-gpu-fuzzer.cc) are updated here.

All 240 tests pass.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 00:15:54 -06:00
Behdad Esfahbod 5e6ae10f62 [raster][vector] Remove SVG-font glyph source rendering
Remove support for rendering glyphs whose source is the OpenType
SVG table, in both hb-raster and hb-vector.  SVG is a large format
and our in-tree parser/renderer was half-finished; the complexity
was not justified by the partial coverage.

This is source-side only: hb-vector's SVG *output* backend (used
to serialize outline / COLR glyphs to SVG) is unaffected.  Fonts
that have an SVG table will still load; glyphs served only by that
table will not render from these libraries.

hb-raster:
  - Delete the hb-raster-svg-* file cluster (11 .cc/.hh files plus
    hb-raster-svg.hh: parse, base, bbox, clip, color, defs,
    defs-scan, fill, gradient, parse, render, use, context).
  - Drop svg_glyph/svg_font/svg_palette state from
    hb_raster_paint_t and the HB_PAINT_IMAGE_FORMAT_SVG branch in
    the image callback.
  - Drop hb_raster_paint_push_clip_path + hb_raster_svg_path_func_t
    (only used from the SVG renderer).
  - Drop zlib dependency (gzip decompression was only used for the
    SVG table reader; libpng brings its own zlib linkage for PNGs).

hb-vector:
  - Delete hb-vector-svg-subset.{cc,hh} (extracted font-SVG glyph
    for embedding in SVG output).
  - Remove the HB_TAG('s','v','g',' ') branch from the image paint
    callback; PNG embedding remains.
  - Drop svg_image tracking state from hb_vector_paint_t
    (current_color_glyph_has_svg_image, current_svg_image_glyph,
    current_face, svg_image_counter, subset_body_scratch).
  - Simplify the color-glyph cache to store just def_id instead of
    packed (def_id, image_like).
  - Drop hb_svg_append_image_instance_translate.

Build wiring: src/meson.build, CMakeLists.txt, harfbuzz-world.cc
updated to match.  Test suite: remove SVG-specific cases from
test/api/test-raster.cc and the handwriting-{svg,picosvg} vector
tests + expected fixtures.

~5700 LOC deleted, ~10 LOC added.  Full test suite (240 tests)
passes.

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-11 22:46:35 -06:00
Behdad Esfahbod 0cf2fa0929 Merge pull request #5891 from harfbuzz/vector-pdf
PDF output from hb-vector
2026-04-11 22:20:01 -06:00
Behdad Esfahbod 30ee1bb2c6 [vector] Rename hb_vector_svg_*set_{flat,precision} API
Drop "svg" from the names since flat/precision settings apply
equally to PDF output.  The new names follow the hb_vector_draw_*
/ hb_vector_paint_* convention:

  hb_vector_svg_set_flat         -> hb_vector_draw_set_flat
  hb_vector_svg_set_precision    -> hb_vector_draw_set_precision
  hb_vector_svg_paint_set_flat   -> hb_vector_paint_set_flat
  hb_vector_svg_paint_set_precision -> hb_vector_paint_set_precision

Assisted-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-10 12:16:55 -06:00
Behdad Esfahbod 6c2afd1e0e [geometry] Clamp glyph extents conversion
hb_extents_t::to_glyph_extents() rounded bounds to hb_position_t
before subtracting them. Extreme extents from fuzzed VARC and
raster paths could therefore overflow signed int arithmetic while
forming glyph widths or heights.

Compute the rounded endpoints in double, clamp the exported values
to hb_position_t, and reject non-finite results. Also avoid the
same overflow in the reverse hb_glyph_extents_t constructor, add
an internal regression for the shared helper, and include the
minimized raster testcase.

Fixes: https://oss-fuzz.com/testcase-detail/4826864710647808
Not-tested: Skipped by request.
Assisted-by: OpenAI Codex
2026-04-09 23:21:05 -06:00
Behdad Esfahbod b5287cb3fb [gpu] Clamp exported draw extents
hb_gpu_draw_get_extents() cast rounded bounds to hb_position_t
before subtracting them. A fuzzed glyph outline with an extreme
negative bound could therefore overflow signed int arithmetic while
forming width or height.

Compute the rounded bounds in double, reject non-finite values, and
clamp the exported extents to hb_position_t before returning them.
Add an API regression that covers the OSS-Fuzz testcase through the
public GPU draw API.

Fixes: https://oss-fuzz.com/testcase-detail/5918954634936320
Tested: meson test -C build --print-errorlogs
Assisted-by: OpenAI Codex
2026-04-09 23:15:34 -06:00
💀 f4600f3c87 test: add hb-ft load-flag preservation regression 2026-04-09 14:57:29 -06:00
Behdad EsfahbodandOpenAI Codex 03c262d1eb [raster] Clamp reflected gradient LUT coordinates
Malformed reflected gradients can produce very large finite
gradient parameters. The raster backend normalized reflect
mode by casting floorf(t) to int, which can overflow and
drive LUT lookups past the fixed-size stack buffer.

Fold reflected coordinates with fmodf(), clamp LUT inputs
defensively, and add the minimized oss-fuzz raster
testcase to the corpus.

Fixes: https://oss-fuzz.com/testcase-detail/6597066439852032
Tested: meson test -C build
Co-Authored-By: OpenAI Codex <codex@openai.com>
2026-04-06 17:13:58 -06:00
Behdad Esfahbod 3b7ee09f0e [gpu] Fix draw encode blob ownership leaks
hb_gpu_draw_encode() could allocate a fresh output buffer and then
return NULL before transferring ownership to a blob, leaking that
temporary buffer on late encode failures.

The recycled-blob growth path also tried hb_realloc() first and fell
back to hb_malloc() when that failed. Once the new buffer was committed
to the recycled blob, the old recycled buffer could be orphaned.

Free temporary buffers on the remaining late-failure exits and keep
the old recycled buffer alive until a replacement has been committed.
Tighten the GPU API recycle test to encode a larger second outline so
that reused-buffer growth stays covered.

Tested:
- meson test -C build

Assisted-by: OpenAI Codex <codex@openai.com>
2026-04-04 19:44:54 -06:00
Behdad Esfahbod e8c5bb452d [raster] Widen edge row interpolation arithmetic
edge_sweep_row() computed the clipped endpoint x coordinates with
int32_t subtractions before casting to int64_t. If an edge started at
INT32_MIN and the scanline clipped endpoint was near zero, ey - yL
overflowed before the slope multiply and UBSan flagged the raster
fuzzer path.

Keep the endpoint interpolation and follow-on dx math in 64-bit until
clamping back to int32_t. This preserves the existing rasterizer logic
while avoiding signed overflow for extreme edge coordinates.

Tested:
- meson test -C build

Assisted-by: OpenAI Codex <codex@openai.com>
2026-04-03 18:07:28 -06:00
Behdad EsfahbodandOpenAI Codex 19e4bf2597 [raster] Validate non-rect clip masks before reuse
The raster paint clip code indexed non-rect clip alpha rows directly,
assuming the clip metadata always matched a full stride*height backing
buffer. Fuzzing showed that reused clip objects can reach those reads
with stale scalar state, which turns the row access in
push_clip_rectangle() into a heap-buffer-overflow.

Reset cached clip geometry on acquire, add a validity check for
non-rect alpha masks, and refuse to build new clip intersections from
an invalid mask. The fallback is an empty clip, which keeps the paint
path safe without affecting well-formed clips.

Testing:
- meson test -C build --suite fuzzing --print-errorlogs
- meson test -C build

Co-authored-by: OpenAI Codex <codex@openai.com>
2026-04-03 17:17:49 -06:00
Behdad EsfahbodandOpenAI Codex 98866c9fef [svg] Zero-initialize doc cache parser structs
MemorySanitizer reported use of uninitialized data while building the
SVG document cache used by vector painting. The parser keeps small
aggregate structs on the stack, and their padding could stay poisoned
as they were copied through the open-element stack.

Zero-initialize the parser stack and the temporary span / element
records before filling their fields. That keeps later copies and
container insertions from carrying undefined padding into the MSan
instrumented path.

Testing:
- meson test -C build --suite fuzzing --print-errorlogs
- meson test -C build

Co-authored-by: OpenAI Codex <codex@openai.com>
2026-04-03 16:49:48 -06:00
Behdad EsfahbodandOpenAI Codex bfafd9d648 [gpu] Check draw encode size arithmetic
The GPU fuzzer can build outline data large enough to stress the size
math inside hb_gpu_draw_encode(). Several totals and byte counts were
computed with unchecked unsigned arithmetic before the existing bounds
checks ran.

Use HarfBuzz's overflow helpers for band-index totals, encoded blob
lengths, byte sizes, and recycled-buffer growth. Also rewrite the
uint16-offset limit check so it does not rely on subtracting from an
already-overflowed total.

Testing:
- meson test -C build --suite fuzzing --print-errorlogs
- meson test -C build

Fixes: https://oss-fuzz.com/testcase-detail/6336503637934080
Co-authored-by: OpenAI Codex <codex@openai.com>
2026-04-03 16:44:50 -06:00
Behdad EsfahbodandOpenAI Codex 92b9b1ed82 [docs] Clarify OOM policy for experimental libraries
Document that raster, vector, and gpu still must not crash,
access out of bounds, or corrupt memory under failing-malloc,
but best-effort early failure is acceptable and complex rollback
logic is not required after OOM.

Co-authored-by: OpenAI Codex <codex@openai.com>
2026-04-03 16:24:31 -06:00
Behdad EsfahbodandOpenAI Codex a4aff49856 [gpu] Fix contour breaks and bounds quantization in encode (#5892)
Encode used adjacent endpoint equality to infer contour breaks, which
fails when two contours touch at the same point. Track contour starts
explicitly during accumulation instead, and use that flag when packing
shared endpoints.

Encode also rounded blob bounds to the nearest quarter unit. That can
shrink the encoded extents and clip coverage at glyph edges. Quantize
mins downward and maxes upward so the blob header always encloses the
actual geometry.

Add focused API tests for both regressions.

Testing:
- meson test -C build

Co-authored-by: OpenAI Codex <codex@openai.com>
2026-04-03 12:09:02 -06:00
Behdad EsfahbodandClaude Opus 4.6 d46f8e3444 [gpu] Rename HB_GPU_SHADER_GLSL_330 to HB_GPU_SHADER_LANG_GLSL
The shader source is compatible with both GLSL 3.30 and
GLSL ES 3.00.  Rename the enum value to reflect this and
match the type name convention.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 00:40:11 -06:00
Behdad EsfahbodandClaude Opus 4.6 f0c127fc62 [fuzzing] Remove _fuzzing_skip_leading_comment
The leading-comment mechanism corrupted fuzzer font files by
prepending non-font data.  Remove the function, its callers,
and strip the one seed file that had a leading comment.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 00:14:36 -06:00
Behdad EsfahbodandClaude Opus 4.6 5343d687d7 [raster,vector] Simplify fuzzers: drop shaping, just render glyphs
Shaping is already exercised by the shape fuzzer.  Simplify
the raster and vector fuzzers to just render gids 0..15
directly, matching the GPU fuzzer pattern.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-28 00:08:25 -06:00
Behdad EsfahbodandClaude Opus 4.6 0582846c08 [gpu] Simplify shader API: return single string per language
Change hb_gpu_shader_{fragment,vertex}_sources() (array + count)
to hb_gpu_shader_{fragment,vertex}_source() returning a single
const char *.  Each language variant is one complete string.

Simplifies the demo shader compilation and documentation.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 17:10:04 -06:00
Behdad EsfahbodandClaude Opus 4.6 89ef51691b [gpu] Add GPU encode fuzzer
Encodes up to 16 glyphs per fuzzed font input, exercising
draw accumulation, cu2qu, encode, extents, and blob recycling.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 15:18:47 -06:00
Behdad EsfahbodandClaude Opus 4.6 971e607319 [gpu] Add API test and google-benchmark encode benchmark
test-gpu: 7 subtests covering create/destroy, empty encode,
glyph encode, reset, draw funcs singleton, shader sources,
and recycle_blob.

benchmark-gpu: benchmarks draw, encode, and draw+encode
per-glyph across several fonts using google-benchmark.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-26 22:27:40 -06:00
Behdad Esfahbod 6e45962326 [raster] Guard direct SVG paint callbacks
The raster SVG image callback assumes hb_raster_paint_glyph()
seeded svg_font/svg_glyph state before the paint protocol
reaches HB_PAINT_IMAGE_FORMAT_SVG. Clients can also call
hb_font_paint_glyph_or_fail() directly with
hb_raster_paint_get_funcs(), which leaves svg_font unset and
crashes in hb_raster_svg_render().

Return false when the callback is reached without that
wrapper-only state, and cover the direct API path with a
regression test.

Testing: CCACHE_DISABLE=1 meson test -C build
Fixes https://github.com/harfbuzz/harfbuzz/security/advisories/GHSA-ggh9-cgg2-r4vr
Assisted-by: OpenAI Codex <codex@openai.com>
2026-03-26 14:41:14 -06:00
Behdad EsfahbodandClaude Opus 4.6 d134f88824 [vector] Fix clear() when vector is in error state
clear() called resize(0) which calls alloc(0), but alloc() bails
immediately when the vector is in error state, so length was never
zeroed. Under failing-malloc, a vector could enter error state from
a failed push, making all subsequent clear() calls silently fail,
leaving stale pointers.

This caused a heap-use-after-free in hb_raster_paint_destroy():
render() released surfaces to surface_cache then called
surface_stack.clear() which silently failed, so destroy() iterated
the stale surface_stack pointers and double-freed images already
in surface_cache.

Fixes https://oss-fuzz.com/testcase?key=5078588000108544

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 20:26:18 -06:00
Behdad Esfahbod 074b6c826a Add a fuzzer test 2026-03-20 17:50:08 -06:00
Behdad Esfahbod be6fca21f5 [vector] Fail closed on SVG paint body OOM
The SVG paint backend could keep going after failing-malloc
poisoned the root or nested group body buffers. In particular,
hb_vector_paint_ensure_initialized() only checked group_stack
itself, not the allocated root body, and some group and glyph
paths ignored push_or_fail() or append-induced vector error
state.

Treat an errored root body as initialization failure, reject
cached and non-cached glyph emission when defs, current body,
or captured scratch enter error state, and stop ignoring
nested group push failure.

Testing: CCACHE_DISABLE=1 meson test -C build-codex

Fixes: https://oss-fuzz.com/testcase-detail/4521131913838592
Assisted-by: OpenAI Codex
2026-03-19 09:55:24 -06:00
Behdad Esfahbod 6af602fa64 Add a fuzzer font 2026-03-19 09:47:44 -06:00
Behdad Esfahbod c89f1c20b0 [raster] Harden color-stop fetches under failing malloc (#5858)
Malformed COLRv1 gradients can report a non-zero stop count and then
leave the effective stop array empty once allocations fail. The raster
path assumed the second fetch always produced at least one stop and
indexed stops[0], which let fuzzing hit a stack-buffer-underflow in
evaluate_color_line().

Reject empty effective stop lists after the second fetch, handle
zero-stop and one-stop evaluation defensively, and clamp non-finite
gradient parameters before lookup. Apply the same stop-fetch
hardening to the Cairo and vector paint helpers, including fixing
Cairos wrong NULL check after hb_malloc().

Testing: not run (per user request)

Fixes: https://oss-fuzz.com/testcase-detail/4635834988167168
Assisted-by: OpenAI Codex
2026-03-18 16:06:29 -06:00
Behdad Esfahbod 4462e9862d [vector][colr] Guard color_glyph reentry in SVG paint backend
The vector color_glyph callback can re-enter painting through
PaintColrGlyph, creating an unbounded callback recursion path that does
not consume COLR paint-context depth counters.

Track active color glyphs to reject immediate/indirect cycles and add a
hard nested callback depth cap based on HB_MAX_NESTING_LEVEL.

Tested:
- CCACHE_DISABLE=1 ninja -C /home/behdad/harfbuzz/build-codex test/fuzzing/hb-vector-fuzzer
- for f in test/fuzzing/fonts/crash-* test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-*; do ./build-codex/test/fuzzing/hb-vector-fuzzer "$f"; done

Fixes: https://oss-fuzz.com/testcase-detail/5985308075425792
Assisted-by: OpenAI Codex
2026-03-17 09:39:46 -06:00
Behdad Esfahbod df9b6ff641 [vector][colr] Sanitize ColorLine extend values
COLR ColorLine::get_extend() cast the raw Extend byte directly to
hb_paint_extend_t. Malformed tables can carry out-of-range values,
which then trip UBSan enum checks in vector paint paths.

Map raw extend values explicitly and fall back to PAD for unknown
values.

Tested:
- CCACHE_DISABLE=1 ninja -C /home/behdad/harfbuzz/build-codex test/fuzzing/hb-vector-fuzzer test/fuzzing/hb-raster-fuzzer
- ./build-codex/test/fuzzing/hb-vector-fuzzer test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-vector-fuzzer-5448347908636672
- ./build-codex/test/fuzzing/hb-vector-fuzzer test/fuzzing/fonts/NotoColorEmoji.subset.ttf

Fixes: https://oss-fuzz.com/testcase-detail/6120408016551936
Assisted-by: OpenAI Codex
2026-03-17 09:33:31 -06:00
Behdad Esfahbod acc0a54ddc [raster] Remove remaining signed-shift UB in edge stepping
Raster edge setup still had signed-shift UB in two places:
- slope scaling in emit_segment() when dx is negative
- x-boundary stepping in edge_sweep_row() when cx0 is negative

Compute slope with widened subtraction and multiplication, and compute
x_b using widened multiply plus clamp.

Tested:
- ./build-codex/test/fuzzing/hb-raster-fuzzer test/fuzzing/fonts/crash-d223bc42a8226c4d655c417d63d9a76760d05985
- ./build-codex/test/fuzzing/hb-raster-fuzzer test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6454028543524864

Assisted-by: OpenAI Codex
2026-03-17 09:27:48 -06:00
Behdad Esfahbod bdbf83e26d [raster] Avoid signed-shift UB in scanline y conversion
hb_raster_draw_render() computed y_top by left-shifting a signed value
that can be negative when extents have a negative y origin.

Switch to 64-bit multiplication by HB_RASTER_ONE_PIXEL and clamp to
int32_t range before use, preserving behavior without signed-shift UB.

Tested:
- CCACHE_DISABLE=1 ninja -C /home/behdad/harfbuzz/build-codex test/fuzzing/hb-raster-fuzzer
- ./build-codex/test/fuzzing/hb-raster-fuzzer test/fuzzing/fonts/clusterfuzz-testcase-minimized-hb-raster-fuzzer-6454028543524864

Fixes: https://oss-fuzz.com/testcase-detail/6454028543524864
Assisted-by: OpenAI Codex
2026-03-17 09:19:07 -06:00
Behdad Esfahbod b4115f23ee [subset] Harden renormalizeValue for malformed avar triples
Avoid abort/assert in renormalizeValue when avar triples are malformed
and guard zero-distance divisions.

Add a regression case in test-subset-instancer-solver and include the
OSS-Fuzz reproducer file.

Fixes: https://oss-fuzz.com/testcase-detail/6461128124465152
Assisted-by: OpenAI Codex
2026-03-17 09:12:35 -06:00
Behdad Esfahbod ba53130d2c [vector][svg] Harden subset spans under failing-malloc
Validate cached SVG spans before use and make cache parsing
allocation-failure safe.

Fixes: https://oss-fuzz.com/testcase-detail/5448347908636672
Assisted-by: OpenAI Codex
2026-03-17 09:12:35 -06:00
Behdad Esfahbod 6815666a74 [raster] Preserve base transform on failed pushes
The raster backend lets push_transform() fail under the fuzzing failing-
allocator path, but pop_transform() always removed the current stack
entry. That can discard the base transform when a nested push did not
actually land, corrupting subsequent raster operations.

Guard pop_transform() so it only removes nested transforms and preserves
the base transform frame.

Tested:
- meson compile -C build hb-raster-fuzzer

Speculative fix for: https://oss-fuzz.com/testcase-detail/4743569679384576
Assisted-by: OpenAI Codex
2026-03-14 13:29:51 -03:00
Behdad Esfahbod 03006b06ba [raster] Avoid signed-shift UB in edge slope setup
emit_segment() formed its 16.16 fixed-point slope by left-shifting a
signed delta-x value. Negative dx values make that shift undefined, and
UBSan reports it on malformed glyph outlines.

Use 64-bit multiplication by 1<<16 instead, preserving the same fixed-
point result without relying on signed-shift behavior.

Tested:
- meson compile -C build hb-raster-fuzzer

Fixes: https://oss-fuzz.com/testcase-detail/4797078076456960
Assisted-by: OpenAI Codex
2026-03-14 13:22:56 -03:00
Behdad Esfahbod cb0757aad9 [raster] Reject empty gradient stop lists
The raster gradient code assumed every color line had at least one stop.
Malformed COLR data can report an empty stop list, which then reaches
normalize_color_line() and evaluate_color_line() and reads stops[0].

Treat empty color lines as a failed gradient fetch so malformed inputs
render nothing instead of dereferencing past the stop array.

Tested:
- meson compile -C build hb-raster-fuzzer

Fixes: https://oss-fuzz.com/testcase-detail/5216672087867392
Assisted-by: OpenAI Codex
2026-03-14 13:19:29 -03:00
Behdad Esfahbod ef191e9b2c [raster] Guard SVG stack pops on allocation failure
The raster SVG renderer assumed push_group() and push_transform()
always succeeded. Under the fuzzing failing-allocator path those pushes
can fail, but the matching pops still ran unconditionally.

When SVG rendering is nested inside an existing paint operation, an
unmatched pop can remove the caller's transform or group and later trip
the stack-balance assertions.

Track whether each raster SVG push actually grew the corresponding stack
and only pop in that case.

Tested:
- meson compile -C build hb-raster-fuzzer
- build/test/fuzzing/hb-raster-fuzzer test/fuzzing/fonts/NotoColorEmoji.subset.ttf

Fixes: https://oss-fuzz.com/testcase-detail/5627961964560384
Assisted-by: OpenAI Codex
2026-03-14 13:17:05 -03:00
Behdad Esfahbod c197509b4b [vector] Avoid stale SVG paint body references (#5843)
The non-cached SVG paint path kept a reference to the current group
body across hb_font_paint_glyph_or_fail(). Nested paint groups can
grow group_stack and move its storage, leaving that reference dangling.

Append to current_body() directly before and after the recursive
paint call so the code never reuses a body reference after the stack
may have reallocated.

Fixes: https://oss-fuzz.com/testcase-detail/5622008821121024
Testing: meson test -C build
Assisted-by: OpenAI Codex
2026-03-13 14:32:22 -06:00
Khaled Hosny b327f0c672 [tests] Fix warning when building without zlib
../test/api/test-raster.cc:67:1: warning: unused function 'render_svg_glyph' [-Wunused-function]
   67 | render_svg_glyph (hb_face_t      *face,
      | ^~~~~~~~~~~~~~~~
1 warning generated.
2026-03-12 17:36:39 +02:00
Behdad Esfahbod 8e6b9ecf4e [test] Add vector coverage for handwriting color fonts
Extend the vector regression tests to cover the remaining handwriting
fuzzing fonts: sbix, glyf COLRv1, and CFF/CFF2 COLRv1.

Reuse the expected output where the CFF and CFF2 variants serialize
identically.

Assisted-by: OpenAI Codex
2026-03-12 01:49:07 -03:00
Behdad Esfahbod 8fc1d1bfd1 [vector] Allow duplicate SVG refs during subsetting
Some SVG glyph documents reuse the same referenced element multiple
times. The vector subsetter treated a duplicate ref as a hard failure,
which made hb-vector drop glyphs like U+270D from the handwriting
picosvg test fonts entirely.

Treat duplicate and empty refs as non-fatal while still uniquing the
collected ids. Add regressions for the handwriting picosvg and picosvgz
fonts, reusing a shared expected output for the pair.

Fixes: https://github.com/harfbuzz/harfbuzz/issues/5829
Assisted-by: OpenAI Codex
2026-03-12 01:46:46 -03:00
Behdad Esfahbod 00da68ef5c [svg] Share svgz normalization across raster and vector
Vector SVG extraction still treated SVG table documents as plain text,
so svgz payloads worked in raster but failed in hb-vector.

Move the gzip helpers into a new internal hb-zlib.hh header and add a
shared SVG blob normalization helper in svg.hh. Raster and vector now
both normalize SVG documents before doc-cache or rendering/subsetting
logic sees them, with the same 16 MiB uncompressed size limit.

Also link zlib into harfbuzz-vector when available and add a vector
regression that checks matching output for the existing plain and
compressed handwriting SVG fonts.

Tested:
- meson test -C build

Assisted-by: OpenAI Codex
2026-03-11 20:23:51 -03:00
Behdad Esfahbod b7e4f5fd62 [raster] Support gzip-compressed SVG documents
The raster SVG path treated hb_ot_color_glyph_reference_svg()
blobs as plain XML, even though SVG table documents may
legally be gzip encoded. That caused hb-raster to reject
fonts that store compressed SVG documents.

Detect gzip payloads in the raster SVG renderer, reject
documents whose advertised uncompressed size exceeds
HB_SVG_MAX_DOCUMENT_SIZE, and inflate the rest through
zlib while keeping the same size cap enforced during
decompression. Plain SVG documents are also bounded by the
same 16 MiB limit.

Add a raster API regression that renders the existing
plain and compressed noto_handwriting SVG fixtures and
verifies identical output.

Testing:
- meson test -C build

Assisted-by: OpenAI Codex
2026-03-11 14:18:31 -03:00
Khaled Hosny b54f2212f1 Don’t build raster and vector fuzzers if the library is disabled
Fixes https://github.com/harfbuzz/harfbuzz/issues/5821
2026-03-11 16:05:12 +02:00
Garret Rieger 51b9d2c756 Add fuzzer seed for malformed axis ranges. 2026-03-10 11:58:52 -06:00
Behdad Esfahbod 47b0ce552d [fuzzing] Guard raster/vector create_or_fail results
The raster and vector fuzzers called create_or_fail() and then
immediately dereferenced the returned pointers. Under CIFuzz's
allocation-failure coverage, that turns a valid NULL return into a
harness crash instead of treating it as an out-of-memory case.

Return early after cleaning up when draw or paint context creation
fails. This keeps the fuzzers aligned with the create_or_fail()
contract and avoids null-deref crashes unrelated to library logic.

Tested:
- meson compile -C build hb-raster-fuzzer hb-vector-fuzzer
- build/test/fuzzing/hb-vector-fuzzer test/fuzzing/fonts/TwemojiMozilla.subset.ttf
- build/test/fuzzing/hb-raster-fuzzer test/fuzzing/fonts/TwemojiMozilla.subset.ttf

Assisted-by: OpenAI Codex
2026-03-10 10:04:25 -03:00