| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
 | #include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
// typedef for our exported target function.
typedef void (*CRASHME)(const uint8_t *Data, size_t Size);
// globals
CRASHME fpn_crashme = NULL;
int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size) {
  fpn_crashme(data, size);
  return 0;
}
int main(int argc, const char *argv[]) {
  for (int i = 1; i < argc; i++) {
    fprintf(stderr, "Running: %s\n", argv[i]);
    FILE *f = fopen(argv[i], "r");
    assert(f);
    fseek(f, 0, SEEK_END);
    size_t len = ftell(f);
    fseek(f, 0, SEEK_SET);
    unsigned char *buf = (unsigned char *)malloc(len);
    size_t         n_read = fread(buf, 1, len, f);
    fclose(f);
    assert(n_read == len);
    LLVMFuzzerTestOneInput(buf, len);
    free(buf);
    fprintf(stderr, "Done:    %s: (%zd bytes)\n", argv[i], n_read);
  }
  return 0;
}
__attribute__((constructor())) void constructor(void) {
  // handles to required libs
  void *dylib = NULL;
  dylib = dlopen("./libcrashme2.dylib", RTLD_NOW);
  if (dylib == NULL) {
    printf("[-] Failed to load lib\n");
    printf("[-] Dlerror: %s\n", dlerror());
    exit(1);
  }
  printf("[+] Resolve function\n");
  fpn_crashme = (CRASHME)dlsym(dylib, "crashme");
  if (!fpn_crashme) {
    printf("[-] Failed to find function\n");
    exit(1);
  }
  printf("[+] Found function.\n");
}
 |