Coverage Report

Created: 2026-12-01 07:39

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.6k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
54.6k
  FuzzedDataProvider fdp(data, size);
34
35
54.6k
  const int frame_size_ms_x2 =
36
54.6k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
37
54.6k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
38
54.6k
                                                     ARG_QEXT(96)}) * 1000;
39
54.6k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
40
54.6k
  const opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
41
54.6k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
42
54.6k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
43
54.6k
  const bool use_fec = fdp.ConsumeBool();
44
54.6k
  const bool use_float = fdp.ConsumeBool();
45
46
54.6k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
47
54.6k
  if (!mapping) {
48
0
    return 0;
49
0
  }
50
345k
  for (unsigned char x = 0; x < nb_channels; ++x) {
51
290k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
52
290k
  }
53
54
54.6k
  int err = OPUS_OK;
55
54.6k
  OpusMSDecoder *const decoder = opus_multistream_decoder_create(
56
54.6k
      frequency, nb_channels, streams, coupled_streams, mapping, &err);
57
54.6k
  free(mapping);
58
59
54.6k
  if (decoder == nullptr || err != OPUS_OK) {
60
448
    return 0;
61
448
  }
62
54.2k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
63
54.2k
  const opus_int16 payload_size =
64
54.2k
      std::min((const unsigned long)SHRT_MAX, payload.size());
65
66
54.2k
  if (use_float) {
67
19.8k
    float *pcm = (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
68
19.8k
    if (pcm == NULL) {
69
0
      goto end;
70
0
    }
71
19.8k
    const int foo = opus_multistream_decode_float(
72
19.8k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
73
19.8k
    (void)foo;
74
75
19.8k
    opus_free(pcm);
76
34.3k
  } else {
77
34.3k
    opus_int16 *pcm =
78
34.3k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
79
34.3k
    if (pcm == NULL) {
80
0
      goto end;
81
0
    }
82
34.3k
    const int foo = opus_multistream_decode(
83
34.3k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
84
34.3k
    (void)foo;
85
86
34.3k
    opus_free(pcm);
87
34.3k
  }
88
54.2k
end:
89
54.2k
  opus_multistream_decoder_destroy(decoder);
90
91
54.2k
  return 0;
92
54.2k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
27.3k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
27.3k
  FuzzedDataProvider fdp(data, size);
34
35
27.3k
  const int frame_size_ms_x2 =
36
27.3k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
37
27.3k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
38
27.3k
                                                     ARG_QEXT(96)}) * 1000;
39
27.3k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
40
27.3k
  const opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
41
27.3k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
42
27.3k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
43
27.3k
  const bool use_fec = fdp.ConsumeBool();
44
27.3k
  const bool use_float = fdp.ConsumeBool();
45
46
27.3k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
47
27.3k
  if (!mapping) {
48
0
    return 0;
49
0
  }
50
172k
  for (unsigned char x = 0; x < nb_channels; ++x) {
51
145k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
52
145k
  }
53
54
27.3k
  int err = OPUS_OK;
55
27.3k
  OpusMSDecoder *const decoder = opus_multistream_decoder_create(
56
27.3k
      frequency, nb_channels, streams, coupled_streams, mapping, &err);
57
27.3k
  free(mapping);
58
59
27.3k
  if (decoder == nullptr || err != OPUS_OK) {
60
224
    return 0;
61
224
  }
62
27.1k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
63
27.1k
  const opus_int16 payload_size =
64
27.1k
      std::min((const unsigned long)SHRT_MAX, payload.size());
65
66
27.1k
  if (use_float) {
67
9.91k
    float *pcm = (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
68
9.91k
    if (pcm == NULL) {
69
0
      goto end;
70
0
    }
71
9.91k
    const int foo = opus_multistream_decode_float(
72
9.91k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
73
9.91k
    (void)foo;
74
75
9.91k
    opus_free(pcm);
76
17.1k
  } else {
77
17.1k
    opus_int16 *pcm =
78
17.1k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
79
17.1k
    if (pcm == NULL) {
80
0
      goto end;
81
0
    }
82
17.1k
    const int foo = opus_multistream_decode(
83
17.1k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
84
17.1k
    (void)foo;
85
86
17.1k
    opus_free(pcm);
87
17.1k
  }
88
27.1k
end:
89
27.1k
  opus_multistream_decoder_destroy(decoder);
90
91
27.1k
  return 0;
92
27.1k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
27.3k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
27.3k
  FuzzedDataProvider fdp(data, size);
34
35
27.3k
  const int frame_size_ms_x2 =
36
27.3k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
37
27.3k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
38
27.3k
                                                     ARG_QEXT(96)}) * 1000;
39
27.3k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
40
27.3k
  const opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
41
27.3k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
42
27.3k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
43
27.3k
  const bool use_fec = fdp.ConsumeBool();
44
27.3k
  const bool use_float = fdp.ConsumeBool();
45
46
27.3k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
47
27.3k
  if (!mapping) {
48
0
    return 0;
49
0
  }
50
172k
  for (unsigned char x = 0; x < nb_channels; ++x) {
51
145k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
52
145k
  }
53
54
27.3k
  int err = OPUS_OK;
55
27.3k
  OpusMSDecoder *const decoder = opus_multistream_decoder_create(
56
27.3k
      frequency, nb_channels, streams, coupled_streams, mapping, &err);
57
27.3k
  free(mapping);
58
59
27.3k
  if (decoder == nullptr || err != OPUS_OK) {
60
224
    return 0;
61
224
  }
62
27.1k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
63
27.1k
  const opus_int16 payload_size =
64
27.1k
      std::min((const unsigned long)SHRT_MAX, payload.size());
65
66
27.1k
  if (use_float) {
67
9.91k
    float *pcm = (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
68
9.91k
    if (pcm == NULL) {
69
0
      goto end;
70
0
    }
71
9.91k
    const int foo = opus_multistream_decode_float(
72
9.91k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
73
9.91k
    (void)foo;
74
75
9.91k
    opus_free(pcm);
76
17.1k
  } else {
77
17.1k
    opus_int16 *pcm =
78
17.1k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
79
17.1k
    if (pcm == NULL) {
80
0
      goto end;
81
0
    }
82
17.1k
    const int foo = opus_multistream_decode(
83
17.1k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
84
17.1k
    (void)foo;
85
86
17.1k
    opus_free(pcm);
87
17.1k
  }
88
27.1k
end:
89
27.1k
  opus_multistream_decoder_destroy(decoder);
90
91
27.1k
  return 0;
92
27.1k
}