xref: /aosp_15_r20/external/AFLplusplus/include/common.h (revision 08b48e0b10e97b33e7b60c5b6e2243bd915777f2)
1 /*
2    american fuzzy lop++ - common routines header
3    ---------------------------------------------
4 
5    Originally written by Michal Zalewski
6 
7    Now maintained by Marc Heuse <[email protected]>,
8                      Heiko Eißfeldt <[email protected]>,
9                      Andrea Fioraldi <[email protected]>,
10                      Dominik Maier <[email protected]>
11 
12    Copyright 2016, 2017 Google Inc. All rights reserved.
13    Copyright 2019-2024 AFLplusplus Project. All rights reserved.
14 
15    Licensed under the Apache License, Version 2.0 (the "License");
16    you may not use this file except in compliance with the License.
17    You may obtain a copy of the License at:
18 
19      https://www.apache.org/licenses/LICENSE-2.0
20 
21    Gather some functions common to multiple executables
22 
23    - detect_file_args
24 
25  */
26 
27 #ifndef __AFLCOMMON_H
28 #define __AFLCOMMON_H
29 
30 #include <stdio.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/time.h>
34 #include <stdbool.h>
35 #include "forkserver.h"
36 #include "types.h"
37 
38 /* STRINGIFY_VAL_SIZE_MAX will fit all stringify_ strings. */
39 
40 #define STRINGIFY_VAL_SIZE_MAX (16)
41 
42 u32  check_binary_signatures(u8 *fn);
43 void detect_file_args(char **argv, u8 *prog_in, bool *use_stdin);
44 void print_suggested_envs(char *mispelled_env);
45 void check_environment_vars(char **env);
46 void set_sanitizer_defaults();
47 
48 char **argv_cpy_dup(int argc, char **argv);
49 void   argv_cpy_free(char **argv);
50 
51 char **get_cs_argv(u8 *own_loc, u8 **target_path_p, int argc, char **argv);
52 char **get_qemu_argv(u8 *own_loc, u8 **target_path_p, int argc, char **argv);
53 char **get_wine_argv(u8 *own_loc, u8 **target_path_p, int argc, char **argv);
54 char  *get_afl_env(char *env);
55 
56 /* Extract env vars from input string and set them using setenv()
57    For use with AFL_TARGET_ENV, ... */
58 bool extract_and_set_env(u8 *env_str);
59 
60 extern u8  be_quiet;
61 extern u8 *doc_path;                    /* path to documentation dir        */
62 
63 /* Find binary, used by analyze, showmap, tmin
64    @returns the path, allocating the string */
65 
66 u8 *find_binary(u8 *fname);
67 
68 /* find an afl binary */
69 
70 u8 *find_afl_binary(u8 *own_loc, u8 *fname);
71 
72 /* Parses the (numeric) kill signal environment variable passed
73    via `numeric_signal_as_str`.
74    If NULL is passed, the `default_signal` value is returned.
75    FATALs if `numeric_signal_as_str` is not a valid integer .*/
76 int parse_afl_kill_signal(u8 *numeric_signal_as_str, int default_signal);
77 
78 /* Configure the signals that are used to kill the forkserver
79    and the forked childs. If `afl_kill_signal_env` or `afl_fsrv_kill_signal_env`
80    is NULL, the appropiate values are read from the environment. */
81 void configure_afl_kill_signals(afl_forkserver_t *fsrv,
82                                 char             *afl_kill_signal_env,
83                                 char             *afl_fsrv_kill_signal_env,
84                                 int               default_server_kill_signal);
85 
86 /* Read a bitmap from file fname to memory
87    This is for the -B option again. */
88 
89 void read_bitmap(u8 *fname, u8 *map, size_t len);
90 
91 /* Get unix time in milliseconds */
92 
93 u64 get_cur_time(void);
94 
95 /* Get unix time in microseconds */
96 
97 u64 get_cur_time_us(void);
98 
99 /* Describe integer. The buf should be
100    at least 6 bytes to fit all ints we randomly see.
101    Will return buf for convenience. */
102 
103 u8 *stringify_int(u8 *buf, size_t len, u64 val);
104 
105 /* Describe float. Similar as int. */
106 
107 u8 *stringify_float(u8 *buf, size_t len, double val);
108 
109 /* Describe integer as memory size. */
110 
111 u8 *stringify_mem_size(u8 *buf, size_t len, u64 val);
112 
113 /* Describe time delta as string.
114    Returns a pointer to buf for convenience. */
115 
116 u8 *stringify_time_diff(u8 *buf, size_t len, u64 cur_ms, u64 event_ms);
117 
118 /* Unsafe describe time delta as simple string.
119    Returns a pointer to buf for convenience. */
120 
121 u8 *u_simplestring_time_diff(u8 *buf, u64 cur_ms, u64 event_ms);
122 
123 /* Unsafe Describe integer. The buf sizes are not checked.
124    This is unsafe but fast.
125    Will return buf for convenience. */
126 
127 u8 *u_stringify_int(u8 *buf, u64 val);
128 
129 /* Unsafe describe float. Similar as unsafe int. */
130 
131 u8 *u_stringify_float(u8 *buf, double val);
132 
133 /* Unsafe describe integer as memory size. */
134 
135 u8 *u_stringify_mem_size(u8 *buf, u64 val);
136 
137 /* Unsafe describe time delta as string.
138    Returns a pointer to buf for convenience. */
139 
140 u8 *u_stringify_time_diff(u8 *buf, u64 cur_ms, u64 event_ms);
141 
142 /* Reads the map size from ENV */
143 u32 get_map_size(void);
144 
145 /* create a stream file */
146 FILE *create_ffile(u8 *fn);
147 
148 /* create a file */
149 s32 create_file(u8 *fn);
150 
151 /* memmem implementation as not all platforms support this */
152 void *afl_memmem(const void *haystack, size_t haystacklen, const void *needle,
153                  size_t needlelen);
154 
155 #ifdef __linux__
156 /* Nyx helper functions to create and remove tmp workdirs */
157 char *create_nyx_tmp_workdir(void);
158 void  remove_nyx_tmp_workdir(afl_forkserver_t *fsrv, char *nyx_out_dir_path);
159 #endif
160 
161 #endif
162 
163