]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | /* |
2 | * Copyright (c) 2016-present, Facebook, Inc. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This source code is licensed under both the BSD-style license (found in the | |
6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found | |
7 | * in the COPYING file in the root directory of this source tree). | |
8 | */ | |
9 | ||
10 | #include "fuzz.h" | |
11 | #include "fuzz_helpers.h" | |
12 | #include "util.h" | |
13 | #include <stddef.h> | |
14 | #include <stdint.h> | |
15 | #include <stdio.h> | |
16 | #include <stdlib.h> | |
17 | ||
18 | int main(int argc, char const **argv) { | |
9f95a23c | 19 | size_t const kMaxFileSize = (size_t)1 << 27; |
11fdf7f2 TL |
20 | int const kFollowLinks = 1; |
21 | char *fileNamesBuf = NULL; | |
22 | char const **files = argv + 1; | |
23 | unsigned numFiles = argc - 1; | |
24 | uint8_t *buffer = NULL; | |
25 | size_t bufferSize = 0; | |
26 | unsigned i; | |
27 | int ret; | |
28 | ||
29 | #ifdef UTIL_HAS_CREATEFILELIST | |
30 | files = UTIL_createFileList(files, numFiles, &fileNamesBuf, &numFiles, | |
31 | kFollowLinks); | |
32 | if (!files) | |
33 | numFiles = 0; | |
34 | #endif | |
35 | if (numFiles == 0) | |
36 | fprintf(stderr, "WARNING: No files passed to %s\n", argv[0]); | |
37 | for (i = 0; i < numFiles; ++i) { | |
38 | char const *fileName = files[i]; | |
39 | size_t const fileSize = UTIL_getFileSize(fileName); | |
40 | size_t readSize; | |
41 | FILE *file; | |
42 | ||
9f95a23c TL |
43 | /* Check that it is a regular file, and that the fileSize is valid. |
44 | * If it is not a regular file, then it may have been deleted since we | |
45 | * constructed the list, so just skip it. | |
46 | */ | |
47 | if (!UTIL_isRegularFile(fileName)) { | |
48 | continue; | |
49 | } | |
11fdf7f2 TL |
50 | FUZZ_ASSERT_MSG(fileSize <= kMaxFileSize, fileName); |
51 | /* Ensure we have a large enough buffer allocated */ | |
52 | if (fileSize > bufferSize) { | |
53 | free(buffer); | |
54 | buffer = (uint8_t *)malloc(fileSize); | |
55 | FUZZ_ASSERT_MSG(buffer, fileName); | |
56 | bufferSize = fileSize; | |
57 | } | |
58 | /* Open the file */ | |
59 | file = fopen(fileName, "rb"); | |
60 | FUZZ_ASSERT_MSG(file, fileName); | |
61 | /* Read the file */ | |
62 | readSize = fread(buffer, 1, fileSize, file); | |
63 | FUZZ_ASSERT_MSG(readSize == fileSize, fileName); | |
64 | /* Close the file */ | |
65 | fclose(file); | |
66 | /* Run the fuzz target */ | |
67 | LLVMFuzzerTestOneInput(buffer, fileSize); | |
68 | } | |
69 | ||
70 | ret = 0; | |
71 | free(buffer); | |
72 | #ifdef UTIL_HAS_CREATEFILELIST | |
73 | UTIL_freeFileList(files, fileNamesBuf); | |
74 | #endif | |
75 | return ret; | |
76 | } |