Coverage Report

Created: 2024-03-26 07:25

/src/opus/tests/opus_repacketizer_fuzzer.cc
Line
Count
Source
1
// Copyright 2021 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 <fuzzer/FuzzedDataProvider.h>
16
#include <stdint.h>
17
#include <stdlib.h>
18
#include <string.h>
19
20
#include "opus.h"
21
#include "opus_types.h"
22
23
2.23k
#define MAX_PACKETOUT 32000
24
25
11.5k
static opus_uint32 char_to_int(const unsigned char ch[4]) {
26
11.5k
  return ((opus_uint32)ch[0] << 24) | ((opus_uint32)ch[1] << 16) |
27
11.5k
         ((opus_uint32)ch[2] << 8) | (opus_uint32)ch[3];
28
11.5k
}
29
30
2.23k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
31
2.23k
  FuzzedDataProvider fdp(data, size);
32
33
2.23k
  unsigned char output_packet[MAX_PACKETOUT];
34
2.23k
  OpusRepacketizer *rp = opus_repacketizer_create();
35
2.23k
  opus_repacketizer_init(rp);
36
2.23k
  const size_t nb_packets_to_add = fdp.ConsumeIntegralInRange(1, 48);
37
2.23k
  const auto packets = fdp.ConsumeRemainingBytes<unsigned char>();
38
39
2.23k
  size_t start = 0;
40
13.5k
  for (size_t i = 0; i < nb_packets_to_add; i++) {
41
13.2k
    if (packets.size() - start < 4) {
42
1.75k
      break;
43
1.75k
    }
44
11.5k
    const size_t packet_length = char_to_int(packets.data() + start);
45
11.5k
    start += 4;
46
47
11.5k
    if (packets.size() - start < packet_length || packet_length > 1500) {
48
211
      break;
49
211
    }
50
51
11.3k
    opus_repacketizer_cat(rp, packets.data() + start, packet_length);
52
11.3k
    start += packet_length;
53
11.3k
  }
54
2.23k
  int foo = opus_repacketizer_out(rp, output_packet, MAX_PACKETOUT);
55
2.23k
  (void)foo;
56
57
2.23k
  opus_repacketizer_destroy(rp);
58
59
2.23k
  return 0;
60
2.23k
}