Coverage Report

Created: 2026-07-30 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/fuzzing/tile_fuzzer.cc
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2026 Dirk Farin <dirk.farin@gmail.com>
4
 *
5
 * This file is part of libheif.
6
 *
7
 * libheif is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Lesser General Public License as
9
 * published by the Free Software Foundation, either version 3 of
10
 * the License, or (at your option) any later version.
11
 *
12
 * libheif is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public License
18
 * along with libheif.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
// Fuzz harness for the per-tile read API (heif_image_handle_get_image_tiling,
22
// heif_image_handle_get_grid_image_tile_id, heif_image_handle_decode_image_tile).
23
// file_fuzzer.cc only calls heif_decode_image, which routes grid images through
24
// decode_full_grid_image and never exercises decode_grid_tile or the surrounding
25
// tiling-API surface. This harness fills that gap.
26
27
#include <assert.h>
28
#include <limits.h>
29
#include <stdint.h>
30
#include <stdlib.h>
31
32
#include "libheif/heif.h"
33
#include "libheif/heif_tiling.h"
34
35
static const enum heif_colorspace kFuzzColorSpace = heif_colorspace_YCbCr;
36
static const enum heif_chroma kFuzzChroma = heif_chroma_420;
37
38
// Cap how many tiles we try to decode per image. Synthetic grids can claim
39
// millions of tiles; bounding here keeps the per-input time budget sane
40
// without losing meaningful coverage (crashes show up on the first few tiles).
41
static const uint32_t kMaxTilesPerImage = 32;
42
43
static void TestTileAPI(const struct heif_image_handle* handle,
44
                        int process_image_transformations)
45
54.4k
{
46
54.4k
  struct heif_image_tiling tiling = {};
47
54.4k
  struct heif_error err = heif_image_handle_get_image_tiling(
48
54.4k
      handle, process_image_transformations, &tiling);
49
54.4k
  if (err.code != heif_error_Ok) {
50
55
    return;
51
55
  }
52
53
  // Probe per-tile id lookup and decode within the declared grid, capped.
54
54.3k
  uint32_t cols = tiling.num_columns;
55
54.3k
  uint32_t rows = tiling.num_rows;
56
54.3k
  uint32_t total = 0;
57
58
115k
  for (uint32_t ty = 0; ty < rows && total < kMaxTilesPerImage; ty++) {
59
146k
    for (uint32_t tx = 0; tx < cols && total < kMaxTilesPerImage; tx++) {
60
85.4k
      heif_item_id tile_id = 0;
61
      // Return value intentionally ignored; we only care that the call does
62
      // not crash on malformed input.
63
85.4k
      heif_image_handle_get_grid_image_tile_id(
64
85.4k
          handle, process_image_transformations, tx, ty, &tile_id);
65
66
85.4k
      struct heif_image* tile_img = nullptr;
67
85.4k
      err = heif_image_handle_decode_image_tile(
68
85.4k
          handle, &tile_img, kFuzzColorSpace, kFuzzChroma, nullptr, tx, ty);
69
85.4k
      if (err.code == heif_error_Ok && tile_img != nullptr) {
70
7.94k
        heif_image_release(tile_img);
71
77.5k
      } else if (tile_img != nullptr) {
72
        // Defensive: some error paths may still produce an image we own.
73
0
        heif_image_release(tile_img);
74
0
      }
75
76
85.4k
      total++;
77
85.4k
    }
78
61.1k
  }
79
54.3k
}
80
81
static int clip_int(size_t size)
82
24.8k
{
83
24.8k
  return size > INT_MAX ? INT_MAX : static_cast<int>(size);
84
24.8k
}
85
86
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
87
24.8k
{
88
24.8k
  struct heif_context* ctx;
89
24.8k
  struct heif_error err;
90
24.8k
  struct heif_image_handle* primary_handle = nullptr;
91
24.8k
  int images_count;
92
24.8k
  heif_item_id* image_IDs = nullptr;
93
24.8k
  bool explicit_init = size == 0 || data[size - 1] & 1;
94
95
24.8k
  if (explicit_init) {
96
10.3k
    heif_init(nullptr);
97
10.3k
  }
98
99
24.8k
  heif_check_filetype(data, clip_int(size));
100
101
24.8k
  ctx = heif_context_alloc();
102
24.8k
  assert(ctx);
103
104
24.8k
  auto* limits = heif_context_get_security_limits(ctx);
105
24.8k
  limits->max_total_memory = UINT64_C(2) * 1024 * 1024 * 1024;
106
24.8k
  limits->max_memory_block_size = 128 * 1024 * 1024;
107
108
24.8k
  err = heif_context_read_from_memory(ctx, data, size, nullptr);
109
24.8k
  if (err.code != heif_error_Ok) {
110
6.01k
    goto quit;
111
6.01k
  }
112
113
18.8k
  err = heif_context_get_primary_image_handle(ctx, &primary_handle);
114
18.8k
  if (err.code == heif_error_Ok) {
115
17.6k
    TestTileAPI(primary_handle, /*process_image_transformations=*/1);
116
17.6k
    TestTileAPI(primary_handle, /*process_image_transformations=*/0);
117
17.6k
    heif_image_handle_release(primary_handle);
118
17.6k
    primary_handle = nullptr;
119
17.6k
  }
120
121
18.8k
  images_count = heif_context_get_number_of_top_level_images(ctx);
122
18.8k
  if (!images_count) {
123
213
    goto quit;
124
213
  }
125
126
18.6k
  image_IDs = static_cast<heif_item_id*>(malloc(images_count * sizeof(heif_item_id)));
127
18.6k
  assert(image_IDs);
128
18.6k
  images_count = heif_context_get_list_of_top_level_image_IDs(ctx, image_IDs, images_count);
129
18.6k
  if (!images_count) {
130
0
    goto quit;
131
0
  }
132
133
39.7k
  for (int i = 0; i < images_count; ++i) {
134
21.0k
    struct heif_image_handle* image_handle = nullptr;
135
21.0k
    err = heif_context_get_image_handle(ctx, image_IDs[i], &image_handle);
136
21.0k
    if (err.code != heif_error_Ok) {
137
2.05k
      heif_image_handle_release(image_handle);
138
2.05k
      continue;
139
2.05k
    }
140
141
19.0k
    TestTileAPI(image_handle, /*process_image_transformations=*/1);
142
143
    // Also iterate thumbnails — these can themselves be grid/overlay items
144
    // and have separate decoder paths.
145
19.0k
    int num_thumbnails = heif_image_handle_get_number_of_thumbnails(image_handle);
146
19.0k
    for (int t = 0; t < num_thumbnails; ++t) {
147
7
      struct heif_image_handle* thumbnail_handle = nullptr;
148
7
      heif_image_handle_get_thumbnail(image_handle, t, &thumbnail_handle);
149
7
      if (thumbnail_handle) {
150
0
        TestTileAPI(thumbnail_handle, /*process_image_transformations=*/1);
151
0
        heif_image_handle_release(thumbnail_handle);
152
0
      }
153
7
    }
154
155
19.0k
    heif_image_handle_release(image_handle);
156
19.0k
  }
157
158
24.8k
quit:
159
24.8k
  heif_image_handle_release(primary_handle);
160
24.8k
  heif_context_free(ctx);
161
24.8k
  free(image_IDs);
162
163
24.8k
  if (explicit_init) {
164
10.3k
    heif_deinit();
165
10.3k
  }
166
167
24.8k
  return 0;
168
18.6k
}