]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/fuzz/regression_driver.c
Import Zstd 1.4.4
[FreeBSD/FreeBSD.git] / tests / fuzz / regression_driver.c
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) {
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;
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     DEBUGLOG(3, "Running %s", fileName);
40     size_t const fileSize = UTIL_getFileSize(fileName);
41     size_t readSize;
42     FILE *file;
43
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.
47      */
48     if (!UTIL_isRegularFile(fileName)) {
49       continue;
50     }
51     FUZZ_ASSERT_MSG(fileSize <= kMaxFileSize, fileName);
52     /* Ensure we have a large enough buffer allocated */
53     if (fileSize > bufferSize) {
54       free(buffer);
55       buffer = (uint8_t *)malloc(fileSize);
56       FUZZ_ASSERT_MSG(buffer, fileName);
57       bufferSize = fileSize;
58     }
59     /* Open the file */
60     file = fopen(fileName, "rb");
61     FUZZ_ASSERT_MSG(file, fileName);
62     /* Read the file */
63     readSize = fread(buffer, 1, fileSize, file);
64     FUZZ_ASSERT_MSG(readSize == fileSize, fileName);
65     /* Close the file */
66     fclose(file);
67     /* Run the fuzz target */
68     LLVMFuzzerTestOneInput(buffer, fileSize);
69   }
70
71   ret = 0;
72   free(buffer);
73 #ifdef UTIL_HAS_CREATEFILELIST
74   UTIL_freeFileList(files, fileNamesBuf);
75 #endif
76   return ret;
77 }