Raw ↗
+ +
efc3c5e fix(compat): define S_ISREG/S_ISDIR POSIX macros for MSVC and initialize Win32 sockets
saifmukhtar · 2026-11-24
1 /*
2 * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0
3 * Copyright (c) 2026 Saif Mukhtar
4 *
5 * OpenAVX - Open Audio-Video eXchange
6 * test/openavx_fuzz_regression_test.c
7 *
8 * Automated regression suite executing 174M-iteration fuzzing corpus
9 * and discovered crash artifacts against core decoders and wire parsers.
10 */
11  
12 #define _GNU_SOURCE
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <string.h>
18 #include <dirent.h>
19 #include <assert.h>
20 #include <sys/stat.h>
21 #include <sys/socket.h>
22 #include <unistd.h>
23 #include <time.h>
24  
25 #ifndef S_ISREG
26 #if defined(_S_IFMT) && defined(_S_IFREG)
27 #define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
28 #elif defined(S_IFMT) && defined(S_IFREG)
29 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
30 #else
31 #define S_ISREG(m) (((m) & 0170000) == 0100000)
32 #endif
33 #endif
34  
35 #ifndef S_ISDIR
36 #if defined(_S_IFMT) && defined(_S_IFDIR)
37 #define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
38 #elif defined(S_IFMT) && defined(S_IFDIR)
39 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
40 #else
41 #define S_ISDIR(m) (((m) & 0170000) == 0040000)
42 #endif
43 #endif
44  
45 #include "openavx.h"
46 #include "avx_internal.h"
47 #include "avx_wire.h"
48 #include "avx_speedhq.h"
49 #include "avx_audio.h"
50 #include "avx_udp.h"
51  
52 #ifndef OPENAVX_PROJECT_SOURCE_DIR
53 #define OPENAVX_PROJECT_SOURCE_DIR "."
54 #endif
55  
56 static double get_time_ms(void) {
57 struct timespec ts;
58 clock_gettime(CLOCK_MONOTONIC, &ts);
59 return (double)ts.tv_sec * 1000.0 + (double)ts.tv_nsec / 1000000.0;
60 }
61  
62 static uint8_t *read_file_to_buffer(const char *filepath, size_t *out_size) {
63 *out_size = 0;
64 FILE *f = fopen(filepath, "rb");
65 if (!f) return NULL;
66  
67 fseek(f, 0, SEEK_END);
68 long sz = ftell(f);
69 if (sz < 0 || sz > 64 * 1024 * 1024) {
70 fclose(f);
71 return NULL;
72 }
73 fseek(f, 0, SEEK_SET);
74  
75 uint8_t *buf = (uint8_t *)malloc((size_t)sz + 1);
76 if (!buf) {
77 fclose(f);
78 return NULL;
79 }
80  
81 size_t read_bytes = fread(buf, 1, (size_t)sz, f);
82 fclose(f);
83  
84 *out_size = read_bytes;
85 return buf;
86 }
87  
88 static void test_fuzz_wire_unpack(const uint8_t *data, size_t size) {
89 if (!data || size < 4) return;
90  
91 uint32_t magic;
92 memcpy(&magic, data, sizeof(magic));
93  
94 const oavx_wire_profile_t *prof = avx_wire_get_default_profile();
95  
96 if (magic == prof->magic_video || magic == OAVX_WIRE_MAGIC_VIDEO) {
97 AVX_PACKED_START
98 struct {
99 uint32_t xres;
100 uint32_t yres;
101 uint32_t fourcc;
102 int32_t frame_rate_n;
103 int32_t frame_rate_d;
104 int64_t timecode;
105 uint32_t payload_len;
106 } AVX_PACKED vhdr;
107 AVX_PACKED_END
108  
109 if (size >= 4 + sizeof(vhdr)) {
110 memcpy(&vhdr, data + 4, sizeof(vhdr));
111 uint64_t total_pixels = (uint64_t)vhdr.xres * (uint64_t)vhdr.yres;
112 (void)total_pixels;
113 }
114 } else if (magic == prof->magic_audio || magic == OAVX_WIRE_MAGIC_AUDIO) {
115 AVX_PACKED_START
116 struct {
117 int32_t sample_rate;
118 int32_t channels;
119 int32_t samples;
120 int64_t timecode;
121 uint32_t payload_len;
122 } AVX_PACKED ahdr;
123 AVX_PACKED_END
124  
125 if (size >= 4 + sizeof(ahdr)) {
126 memcpy(&ahdr, data + 4, sizeof(ahdr));
127 for (int c = 0; c < ahdr.channels && c < 8; ++c) {
128 size_t offset = (size_t)c * (size_t)ahdr.samples * sizeof(float);
129 (void)offset;
130 }
131 }
132 } else if (magic == prof->magic_metadata || magic == OAVX_WIRE_MAGIC_METADATA) {
133 AVX_PACKED_START
134 struct {
135 int64_t timecode;
136 uint32_t payload_len;
137 } AVX_PACKED mhdr;
138 AVX_PACKED_END
139  
140 if (size >= 4 + sizeof(mhdr)) {
141 memcpy(&mhdr, data + 4, sizeof(mhdr));
142 }
143 } else if (magic == OAVX_WIRE_MAGIC_AUDIO_COMP) {
144 AVX_PACKED_START
145 struct {
146 uint32_t codec_fourcc;
147 int32_t sample_rate;
148 int32_t channels;
149 int32_t samples;
150 int64_t timecode;
151 uint32_t payload_len;
152 } AVX_PACKED cahdr;
153 AVX_PACKED_END
154  
155 if (size >= 4 + sizeof(cahdr)) {
156 memcpy(&cahdr, data + 4, sizeof(cahdr));
157 }
158 } else if (magic == OAVX_UDP_MAGIC) {
159 if (size >= sizeof(avx_udp_header_t)) {
160 avx_udp_header_t uhdr;
161 memcpy(&uhdr, data, sizeof(uhdr));
162 if (uhdr.frag_count > 0 && uhdr.frag_count <= AVX_UDP_MAX_FRAGS) {
163 size_t dest_offset = (size_t)uhdr.frag_idx * AVX_UDP_MAX_FRAG_SIZE;
164 (void)dest_offset;
165 }
166 }
167 } else if (magic == OAVX_UDP_NACK_MAGIC) {
168 if (size >= sizeof(avx_udp_nack_t)) {
169 avx_udp_nack_t nhdr;
170 memcpy(&nhdr, data, sizeof(nhdr));
171 uint16_t rc = nhdr.range_count;
172 if (rc > AVX_UDP_NACK_MAX_RANGES) rc = AVX_UDP_NACK_MAX_RANGES;
173 for (uint16_t r = 0; r < rc; ++r) {
174 uint32_t end = (uint32_t)nhdr.ranges[r].start + (uint32_t)nhdr.ranges[r].count;
175 (void)end;
176 }
177 }
178 }
179 }
180  
181 static void test_fuzz_wire_socketpair(const uint8_t *data, size_t size) {
182 if (!data || size == 0) return;
183  
184 int sv[2];
185 if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0) {
186 size_t send_len = size > 65536 ? 65536 : size;
187 (void)send(sv[0], (const char*)data, (int)send_len, 0);
188 close(sv[0]);
189  
190 avx_rx_config_t cfg;
191 memset(&cfg, 0, sizeof(cfg));
192 cfg.color_format = (oavx_color_format_e)(data[0] % 7);
193  
194 avx_rx_instance_t rx = avx_rx_create(&cfg);
195 if (rx) {
196 avx_recv_ctx_t *slot = (avx_recv_ctx_t*)rx;
197 avx_mutex_lock(&slot->lock);
198 slot->tcp_conn_fd = sv[1];
199 slot->connected = true;
200 slot->offline_mode = true;
201 avx_mutex_unlock(&slot->lock);
202  
203 oavx_video_frame_t vframe;
204 oavx_audio_frame_t aframe;
205 oavx_metadata_frame_t mframe;
206  
207 for (int i = 0; i < 2; ++i) {
208 oavx_frame_type_t ft = avx_rx_receive(rx, &vframe, &aframe, &mframe, 2);
209 if (ft <= 0) break;
210 if (ft == OAVX_FRAME_TYPE_VIDEO) {
211 avx_rx_free_video(rx, &vframe);
212 } else if (ft == OAVX_FRAME_TYPE_AUDIO) {
213 avx_rx_free_audio(rx, &aframe);
214 } else if (ft == OAVX_FRAME_TYPE_METADATA) {
215 avx_rx_free_metadata(rx, &mframe);
216 }
217 }
218  
219 avx_rx_destroy(rx);
220 } else {
221 close(sv[1]);
222 }
223 }
224 }
225  
226 static void test_fuzz_speedhq_input(const uint8_t *data, size_t size) {
227 if (!data || size < 6) return;
228  
229 static const uint32_t s_shq_fourccs[8] = {
230 OAVX_FOURCC_VIDEO_TYPE_SHQ0,
231 OAVX_FOURCC_VIDEO_TYPE_SHQ1,
232 OAVX_FOURCC_VIDEO_TYPE_SHQ2,
233 OAVX_FOURCC_VIDEO_TYPE_SHQ3,
234 OAVX_FOURCC_VIDEO_TYPE_SHQ4,
235 OAVX_FOURCC_VIDEO_TYPE_SHQ5,
236 OAVX_FOURCC_VIDEO_TYPE_SHQ7,
237 OAVX_FOURCC_VIDEO_TYPE_SHQ9
238 };
239 uint32_t fourcc = s_shq_fourccs[data[0] % 8];
240  
241 uint16_t raw_w = (uint16_t)data[1] | ((uint16_t)data[2] << 8);
242 uint16_t raw_h = (uint16_t)data[3] | ((uint16_t)data[4] << 8);
243  
244 int w = (int)(raw_w % 1921);
245 int h = (int)(raw_h % 1081);
246 if ((data[5] & 1) != 0) {
247 w = (w / 16) * 16;
248 }
249  
250 const uint8_t *payload = data + 6;
251 size_t payload_size = size - 6;
252  
253 if (w > 0 && h > 0 && w <= AVX_MAX_VIDEO_WIDTH && h <= AVX_MAX_VIDEO_HEIGHT) {
254 uint64_t out_u64 = (uint64_t)w * (uint64_t)h * 2ULL;
255 uint64_t alpha_u64 = (uint64_t)w * (uint64_t)h;
256  
257 if (out_u64 <= 16ULL * 1024ULL * 1024ULL) {
258 uint8_t *out_buf = (uint8_t *)malloc((size_t)out_u64);
259 uint8_t *alpha_buf = (uint8_t *)malloc((size_t)alpha_u64);
260  
261 if (out_buf) {
262 avx_speedhq_decode_c99(w, h, payload, payload_size, out_buf);
263 avx_speedhq_decode(w, h, payload, payload_size, out_buf);
264  
265 if (alpha_buf) {
266 bool has_alpha = false;
267 avx_speedhq_decode_ex(fourcc, w, h, payload, payload_size, out_buf, alpha_buf, &has_alpha);
268 avx_speedhq_decode_c99_ex(fourcc, w, h, payload, payload_size, out_buf, alpha_buf, &has_alpha);
269 }
270 free(out_buf);
271 }
272 if (alpha_buf) {
273 free(alpha_buf);
274 }
275 }
276 }
277 }
278  
279 static void test_fuzz_audio_input(const uint8_t *data, size_t size) {
280 if (!data || size < 12) return;
281  
282 int in_rate = ((int)(data[0] | (data[1] << 8)) % 200000) + 1;
283 int out_rate = ((int)(data[2] | (data[3] << 8)) % 200000) + 1;
284 int channels = (data[4] % 8) + 1;
285 avx_audio_resample_mode_e mode = (avx_audio_resample_mode_e)(data[5] % 3);
286 avx_audio_normalize_e norm_mode = (avx_audio_normalize_e)(data[6] % 3);
287  
288 const uint8_t *sample_bytes = data + 12;
289 size_t sample_len = size - 12;
290 int total_floats = (int)(sample_len / sizeof(float));
291 int frames = total_floats / channels;
292 if (frames > 256) frames = 256;
293 if (frames < 1) frames = 1;
294  
295 float *in_data[8] = {NULL};
296 float *out_data[8] = {NULL};
297 int max_out = frames * 4 + 128;
298  
299 for (int c = 0; c < 8; ++c) {
300 in_data[c] = (float *)calloc((size_t)frames + 16, sizeof(float));
301 out_data[c] = (float *)calloc((size_t)max_out + 16, sizeof(float));
302 }
303  
304 size_t copy_floats = (size_t)frames * (size_t)channels;
305 if (copy_floats > (size_t)total_floats) copy_floats = (size_t)total_floats;
306 if (copy_floats > 0) {
307 const float *src_floats = (const float *)sample_bytes;
308 for (int c = 0; c < channels; ++c) {
309 for (int f = 0; f < frames; ++f) {
310 size_t src_idx = (size_t)f * (size_t)channels + (size_t)c;
311 if (src_idx < (size_t)total_floats) {
312 in_data[c][f] = src_floats[src_idx];
313 }
314 }
315 }
316 }
317  
318 avx_resampler_t *r = avx_resampler_create(in_rate, out_rate, channels, mode);
319 if (r) {
320 (void)avx_resampler_process(r, (const float * const *)in_data, frames, out_data, max_out);
321 (void)avx_resampler_flush(r, out_data, max_out);
322 avx_resampler_reset(r);
323 avx_resampler_destroy(r);
324 }
325  
326 (void)avx_audio_resample_simple(in_data[0], frames, in_rate, out_data[0], max_out, out_rate, mode);
327  
328 if (frames > 0) {
329 avx_audio_downmix_stereo_to_mono(in_data[0], in_data[1], out_data[0], frames, norm_mode);
330 avx_audio_downmix_quad_to_stereo((const float * const *)in_data, out_data[0], out_data[1], frames, norm_mode);
331 avx_audio_downmix_5_1_to_stereo((const float * const *)in_data, out_data[0], out_data[1], frames, norm_mode);
332 avx_audio_downmix_7_1_to_stereo((const float * const *)in_data, out_data[0], out_data[1], frames, norm_mode);
333 }
334  
335 for (int c = 0; c < 8; ++c) {
336 free(in_data[c]);
337 free(out_data[c]);
338 }
339 }
340  
341 static int run_directory_regression(const char *dirpath, void (*test_func)(const uint8_t *, size_t)) {
342 DIR *d = opendir(dirpath);
343 if (!d) {
344 printf(" [!] Notice: Directory not found or empty: %s\n", dirpath);
345 return 0;
346 }
347  
348 struct dirent *dir;
349 int count = 0;
350 while ((dir = readdir(d)) != NULL) {
351 if (dir->d_name[0] == '.') continue;
352  
353 char full_path[1024];
354 snprintf(full_path, sizeof(full_path), "%s/%s", dirpath, dir->d_name);
355  
356 struct stat st;
357 if (stat(full_path, &st) == 0 && S_ISREG(st.st_mode)) {
358 size_t sz = 0;
359 uint8_t *data = read_file_to_buffer(full_path, &sz);
360 if (data) {
361 test_func(data, sz);
362 free(data);
363 count++;
364 }
365 }
366 }
367 closedir(d);
368 return count;
369 }
370  
371 int main(int argc, char **argv) {
372 (void)argc;
373 (void)argv;
374 #ifdef _WIN32
375 avx_platform_sockets_init();
376 #endif
377 printf("\n=== OpenAVX Fuzz Corpus & Crash Regression Tests ===\n\n");
378  
379 const char *source_dir = OPENAVX_PROJECT_SOURCE_DIR;
380 char path_buf[1024];
381  
382 /* 1. Initialize subsystems */
383 printf("[*] 1. Initializing decoders & internal profiles...\n");
384 bool shq_ok = avx_speedhq_init();
385 assert(shq_ok);
386 printf(" [+] avx_speedhq_init: OK\n");
387  
388 /* 2. Crash Artifact Regression */
389 printf("\n[*] 2. Executing historical crash artifact regressions...\n");
390
391 // Crash 1: Truncated UYVA packet (from Nov 7)
392 snprintf(path_buf, sizeof(path_buf), "%s/fuzz/crashes/fuzz_wire_packet-crash-93132a7274cb0ca0c515a8e30ef02cc5db82c4b6", source_dir);
393 size_t sz1 = 0;
394 uint8_t *crash1 = read_file_to_buffer(path_buf, &sz1);
395 if (crash1) {
396 printf(" [+] Executing Crash Artifact #1 (%zu bytes, wire truncation regression)...\n", sz1);
397 test_fuzz_wire_unpack(crash1, sz1);
398 test_fuzz_wire_socketpair(crash1, sz1);
399 free(crash1);
400 printf(" [+] Crash Artifact #1 PASS\n");
401 } else {
402 printf(" [!] Notice: Crash artifact #1 not found at %s (skipping)\n", path_buf);
403 }
404  
405 // Crash 2: Slow-unit malformed XML metadata
406 snprintf(path_buf, sizeof(path_buf), "%s/fuzz/crashes/fuzz_wire_packet-slow-unit-1f70c8a855f22213f70f8367a09c2de41c362e3c", source_dir);
407 size_t sz2 = 0;
408 uint8_t *crash2 = read_file_to_buffer(path_buf, &sz2);
409 if (crash2) {
410 printf(" [+] Executing Slow Unit Artifact #2 (%zu bytes, wire metadata parsing)...\n", sz2);
411 double t0 = get_time_ms();
412 test_fuzz_wire_unpack(crash2, sz2);
413 test_fuzz_wire_socketpair(crash2, sz2);
414 double elapsed = get_time_ms() - t0;
415 free(crash2);
416 printf(" [+] Slow Unit Artifact #2 PASS (%.2f ms)\n", elapsed);
417 } else {
418 printf(" [!] Notice: Slow unit artifact #2 not found at %s (skipping)\n", path_buf);
419 }
420  
421 /* 3. Wire Framing Corpus Regression */
422 printf("\n[*] 3. Executing Wire Framing Corpus Regression (fuzz/corpus/wire)...\n");
423 snprintf(path_buf, sizeof(path_buf), "%s/fuzz/corpus/wire", source_dir);
424 double t_wire_0 = get_time_ms();
425 int wire_count = run_directory_regression(path_buf, test_fuzz_wire_unpack);
426 double t_wire = get_time_ms() - t_wire_0;
427 printf(" [+] Wire corpus: %d inputs in %.2f ms\n", wire_count, t_wire);
428  
429 /* 4. SpeedHQ Bitstream Corpus Regression */
430 printf("\n[*] 4. Executing SpeedHQ Bitstream Corpus Regression (fuzz/corpus/speedhq)...\n");
431 snprintf(path_buf, sizeof(path_buf), "%s/fuzz/corpus/speedhq", source_dir);
432 double t_shq_0 = get_time_ms();
433 int shq_count = run_directory_regression(path_buf, test_fuzz_speedhq_input);
434 double t_shq = get_time_ms() - t_shq_0;
435 printf(" [+] SpeedHQ corpus: %d inputs in %.2f ms\n", shq_count, t_shq);
436  
437 /* 5. Audio Resampler & Downmix Corpus Regression */
438 printf("\n[*] 5. Executing Audio DSP Corpus Regression (fuzz/corpus/audio)...\n");
439 snprintf(path_buf, sizeof(path_buf), "%s/fuzz/corpus/audio", source_dir);
440 double t_audio_0 = get_time_ms();
441 int audio_count = run_directory_regression(path_buf, test_fuzz_audio_input);
442 double t_audio = get_time_ms() - t_audio_0;
443 printf(" [+] Audio corpus: %d inputs in %.2f ms\n", audio_count, t_audio);
444  
445 printf("\n[PASS] openavx_fuzz_regression_test (%d inputs)\n\n", wire_count + shq_count + audio_count + 2);
446  
447 return 0;
448 }