Coverage Report

Created: 2026-08-13 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/tests/opus_projection_decoder_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 <limits.h>
17
#include <stddef.h>
18
19
#include <array>
20
#include <cmath>
21
#include <memory>
22
23
#include "../celt/mathops.h"
24
#include "../celt/os_support.h"
25
#include "opus.h"
26
#include "opus_defines.h"
27
#include "opus_projection.h"
28
#include "opus_types.h"
29
30
#include "opus_ossfuzz_utils.h"
31
32
// Having a huge-size vastly reduces the fuzzer's speed
33
55.8k
#define MAX_MATRIX_SIZE 4096
34
35
55.8k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
55.8k
  FuzzedDataProvider fdp(data, size);
37
38
55.8k
  const int frame_size_ms_x2 =
39
55.8k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
40
55.8k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
41
55.8k
                                                     ARG_QEXT(96)}) * 1000;
42
55.8k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
43
44
  // The allowed number of channels is either n^2 or n^2 + 2
45
55.8k
  opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2);
46
55.8k
  if (fdp.ConsumeBool()) {
47
2.30k
    nb_channels += 2;
48
2.30k
  }
49
55.8k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
50
55.8k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams);
51
52
55.8k
  const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE);
53
55.8k
  unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size);
54
55.8k
  if (matrix == NULL) {
55
0
    return 0;
56
0
  }
57
58
55.8k
  fdp.ConsumeData(matrix, matrix_size);
59
60
55.8k
  int err = OPUS_OK;
61
55.8k
  OpusProjectionDecoder *const decoder = opus_projection_decoder_create(
62
55.8k
      frequency, nb_channels, streams, coupled_streams, matrix, matrix_size,
63
55.8k
      &err);
64
55.8k
  opus_free(matrix);
65
55.8k
  if (decoder == nullptr || err != OPUS_OK) {
66
966
    return 0;
67
966
  }
68
54.8k
  bool use_float = fdp.ConsumeBool();
69
54.8k
  bool use_fec = fdp.ConsumeBool();
70
54.8k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
71
54.8k
  const opus_int16 payload_size =
72
54.8k
      std::min((unsigned long)SHRT_MAX, payload.size());
73
74
54.8k
  if (use_float) {
75
19.5k
    float *pcm =
76
19.5k
        (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
77
19.5k
    if (pcm == NULL) {
78
0
      goto end;
79
0
    }
80
81
19.5k
    const int foo = opus_projection_decode_float(
82
19.5k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
83
19.5k
    (void)foo;
84
19.5k
    opus_free(pcm);
85
35.2k
  } else {
86
35.2k
    opus_int16 *pcm =
87
35.2k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
88
35.2k
    if (pcm == NULL) {
89
0
      goto end;
90
0
    }
91
92
35.2k
    const int foo = opus_projection_decode(
93
35.2k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
94
35.2k
    (void)foo;
95
35.2k
    opus_free(pcm);
96
35.2k
  }
97
54.8k
end:
98
54.8k
  opus_projection_decoder_destroy(decoder);
99
100
54.8k
  return 0;
101
54.8k
}
LLVMFuzzerTestOneInput
Line
Count
Source
35
27.9k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
27.9k
  FuzzedDataProvider fdp(data, size);
37
38
27.9k
  const int frame_size_ms_x2 =
39
27.9k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
40
27.9k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
41
27.9k
                                                     ARG_QEXT(96)}) * 1000;
42
27.9k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
43
44
  // The allowed number of channels is either n^2 or n^2 + 2
45
27.9k
  opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2);
46
27.9k
  if (fdp.ConsumeBool()) {
47
1.15k
    nb_channels += 2;
48
1.15k
  }
49
27.9k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
50
27.9k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams);
51
52
27.9k
  const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE);
53
27.9k
  unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size);
54
27.9k
  if (matrix == NULL) {
55
0
    return 0;
56
0
  }
57
58
27.9k
  fdp.ConsumeData(matrix, matrix_size);
59
60
27.9k
  int err = OPUS_OK;
