xref: /aosp_15_r20/external/AFLplusplus/utils/argv_fuzzing/argv_fuzz_persistent_demo.c (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 /*
2 This file contains a simple fuzzer for testing command line argument parsing
3 using persistent mode.
4 */
5 
6 #include <stdio.h>
7 #include <string.h>
8 #include "argv-fuzz-inl.h"
9 
10 __AFL_FUZZ_INIT();
11 
12 /* The main function is an entry point for a program.
13    The argc parameter is an integer that indicates the number of arguments
14    passed to the program. The argv parameter is an array of character pointers,
15    with each element pointing to a null-terminated string that represents
16    one of the arguments.
17  */
main(int argc,char ** argv)18 int main(int argc, char **argv) {
19 
20 #ifdef __AFL_HAVE_MANUAL_CONTROL
21   __AFL_INIT();
22 #endif
23   unsigned char *buf = __AFL_FUZZ_TESTCASE_BUF;
24 
25   /* __AFL_LOOP() limits the maximum number of iterations before exiting
26      the loop and allowing the program to terminate. It protects against
27      accidental memory leaks and similar issues. */
28   while (__AFL_LOOP(100000)) {
29 
30     int len = __AFL_FUZZ_TESTCASE_LEN;
31 
32     // Check that the length of the test case is at least 8 bytes
33     if (len < 8) continue;
34 
35     // Initialize the command line arguments using the testcase buffer
36     AFL_INIT_ARGV_PERSISTENT(buf);
37 
38     /* Check if the first argument is "XYZ" and the second argument is "TEST2"
39        If so, call the "abort" function to terminate the program.
40        Otherwise, print an error message. */
41     if (argc > 1 && strcmp(argv[1], "XYZ") == 0) {
42 
43       if (strcmp(argv[2], "TEST2") == 0) { abort(); }
44 
45     } else {
46 
47       printf("Bad number of arguments!\n");
48 
49     }
50 
51   }
52 
53   /* Exiting the loop allows the program to terminate normally. AFL will restart
54      the process with a clean slate for allocated memory, file descriptors, etc.
55   */
56   return 0;
57 
58 }
59 
60