Path: blob/main/crypto/openssl/fuzz/test-corpus.c
105174 views
/*1* Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.2*3* Licensed under the Apache License 2.0 (the "License");4* you may not use this file except in compliance with the License.5* You may obtain a copy of the License at6* https://www.openssl.org/source/license.html7* or in the file LICENSE in the source distribution.8*/910/*11* Given a list of files, run each of them through the fuzzer. Note that12* failure will be indicated by some kind of crash. Switching on things like13* asan improves the test.14*/1516#include <stdio.h>17#include <stdlib.h>18#include <string.h>19#include <sys/stat.h>20#include <openssl/crypto.h>21#include "fuzzer.h"22#include "internal/o_dir.h"2324#if defined(_WIN32) && defined(_MAX_PATH) && !defined(PATH_MAX)25#define PATH_MAX _MAX_PATH26#endif2728#ifndef PATH_MAX29#define PATH_MAX 409630#endif3132#if !defined(S_ISREG)33#define S_ISREG(m) ((m) & S_IFREG)34#endif3536static void testfile(const char *pathname)37{38struct stat st;39FILE *f;40unsigned char *buf;41size_t s;4243if (stat(pathname, &st) < 0 || !S_ISREG(st.st_mode))44return;45printf("# %s\n", pathname);46fflush(stdout);47f = fopen(pathname, "rb");48if (f == NULL)49return;50buf = malloc(st.st_size);51if (buf != NULL) {52s = fread(buf, 1, st.st_size, f);53OPENSSL_assert(s == (size_t)st.st_size);54FuzzerTestOneInput(buf, s);55free(buf);56}57fclose(f);58}5960int main(int argc, char **argv)61{62int n;6364FuzzerInitialize(&argc, &argv);6566for (n = 1; n < argc; ++n) {67size_t dirname_len = strlen(argv[n]);68const char *filename = NULL;69char *pathname = NULL;70OPENSSL_DIR_CTX *ctx = NULL;71int wasdir = 0;7273/*74* We start with trying to read the given path as a directory.75*/76while ((filename = OPENSSL_DIR_read(&ctx, argv[n])) != NULL) {77wasdir = 1;78if (pathname == NULL) {79pathname = malloc(PATH_MAX);80if (pathname == NULL)81break;82strcpy(pathname, argv[n]);83#ifdef __VMS84if (strchr(":<]", pathname[dirname_len - 1]) == NULL)85#endif86pathname[dirname_len++] = '/';87pathname[dirname_len] = '\0';88}89strcpy(pathname + dirname_len, filename);90testfile(pathname);91}92OPENSSL_DIR_end(&ctx);9394/* If it wasn't a directory, treat it as a file instead */95if (!wasdir)96testfile(argv[n]);9798free(pathname);99}100101FuzzerCleanup();102103return 0;104}105106107