Commit dacac9e1e998c2fa185733236017d66b524e73b9

Patrick Steinhardt 2019-07-12T08:30:07

Merge pull request #5160 from pks-t/pks/win32-fuzzers win32: fix fuzzers and have CI build them

diff --git a/ci/build.ps1 b/ci/build.ps1
index 20a265d..dbc458d 100644
--- a/ci/build.ps1
+++ b/ci/build.ps1
@@ -18,7 +18,7 @@ Write-Host "####################################################################
 Write-Host "## Configuring build environment"
 Write-Host "##############################################################################"
 
-Invoke-Expression "cmake ${SourceDirectory} -DBUILD_EXAMPLES=ON -DENABLE_WERROR=ON ${Env:CMAKE_OPTIONS}"
+Invoke-Expression "cmake ${SourceDirectory} -DBUILD_EXAMPLES=ON -DBUILD_FUZZERS=ON -DUSE_STANDALONE_FUZZERS=ON -DENABLE_WERROR=ON ${Env:CMAKE_OPTIONS}"
 if ($LastExitCode -ne 0) { [Environment]::Exit($LastExitCode) }
 
 Write-Host ""
diff --git a/fuzzers/config_file_fuzzer.c b/fuzzers/config_file_fuzzer.c
index fa52642..526c939 100644
--- a/fuzzers/config_file_fuzzer.c
+++ b/fuzzers/config_file_fuzzer.c
@@ -7,15 +7,9 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
-#include <git2.h>
+#include "git2.h"
 #include "config_backend.h"
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <unistd.h>
-#include <limits.h>
-#include <errno.h>
-
 #define UNUSED(x) (void)(x)
 
 int foreach_cb(const git_config_entry *entry, void *payload)
diff --git a/fuzzers/download_refs_fuzzer.c b/fuzzers/download_refs_fuzzer.c
index 93f1b49..facfaa2 100644
--- a/fuzzers/download_refs_fuzzer.c
+++ b/fuzzers/download_refs_fuzzer.c
@@ -7,14 +7,13 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
-#include <string.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdio.h>
-#include <unistd.h>
 
 #include "git2.h"
 #include "git2/sys/transport.h"
+#include "fileops.h"
 
 #define UNUSED(x) (void)(x)
 
