Skip to content

Commit dacac9e

Browse files
authored
Merge pull request libgit2#5160 from pks-t/pks/win32-fuzzers
win32: fix fuzzers and have CI build them
2 parents a6ad9e8 + eb27fb9 commit dacac9e

File tree

5 files changed

+45
-47
lines changed

5 files changed

+45
-47
lines changed

ci/build.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Write-Host "####################################################################
1818
Write-Host "## Configuring build environment"
1919
Write-Host "##############################################################################"
2020

21-
Invoke-Expression "cmake ${SourceDirectory} -DBUILD_EXAMPLES=ON -DENABLE_WERROR=ON ${Env:CMAKE_OPTIONS}"
21+
Invoke-Expression "cmake ${SourceDirectory} -DBUILD_EXAMPLES=ON -DBUILD_FUZZERS=ON -DUSE_STANDALONE_FUZZERS=ON -DENABLE_WERROR=ON ${Env:CMAKE_OPTIONS}"
2222
if ($LastExitCode -ne 0) { [Environment]::Exit($LastExitCode) }
2323

2424
Write-Host ""

fuzzers/config_file_fuzzer.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,9 @@
77
* a Linking Exception. For full terms see the included COPYING file.
88
*/
99

10-
#include <git2.h>
10+
#include "git2.h"
1111
#include "config_backend.h"
1212

13-
#include <stdlib.h>
14-
#include <stdio.h>
15-
#include <unistd.h>
16-
#include <limits.h>
17-
#include <errno.h>
18-
1913
#define UNUSED(x) (void)(x)
2014

2115
int foreach_cb(const git_config_entry *entry, void *payload)

fuzzers/download_refs_fuzzer.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@
77
* a Linking Exception. For full terms see the included COPYING file.
88
*/
99

10-
#include <string.h>
10+
#include <stdio.h>
1111
#include <stdlib.h>
1212
#include <string.h>
13-
#include <stdio.h>
14-
#include <unistd.h>
1513

1614
#include "git2.h"
1715
#include "git2/sys/transport.h"
16+
#include "fileops.h"
1817

1918
#define UNUSED(x) (void)(x)
2019

@@ -166,21 +165,34 @@ void fuzzer_git_abort(const char *op)
166165

167166
int LLVMFuzzerInitialize(int *argc, char ***argv)
168167
{
169-
char tmp[] = "/tmp/git2.XXXXXX";
168+
#if defined(_WIN32)
169+
char tmpdir[MAX_PATH], path[MAX_PATH];
170170

171-
UNUSED(argc);
172-
UNUSED(argv);
171+
if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0)
172+
abort();
173+
174+
if (GetTempFileName(tmpdir, "lg2", 1, path) == 0)
175+
abort();
176+
177+
if (git_futils_mkdir(path, 0700, 0) < 0)
178+
abort();
179+
#else
180+
char path[] = "/tmp/git2.XXXXXX";
181+
182+
if (mkdtemp(path) != path)
183+
abort();
184+
#endif
173185

174186
if (git_libgit2_init() < 0)
175187
abort();
176188

177189
if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
178190
abort();
179191

180-
if (mkdtemp(tmp) != tmp)
181-
abort();
192+
UNUSED(argc);
193+
UNUSED(argv);
182194

183-
if (git_repository_init(&repo, tmp, 1) < 0)
195+
if (git_repository_init(&repo, path, 1) < 0)
184196
fuzzer_git_abort("git_repository_init");
185197

186198
return 0;

fuzzers/packfile_fuzzer.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
* a Linking Exception. For full terms see the included COPYING file.
88
*/
99

10-
#include <stdbool.h>
11-
#include <stdint.h>
1210
#include <stdio.h>
13-
#include <limits.h>
14-
#include <unistd.h>
1511

1612
#include "git2.h"
1713
#include "git2/sys/mempack.h"
18-
19-
#define UNUSED(x) (void)(x)
14+
#include "common.h"
15+
#include "buffer.h"
2016

