Coverage Report

Created: 2026-12-01 06:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libheif/fuzzing/sequence_fuzzer.cc
Line
Count
Source
1
/*
2
 * HEIF codec.
3
 * Copyright (c) 2026 struktur AG, Arthur SC Chan <arthur.chan@adalogics.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
#include <stdint.h>
22
#include <stdlib.h>
23
24
#include "libheif/heif.h"
25
#include "libheif/heif_sequences.h"
26
27
49.3k
#define MAX_FRAMES 32
28
29
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
30
25.5k
{
31
25.5k
  heif_context* ctx = heif_context_alloc();
32
25.5k
  if (!ctx) {
33
0
    return 0;
34
0
  }
35
36
25.5k
  auto* limits = heif_context_get_security_limits(ctx);
37
25.5k
  limits->max_total_memory = UINT64_C(2) * 1024 * 1024 * 1024;
38
25.5k
  limits->max_memory_block_size = 128 * 1024 * 1024;
39
  // Also bound the image size itself. libheif's memory accounting only covers its own
40
  // allocations, but the codec libraries allocate their frame buffers before libheif
41
  // sees anything (dav1d needs ~12.5 bytes/pixel for a frame header alone). Under MSan,
42
  // where every allocation costs about three times its size in RSS, a 56 Mpixel AV1 frame
43
  // header was enough to exceed libFuzzer's 2560 MB limit.
44
25.5k
  limits->max_image_size_pixels = 16 * 1024 * 1024; // 16 Mpixel
45
46
25.5k
  heif_error err = heif_context_read_from_memory(ctx, data, size, nullptr);
47
25.5k
  if (err.code != heif_error_Ok) {
48
12.1k
    heif_context_free(ctx);
49
12.1k
    return 0;
50
12.1k
  }
51
52
13.3k
  if (heif_context_has_sequence(ctx)) {
53
11.8k
    heif_track* track = heif_context_get_track(ctx, 0);
54
11.8k
    if (track) {
55
11.8k
      uint16_t w = 0, h = 0;
56
11.8k
      heif_track_get_image_resolution(track, &w, &h);
57
11.8k
      heif_track_get_timescale(track);
58
11.8k
      heif_track_get_track_handler_type(track);
59
60
11.8k
      int frames = 0;
61
49.3k
      while (frames++ < MAX_FRAMES) {
62
49.2k
        heif_image* img = nullptr;
63
49.2k
        err = heif_track_decode_next_image(track, &img,
64
49.2k
                                           heif_colorspace_YCbCr,
65
49.2k
                                           heif_chroma_420, nullptr);
66
49.2k
        if (err.code != heif_error_Ok || img == nullptr) {
67
11.7k
          break;
68
11.7k
        }
69
37.5k
        heif_image_release(img);
70
37.5k
      }
71
72
11.8k
      heif_track_release(track);
73
11.8k
    }
74
11.8k
  }
75
76
13.3k
  heif_context_free(ctx);
77
13.3k
  return 0;
78
25.5k
}