Coverage Report

Created: 2026-09-28 07:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/tests/opus_multistream_decode_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 <stddef.h>
17
18
#include <algorithm>
19
#include <memory>
20
#include <random>
21
22
#include "../celt/mathops.h"
23
#include "../celt/os_support.h"
24
#include "opus.h"
25
#include "opus_defines.h"
26
#include "opus_multistream.h"
27
#include "opus_projection.h"
28
#include "opus_types.h"
29
30
#include "opus_ossfuzz_utils.h"
31
32
54.0k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
54.0k
  FuzzedDataProvider fdp(data, size);
34
35
54.0k
  const int frame_size_ms_x2 =
36
54.0k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
37
54.0k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
38
54.0k
                                                     ARG_QEXT(96)}) * 1000;
39
54.0k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
40
54.0k
  const opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
41
54.0k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
42
54.0k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
43
54.0k
  const bool use_fec = fdp.ConsumeBool();
44
54.0k
  const bool use_float = fdp.ConsumeBool();
45
46
54.0k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
47
54.0k
  if (!mapping) {
48
0
    return 0;
49
0
  }
50
328k
  for (unsigned char x = 0; x < nb_channels; ++x) {
51
274k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
52
274k
  }
53
54
54.0k
  int err = OPUS_OK;
55
54.0k
  OpusMSDecoder *const decoder = opus_multistream_decoder_create(
56
54.0k
      frequency, nb_channels, streams, coupled_streams, mapping, &err);
57
54.0k
  free(mapping);
58
59
54.0k
  if (decoder == nullptr || err != OPUS_OK) {
60
442
    return 0;
61
442
  }
62
53.5k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
63
53.5k
  const opus_int16 payload_size =
64
53.5k
      std::min((const unsigned long)SHRT_MAX, payload.size());
65
66
53.5k
  if (use_float) {
67
19.6k
    float *pcm = (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
68
19.6k
    if (pcm == NULL) {
69
0
      goto end;
70
0
    }
71
19.6k
    const int foo = opus_multistream_decode_float(
72
19.6k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
73
19.6k
    (void)foo;
74
75
19.6k
    opus_free(pcm);
76
33.9k
  } else {
77
33.9k
    opus_int16 *pcm =
78
33.9k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
79
33.9k
    if (pcm == NULL) {
80
0
      goto end;
81
0
    }
82
33.9k
    const int foo = opus_multistream_decode(
83
33.9k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
84
33.9k
    (void)foo;
85
86
33.9k
    opus_free(pcm);
87
33.9k
  }
88
53.5k
end:
89
53.5k
  opus_multistream_decoder_destroy(decoder);
90
91
53.5k
  return 0;
92
53.5k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
27.0k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
27.0k
  FuzzedDataProvider fdp(data, size);
34
35
27.0k
  const int frame_size_ms_x2 =
36
27.0k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
37
27.0k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
38
27.0k
                                                     ARG_QEXT(96)}) * 1000;
39
27.0k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
40
27.0k
  const opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
41
27.0k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
42
27.0k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
43
27.0k
  const bool use_fec = fdp.ConsumeBool();
44
27.0k
  const bool use_float = fdp.ConsumeBool();
45
46
27.0k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
47
27.0k
  if (!mapping) {
48
0
    return 0;
49
0
  }
50
164k
  for (unsigned char x = 0; x < nb_channels; ++x) {
51
137k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
52
137k
  }
53
54
27.0k
  int err = OPUS_OK;
55
27.0k
  OpusMSDecoder *const decoder = opus_multistream_decoder_create(
56
27.0k
      frequency, nb_channels, streams, coupled_streams, mapping, &err);
57
27.0k
  free(mapping);
58
59
27.0k
  if (decoder == nullptr || err != OPUS_OK) {
60
221
    return 0;
61
221
  }
62
26.7k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
63
26.7k
  const opus_int16 payload_size =
64
26.7k
      std::min((const unsigned long)SHRT_MAX, payload.size());
65
66
26.7k
  if (use_float) {
67
9.80k
    float *pcm = (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
68
9.80k
    if (pcm == NULL) {
69
0
      goto end;
70
0
    }
71
9.80k
    const int foo = opus_multistream_decode_float(
72
9.80k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
73
9.80k
    (void)foo;
74
75
9.80k
    opus_free(pcm);
76
16.9k
  } else {
77
16.9k
    opus_int16 *pcm =
78
16.9k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
79
16.9k
    if (pcm == NULL) {
80
0
      goto end;
81
0
    }
82
16.9k
    const int foo = opus_multistream_decode(
83
16.9k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
84
16.9k
    (void)foo;
85
86
16.9k
    opus_free(pcm);
87
16.9k
  }
88
26.7k
end:
89
26.7k
  opus_multistream_decoder_destroy(decoder);
90
91
26.7k
  return 0;
92
26.7k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
27.0k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
27.0k
  FuzzedDataProvider fdp(data, size);
34
35
27.0k
  const int frame_size_ms_x2 =
36
27.0k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
37
27.0k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
38
27.0k
                                                     ARG_QEXT(96)}) * 1000;
39
27.0k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
40
27.0k
  const opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
41
27.0k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
42
27.0k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
43
27.0k
  const bool use_fec = fdp.ConsumeBool();
44
27.0k
  const bool use_float = fdp.ConsumeBool();
45
46
27.0k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
47
27.0k
  if (!mapping) {
48
0
    return 0;
49
0
  }
50
164k
  for (unsigned char x = 0; x < nb_channels; ++x) {
51
137k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
52
137k
  }
53
54
27.0k
  int err = OPUS_OK;
55
27.0k
  OpusMSDecoder *const decoder = opus_multistream_decoder_create(
56
27.0k
      frequency, nb_channels, streams, coupled_streams, mapping, &err);
57
27.0k
  free(mapping);
58
59
27.0k
  if (decoder == nullptr || err != OPUS_OK) {
60
221
    return 0;
61
221
  }
62
26.7k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
63
26.7k
  const opus_int16 payload_size =
64
26.7k
      std::min((const unsigned long)SHRT_MAX, payload.size());
65
66
26.7k
  if (use_float) {
67
9.80k
    float *pcm = (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
68
9.80k
    if (pcm == NULL) {
69
0
      goto end;
70
0
    }
71
9.80k
    const int foo = opus_multistream_decode_float(
72
9.80k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
73
9.80k
    (void)foo;
74
75
9.80k
    opus_free(pcm);
76
16.9k
  } else {
77
16.9k
    opus_int16 *pcm =
78
16.9k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
79
16.9k
    if (pcm == NULL) {
80
0
      goto end;
81
0
    }
82
16.9k
    const int foo = opus_multistream_decode(
83
16.9k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
84
16.9k
    (void)foo;
85
86
16.9k
    opus_free(pcm);
87
16.9k
  }
88
26.7k
end:
89
26.7k
  opus_multistream_decoder_destroy(decoder);
90
91
26.7k
  return 0;
92
26.7k
}