Coverage Report

Created: 2025-08-26 06:13

/src/opusfile_fuzzer.c
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <errno.h>
16
#include <stdint.h>
17
#include <stdio.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "opusfile/config.h"
22
#include "opusfile/include/opusfile.h"
23
24
// Opusfile fuzzing wrapper to help with automated fuzz testing. It's based on
25
// https://github.com/xiph/opusfile/blob/master/examples/opusfile_example.c
26
6.17k
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
27
6.17k
  int ret, tmp;
28
6.17k
  OggOpusFile *of = op_open_memory(data, size, &ret);
29
6.17k
  if (!of)
30
1.58k
    return 0;
31
32
4.58k
  op_link_count(of);
33
34
4.58k
  int link_index = -1;
35
4.58k
  op_pcm_total(of, link_index);
36
4.58k
  op_raw_total(of, link_index);
37
4.58k
  op_pcm_tell(of);
38
4.58k
  op_raw_tell(of);
39
40
4.58k
  ogg_int64_t total_sample_count = 0;
41
4.58k
  const int pcm_size = 120 * 48 * 2; // 120ms/ch@48kHz is recommended
42
4.58k
  opus_int16 pcm[pcm_size];
43
429k
  for (;;) {
44
429k
    ret = op_read_stereo(of, pcm, pcm_size);
45
429k
    if (ret < 0) {
46
3.08k
      break;
47
3.08k
    }
48
49
426k
    if (op_current_link(of) != link_index) {
50
12.5k
      link_index = op_current_link(of);
51
12.5k
      op_pcm_total(of, link_index);
52
12.5k
      op_raw_total(of, link_index);
53
12.5k
      op_pcm_tell(of);
54
12.5k
      op_raw_tell(of);
55
12.5k
      op_bitrate_instant(of);
56
12.5k
      tmp = op_head(of, link_index)->version;
57
58
12.5k
      const OpusTags *tags = op_tags(of, link_index);
59
20.9k
      for (int i = 0; i < tags->comments; ++i) {
60
        // Note: The compare also touches memory allocated for user_comments[i].
61
        // This is a desired side effect and should be kept even if this
62
        // comparison is removed.
63
8.45k
        if (opus_tagncompare("METADATA_BLOCK_PICTURE", 22,
64
8.45k
                             tags->user_comments[i]) == 0) {
65
0
          OpusPictureTag pic;
66
0
          if (opus_picture_tag_parse(&pic, tags->user_comments[i]) >= 0) {
67
0
            opus_picture_tag_clear(&pic);
68
0
          }
69
0
        }
70
8.45k
      }
71
72
12.5k
      if (tags->vendor) {
73
12.5k
        tmp = tags->vendor[0];
74
12.5k
      }
75
76
12.5k
      int binary_suffix_len;
77
12.5k
      opus_tags_get_binary_suffix(tags, &binary_suffix_len);
78
12.5k
    }
79
80
426k
    if (ret == 0) {
81
1.50k
      break;
82
1.50k
    }
83
84
424k
    total_sample_count += ret;
85
424k
  }
86
87
4.58k
  if (total_sample_count > 0) {
88
    // Try random-access PCM reads. The number of tests is arbitrary and the
89
    // offset is designed to be pseudo-random, but deterministic - this is
90
    // implemented using Lehmer RNG with a minor hack that probably breaks some
91
    // properties of the RNG (but that is acceptable).
92
4.17k
    ogg_int64_t rng_seed = 1307832949LL;
93
137k
    for (int i = 0; i < 32; ++i) {
94
      // Derive the next deterministic offset to test and iterate the RNG.
95
133k
      rng_seed = (rng_seed * 279470273LL);
96
133k
      const ogg_int64_t offset = rng_seed % total_sample_count;
97
133k
      rng_seed = rng_seed % 4294967291LL;
98
99
133k
      if (op_pcm_seek(of, offset) == 0) {
100
83.2k
        tmp = op_read_stereo(of, pcm, pcm_size);
101
83.2k
      }
102
133k
    }
103
4.17k
  }
104
105
4.58k
  op_free(of);
106
4.58k
  return 0;
107
6.17k
}