fuzzers: provide util functions like repo init

Many fuzzers will need to operate with a repository; extract the
repository initialization from downloads_refs_fuzzer.c into its own
utility area.
This commit is contained in:
Edward Thomson
2023-11-17 10:29:08 +00:00
parent 802f08c696
commit 8bd69574bd
4 changed files with 73 additions and 32 deletions

View File

@@ -12,10 +12,13 @@ foreach(fuzz_target_src ${SRC_FUZZERS})
string(REPLACE ".c" "" fuzz_target_name ${fuzz_target_src})
string(REPLACE "_fuzzer" "" fuzz_name ${fuzz_target_name})
set(${fuzz_target_name}_SOURCES ${fuzz_target_src} ${LIBGIT2_OBJECTS})
set(${fuzz_target_name}_SOURCES
${fuzz_target_src} "fuzzer_utils.c" ${LIBGIT2_OBJECTS})
if(USE_STANDALONE_FUZZERS)
list(APPEND ${fuzz_target_name}_SOURCES "standalone_driver.c")
endif()
add_executable(${fuzz_target_name} ${${fuzz_target_name}_SOURCES})
set_target_properties(${fuzz_target_name} PROPERTIES C_STANDARD 90)

View File

@@ -16,6 +16,7 @@
#include "futils.h"
#include "standalone_driver.h"
#include "fuzzer_utils.h"
#define UNUSED(x) (void)(x)
@@ -157,33 +158,10 @@ static int fuzzer_transport_cb(git_transport **out, git_remote *owner, void *par
return git_transport_smart(out, owner, &def);
}
static void fuzzer_git_abort(const char *op)
{
const git_error *err = git_error_last();
fprintf(stderr, "unexpected libgit error: %s: %s\n",
op, err ? err->message : "<none>");
abort();
}
int LLVMFuzzerInitialize(int *argc, char ***argv)
{
#if defined(_WIN32)
char tmpdir[MAX_PATH], path[MAX_PATH];
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
UNUSED(argc);
UNUSED(argv);
if (git_libgit2_init() < 0)
abort();
@@ -191,12 +169,7 @@ int LLVMFuzzerInitialize(int *argc, char ***argv)
if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
abort();
UNUSED(argc);
UNUSED(argv);
if (git_repository_init(&repo, path, 1) < 0)
fuzzer_git_abort("git_repository_init");
repo = fuzzer_repo_init();
return 0;
}

51
fuzzers/fuzzer_utils.c Normal file
View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "git2.h"
#include "futils.h"
#include "fuzzer_utils.h"
void fuzzer_git_abort(const char *op)
{
const git_error *err = git_error_last();
fprintf(stderr, "unexpected libgit error: %s: %s\n",
op, err ? err->message : "<none>");
abort();
}
git_repository *fuzzer_repo_init(void)
{
git_repository *repo;
#if defined(_WIN32)
char tmpdir[MAX_PATH], path[MAX_PATH];
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_repository_init(&repo, path, 1) < 0)
fuzzer_git_abort("git_repository_init");
return repo;
}

14
fuzzers/fuzzer_utils.h Normal file
View File

@@ -0,0 +1,14 @@
/*
* Copyright (C) the libgit2 contributors. All rights reserved.
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#ifndef INCLUDE_fuzzer_utils_h__
#define INCLUDE_fuzzer_utils_h__
extern void fuzzer_git_abort(const char *op);
extern git_repository *fuzzer_repo_init(void);
#endif