2 * Copyright (c) 2016-present, Facebook, Inc.
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).
11 #include "fuzz_helpers.h"
18 int main(int argc, char const **argv) {
19 size_t const kMaxFileSize = (size_t)1 << 27;
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;
29 #ifdef UTIL_HAS_CREATEFILELIST
30 files = UTIL_createFileList(files, numFiles, &fileNamesBuf, &numFiles,
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 DEBUGLOG(3, "Running %s", fileName);
40 size_t const fileSize = UTIL_getFileSize(fileName);
44 /* Check that it is a regular file, and that the fileSize is valid.
45 * If it is not a regular file, then it may have been deleted since we
46 * constructed the list, so just skip it.
48 if (!UTIL_isRegularFile(fileName)) {
51 FUZZ_ASSERT_MSG(fileSize <= kMaxFileSize, fileName);
52 /* Ensure we have a large enough buffer allocated */
53 if (fileSize > bufferSize) {
55 buffer = (uint8_t *)malloc(fileSize);
56 FUZZ_ASSERT_MSG(buffer, fileName);
57 bufferSize = fileSize;
60 file = fopen(fileName, "rb");
61 FUZZ_ASSERT_MSG(file, fileName);
63 readSize = fread(buffer, 1, fileSize, file);
64 FUZZ_ASSERT_MSG(readSize == fileSize, fileName);
67 /* Run the fuzz target */
68 LLVMFuzzerTestOneInput(buffer, fileSize);
73 #ifdef UTIL_HAS_CREATEFILELIST
74 UTIL_freeFileList(files, fileNamesBuf);