2117
static git_odb *odb = NULL;
2218
static git_odb_backend *mempack = NULL;
@@ -27,8 +23,9 @@ static const unsigned int base_obj_len = 2;
2723

2824
int LLVMFuzzerInitialize(int *argc, char ***argv)
2925
{
30-
UNUSED(argc);
31-
UNUSED(argv);
26+
GIT_UNUSED(argc);
27+
GIT_UNUSED(argv);
28+
3229
if (git_libgit2_init() < 0) {
3330
fprintf(stderr, "Failed to initialize libgit2\n");
3431
abort();
@@ -54,12 +51,11 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
5451

5552
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
5653
{
57-
git_indexer *indexer = NULL;
5854
git_indexer_progress stats = {0, 0};
55+
git_indexer *indexer = NULL;
56+
git_buf path = GIT_BUF_INIT;
57+
git_oid oid;
5958
bool append_hash = false;
60-
git_oid id;
61-
char hash[GIT_OID_HEXSZ + 1] = {0};
62-
char path[PATH_MAX];
6359

6460
if (size == 0)
6561
return 0;
@@ -70,7 +66,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
7066
}
7167
git_mempack_reset(mempack);
7268

73-
if (git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
69+
if (git_odb_write(&oid, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
7470
fprintf(stderr, "Failed to add an object to the odb\n");
7571
abort();
7672
}
@@ -92,7 +88,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
9288
if (git_indexer_append(indexer, data, size, &stats) < 0)
9389
goto cleanup;
9490
if (append_hash) {
95-
git_oid oid;
9691
if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
9792
fprintf(stderr, "Failed to compute the SHA1 hash\n");
9893
abort();
@@ -104,19 +99,19 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
10499
if (git_indexer_commit(indexer, &stats) < 0)
105100
goto cleanup;
106101

107-
/*
108-
* We made it! We managed to produce a valid packfile.
109-
* Let's clean it up.
110-
*/
111-
git_oid_fmt(hash, git_indexer_hash(indexer));
112-
printf("Generated packfile %s\n", hash);
113-
snprintf(path, sizeof(path), "pack-%s.idx", hash);
114-
unlink(path);
115-
snprintf(path, sizeof(path), "pack-%s.pack", hash);
116-
unlink(path);
102+
if (git_buf_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
103+
goto cleanup;
104+
p_unlink(git_buf_cstr(&path));
105+
106+
git_buf_clear(&path);
107+
108+
if (git_buf_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
109+
goto cleanup;
110+
p_unlink(git_buf_cstr(&path));
117111

118112
cleanup:
119113
git_mempack_reset(mempack);
120114
git_indexer_free(indexer);
115+
git_buf_dispose(&path);
121116
return 0;
122117
}

fuzzers/standalone_driver.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
* a Linking Exception. For full terms see the included COPYING file.
66
*/
77

8-
#include <assert.h>
9-
#include <dirent.h>
108
#include <stdio.h>
11-
#include <stdlib.h>
12-
#include <sys/types.h>
139

1410
#include "git2.h"
1511
#include "fileops.h"
@@ -24,7 +20,7 @@ static int run_one_file(const char *filename)
2420
int error = 0;
2521

2622
if (git_futils_readbuffer(&buf, filename) < 0) {
27-
fprintf(stderr, "Failed to read %s: %m\n", filename);
23+
fprintf(stderr, "Failed to read %s: %s\n", filename, git_error_last()->message);
2824
error = -1;
2925
goto exit;
3026
}
@@ -57,7 +53,8 @@ int main(int argc, char **argv)
5753
LLVMFuzzerInitialize(&argc, &argv);
5854

5955
if (git_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) {
60-
fprintf(stderr, "Failed to scan corpus directory: %m\n");
56+
fprintf(stderr, "Failed to scan corpus directory '%s': %s\n",
57+
argv[1], git_error_last()->message);
6158
error = -1;
6259
goto exit;
6360
}

0 commit comments

Comments
 (0)