Coverage Report

Created: 2026-06-07 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opus/tests/opus_multistream_encode_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 <math.h>
17
#include <stdint.h>
18
#include <stdlib.h>
19
#include <string.h>
20
21
#include "opus.h"
22
#include "opus_defines.h"
23
#include "opus_multistream.h"
24
#include "opus_types.h"
25
#include "../celt/os_support.h"
26
27
#include "opus_ossfuzz_utils.h"
28
29
77.7k
#define MAX_PACKET (1500)
30
static unsigned char out[MAX_PACKET];
31
32
81.9k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
81.9k
  FuzzedDataProvider fdp(data, size);
34
35
81.9k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
81.9k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
81.9k
                                                     ARG_QEXT(96)}) * 1000;
38
81.9k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
81.9k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
81.9k
  int frame_size_ms_x2 =
41
81.9k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
81.9k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
81.9k
  int application =
44
81.9k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
81.9k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
81.9k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
48
81.9k
  if (!mapping) {
49
0
    return 0;
50
0
  }
51
1.16M
  for (unsigned char x = 0; x < nb_channels; ++x) {
52
1.08M
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
53
1.08M
  }
54
55
81.9k
  int err = OPUS_OK;
56
81.9k
  OpusMSEncoder *enc = NULL;
57
81.9k
  if (fdp.ConsumeBool()) {
58
55.4k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
59
55.4k
    enc = opus_multistream_surround_encoder_create(
60
55.4k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
61
55.4k
        mapping, application, &err);
62
55.4k
  } else {
63
26.5k
    enc = opus_multistream_encoder_create(frequency, nb_channels, streams,
64
26.5k
                                          coupled_streams, mapping, application,
65
26.5k
                                          &err);
66
26.5k
  }
67
81.9k
  free(mapping);
68
81.9k
  if (err != OPUS_OK || enc == NULL) {
69
1.81k
    opus_multistream_encoder_destroy(enc);
70
1.81k
    return 0;
71
1.81k
  }
72
73
80.1k
  opus_multistream_encoder_ctl(
74
80.1k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
75
80.1k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
76
80.1k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
77
80.1k
  opus_multistream_encoder_ctl(
78
80.1k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
79
80.1k
  opus_multistream_encoder_ctl(
80
80.1k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
81
80.1k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
82
80.1k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
83
80.1k
                OPUS_BANDWIDTH_FULLBAND})));
84
80.1k
  opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
85
80.1k
  opus_multistream_encoder_ctl(
86
80.1k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
87
80.1k
  opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
88
80.1k
  opus_multistream_encoder_ctl(
89
80.1k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
90
80.1k
  opus_multistream_encoder_ctl(
91
80.1k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
92
80.1k
  opus_multistream_encoder_ctl(
93
80.1k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
94
80.1k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
95
80.1k
  opus_multistream_encoder_ctl(
96
80.1k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
97
98
80.1k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
99
80.1k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
100
80.1k
  if (pcm == NULL) {
101
0
    opus_multistream_encoder_destroy(enc);
102
0
    return 0;
103
0
  }
104
80.1k
  memset(pcm, 0, pcm_size);
105
106
80.1k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
107
77.7k
    const int len =
108
77.7k
        opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET);
109
77.7k
    (void)len;
110
77.7k
  }
111
112
80.1k
  opus_free(pcm);
113
80.1k
  opus_multistream_encoder_destroy(enc);
114
115
80.1k
  return 0;
116
80.1k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
40.9k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
40.9k
  FuzzedDataProvider fdp(data, size);
34
35
40.9k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
40.9k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
40.9k
                                                     ARG_QEXT(96)}) * 1000;
38
40.9k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
40.9k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
40.9k
  int frame_size_ms_x2 =
41
40.9k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
40.9k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
40.9k
  int application =
44
40.9k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
40.9k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
40.9k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
48
40.9k
  if (!mapping) {
49
0
    return 0;
50
0
  }
51
584k
  for (unsigned char x = 0; x < nb_channels; ++x) {
52
543k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
53
543k
  }
54
55
40.9k
  int err = OPUS_OK;
56
40.9k
  OpusMSEncoder *enc = NULL;
57
40.9k
  if (fdp.ConsumeBool()) {
58
27.7k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
59
27.7k
    enc = opus_multistream_surround_encoder_create(
60
27.7k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
61
27.7k
        mapping, application, &err);
62
27.7k
  } else {
63
13.2k
    enc = opus_multistream_encoder_create(frequency, nb_channels, streams,
64
13.2k
                                          coupled_streams, mapping, application,
65
13.2k
                                          &err);
66
13.2k
  }
67
40.9k
  free(mapping);
68
40.9k
  if (err != OPUS_OK || enc == NULL) {
69
908
    opus_multistream_encoder_destroy(enc);
70
908
    return 0;
71
908
  }
72
73
40.0k
  opus_multistream_encoder_ctl(
74
40.0k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
75
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
76
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
77
40.0k
  opus_multistream_encoder_ctl(
78
40.0k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
79
40.0k
  opus_multistream_encoder_ctl(
80
40.0k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
81
40.0k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
82
40.0k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
83
40.0k
                OPUS_BANDWIDTH_FULLBAND})));
