Debian Codegraph @

fuzzers/packfile_fuzzer.c

raw
1 /*
2 * libgit2 packfile fuzzer target.
3 *
4 * Copyright (C) the libgit2 contributors. All rights reserved.
5 *
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.
8 */
9
10 #include <stdio.h>
11
12 #include "git2.h"
13 #include "git2/sys/mempack.h"
14 #include "common.h"
15 #include "str.h"
16
17 #include "standalone_driver.h"
18
19 static git_odb *odb = NULL;
20 static git_odb_backend *mempack = NULL;
21
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;
25
26 int LLVMFuzzerInitialize(int *argc, char ***argv)
27 {
28 GIT_UNUSED(argc);
29 GIT_UNUSED(argv);
30
31 if (git_libgit2_init() < 0) {
32 fprintf(stderr, "Failed to initialize libgit2\n");
33 abort();
34 }
35 if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0) {
36 fprintf(stderr, "Failed to limit maximum pack object count\n");
37 abort();
38 }
39
40 #ifdef GIT_EXPERIMENTAL_SHA256
41 if (git_odb_new(&odb, NULL) < 0) {
42 fprintf(stderr, "Failed to create the odb\n");
43 abort();
44 }
45 #else
46 if (git_odb_new(&odb) < 0) {
47 fprintf(stderr, "Failed to create the odb\n");
48 abort();
49 }
50 #endif
51
52 if (git_mempack_new(&mempack) < 0) {
53 fprintf(stderr, "Failed to create the mempack\n");
54 abort();
55 }
56 if (git_odb_add_backend(odb, mempack, 999) < 0) {
57 fprintf(stderr, "Failed to add the mempack\n");
58 abort();
59 }
60 return 0;
61 }
62
63 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
64 {
65 git_indexer_progress stats = {0, 0};
66 git_indexer *indexer = NULL;
67 git_str path = GIT_STR_INIT;
68 git_oid oid;
69 bool append_hash = false;
70 int error;
71
72 if (size == 0)
73 return 0;
74
75 if (!odb || !mempack) {
76 fprintf(stderr, "Global state not initialized\n");
77 abort();
78 }
79 git_mempack_reset(mempack);
80
81 if (git_odb_write(&oid, odb, base_obj, base_obj_len, GIT_OBJECT_BLOB) < 0) {
82 fprintf(stderr, "Failed to add an object to the odb\n");
83 abort();
84 }
85
86 #ifdef GIT_EXPERIMENTAL_SHA256
87 error = git_indexer_new(&indexer, ".", NULL);
88 #else
89 error = git_indexer_new(&indexer, ".", 0, odb, NULL);
90 #endif
91
92 if (error < 0) {
93 fprintf(stderr, "Failed to create the indexer: %s\n",
94 git_error_last()->message);
95 abort();
96 }
97
98 /*
99 * If the first byte in the stream has the high bit set, append the
100 * SHA1 hash so that the packfile is somewhat valid.
101 */
102 append_hash = *data & 0x80;
103 ++data;
104 --size;
105
106 if (git_indexer_append(indexer, data, size, &stats) < 0)
107 goto cleanup;
108 if (append_hash) {
109 #ifdef GIT_EXPERIMENTAL_SHA256
110 if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB, GIT_OID_SHA1) < 0) {
111 fprintf(stderr, "Failed to compute the SHA1 hash\n");
112 abort();
113 }
114 #else
115 if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
116 fprintf(stderr, "Failed to compute the SHA1 hash\n");
117 abort();
118 }
119 #endif
120
121 if (git_indexer_append(indexer, &oid.id, GIT_OID_SHA1_SIZE, &stats) < 0) {
122 goto cleanup;
123 }
124 }
125 if (git_indexer_commit(indexer, &stats) < 0)
126 goto cleanup;
127
128 if (git_str_printf(&path, "pack-%s.idx", git_indexer_name(indexer)) < 0)
129 goto cleanup;
130 p_unlink(git_str_cstr(&path));
131
132 git_str_clear(&path);
133
134 if (git_str_printf(&path, "pack-%s.pack", git_indexer_name(indexer)) < 0)
135 goto cleanup;
136 p_unlink(git_str_cstr(&path));
137
138 cleanup:
139 git_mempack_reset(mempack);
140 git_indexer_free(indexer);
141 git_str_dispose(&path);
142 return 0;
143 }