Home | History | Annotate | Download | only in fuzzer
      1 // Copyright 2016 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <stddef.h>
      6 #include <stdint.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 
     10 extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv);
     11 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size);
     12 
     13 int main(int argc, char* argv[]) {
     14   if (LLVMFuzzerInitialize(&argc, &argv)) {
     15     fprintf(stderr, "Failed to initialize fuzzer target\n");
     16     return 1;
     17   }
     18 
     19   if (argc < 2) {
     20     fprintf(stderr, "USAGE: %s <input>\n", argv[0]);
     21     return 1;
     22   }
     23 
     24   FILE* input = fopen(argv[1], "rb");
     25 
     26   if (!input) {
     27     fprintf(stderr, "Failed to open '%s'\n", argv[1]);
     28     return 1;
     29   }
     30 
     31   fseek(input, 0, SEEK_END);
     32   long size = ftell(input);
     33   fseek(input, 0, SEEK_SET);
     34 
     35   uint8_t* data = reinterpret_cast<uint8_t*>(malloc(size));
     36   if (!data) {
     37     fclose(input);
     38     fprintf(stderr, "Failed to allocate %ld bytes\n", size);
     39     return 1;
     40   }
     41 
     42   size_t bytes_read = fread(data, 1, size, input);
     43   fclose(input);
     44 
     45   if (bytes_read != size) {
     46     free(data);
     47     fprintf(stderr, "Failed to read %s\n", argv[1]);
     48     return 1;
     49   }
     50 
     51   int result = LLVMFuzzerTestOneInput(data, size);
     52 
     53   free(data);
     54 
     55   return result;
     56 }
     57