84
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
85
40.0k
  opus_multistream_encoder_ctl(
86
40.0k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
87
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
88
40.0k
  opus_multistream_encoder_ctl(
89
40.0k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
90
40.0k
  opus_multistream_encoder_ctl(
91
40.0k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
92
40.0k
  opus_multistream_encoder_ctl(
93
40.0k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
94
40.0k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
95
40.0k
  opus_multistream_encoder_ctl(
96
40.0k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
97
98
40.0k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
99
40.0k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
100
40.0k
  if (pcm == NULL) {
101
0
    opus_multistream_encoder_destroy(enc);
102
0
    return 0;
103
0
  }
104
40.0k
  memset(pcm, 0, pcm_size);
105
106
40.0k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
107
38.8k
    const int len =
108
38.8k
        opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET);
109
38.8k
    (void)len;
110
38.8k
  }
111
112
40.0k
  opus_free(pcm);
113
40.0k
  opus_multistream_encoder_destroy(enc);
114
115
40.0k
  return 0;
116
40.0k
}
LLVMFuzzerTestOneInput
Line
Count
Source
32
40.9k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
33
40.9k
  FuzzedDataProvider fdp(data, size);
34
35
40.9k
  opus_int32 nb_channels = fdp.ConsumeIntegralInRange(0, 255);
36
40.9k
  const opus_int32 frequency = fdp.PickValueInArray({8, 12, 16, 24, 48
37
40.9k
                                                     ARG_QEXT(96)}) * 1000;
38
40.9k
  int streams = fdp.ConsumeIntegralInRange(0, 255);
39
40.9k
  int coupled_streams = fdp.ConsumeIntegralInRange(0, 255);
40
40.9k
  int frame_size_ms_x2 =
41
40.9k
      fdp.PickValueInArray({5, 10, 20, 40, 80, 120, 160, 200, 240});
42
40.9k
  int frame_size = frame_size_ms_x2 * frequency / 2000;
43
40.9k
  int application =
44
40.9k
      fdp.PickValueInArray({OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
45
40.9k
                            OPUS_APPLICATION_RESTRICTED_LOWDELAY});
46
47
40.9k
  unsigned char *mapping = (unsigned char *)malloc(nb_channels);
48
40.9k
  if (!mapping) {
49
0
    return 0;
50
0
  }
51
584k
  for (unsigned char x = 0; x < nb_channels; ++x) {
52
543k
    mapping[x] = fdp.ConsumeIntegralInRange(0, 255);
53
543k
  }
54
55
40.9k
  int err = OPUS_OK;
56
40.9k
  OpusMSEncoder *enc = NULL;
57
40.9k
  if (fdp.ConsumeBool()) {
58
27.7k
    int mapping_family = fdp.PickValueInArray({0, 1, 2, 3, 255});
59
27.7k
    enc = opus_multistream_surround_encoder_create(
60
27.7k
        frequency, nb_channels, mapping_family, &streams, &coupled_streams,
61
27.7k
        mapping, application, &err);
62
27.7k
  } else {
63
13.2k
    enc = opus_multistream_encoder_create(frequency, nb_channels, streams,
64
13.2k
                                          coupled_streams, mapping, application,
65
13.2k
                                          &err);
66
13.2k
  }
67
40.9k
  free(mapping);
68
40.9k
  if (err != OPUS_OK || enc == NULL) {
69
908
    opus_multistream_encoder_destroy(enc);
70
908
    return 0;
71
908
  }
72
73
40.0k
  opus_multistream_encoder_ctl(
74
40.0k
      enc, OPUS_SET_COMPLEXITY(fdp.ConsumeIntegralInRange(0, 10)));
75
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR(fdp.ConsumeBool()));
76
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(fdp.ConsumeBool()));
77
40.0k
  opus_multistream_encoder_ctl(
78
40.0k
      enc, OPUS_SET_FORCE_CHANNELS(fdp.PickValueInArray({OPUS_AUTO, 1, 2})));
