xref: /aosp_15_r20/external/AFLplusplus/test/test-dlopen.c (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 #include <stdio.h>
2 #include <errno.h>
3 #include <dlfcn.h>
4 #include <stdlib.h>
5 
main(int argc,char ** argv)6 int main(int argc, char **argv) {
7 
8   if (!getenv("TEST_DLOPEN_TARGET")) {
9 
10     fprintf(stderr, "Error: TEST_DLOPEN_TARGET not set!\n");
11     return 1;
12 
13   }
14 
15   void *lib = dlopen(getenv("TEST_DLOPEN_TARGET"), RTLD_LAZY);
16   if (!lib) {
17 
18     perror(dlerror());
19     return 2;
20 
21   }
22 
23   int (*func)(int, char **) = dlsym(lib, "main_exported");
24   if (!func) {
25 
26     fprintf(stderr, "Error: main_exported not found!\n");
27     return 3;
28 
29   }
30 
31   // must use deferred forkserver as otherwise AFL++ instrumentation aborts
32   // because all dlopen() of instrumented libs must be before the forkserver
33   __AFL_INIT();
34 
35   fprintf(stderr, "Running main_exported\n");
36   return func(argc, argv);
37 
38 }
39 
40