| 1 |
/* |
| 2 |
* Copyright (C) the libgit2 contributors. All rights reserved. |
| 3 |
* |
| 4 |
* This file is part of libgit2, distributed under the GNU GPL v2 with |
| 5 |
* a Linking Exception. For full terms see the included COPYING file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
|
| 10 |
#include "git2.h" |
| 11 |
#include "futils.h" |
| 12 |
#include "path.h" |
| 13 |
|
| 14 |
#include "standalone_driver.h" |
| 15 |
|
| 16 |
static int run_one_file(const char *filename) |
| 17 |
{ |
| 18 |
git_str buf = GIT_STR_INIT; |
| 19 |
int error = 0; |
| 20 |
|
| 21 |
if (git_futils_readbuffer(&buf, filename) < 0) { |
| 22 |
fprintf(stderr, "Failed to read %s: %s\n", filename, git_error_last()->message); |
| 23 |
error = -1; |
| 24 |
goto exit; |
| 25 |
} |
| 26 |
|
| 27 |
LLVMFuzzerTestOneInput((const unsigned char *)buf.ptr, buf.size); |
| 28 |
exit: |
| 29 |
git_str_dispose(&buf); |
| 30 |
return error; |
| 31 |
} |
| 32 |
|
| 33 |
int main(int argc, char **argv) |
| 34 |
{ |
| 35 |
git_vector corpus_files = GIT_VECTOR_INIT; |
| 36 |
char *filename = NULL; |
| 37 |
unsigned i = 0; |
| 38 |
int error = 0; |
| 39 |
|
| 40 |
if (git_libgit2_init() < 0) { |
| 41 |
fprintf(stderr, "Failed to initialize libgit2\n"); |
| 42 |
abort(); |
| 43 |
} |
| 44 |
|
| 45 |
if (argc != 2) { |
| 46 |
fprintf(stderr, "Usage: %s <corpus directory>\n", argv[0]); |
| 47 |
error = -1; |
| 48 |
goto exit; |
| 49 |
} |
| 50 |
|
| 51 |
fprintf(stderr, "Running %s against %s\n", argv[0], argv[1]); |
| 52 |
LLVMFuzzerInitialize(&argc, &argv); |
| 53 |
|
| 54 |
if (git_fs_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) { |
| 55 |
fprintf(stderr, "Failed to scan corpus directory '%s': %s\n", |
| 56 |
argv[1], git_error_last()->message); |
| 57 |
error = -1; |
| 58 |
goto exit; |
| 59 |
} |
| 60 |
git_vector_foreach(&corpus_files, i, filename) { |
| 61 |
fprintf(stderr, "\tRunning %s...\n", filename); |
| 62 |
if (run_one_file(filename) < 0) { |
| 63 |
error = -1; |
| 64 |
goto exit; |
| 65 |
} |
| 66 |
} |
| 67 |
fprintf(stderr, "Done %d runs\n", i); |
| 68 |
|
| 69 |
exit: |
| 70 |
git_vector_dispose_deep(&corpus_files); |
| 71 |
git_libgit2_shutdown(); |
| 72 |
return error; |
| 73 |
} |