61
27.9k
  OpusProjectionDecoder *const decoder = opus_projection_decoder_create(
62
27.9k
      frequency, nb_channels, streams, coupled_streams, matrix, matrix_size,
63
27.9k
      &err);
64
27.9k
  opus_free(matrix);
65
27.9k
  if (decoder == nullptr || err != OPUS_OK) {
66
483
    return 0;
67
483
  }
68
27.4k
  bool use_float = fdp.ConsumeBool();
69
27.4k
  bool use_fec = fdp.ConsumeBool();
70
27.4k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
71
27.4k
  const opus_int16 payload_size =
72
27.4k
      std::min((unsigned long)SHRT_MAX, payload.size());
73
74
27.4k
  if (use_float) {
75
9.78k
    float *pcm =
76
9.78k
        (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
77
9.78k
    if (pcm == NULL) {
78
0
      goto end;
79
0
    }
80
81
9.78k
    const int foo = opus_projection_decode_float(
82
9.78k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
83
9.78k
    (void)foo;
84
9.78k
    opus_free(pcm);
85
17.6k
  } else {
86
17.6k
    opus_int16 *pcm =
87
17.6k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
88
17.6k
    if (pcm == NULL) {
89
0
      goto end;
90
0
    }
91
92
17.6k
    const int foo = opus_projection_decode(
93
17.6k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
94
17.6k
    (void)foo;
95
17.6k
    opus_free(pcm);
96
17.6k
  }
97
27.4k
end:
98
27.4k
  opus_projection_decoder_destroy(decoder);
99
100
27.4k
  return 0;
101
27.4k
}
LLVMFuzzerTestOneInput
Line
Count
Source
35
27.9k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
27.9k
  FuzzedDataProvider fdp(data, size);
37
38
27.9k
  const int frame_size_ms_x2 =
39
27.9k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
40
27.9k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
41
27.9k
                                                     ARG_QEXT(96)}) * 1000;
42
27.9k
  const int frame_size = frame_size_ms_x2 * frequency / 2000;
43
44
  // The allowed number of channels is either n^2 or n^2 + 2
45
27.9k
  opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2);
46
27.9k
  if (fdp.ConsumeBool()) {
47
1.15k
    nb_channels += 2;
48
1.15k
  }
49
27.9k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
50
27.9k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams);
51
52
27.9k
  const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE);
53
27.9k
  unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size);
54
27.9k
  if (matrix == NULL) {
55
0
    return 0;
56
0
  }
57
58
27.9k
  fdp.ConsumeData(matrix, matrix_size);
59
60
27.9k
  int err = OPUS_OK;
61
27.9k
  OpusProjectionDecoder *const decoder = opus_projection_decoder_create(
62
27.9k
      frequency, nb_channels, streams, coupled_streams, matrix, matrix_size,
63
27.9k
      &err);
64
27.9k
  opus_free(matrix);
65
27.9k
  if (decoder == nullptr || err != OPUS_OK) {
66
483
    return 0;
67
483
  }
68
27.4k
  bool use_float = fdp.ConsumeBool();
69
27.4k
  bool use_fec = fdp.ConsumeBool();
70
27.4k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
71
27.4k
  const opus_int16 payload_size =
72
27.4k
      std::min((unsigned long)SHRT_MAX, payload.size());
73
74
27.4k
  if (use_float) {
75
9.78k
    float *pcm =
76
9.78k
        (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
77
9.78k
    if (pcm == NULL) {
78
0
      goto end;
79
0
    }
80
81
9.78k
    const int foo = opus_projection_decode_float(
82
9.78k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
83
9.78k
    (void)foo;
84
9.78k
    opus_free(pcm);
85
17.6k
  } else {
86
17.6k
    opus_int16 *pcm =
87
17.6k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
88
17.6k
    if (pcm == NULL) {
89
0
      goto end;
90
0
    }
91
92
17.6k
    const int foo = opus_projection_decode(
93
17.6k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
94
17.6k
    (void)foo;
95
17.6k
    opus_free(pcm);
96
17.6k
  }
97
27.4k
end:
98
27.4k
  opus_projection_decoder_destroy(decoder);
99
100
27.4k
  return 0;
101
27.4k
}