aboutsummaryrefslogtreecommitdiffhomepage
path: root/projects/opus/opus_multistream_decode_fuzzer.cc
blob: a9413b15a81ff2e892302dd2a635e565f090cba0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright 2021 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <fuzzer/FuzzedDataProvider.h>
#include <stddef.h>

#include <algorithm>
#include <memory>
#include <random>

#include "../celt/mathops.h"
#include "../celt/os_support.h"
#include "opus.h"
#include "opus_defines.h"
#include "opus_multistream.h"
#include "opus_projection.h"
#include "opus_types.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  FuzzedDataProvider fdp(data, size);

  const int frame_size_ms_x2 =
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48}) * 1000;
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
  const opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
  const bool use_fec = fdp.ConsumeBool();
  const bool use_float = fdp.ConsumeBool();

  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
  if (!mapping) {
    return 0;
  }
  for (unsigned char x = 0; x < nb_channels; ++x) {
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
  }

  int err = OPUS_OK;
  OpusMSDecoder *const decoder = opus_multistream_decoder_create(
      frequency, nb_channels, streams, coupled_streams, mapping, &err);
  free(mapping);

  if (decoder == nullptr || err != OPUS_OK) {
    return 0;
  }
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
  const opus_int16 payload_size =
      std::min((const unsigned long)SHRT_MAX, payload.size());

  if (use_float) {
    float *pcm = (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
    if (pcm == NULL) {
      goto end;
    }
    const int foo = opus_multistream_decode_float(
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
    (void)foo;

    opus_free(pcm);
  } else {
    opus_int16 *pcm =
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
    if (pcm == NULL) {
      goto end;
    }
    const int foo = opus_multistream_decode(
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
    (void)foo;

    opus_free(pcm);
  }
end:
  opus_multistream_decoder_destroy(decoder);

  return 0;
}