aboutsummaryrefslogtreecommitdiff
path: root/utils/argv_fuzzing/argv_fuzz_persistent_demo.c
blob: 016c3d350fc80187d5fb9ba3a3238c056d063cf4 (plain)
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
/*
This file contains a simple fuzzer for testing command line argument parsing
using persistent mode.
*/

#include <stdio.h>
#include <string.h>
#include "argv-fuzz-inl.h"

__AFL_FUZZ_INIT();

/* The main function is an entry point for a program.
   The argc parameter is an integer that indicates the number of arguments
   passed to the program. The argv parameter is an array of character pointers,
   with each element pointing to a null-terminated string that represents
   one of the arguments.
 */
int main(int argc, char **argv) {

#ifdef __AFL_HAVE_MANUAL_CONTROL
  __AFL_INIT();
#endif
  unsigned char *buf = __AFL_FUZZ_TESTCASE_BUF;

  /* __AFL_LOOP() limits the maximum number of iterations before exiting
     the loop and allowing the program to terminate. It protects against
     accidental memory leaks and similar issues. */
  while (__AFL_LOOP(100000)) {

    int len = __AFL_FUZZ_TESTCASE_LEN;

    // Check that the length of the test case is at least 8 bytes
    if (len < 8) continue;

    // Initialize the command line arguments using the testcase buffer
    AFL_INIT_ARGV_PERSISTENT(buf);

    /* Check if the first argument is "XYZ" and the second argument is "TEST2"
       If so, call the "abort" function to terminate the program.
       Otherwise, print an error message. */
    if (argc > 1 && strcmp(argv[1], "XYZ") == 0) {

      if (strcmp(argv[2], "TEST2") == 0) { abort(); }

    } else {

      printf("Bad number of arguments!\n");

    }

  }

  /* Exiting the loop allows the program to terminate normally. AFL will restart
     the process with a clean slate for allocated memory, file descriptors, etc.
  */
  return 0;

}