Coverage Report

Created: 2025-10-10 07:38

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
53.6k
#define MAX_MATRIX_SIZE 4096
34
35
53.6k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
53.6k
  FuzzedDataProvider fdp(data, size);
37
38
53.6k
  const int frame_size_ms_x2 =
39
53.6k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
40
53.6k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
41
53.6k
                                                     ARG_QEXT(96)}) * 1000;
42
53.6k
  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
53.6k
  opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2);
46
53.6k
  if (fdp.ConsumeBool()) {
47
3.16k
    nb_channels += 2;
48
3.16k
  }
49
53.6k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
50
53.6k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams);
51
52
53.6k
  const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE);
53
53.6k
  unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size);
54
53.6k
  if (matrix == NULL) {
55
0
    return 0;
56
0
  }
57
58
53.6k
  fdp.ConsumeData(matrix, matrix_size);
59
60
53.6k
  int err = OPUS_OK;
61
53.6k
  OpusProjectionDecoder *const decoder = opus_projection_decoder_create(
62
53.6k
      frequency, nb_channels, streams, coupled_streams, matrix, matrix_size,
63
53.6k
      &err);
64
53.6k
  opus_free(matrix);
65
53.6k
  if (decoder == nullptr || err != OPUS_OK) {
66
978
    return 0;
67
978
  }
68
52.6k
  bool use_float = fdp.ConsumeBool();
69
52.6k
  bool use_fec = fdp.ConsumeBool();
70
52.6k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
71
52.6k
  const opus_int16 payload_size =
72
52.6k
      std::min((unsigned long)SHRT_MAX, payload.size());
73
74
52.6k
  if (use_float) {
75
18.6k
    float *pcm =
76
18.6k
        (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
77
18.6k
    if (pcm == NULL) {
78
0
      goto end;
79
0
    }
80
81
18.6k
    const int foo = opus_projection_decode_float(
82
18.6k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
83
18.6k
    (void)foo;
84
18.6k
    opus_free(pcm);
85
34.0k
  } else {
86
34.0k
    opus_int16 *pcm =
87
34.0k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
88
34.0k
    if (pcm == NULL) {
89
0
      goto end;
90
0
    }
91
92
34.0k
    const int foo = opus_projection_decode(
93
34.0k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
94
34.0k
    (void)foo;
95
34.0k
    opus_free(pcm);
96
34.0k
  }
97
52.6k
end:
98
52.6k
  opus_projection_decoder_destroy(decoder);
99
100
52.6k
  return 0;
101
52.6k
}
LLVMFuzzerTestOneInput
Line
Count
Source
35
26.8k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
26.8k
  FuzzedDataProvider fdp(data, size);
37
38
26.8k
  const int frame_size_ms_x2 =
39
26.8k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
40
26.8k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
41
26.8k
                                                     ARG_QEXT(96)}) * 1000;
42
26.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
26.8k
  opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2);
46
26.8k
  if (fdp.ConsumeBool()) {
47
1.58k
    nb_channels += 2;
48
1.58k
  }
49
26.8k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
50
26.8k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams);
51
52
26.8k
  const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE);
53
26.8k
  unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size);
54
26.8k
  if (matrix == NULL) {
55
0
    return 0;
56
0
  }
57
58
26.8k
  fdp.ConsumeData(matrix, matrix_size);
59
60
26.8k
  int err = OPUS_OK;
61
26.8k
  OpusProjectionDecoder *const decoder = opus_projection_decoder_create(
62
26.8k
      frequency, nb_channels, streams, coupled_streams, matrix, matrix_size,
63
26.8k
      &err);
64
26.8k
  opus_free(matrix);
65
26.8k
  if (decoder == nullptr || err != OPUS_OK) {
66
489
    return 0;
67
489
  }
68
26.3k
  bool use_float = fdp.ConsumeBool();
69
26.3k
  bool use_fec = fdp.ConsumeBool();
70
26.3k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
71
26.3k
  const opus_int16 payload_size =
72
26.3k
      std::min((unsigned long)SHRT_MAX, payload.size());
