packfile_fuzzer.c 2.8 KB
Newer Older
1
/*
2
 * libgit2 packfile fuzzer target.
3 4 5 6 7 8 9 10 11 12 13
 *
 * 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 "git2.h"
#include "git2/sys/mempack.h"
14
#include "common.h"
15
#include "str.h"
16

17 18 19 20 21 22 23 24 25
static git_odb *odb = NULL;
static git_odb_backend *mempack = NULL;

/* Arbitrary object to seed the ODB. */
static const unsigned char base_obj[] = { 07, 076 };
static const unsigned int base_obj_len = 2;

int LLVMFuzzerInitialize(int *argc, char ***argv)
{
26 27 28
	GIT_UNUSED(argc);
	GIT_UNUSED(argv);

29 30 31 32
	if (git_libgit2_init() < 0) {
		fprintf(stderr, "Failed to initialize libgit2\n");
		abort();
	}
33 34 35 36
	if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0) {
		fprintf(stderr, "Failed to limit maximum pack object count\n");
		abort();
	}
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
	if (git_odb_new(&odb) < 0) {
		fprintf(stderr, "Failed to create the odb\n");
		abort();
	}
	if (git_mempack_new(&mempack) < 0) {
		fprintf(stderr, "Failed to create the mempack\n");
		abort();
	}
	if (git_odb_add_backend(odb, mempack, 999) < 0) {
		fprintf(stderr, "Failed to add the mempack\n");
		abort();
	}
	return 0;
}

int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
54
	git_indexer_progress stats = {0, 0};
55
	git_indexer *indexer = NULL;
56
	git_str path = GIT_STR_INIT;
57
	git_oid oid;
58 59 60 61 62 63 64 65 66 67 68
	bool append_hash = false;

	if (size == 0)
		return 0;

	if (!odb || !mempack) {
		fprintf(stderr, "Global state not initialized\n");
		abort();
	}
	git_mempack_reset(mempack);

69
	if (git_odb_write(&oid, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
70 71 72 73
		fprintf(stderr, "Failed to add an object to the odb\n");
		abort();
	}

74
	if (git_indexer_new(&indexer, ".", 0, odb, NULL) < 0) {
75
		fprintf(stderr, "Failed to create the indexer: %s\n",
76
			git_error_last()->message);
77 78 79 80 81 82 83 84 85 86 87 88 89 90
		abort();
	}

	/*
	 * If the first byte in the stream has the high bit set, append the
	 * SHA1 hash so that the packfile is somewhat valid.
	 */
	append_hash = *data & 0x80;
	++data;
	--size;

	if (git_indexer_append(indexer, data, size, &stats) < 0)
		goto cleanup;
	if (append_hash) {
91
		if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
92 93 94 95 96 97 98 99 100 101
			fprintf(stderr, "Failed to compute the SHA1 hash\n");
			abort();
		}
		if (git_indexer_append(indexer, &oid, sizeof(oid), &stats) < 0) {
			goto cleanup;
		}
	}
	if (git_indexer_commit(indexer, &stats) < 0)
		goto cleanup;

102
	if (git_str_printf(&path, "pack-%s.idx", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
103
		goto cleanup;
104
	p_unlink(git_str_cstr(&path));
105

106
	git_str_clear(&path);
107

108
	if (git_str_printf(&path, "pack-%s.pack", git_oid_tostr_s(git_indexer_hash(indexer))) < 0)
109
		goto cleanup;
110
	p_unlink(git_str_cstr(&path));
111 112 113 114

cleanup:
	git_mempack_reset(mempack);
	git_indexer_free(indexer);
115
	git_str_dispose(&path);
116 117
	return 0;
}