2 * libgit2 packfile fuzzer target.
4 * Copyright (C) the libgit2 contributors. All rights reserved.
6 * This file is part of libgit2, distributed under the GNU GPL v2 with
7 * a Linking Exception. For full terms see the included COPYING file.
13 #include "git2/sys/mempack.h"
17 #include "standalone_driver.h"
19 static git_odb
*odb
= NULL
;
20 static git_odb_backend
*mempack
= NULL
;
22 /* Arbitrary object to seed the ODB. */
23 static const unsigned char base_obj
[] = { 07, 076 };
24 static const unsigned int base_obj_len
= 2;
26 int LLVMFuzzerInitialize(int *argc
, char ***argv
)
31 if (git_libgit2_init() < 0) {
32 fprintf(stderr
, "Failed to initialize libgit2\n");
35 if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS
, 10000000) < 0) {
36 fprintf(stderr
, "Failed to limit maximum pack object count\n");
39 if (git_odb_new(&odb
) < 0) {
40 fprintf(stderr
, "Failed to create the odb\n");
43 if (git_mempack_new(&mempack
) < 0) {
44 fprintf(stderr
, "Failed to create the mempack\n");
47 if (git_odb_add_backend(odb
, mempack
, 999) < 0) {
48 fprintf(stderr
, "Failed to add the mempack\n");
54 int LLVMFuzzerTestOneInput(const uint8_t *data
, size_t size
)
56 git_indexer_progress stats
= {0, 0};
57 git_indexer
*indexer
= NULL
;
58 git_str path
= GIT_STR_INIT
;
60 bool append_hash
= false;
65 if (!odb
|| !mempack
) {
66 fprintf(stderr
, "Global state not initialized\n");
69 git_mempack_reset(mempack
);
71 if (git_odb_write(&oid
, odb
, base_obj
, base_obj_len
, GIT_OBJECT_BLOB
) < 0) {
72 fprintf(stderr
, "Failed to add an object to the odb\n");
76 if (git_indexer_new(&indexer
, ".", 0, odb
, NULL
) < 0) {
77 fprintf(stderr
, "Failed to create the indexer: %s\n",
78 git_error_last()->message
);
83 * If the first byte in the stream has the high bit set, append the
84 * SHA1 hash so that the packfile is somewhat valid.
86 append_hash
= *data
& 0x80;
90 if (git_indexer_append(indexer
, data
, size
, &stats
) < 0)
93 if (git_odb_hash(&oid
, data
, size
, GIT_OBJECT_BLOB
) < 0) {
94 fprintf(stderr
, "Failed to compute the SHA1 hash\n");
97 if (git_indexer_append(indexer
, &oid
.id
, GIT_OID_RAWSZ
, &stats
) < 0) {
101 if (git_indexer_commit(indexer
, &stats
) < 0)
104 if (git_str_printf(&path
, "pack-%s.idx", git_indexer_name(indexer
)) < 0)
106 p_unlink(git_str_cstr(&path
));
108 git_str_clear(&path
);
110 if (git_str_printf(&path
, "pack-%s.pack", git_indexer_name(indexer
)) < 0)
112 p_unlink(git_str_cstr(&path
));
115 git_mempack_reset(mempack
);
116 git_indexer_free(indexer
);
117 git_str_dispose(&path
);