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 size_t const fileSize = UTIL_getFileSize(fileName);
43 /* Check that it is a regular file, and that the fileSize is valid */
44 FUZZ_ASSERT_MSG(UTIL_isRegularFile(fileName), fileName);
45 FUZZ_ASSERT_MSG(fileSize <= kMaxFileSize, fileName);
46 /* Ensure we have a large enough buffer allocated */
47 if (fileSize > bufferSize) {
49 buffer = (uint8_t *)malloc(fileSize);
50 FUZZ_ASSERT_MSG(buffer, fileName);
51 bufferSize = fileSize;
54 file = fopen(fileName, "rb");
55 FUZZ_ASSERT_MSG(file, fileName);
57 readSize = fread(buffer, 1, fileSize, file);
58 FUZZ_ASSERT_MSG(readSize == fileSize, fileName);
61 /* Run the fuzz target */
62 LLVMFuzzerTestOneInput(buffer, fileSize);
67 #ifdef UTIL_HAS_CREATEFILELIST
68 UTIL_freeFileList(files, fileNamesBuf);