Coverage Report

Created: 2025-12-14 07:08

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