@@ -166,10 +165,23 @@ void fuzzer_git_abort(const char *op)
 
 int LLVMFuzzerInitialize(int *argc, char ***argv)
 {
-	char tmp[] = "/tmp/git2.XXXXXX";
+#if defined(_WIN32)
+	char tmpdir[MAX_PATH], path[MAX_PATH];
 
-	UNUSED(argc);
-	UNUSED(argv);
+	if (GetTempPath((DWORD)sizeof(tmpdir), tmpdir) == 0)
+		abort();
+
+	if (GetTempFileName(tmpdir, "lg2", 1, path) == 0)
+		abort();
+
+	if (git_futils_mkdir(path, 0700, 0) < 0)
+		abort();
+#else
+	char path[] = "/tmp/git2.XXXXXX";
+
+	if (mkdtemp(path) != path)
+		abort();
+#endif
 
 	if (git_libgit2_init() < 0)
 		abort();
@@ -177,10 +189,10 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
 	if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
 		abort();
 
-	if (mkdtemp(tmp) != tmp)
-		abort();
+	UNUSED(argc);
+	UNUSED(argv);
 
-	if (git_repository_init(&repo, tmp, 1) < 0)
+	if (git_repository_init(&repo, path, 1) < 0)
 		fuzzer_git_abort("git_repository_init");
 
 	return 0;
diff --git a/fuzzers/packfile_fuzzer.c b/fuzzers/packfile_fuzzer.c
index f5e6718..50c1157 100644
--- a/fuzzers/packfile_fuzzer.c
+++ b/fuzzers/packfile_fuzzer.c
@@ -7,16 +7,12 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
-#include <stdbool.h>
-#include <stdint.h>
 #include <stdio.h>
-#include <limits.h>
-#include <unistd.h>
 
 #include "git2.h"
 #include "git2/sys/mempack.h"
-
-#define UNUSED(x) (void)(x)
+#include "common.h"
+#include "buffer.h"
 
 static git_odb *odb = NULL;
 static git_odb_backend *mempack = NULL;
@@ -27,8 +23,9 @@ static const unsigned int base_obj_len = 2;
 
 int LLVMFuzzerInitialize(int *argc, char ***argv)
 {
-	UNUSED(argc);
-	UNUSED(argv);
+	GIT_UNUSED(argc);
+	GIT_UNUSED(argv);
+
 	if (git_libgit2_init() < 0) {
 		fprintf(stderr, "Failed to initialize libgit2\n");
 		abort();
@@ -54,12 +51,11 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
 
 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 {
-	git_indexer *indexer = NULL;
 	git_indexer_progress stats = {0, 0};
+	git_indexer *indexer = NULL;
+	git_buf path = GIT_BUF_INIT;
+	git_oid oid;
 	bool append_hash = false;
-	git_oid id;
-	char hash[GIT_OID_HEXSZ + 1] = {0};
-	char path[PATH_MAX];
 
 	if (size == 0)
 		return 0;
@@ -70,7 +66,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 	}
 	git_mempack_reset(mempack);
 
-	if (git_odb_write(&id, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
+	if (git_odb_write(&oid, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
 		fprintf(stderr, "Failed to add an object to the odb\n");
 		abort();
 	}
@@ -92,7 +88,6 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 	if (git_indexer_append(indexer, data, size, &stats) < 0)
 		goto cleanup;
 	if (append_hash) {
-		git_oid oid;
 		if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
 			fprintf(stderr, "Failed to compute the SHA1 hash\n");
 			abort();
@@ -104,19 +99,19 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 	if (git_indexer_commit(indexer, &stats) < 0)
 		goto cleanup;
 
-	/*
-	 * We made it! We managed to produce a valid packfile.
-	 * Let's clean it up.
-	 */
-	git_oid_fmt(hash, git_indexer_hash(indexer));
-	printf("Generated packfile %s\n", hash);
-	snprintf(path, sizeof(path), "pack-%s.idx", hash);
-	unlink(path);
-	snprintf(path, sizeof(path), "pack-%s.pack", hash);
-	unlink(path);
+	if (git_buf_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
+		goto cleanup;
+	p_unlink(git_buf_cstr(&path));
+
+	git_buf_clear(&path);
+
+	if (git_buf_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
+		goto cleanup;
+	p_unlink(git_buf_cstr(&path));
 
 cleanup:
 	git_mempack_reset(mempack);
 	git_indexer_free(indexer);
+	git_buf_dispose(&path);
 	return 0;
 }
diff --git a/fuzzers/standalone_driver.c b/fuzzers/standalone_driver.c
index 000bfbf..c661970 100644
--- a/fuzzers/standalone_driver.c
+++ b/fuzzers/standalone_driver.c
@@ -5,11 +5,7 @@
  * a Linking Exception. For full terms see the included COPYING file.
  */
 
-#include <assert.h>
-#include <dirent.h>
 #include <stdio.h>
-#include <stdlib.h>
-#include <sys/types.h>
 
 #include "git2.h"
 #include "fileops.h"
@@ -24,7 +20,7 @@ static int run_one_file(const char *filename)
 	int error = 0;
 
 	if (git_futils_readbuffer(&buf, filename) < 0) {
-		fprintf(stderr, "Failed to read %s: %m\n", filename);
+		fprintf(stderr, "Failed to read %s: %s\n", filename, git_error_last()->message);
 		error = -1;
 		goto exit;
 	}
@@ -57,7 +53,8 @@ int main(int argc, char **argv)
 	LLVMFuzzerInitialize(&argc, &argv);
 
 	if (git_path_dirload(&corpus_files, argv[1], 0, 0x0) < 0) {
-		fprintf(stderr, "Failed to scan corpus directory: %m\n");
+		fprintf(stderr, "Failed to scan corpus directory '%s': %s\n",
+			argv[1], git_error_last()->message);
 		error = -1;
 		goto exit;
 	}