xref: /aosp_15_r20/external/AFLplusplus/utils/argv_fuzzing/argvfuzz.c (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 /*
2    american fuzzy lop++ - LD_PRELOAD for fuzzing argv in binaries
3    ------------------------------------------------------------
4 
5    Copyright 2019-2024 Kjell Braden <[email protected]>
6 
7    Licensed under the Apache License, Version 2.0 (the "License");
8    you may not use this file except in compliance with the License.
9    You may obtain a copy of the License at:
10 
11      http://www.apache.org/licenses/LICENSE-2.0
12 
13  */
14 
15 #define _GNU_SOURCE                                        /* for RTLD_NEXT */
16 #include <dlfcn.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include "argv-fuzz-inl.h"
21 
__libc_start_main(int (* main)(int,char **,char **),int argc,char ** argv,void (* init)(void),void (* fini)(void),void (* rtld_fini)(void),void * stack_end)22 int __libc_start_main(int (*main)(int, char **, char **), int argc, char **argv,
23                       void (*init)(void), void (*fini)(void),
24                       void (*rtld_fini)(void), void *stack_end) {
25 
26   int (*orig)(int (*main)(int, char **, char **), int argc, char **argv,
27               void (*init)(void), void (*fini)(void), void (*rtld_fini)(void),
28               void *stack_end);
29   int    sub_argc;
30   char **sub_argv;
31 
32   (void)argc;
33   (void)argv;
34 
35   orig = dlsym(RTLD_NEXT, __func__);
36 
37   if (!orig) {
38 
39     fprintf(stderr, "hook did not find original %s: %s\n", __func__, dlerror());
40     exit(EXIT_FAILURE);
41 
42   }
43 
44   sub_argv = afl_init_argv(&sub_argc);
45 
46   return orig(main, sub_argc, sub_argv, init, fini, rtld_fini, stack_end);
47 
48 }
49 
50