1*01826a49SYabin Cui /*
2*01826a49SYabin Cui * Copyright (c) Meta Platforms, Inc. and affiliates.
3*01826a49SYabin Cui * All rights reserved.
4*01826a49SYabin Cui *
5*01826a49SYabin Cui * This source code is licensed under both the BSD-style license (found in the
6*01826a49SYabin Cui * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*01826a49SYabin Cui * in the COPYING file in the root directory of this source tree).
8*01826a49SYabin Cui * You may select, at your option, one of the above-listed licenses.
9*01826a49SYabin Cui */
10*01826a49SYabin Cui
11*01826a49SYabin Cui #include "fuzz.h"
12*01826a49SYabin Cui #include "fuzz_helpers.h"
13*01826a49SYabin Cui #include "util.h"
14*01826a49SYabin Cui #include <stddef.h>
15*01826a49SYabin Cui #include <stdint.h>
16*01826a49SYabin Cui #include <stdio.h>
17*01826a49SYabin Cui #include <stdlib.h>
18*01826a49SYabin Cui
main(int argc,char const ** argv)19*01826a49SYabin Cui int main(int argc, char const **argv) {
20*01826a49SYabin Cui size_t const kMaxFileSize = (size_t)1 << 27;
21*01826a49SYabin Cui int const kFollowLinks = 1;
22*01826a49SYabin Cui FileNamesTable* files;
23*01826a49SYabin Cui const char** const fnTable = argv + 1;
24*01826a49SYabin Cui uint8_t *buffer = NULL;
25*01826a49SYabin Cui size_t bufferSize = 0;
26*01826a49SYabin Cui unsigned i;
27*01826a49SYabin Cui unsigned numFilesTested = 0;
28*01826a49SYabin Cui int ret = 0;
29*01826a49SYabin Cui
30*01826a49SYabin Cui {
31*01826a49SYabin Cui unsigned const numFiles = (unsigned)(argc - 1);
32*01826a49SYabin Cui #ifdef UTIL_HAS_CREATEFILELIST
33*01826a49SYabin Cui files = UTIL_createExpandedFNT(fnTable, numFiles, kFollowLinks);
34*01826a49SYabin Cui #else
35*01826a49SYabin Cui files = UTIL_createFNT_fromROTable(fnTable, numFiles);
36*01826a49SYabin Cui assert(numFiles == files->tableSize);
37*01826a49SYabin Cui #endif
38*01826a49SYabin Cui }
39*01826a49SYabin Cui if (!files) {
40*01826a49SYabin Cui fprintf(stderr, "ERROR: Failed to create file names table\n");
41*01826a49SYabin Cui return 1;
42*01826a49SYabin Cui }
43*01826a49SYabin Cui if (files->tableSize == 0)
44*01826a49SYabin Cui fprintf(stderr, "WARNING: No files passed to %s\n", argv[0]);
45*01826a49SYabin Cui for (i = 0; i < files->tableSize; ++i) {
46*01826a49SYabin Cui char const *fileName = files->fileNames[i];
47*01826a49SYabin Cui size_t const fileSize = UTIL_getFileSize(fileName);
48*01826a49SYabin Cui size_t readSize;
49*01826a49SYabin Cui FILE *file;
50*01826a49SYabin Cui
51*01826a49SYabin Cui DEBUGLOG(3, "Running %s", fileName);
52*01826a49SYabin Cui
53*01826a49SYabin Cui /* Check that it is a regular file, and that the fileSize is valid.
54*01826a49SYabin Cui * If it is not a regular file, then it may have been deleted since we
55*01826a49SYabin Cui * constructed the list, so just skip it, but return an error exit code.
56*01826a49SYabin Cui */
57*01826a49SYabin Cui if (!UTIL_isRegularFile(fileName)) {
58*01826a49SYabin Cui ret = 1;
59*01826a49SYabin Cui continue;
60*01826a49SYabin Cui }
61*01826a49SYabin Cui FUZZ_ASSERT_MSG(fileSize <= kMaxFileSize, fileName);
62*01826a49SYabin Cui /* Ensure we have a large enough buffer allocated */
63*01826a49SYabin Cui if (fileSize > bufferSize) {
64*01826a49SYabin Cui free(buffer);
65*01826a49SYabin Cui buffer = (uint8_t *)malloc(fileSize);
66*01826a49SYabin Cui FUZZ_ASSERT_MSG(buffer, fileName);
67*01826a49SYabin Cui bufferSize = fileSize;
68*01826a49SYabin Cui }
69*01826a49SYabin Cui /* Open the file */
70*01826a49SYabin Cui file = fopen(fileName, "rb");
71*01826a49SYabin Cui FUZZ_ASSERT_MSG(file, fileName);
72*01826a49SYabin Cui /* Read the file */
73*01826a49SYabin Cui readSize = fread(buffer, 1, fileSize, file);
74*01826a49SYabin Cui FUZZ_ASSERT_MSG(readSize == fileSize, fileName);
75*01826a49SYabin Cui /* Close the file */
76*01826a49SYabin Cui fclose(file);
77*01826a49SYabin Cui /* Run the fuzz target */
78*01826a49SYabin Cui LLVMFuzzerTestOneInput(buffer, fileSize);
79*01826a49SYabin Cui ++numFilesTested;
80*01826a49SYabin Cui }
81*01826a49SYabin Cui fprintf(stderr, "Tested %u files: ", numFilesTested);
82*01826a49SYabin Cui if (ret == 0) {
83*01826a49SYabin Cui fprintf(stderr, "Success!\n");
84*01826a49SYabin Cui } else {
85*01826a49SYabin Cui fprintf(stderr, "Failure!\n");
86*01826a49SYabin Cui }
87*01826a49SYabin Cui free(buffer);
88*01826a49SYabin Cui UTIL_freeFileNamesTable(files);
89*01826a49SYabin Cui return ret;
90*01826a49SYabin Cui }
91