79
40.0k
  opus_multistream_encoder_ctl(
80
40.0k
      enc, OPUS_SET_MAX_BANDWIDTH(fdp.PickValueInArray(
81
40.0k
               {OPUS_BANDWIDTH_NARROWBAND, OPUS_BANDWIDTH_MEDIUMBAND,
82
40.0k
                OPUS_BANDWIDTH_WIDEBAND, OPUS_BANDWIDTH_SUPERWIDEBAND,
83
40.0k
                OPUS_BANDWIDTH_FULLBAND})));
84
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_INBAND_FEC(fdp.ConsumeBool()));
85
40.0k
  opus_multistream_encoder_ctl(
86
40.0k
      enc, OPUS_SET_PACKET_LOSS_PERC(fdp.ConsumeIntegralInRange(0, 100)));
87
40.0k
  opus_multistream_encoder_ctl(enc, OPUS_SET_DTX(fdp.ConsumeBool()));
88
40.0k
  opus_multistream_encoder_ctl(
89
40.0k
      enc, OPUS_SET_LSB_DEPTH(fdp.ConsumeIntegralInRange(8, 24)));
90
40.0k
  opus_multistream_encoder_ctl(
91
40.0k
      enc, OPUS_SET_PREDICTION_DISABLED((fdp.ConsumeBool())));
92
40.0k
  opus_multistream_encoder_ctl(
93
40.0k
      enc, OPUS_SET_SIGNAL(fdp.PickValueInArray(
94
40.0k
               {OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC})));
95
40.0k
  opus_multistream_encoder_ctl(
96
40.0k
      enc, OPUS_SET_PHASE_INVERSION_DISABLED(((fdp.ConsumeBool()))));
97
98
40.0k
  const int pcm_size = sizeof(opus_int16) * frame_size * nb_channels;
99
40.0k
  opus_int16 *pcm = (opus_int16 *)opus_alloc(pcm_size);
100
40.0k
  if (pcm == NULL) {
101
0
    opus_multistream_encoder_destroy(enc);
102
0
    return 0;
103
0
  }
104
40.0k
  memset(pcm, 0, pcm_size);
105
106
40.0k
  if (pcm_size == fdp.ConsumeData(pcm, pcm_size)) {
107
38.8k
    const int len =
108
38.8k
        opus_multistream_encode(enc, pcm, frame_size, out, MAX_PACKET);
109
38.8k
    (void)len;
110
38.8k
  }
111
112
40.0k
  opus_free(pcm);
113
40.0k
  opus_multistream_encoder_destroy(enc);
114
115
40.0k
  return 0;
116
40.0k
}