]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/zstd/tests/fuzz/regression_driver.c
MFV r337029:
[FreeBSD/FreeBSD.git] / sys / contrib / zstd / 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 << 20;
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
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) {
48       free(buffer);
49       buffer = (uint8_t *)malloc(fileSize);
50       FUZZ_ASSERT_MSG(buffer, fileName);
51       bufferSize = fileSize;
52     }
53     /* Open the file */
54     file = fopen(fileName, "rb");
55     FUZZ_ASSERT_MSG(file, fileName);
56     /* Read the file */
57     readSize = fread(buffer, 1, fileSize, file);
58     FUZZ_ASSERT_MSG(readSize == fileSize, fileName);
59     /* Close the file */
60     fclose(file);
61     /* Run the fuzz target */
62     LLVMFuzzerTestOneInput(buffer, fileSize);
63   }
64
65   ret = 0;
66   free(buffer);
67 #ifdef UTIL_HAS_CREATEFILELIST
68   UTIL_freeFileList(files, fileNamesBuf);
69 #endif
70   return ret;
71 }