xref: /aosp_15_r20/external/AFLplusplus/utils/aflpp_driver/aflpp_qemu_driver.c (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 #include <stdio.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 
6 // libFuzzer interface is thin, so we don't include any libFuzzer headers.
7 int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
8 __attribute__((weak)) int LLVMFuzzerInitialize(int *argc, char ***argv);
9 
10 #define kMaxAflInputSize (1 * 1024 * 1024)
11 static uint8_t AflInputBuf[kMaxAflInputSize];
12 
afl_qemu_driver_stdin_input(void)13 void __attribute__((noinline)) afl_qemu_driver_stdin_input(void) {
14 
15   size_t l = read(0, AflInputBuf, kMaxAflInputSize);
16   LLVMFuzzerTestOneInput(AflInputBuf, l);
17 
18 }
19 
main(int argc,char ** argv)20 int main(int argc, char **argv) {
21 
22   if (LLVMFuzzerInitialize) LLVMFuzzerInitialize(&argc, &argv);
23   // Do any other expensive one-time initialization here.
24 
25   if (getenv("AFL_QEMU_DRIVER_NO_HOOK") || getenv("AFL_FRIDA_DRIVER_NO_HOOK")) {
26 
27     afl_qemu_driver_stdin_input();
28 
29   } else {
30 
31     fprintf(stderr,
32             "Using shared-memory testcases. To read via stdin, set "
33             "AFL_QEMU_DRIVER_NO_HOOK=1.\n");
34     uint8_t dummy_input[1024000] = {0};
35     LLVMFuzzerTestOneInput(dummy_input, 1);
36 
37   }
38 
39   return 0;
40 
41 }
42 
43