lib: Give lib/util/util_file.c its own header file
[samba.git] / lib / fuzzing / afl-fuzz-main.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Fuzz driver (AFL style)
5
6    Copyright (C) Andrew Bartlett 2019
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/util/util_file.h"
24 #include "fuzzing.h"
25
26 int main(int argc, char *argv[]) {
27         int ret;
28         size_t size = 0;
29         int i;
30
31         ret = LLVMFuzzerInitialize(&argc, &argv);
32         if (ret != 0) {
33                 printf("LLVMFuzzerInitialize returned %d\n", ret);
34                 return ret;
35         }
36
37
38 #ifdef __AFL_LOOP
39         while (__AFL_LOOP(1000))
40 #else
41         for (i = 1; i < argc; i++) {
42                 uint8_t *buf = (uint8_t *)file_load(argv[i],
43                                                     &size,
44                                                     0,
45                                                     NULL);
46                 ret = LLVMFuzzerTestOneInput(buf, size);
47                 TALLOC_FREE(buf);
48                 if (ret != 0) {
49                         printf("LLVMFuzzerTestOneInput returned %d on argument %d\n",
50                                ret, i);
51                         return ret;
52                 }
53         }
54         if (i == 1)
55 #endif
56         {
57                 uint8_t *buf = (uint8_t *)fd_load(0, &size, 0, NULL);
58                 if (buf == NULL) {
59                         exit(1);
60                 }
61
62                 ret = LLVMFuzzerTestOneInput(buf, size);
63                 TALLOC_FREE(buf);
64         }
65         return ret;
66 }