@@ -48,8 +48,8 @@ enum TEST_ID {
48
48
TEST_ID_END
49
49
};
50
50
51
- bool read_stdin (std::vector<char > &data) {
52
- char buffer[1024 ];
51
+ bool read_stdin (std::vector<uint8_t > &data) {
52
+ uint8_t buffer[1024 ];
53
53
ssize_t length=0 ;
54
54
while ((length = read (STDIN_FILENO, buffer, 1024 )) > 0 ) {
55
55
data.insert (data.end (), buffer, buffer+length);
@@ -59,11 +59,7 @@ bool read_stdin(std::vector<char> &data) {
59
59
return length==0 ;
60
60
}
61
61
62
- int do_fuzz ()
63
- {
64
- std::vector<char > buffer;
65
- if (!read_stdin (buffer)) return 0 ;
66
-
62
+ int test_one_input (std::vector<uint8_t > buffer) {
67
63
if (buffer.size () < sizeof (uint32_t )) return 0 ;
68
64
69
65
uint32_t test_id = 0xffffffff ;
@@ -255,9 +251,32 @@ int do_fuzz()
255
251
return 0 ;
256
252
}
257
253
254
+ static std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
255
+ void initialize () {
256
+ globalVerifyHandle = std::unique_ptr<ECCVerifyHandle>(new ECCVerifyHandle ());
257
+ }
258
+
259
+ // This function is used by libFuzzer
260
+ extern " C" int LLVMFuzzerTestOneInput (const uint8_t *data, size_t size) {
261
+ test_one_input (std::vector<uint8_t >(data, data + size));
262
+ return 0 ;
263
+ }
264
+
265
+ // This function is used by libFuzzer
266
+ extern " C" int LLVMFuzzerInitialize (int *argc, char ***argv) {
267
+ initialize ();
268
+ return 0 ;
269
+ }
270
+
271
+ // Disabled under WIN32 due to clash with Cygwin's WinMain.
272
+ #ifndef WIN32
273
+ // Declare main(...) "weak" to allow for libFuzzer linking. libFuzzer provides
274
+ // the main(...) function.
275
+ __attribute__ ((weak))
276
+ #endif
258
277
int main (int argc, char **argv)
259
278
{
260
- ECCVerifyHandle globalVerifyHandle ;
279
+ initialize () ;
261
280
#ifdef __AFL_INIT
262
281
// Enable AFL deferred forkserver mode. Requires compilation using
263
282
// afl-clang-fast++. See fuzzing.md for details.
@@ -267,11 +286,20 @@ int main(int argc, char **argv)
267
286
#ifdef __AFL_LOOP
268
287
// Enable AFL persistent mode. Requires compilation using afl-clang-fast++.
269
288
// See fuzzing.md for details.
289
+ int ret = 0 ;
270
290
while (__AFL_LOOP (1000 )) {
271
- do_fuzz ();
291
+ std::vector<uint8_t > buffer;
292
+ if (!read_stdin (buffer)) {
293
+ continue ;
294
+ }
295
+ ret = test_one_input (buffer);
272
296
}
273
- return 0 ;
297
+ return ret ;
274
298
#else
275
- return do_fuzz ();
299
+ std::vector<uint8_t > buffer;
300
+ if (!read_stdin (buffer)) {
301
+ return 0 ;
302
+ }
303
+ return test_one_input (buffer);
276
304
#endif
277
305
}
0 commit comments