• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "sysdep.h"
2 #include "bfd.h"
3 
4 #include <stdint.h>
5 #include <stdio.h>
6 
7 
bufferToFile(const char * name,const uint8_t * Data,size_t Size)8 static int bufferToFile(const char * name, const uint8_t *Data, size_t Size) {
9     FILE * fd;
10     if (remove(name) != 0) {
11         if (errno != ENOENT) {
12             printf("failed remove, errno=%d\n", errno);
13             return -1;
14         }
15     }
16     fd = fopen(name, "wb");
17     if (fd == NULL) {
18         printf("failed open, errno=%d\n", errno);
19         return -2;
20     }
21     if (fwrite (Data, 1, Size, fd) != Size) {
22         fclose(fd);
23         return -3;
24     }
25     fclose(fd);
26     return 0;
27 }
28 
29 static int initialized = 0;
30 //TODO? part of fuzzing
31 char *target = NULL;
32 
LLVMFuzzerTestOneInput(const uint8_t * Data,size_t Size)33 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
34     if (initialized == 0) {
35         if (bfd_init () != BFD_INIT_MAGIC) {
36             abort();
37         }
38         initialized = 1;
39     }
40 
41     if (bufferToFile("/tmp/fuzz.bfd", Data, Size) < 0) {
42         abort();
43     }
44     bfd *file = bfd_openr ("/tmp/fuzz.bfd", target);
45     if (file == NULL)
46     {
47         return 0;
48     }
49     bfd_check_format (file, bfd_archive);
50     //TODO loop over subfiles and more processing
51     bfd_close (file);
52 
53     return 0;
54 }
55