History

add clang libFuzzer support

lieff committed 8 years ago

Commit f3b33b01 parent 0b82f032 files · +43 −22Browse files

Unified · Split

2 files
  1. minimp3_test.c +36 −22
  2. scripts/fuzz_libfuzzer.sh +7 −0
minimp3_test.c+36−22
@@ -48,14 +48,12 @@ static unsigned char *preload(FILE *file, int *data_size) · 47 unmodified lines
48 return data;48 return data;
49}49}
50 50
51static void decode_file(FILE *file_mp3, FILE *file_ref, FILE *file_out, const int wave_out)51static void decode_file(const unsigned char *buf_mp3, int mp3_size, const unsigned char *buf_ref, int ref_size, FILE *file_out, const int wave_out)
52{52{
53 static mp3dec_t mp3d;53 static mp3dec_t mp3d;
54 mp3dec_frame_info_t info = { 0, };54 mp3dec_frame_info_t info = { 0, };
55 int i, data_bytes, samples, total_samples = 0, maxdiff = 0, mp3_size, ref_size;55 int i, data_bytes, samples, total_samples = 0, maxdiff = 0;
56 double MSE = 0.0, psnr;56 double MSE = 0.0, psnr;
57 unsigned char *buf_mp3 = preload(file_mp3, &mp3_size), *buf_ref = preload(file_ref, &ref_size);
58 unsigned char *alloc_buf_mp3 = buf_mp3, *alloc_buf_ref = buf_ref;
59 57
60 mp3dec_init(&mp3d);58 mp3dec_init(&mp3d);
61#ifndef MINIMP3_NO_WAV59#ifndef MINIMP3_NO_WAV
@@ -87,7 +85,7 @@ static void decode_file(FILE *file_mp3, FILE *file_ref, FILE *file_out, const in · 25 unmodified lines
87 buf_mp3 += info.frame_bytes;85 buf_mp3 += info.frame_bytes;
88 mp3_size -= info.frame_bytes;86 mp3_size -= info.frame_bytes;
89 } while (info.frame_bytes);87 } while (info.frame_bytes);
90 88#ifndef LIBFUZZER
91 MSE /= total_samples ? total_samples : 1;89 MSE /= total_samples ? total_samples : 1;
92 if (0 == MSE)90 if (0 == MSE)
93 psnr = 99.0;91 psnr = 99.0;
@@ -99,6 +97,7 @@ static void decode_file(FILE *file_mp3, FILE *file_ref, FILE *file_out, const in · 5 unmodified lines
99 printf("PSNR compliance failed\n");97 printf("PSNR compliance failed\n");
100 exit(1);98 exit(1);
101 }99 }
100#endif
102#ifndef MINIMP3_NO_WAV101#ifndef MINIMP3_NO_WAV
103 if (wave_out && file_out)102 if (wave_out && file_out)
104 {103 {
@@ -107,17 +106,16 @@ static void decode_file(FILE *file_mp3, FILE *file_ref, FILE *file_out, const in · 2 unmodified lines
107 fwrite(wav_header(info.hz, info.channels, 16, data_bytes), 1, 44, file_out);106 fwrite(wav_header(info.hz, info.channels, 16, data_bytes), 1, 44, file_out);
108 }107 }
109#endif108#endif
110 if (alloc_buf_mp3)
111 free(alloc_buf_mp3);
112 if (alloc_buf_ref)
113 free(alloc_buf_ref);
114 fclose(file_mp3);
115 if (file_ref)
116 fclose(file_ref);
117 if (file_out)
118 fclose(file_out);
119}109}
120 110
111#ifdef LIBFUZZER
112int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size)
113{
114 decode_file(Data, Size, 0, 0, 0, 0);
115 return 0;
116}
117#else
118
121#ifdef __arm__119#ifdef __arm__
122int main2(int argc, char *argv[]);120int main2(int argc, char *argv[]);
123int main2(int argc, char *argv[])121int main2(int argc, char *argv[])
@@ -125,31 +123,46 @@ int main2(int argc, char *argv[]) · 1 unmodified line
125int main(int argc, char *argv[])123int main(int argc, char *argv[])
126#endif124#endif
127{125{
128#ifdef __AFL_HAVE_MANUAL_CONTROL126 int wave_out = 0, ref_size, mp3_size;
129 __AFL_INIT();
130 while (__AFL_LOOP(1000)) {
131#endif
132 char *input_file_name = (argc > 1) ? argv[1] : NULL;
133 char *ref_file_name = (argc > 2) ? argv[2] : NULL;127 char *ref_file_name = (argc > 2) ? argv[2] : NULL;
134 char *output_file_name = (argc > 3) ? argv[3] : NULL;128 char *output_file_name = (argc > 3) ? argv[3] : NULL;
135 int wave_out = 0;129 FILE *file_out = NULL;
136#ifndef MINIMP3_NO_WAV
137 if (output_file_name)130 if (output_file_name)
138 {131 {
132 file_out = fopen(output_file_name, "wb");
133#ifndef MINIMP3_NO_WAV
139 char *ext = strrchr(output_file_name, '.');134 char *ext = strrchr(output_file_name, '.');
140 if (ext && !strcasecmp(ext + 1, "wav"))135 if (ext && !strcasecmp(ext + 1, "wav"))
141 wave_out = 1;136 wave_out = 1;
137#endif
142 }138 }
139 FILE *file_ref = fopen(ref_file_name, "rb");
140 unsigned char *buf_ref = preload(file_ref, &ref_size);
141 if (file_ref)
142 fclose(file_ref);
143#ifdef __AFL_HAVE_MANUAL_CONTROL
144 __AFL_INIT();
145 while (__AFL_LOOP(1000)) {
143#endif146#endif
147 char *input_file_name = (argc > 1) ? argv[1] : NULL;
144 if (!input_file_name)148 if (!input_file_name)
145 {149 {
146 printf("error: no file names given\n");150 printf("error: no file names given\n");
147 return 1;151 return 1;
148 }152 }
149 decode_file(fopen(input_file_name, "rb"), fopen(ref_file_name, "rb"), output_file_name ? fopen(output_file_name, "wb") : NULL, wave_out);153 FILE *file_mp3 = fopen(input_file_name, "rb");
154 unsigned char *buf_mp3 = preload(file_mp3, &mp3_size);
155 fclose(file_mp3);
156 decode_file(buf_mp3, mp3_size, buf_ref, ref_size, file_out, wave_out);
157 if (buf_mp3)
158 free(buf_mp3);
150#ifdef __AFL_HAVE_MANUAL_CONTROL159#ifdef __AFL_HAVE_MANUAL_CONTROL
151 }160 }
152#endif161#endif
162 if (buf_ref)
163 free(buf_ref);
164 if (file_out)
165 fclose(file_out);
153 return 0;166 return 0;
154}167}
155 168
@@ -250,3 +263,4 @@ int main() · 94 unmodified lines
250 return 0;263 return 0;
251}264}
252#endif265#endif
266#endif
scripts/fuzz_libfuzzer.sh+7−0
@@ -0,0 +1,7 @@
1_FILENAME=${0##*/}
2CUR_DIR=${0/${_FILENAME}}
3CUR_DIR=$(cd $(dirname ${CUR_DIR}); pwd)/$(basename ${CUR_DIR})/
4
5pushd $CUR_DIR/..
6
7clang-5.0 -g -fsanitize=address,undefined -fsanitize-coverage=trace-pc-guard -DFUZZ -DLIBFUZZER -o minimp3_libfuzz minimp3_test.c -lFuzzer -lstdc++ -lm

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t