Debian Codegraph @

Xpjproject/tests/fuzz/fuzz-audio.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2026 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #include <pjlib.h>
24 #include <pjmedia.h>
25 #include <pjmedia-codec.h>
26 #include <pjmedia-codec/g722.h>
27 #include <pjmedia-codec/gsm.h>
28 #include <pjmedia-codec/speex.h>
29 #include <pjmedia-codec/ilbc.h>
30 #include <pjmedia-codec/l16.h>
31
32 /* Codec configuration structure */
33 typedef struct {
34 pjmedia_codec **codec_ptr;
35 const char *name;
36 size_t min_frame_size;
37 size_t pcm_frame_size;
38 } codec_config_t;
39
40 /* Global state for persistent fuzzing */
41 static pj_caching_pool caching_pool;
42 static pjmedia_endpt *endpt = NULL;
43 static pj_pool_t *pool = NULL;
44 static pjmedia_codec_mgr *codec_mgr = NULL;
45 static int initialized = 0;
46
47 /* Codec instances */
48 static pjmedia_codec *codec_pcma = NULL;
49 static pjmedia_codec *codec_pcmu = NULL;
50 static pjmedia_codec *codec_g722 = NULL;
51 static pjmedia_codec *codec_gsm = NULL;
52 static pjmedia_codec *codec_speex = NULL;
53 static pjmedia_codec *codec_ilbc = NULL;
54 static pjmedia_codec *codec_l16_8k = NULL;
55 static pjmedia_codec *codec_l16_16k = NULL;
56 static pjmedia_codec *codec_l16_48k = NULL;
57
58 /* Codec configurations array */
59 static codec_config_t codec_configs[] = {
60 {&codec_pcma, "G.711 A-Law", 80, 160},
61 {&codec_pcmu, "G.711 U-Law", 80, 160},
62 {&codec_g722, "G.722", 80, 320},
63 {&codec_gsm, "GSM", 33, 320},
64 {&codec_speex, "Speex", 10, 320},
65 {&codec_ilbc, "iLBC", 38, 480},
66 {&codec_l16_8k, "L16 8kHz", 320, 320},
67 {&codec_l16_16k, "L16 16kHz", 640, 640},
68 {&codec_l16_48k, "L16 48kHz", 1920, 1920},
69 };
70
71 #define NUM_CODECS (sizeof(codec_configs) / sizeof(codec_configs[0]))
72
73 /* Codec ID strings for initialization */
74 static const char* codec_id_strings[] = {
75 "PCMA/8000/1",
76 "PCMU/8000/1",
77 "G722/16000/1",
78 "GSM/8000/1",
79 "speex/8000/1",
80 "iLBC/8000/1",
81 "L16/8000/1",
82 "L16/16000/1",
83 "L16/48000/1"
84 };
85
86 /* Clock rates for each codec (must match codec_id_strings order) */
87 static const unsigned codec_clock_rates[] = {
88 8000, /* PCMA */
89 8000, /* PCMU */
90 16000, /* G.722 */
91 8000, /* GSM */
92 8000, /* Speex */
93 8000, /* iLBC */
94 8000, /* L16 8kHz */
95 16000, /* L16 16kHz */
96 48000 /* L16 48kHz */
97 };
98
99 /* Helper function to allocate and open a codec */
100 static void alloc_codec(const char *id_str, pjmedia_codec **codec_ptr)
101 {
102 pj_status_t status;
103 pjmedia_codec_param param;
104 const pjmedia_codec_info *codec_info;
105 unsigned count = 1;
106 pj_str_t codec_id = pj_str((char*)id_str);
107
108 status = pjmedia_codec_mgr_find_codecs_by_id(codec_mgr, &codec_id,
109 &count, &codec_info, NULL);
110 if (status == PJ_SUCCESS && count > 0) {
111 status = pjmedia_codec_mgr_get_default_param(codec_mgr, codec_info, &param);
112 if (status == PJ_SUCCESS) {
113 param.setting.plc = 1;
114 status = pjmedia_codec_mgr_alloc_codec(codec_mgr, codec_info, codec_ptr);
115 if (status == PJ_SUCCESS && *codec_ptr) {
116 pjmedia_codec_open(*codec_ptr, &param);
117 }
118 }
119 }
120 }
121
122 static int init_codecs(void)
123 {
124 pj_status_t status;
125 size_t i;
126
127 if (initialized) {
128 return 0;
129 }
130
131 /* Initialize PJLIB */
132 status = pj_init();
133 if (status != PJ_SUCCESS) {
134 return -1;
135 }
136
137 pj_log_set_level(0);
138
139 /* Initialize caching pool */
140 pj_caching_pool_init(&caching_pool, &pj_pool_factory_default_policy, 0);
141
142 /* Create memory pool */
143 pool = pj_pool_create(&caching_pool.factory, "fuzzer", 4000, 4000, NULL);
144 if (!pool) {
145 return -1;
146 }
147
148 /* Create media endpoint */
149 status = pjmedia_endpt_create(&caching_pool.factory, NULL, 1, &endpt);
150 if (status != PJ_SUCCESS) {
151 return -1;
152 }
153
154 /* Get codec manager */
155 codec_mgr = pjmedia_endpt_get_codec_mgr(endpt);
156 if (!codec_mgr) {
157 return -1;
158 }
159
160 /* Register all codecs */
161 pjmedia_codec_g711_init(endpt);
162 pjmedia_codec_g722_init(endpt);
163 pjmedia_codec_gsm_init(endpt);
164 pjmedia_codec_speex_init(endpt, 0, -1, -1);
165 pjmedia_codec_ilbc_init(endpt, 30);
166 pjmedia_codec_l16_init(endpt, 0);
167
168 /* Allocate all codecs using the configuration array */
169 for (i = 0; i < NUM_CODECS; i++) {
170 alloc_codec(codec_id_strings[i], codec_configs[i].codec_ptr);
171 }
172
173 initialized = 1;
174 return 0;
175 }
176
177 /* Helper function to test codec encode/decode cycle */
178 static void test_codec_cycle(pjmedia_codec *codec, const uint8_t *Data, size_t Size,
179 size_t min_frame_size, size_t pcm_frame_size,
180 unsigned clock_rate)
181 {
182 pj_status_t status;
183 pjmedia_frame input_frame, output_frame;
184 pj_int16_t pcm_buffer[4096] = {0};
185 pj_uint8_t encoded_buffer[2048];
186 pjmedia_codec_param param;
187
188 if (!codec || Size < min_frame_size) return;
189
190 /* Initialise pjmedia_frame */
191 pj_bzero(&input_frame, sizeof(input_frame));
192 pj_bzero(&output_frame, sizeof(output_frame));
193
194 /* Test runtime parameter modification (tests VAD toggling, keep PLC enabled) */
195 if (Size >= min_frame_size + 2) {
196 pj_bzero(&param, sizeof(param));
197 param.info.avg_bps = 128000;
198 param.info.clock_rate = clock_rate;
199 param.info.channel_cnt = 1;
200 param.info.frm_ptime = 20;
201 param.info.pcm_bits_per_sample = 16;
202 param.setting.vad = (Data[min_frame_size] & 0x01) ? 1 : 0;
203 param.setting.plc = 1;
204 if (codec->op->modify) {
205 pjmedia_codec_modify(codec, &param);
206 }
207 }
208
209 /* Test decode */
210 input_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
211 input_frame.buf = (void*)Data;
212 input_frame.size = min_frame_size;
213 input_frame.timestamp.u64 = 0;
214
215 output_frame.buf = pcm_buffer;
216 output_frame.size = sizeof(pcm_buffer);
217
218 status = pjmedia_codec_decode(codec, &input_frame, sizeof(pcm_buffer), &output_frame);
219
220 /* If decode succeeded, try to encode the output back */
221 if (status == PJ_SUCCESS && output_frame.type == PJMEDIA_FRAME_TYPE_AUDIO) {
222 input_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
223 input_frame.buf = output_frame.buf;
224 input_frame.size = output_frame.size;
225 input_frame.timestamp.u64 = 0;
226
227 output_frame.buf = encoded_buffer;
228 output_frame.size = sizeof(encoded_buffer);
229
230 pjmedia_codec_encode(codec, &input_frame, sizeof(encoded_buffer), &output_frame);
231 }
232
233 /* Test encode with fuzzer input as PCM - use proper frame size */
234 if (Size >= pcm_frame_size && pcm_frame_size <= sizeof(pcm_buffer)) {
235 memcpy(pcm_buffer, Data, pcm_frame_size);
236
237 input_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
238 input_frame.buf = pcm_buffer;
239 input_frame.size = pcm_frame_size;
240 input_frame.timestamp.u64 = 0;
241
242 output_frame.buf = encoded_buffer;
243 output_frame.size = sizeof(encoded_buffer);
244
245 pjmedia_codec_encode(codec, &input_frame, sizeof(encoded_buffer), &output_frame);
246 }
247
248 /* Test parse */
249 if (codec->op->parse) {
250 pjmedia_frame frames[256];
251 unsigned frame_cnt = 256;
252
253 input_frame.type = PJMEDIA_FRAME_TYPE_AUDIO;
254 input_frame.buf = (void*)Data;
255 input_frame.size = Size;
256 input_frame.timestamp.u64 = 0;
257
258 status = pjmedia_codec_parse(codec, (void*)Data, Size, &input_frame.timestamp,
259 &frame_cnt, frames);
260 (void)status;
261 }
262
263 /* Test PLC if function exists */
264 if (codec->op->recover) {
265 output_frame.buf = pcm_buffer;
266 output_frame.size = sizeof(pcm_buffer);
267
268 /* Wrap in error handling to avoid asserts in codecs with incomplete PLC */
269 status = pjmedia_codec_recover(codec, sizeof(pcm_buffer), &output_frame);
270 if (status == PJ_SUCCESS) {
271 output_frame.buf = pcm_buffer;
272 output_frame.size = sizeof(pcm_buffer);
273 pjmedia_codec_recover(codec, sizeof(pcm_buffer), &output_frame);
274 }
275 }
276 }
277
278 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
279 {
280 const uint8_t *current_data;
281 size_t remaining_size;
282 size_t i;
283 codec_config_t *config;
284 pjmedia_codec *codec;
285 size_t chunk_size;
286 size_t consumed;
287 pj_int16_t pcm_val;
288 pj_uint8_t alaw;
289 pj_uint8_t ulaw;
290
291 /* Initialize codecs on first run */
292 if (init_codecs() != 0) {
293 return 0;
294 }
295
296 current_data = Data;
297 remaining_size = Size;
298
299 /* Test each codec sequentially with non-overlapping chunks of data */
300 for (i = 0; i < NUM_CODECS; i++) {
301 config = &codec_configs[i];
302 codec = *(config->codec_ptr);
303
304 /* Skip if codec not available or not enough data */
305 if (!codec || remaining_size < config->min_frame_size) {
306 continue;
307 }
308
309 /* Calculate chunk size for this codec (at least min_frame_size, max pcm_frame_size) */
310 chunk_size = config->pcm_frame_size;
311 if (chunk_size > remaining_size) {
312 chunk_size = remaining_size;
313 }
314
315 /* Test this codec with current data chunk */
316 test_codec_cycle(codec, current_data, chunk_size,
317 config->min_frame_size, config->pcm_frame_size,
318 codec_clock_rates[i]);
319
320 /* Advance to next chunk of data */
321 consumed = config->min_frame_size;
322 if (consumed > remaining_size) {
323 consumed = remaining_size;
324 }
325 current_data += consumed;
326 remaining_size -= consumed;
327
328 /* Stop if we've run out of data */
329 if (remaining_size < 10) {
330 break;
331 }
332 }
333
334 /* Test low-level G.711 conversion functions with remaining data */
335 if (remaining_size > 1) {
336 for (i = 0; i + 1 < remaining_size && i < 256; i += 2) {
337 pcm_val = (pj_int16_t)((current_data[i] << 8) | current_data[i+1]);
338
339 alaw = pjmedia_linear2alaw(pcm_val);
340 ulaw = pjmedia_linear2ulaw(pcm_val);
341 pcm_val = pjmedia_alaw2linear(alaw);
342 pcm_val = pjmedia_ulaw2linear(ulaw);
343 ulaw = pjmedia_alaw2ulaw(current_data[i]);
344 alaw = pjmedia_ulaw2alaw(current_data[i]);
345 }
346 }
347
348 return 0;
349 }