73
74
26.3k
  if (use_float) {
75
9.31k
    float *pcm =
76
9.31k
        (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
77
9.31k
    if (pcm == NULL) {
78
0
      goto end;
79
0
    }
80
81
9.31k
    const int foo = opus_projection_decode_float(
82
9.31k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
83
9.31k
    (void)foo;
84
9.31k
    opus_free(pcm);
85
17.0k
  } else {
86
17.0k
    opus_int16 *pcm =
87
17.0k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
88
17.0k
    if (pcm == NULL) {
89
0
      goto end;
90
0
    }
91
92
17.0k
    const int foo = opus_projection_decode(
93
17.0k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
94
17.0k
    (void)foo;
95
17.0k
    opus_free(pcm);
96
17.0k
  }
97
26.3k
end:
98
26.3k
  opus_projection_decoder_destroy(decoder);
99
100
26.3k
  return 0;
101
26.3k
}
LLVMFuzzerTestOneInput
Line
Count
Source
35
26.8k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
36
26.8k
  FuzzedDataProvider fdp(data, size);
37
38
26.8k
  const int frame_size_ms_x2 =
39
26.8k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
40
26.8k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
41
26.8k
                                                     ARG_QEXT(96)}) * 1000;
42
26.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
26.8k
  opus_int32 nb_channels = pow(fdp.ConsumeIntegralInRange(0, 15), 2);
46
26.8k
  if (fdp.ConsumeBool()) {
47
1.58k
    nb_channels += 2;
48
1.58k
  }
49
26.8k
  const int streams = fdp.ConsumeIntegralInRange(0, 255);
50
26.8k
  const int coupled_streams = fdp.ConsumeIntegralInRange(0, streams);
51
52
26.8k
  const opus_int32 matrix_size = fdp.ConsumeIntegralInRange(1, MAX_MATRIX_SIZE);
53
26.8k
  unsigned char *matrix = (unsigned char *)opus_alloc(matrix_size);
54
26.8k
  if (matrix == NULL) {
55
0
    return 0;
56
0
  }
57
58
26.8k
  fdp.ConsumeData(matrix, matrix_size);
59
60
26.8k
  int err = OPUS_OK;
61
26.8k
  OpusProjectionDecoder *const decoder = opus_projection_decoder_create(
62
26.8k
      frequency, nb_channels, streams, coupled_streams, matrix, matrix_size,
63
26.8k
      &err);
64
26.8k
  opus_free(matrix);
65
26.8k
  if (decoder == nullptr || err != OPUS_OK) {
66
489
    return 0;
67
489
  }
68
26.3k
  bool use_float = fdp.ConsumeBool();
69
26.3k
  bool use_fec = fdp.ConsumeBool();
70
26.3k
  auto payload = fdp.ConsumeRemainingBytes<unsigned char>();
71
26.3k
  const opus_int16 payload_size =
72
26.3k
      std::min((unsigned long)SHRT_MAX, payload.size());
73
74
26.3k
  if (use_float) {
75
9.31k
    float *pcm =
76
9.31k
        (float *)opus_alloc(sizeof(float) * frame_size * nb_channels);
77
9.31k
    if (pcm == NULL) {
78
0
      goto end;
79
0
    }
80
81
9.31k
    const int foo = opus_projection_decode_float(
82
9.31k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
83
9.31k
    (void)foo;
84
9.31k
    opus_free(pcm);
85
17.0k
  } else {
86
17.0k
    opus_int16 *pcm =
87
17.0k
        (opus_int16 *)opus_alloc(sizeof(opus_int16) * frame_size * nb_channels);
88
17.0k
    if (pcm == NULL) {
89
0
      goto end;
90
0
    }
91
92
17.0k
    const int foo = opus_projection_decode(
93
17.0k
        decoder, payload.data(), payload_size, pcm, frame_size, use_fec);
94
17.0k
    (void)foo;
95
17.0k
    opus_free(pcm);
96
17.0k
  }
97
26.3k
end:
98
26.3k
  opus_projection_decoder_destroy(decoder);
99
100
26.3k
  return 0;
101
26.3k
}