test-corpus.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. /*
  11. * Given a list of files, run each of them through the fuzzer. Note that
  12. * failure will be indicated by some kind of crash. Switching on things like
  13. * asan improves the test.
  14. */
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/stat.h>
  19. #include <openssl/crypto.h>
  20. #include "fuzzer.h"
  21. #include "internal/o_dir.h"
  22. #if defined(_WIN32) && defined(_MAX_PATH) && !defined(PATH_MAX)
  23. # define PATH_MAX _MAX_PATH
  24. #endif
  25. #ifndef PATH_MAX
  26. # define PATH_MAX 4096
  27. #endif
  28. # if !defined(S_ISREG)
  29. # define S_ISREG(m) ((m) & S_IFREG)
  30. # endif
  31. static void testfile(const char *pathname)
  32. {
  33. struct stat st;
  34. FILE *f;
  35. unsigned char *buf;
  36. size_t s;
  37. if (stat(pathname, &st) < 0 || !S_ISREG(st.st_mode))
  38. return;
  39. printf("# %s\n", pathname);
  40. fflush(stdout);
  41. f = fopen(pathname, "rb");
  42. if (f == NULL)
  43. return;
  44. buf = malloc(st.st_size);
  45. if (buf != NULL) {
  46. s = fread(buf, 1, st.st_size, f);
  47. OPENSSL_assert(s == (size_t)st.st_size);
  48. FuzzerTestOneInput(buf, s);
  49. free(buf);
  50. }
  51. fclose(f);
  52. }
  53. int main(int argc, char **argv) {
  54. int n;
  55. FuzzerInitialize(&argc, &argv);
  56. for (n = 1; n < argc; ++n) {
  57. size_t dirname_len = strlen(argv[n]);
  58. const char *filename = NULL;
  59. char *pathname = NULL;
  60. OPENSSL_DIR_CTX *ctx = NULL;
  61. int wasdir = 0;
  62. /*
  63. * We start with trying to read the given path as a directory.
  64. */
  65. while ((filename = OPENSSL_DIR_read(&ctx, argv[n])) != NULL) {
  66. wasdir = 1;
  67. if (pathname == NULL) {
  68. pathname = malloc(PATH_MAX);
  69. if (pathname == NULL)
  70. break;
  71. strcpy(pathname, argv[n]);
  72. #ifdef __VMS
  73. if (strchr(":<]", pathname[dirname_len - 1]) == NULL)
  74. #endif
  75. pathname[dirname_len++] = '/';
  76. pathname[dirname_len] = '\0';
  77. }
  78. strcpy(pathname + dirname_len, filename);
  79. testfile(pathname);
  80. }
  81. OPENSSL_DIR_end(&ctx);
  82. /* If it wasn't a directory, treat it as a file instead */
  83. if (!wasdir)
  84. testfile(argv[n]);
  85. free(pathname);
  86. }
  87. FuzzerCleanup();
  88. return 0;
  89. }