regression_driver.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2016-2021, 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. * You may select, at your option, one of the above-listed licenses.
  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. int main(int argc, char const **argv) {
  18. size_t const kMaxFileSize = (size_t)1 << 27;
  19. int const kFollowLinks = 1;
  20. FileNamesTable* files;
  21. const char** const fnTable = argv + 1;
  22. uint8_t *buffer = NULL;
  23. size_t bufferSize = 0;
  24. unsigned i;
  25. unsigned numFilesTested = 0;
  26. int ret = 0;
  27. {
  28. unsigned const numFiles = (unsigned)(argc - 1);
  29. #ifdef UTIL_HAS_CREATEFILELIST
  30. files = UTIL_createExpandedFNT(fnTable, numFiles, kFollowLinks);
  31. #else
  32. files = UTIL_createFNT_fromROTable(fnTable, numFiles);
  33. assert(numFiles == files->tableSize);
  34. #endif
  35. }
  36. if (!files) {
  37. fprintf(stderr, "ERROR: Failed to create file names table\n");
  38. return 1;
  39. }
  40. if (files->tableSize == 0)
  41. fprintf(stderr, "WARNING: No files passed to %s\n", argv[0]);
  42. for (i = 0; i < files->tableSize; ++i) {
  43. char const *fileName = files->fileNames[i];
  44. DEBUGLOG(3, "Running %s", fileName);
  45. size_t const fileSize = UTIL_getFileSize(fileName);
  46. size_t readSize;
  47. FILE *file;
  48. /* Check that it is a regular file, and that the fileSize is valid.
  49. * If it is not a regular file, then it may have been deleted since we
  50. * constructed the list, so just skip it, but return an error exit code.
  51. */
  52. if (!UTIL_isRegularFile(fileName)) {
  53. ret = 1;
  54. continue;
  55. }
  56. FUZZ_ASSERT_MSG(fileSize <= kMaxFileSize, fileName);
  57. /* Ensure we have a large enough buffer allocated */
  58. if (fileSize > bufferSize) {
  59. free(buffer);
  60. buffer = (uint8_t *)malloc(fileSize);
  61. FUZZ_ASSERT_MSG(buffer, fileName);
  62. bufferSize = fileSize;
  63. }
  64. /* Open the file */
  65. file = fopen(fileName, "rb");
  66. FUZZ_ASSERT_MSG(file, fileName);
  67. /* Read the file */
  68. readSize = fread(buffer, 1, fileSize, file);
  69. FUZZ_ASSERT_MSG(readSize == fileSize, fileName);
  70. /* Close the file */
  71. fclose(file);
  72. /* Run the fuzz target */
  73. LLVMFuzzerTestOneInput(buffer, fileSize);
  74. ++numFilesTested;
  75. }
  76. fprintf(stderr, "Tested %u files: ", numFilesTested);
  77. if (ret == 0) {
  78. fprintf(stderr, "Success!\n");
  79. } else {
  80. fprintf(stderr, "Failure!\n");
  81. }
  82. free(buffer);
  83. UTIL_freeFileNamesTable(files);
  84. return ret;
  85. }