Debian Codegraph @

fuzzers/download_refs_fuzzer.c

raw
1 /*
2 * libgit2 raw packfile fuzz 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 #include <stdlib.h>
12 #include <string.h>
13
14 #include "git2.h"
15 #include "git2/sys/transport.h"
16 #include "futils.h"
17
18 #include "standalone_driver.h"
19 #include "fuzzer_utils.h"
20
21 #define UNUSED(x) (void)(x)
22
23 struct fuzzer_buffer {
24 const unsigned char *data;
25 size_t size;
26 };
27
28 struct fuzzer_stream {
29 git_smart_subtransport_stream base;
30 const unsigned char *readp;
31 const unsigned char *endp;
32 };
33
34 struct fuzzer_subtransport {
35 git_smart_subtransport base;
36 git_transport *owner;
37 struct fuzzer_buffer data;
38 };
39
40 static git_repository *repo;
41
42 static int fuzzer_stream_read(git_smart_subtransport_stream *stream,
43 char *buffer,
44 size_t buf_size,
45 size_t *bytes_read)
46 {
47 struct fuzzer_stream *fs = (struct fuzzer_stream *) stream;
48 size_t avail = fs->endp - fs->readp;
49
50 *bytes_read = (buf_size > avail) ? avail : buf_size;
51 memcpy(buffer, fs->readp, *bytes_read);
52 fs->readp += *bytes_read;
53
54 return 0;
55 }
56
57 static int fuzzer_stream_write(git_smart_subtransport_stream *stream,
58 const char *buffer, size_t len)
59 {
60 UNUSED(stream);
61 UNUSED(buffer);
62 UNUSED(len);
63 return 0;
64 }
65
66 static void fuzzer_stream_free(git_smart_subtransport_stream *stream)
67 {
68 free(stream);
69 }
70
71 static int fuzzer_stream_new(
72 struct fuzzer_stream **out,
73 const struct fuzzer_buffer *data)
74 {
75 struct fuzzer_stream *stream = malloc(sizeof(*stream));
76 if (!stream)
77 return -1;
78
79 stream->readp = data->data;
80 stream->endp = data->data + data->size;
81 stream->base.read = fuzzer_stream_read;
82 stream->base.write = fuzzer_stream_write;
83 stream->base.free = fuzzer_stream_free;
84
85 *out = stream;
86
87 return 0;
88 }
89
90 static int fuzzer_subtransport_action(
91 git_smart_subtransport_stream **out,
92 git_smart_subtransport *transport,
93 const char *url,
94 git_smart_service_t action)
95 {
96 struct fuzzer_subtransport *ft = (struct fuzzer_subtransport *) transport;
97
98 UNUSED(url);
99 UNUSED(action);
100
101 return fuzzer_stream_new((struct fuzzer_stream **) out, &ft->data);
102 }
103
104 static int fuzzer_subtransport_close(git_smart_subtransport *transport)
105 {
106 UNUSED(transport);
107 return 0;
108 }
109
110 static void fuzzer_subtransport_free(git_smart_subtransport *transport)
111 {
112 free(transport);
113 }
114
115 static int fuzzer_subtransport_new(
116 struct fuzzer_subtransport **out,
117 git_transport *owner,
118 const struct fuzzer_buffer *data)
119 {
120 struct fuzzer_subtransport *sub = malloc(sizeof(*sub));
121 if (!sub)
122 return -1;
123
124 sub->owner = owner;
125 sub->data.data = data->data;
126 sub->data.size = data->size;
127 sub->base.action = fuzzer_subtransport_action;
128 sub->base.close = fuzzer_subtransport_close;
129 sub->base.free = fuzzer_subtransport_free;
130
131 *out = sub;
132
133 return 0;
134 }
135
136 static int fuzzer_subtransport_cb(
137 git_smart_subtransport **out,
138 git_transport *owner,
139 void *payload)
140 {
141 struct fuzzer_buffer *buf = (struct fuzzer_buffer *) payload;
142 struct fuzzer_subtransport *sub;
143
144 if (fuzzer_subtransport_new(&sub, owner, buf) < 0)
145 return -1;
146
147 *out = &sub->base;
148 return 0;
149 }
150
151 static int fuzzer_transport_cb(git_transport **out, git_remote *owner, void *param)
152 {
153 git_smart_subtransport_definition def = {
154 fuzzer_subtransport_cb,
155 1,
156 param
157 };
158 return git_transport_smart(out, owner, &def);
159 }
160
161 int LLVMFuzzerInitialize(int *argc, char ***argv)
162 {
163 UNUSED(argc);
164 UNUSED(argv);
165
166 if (git_libgit2_init() < 0)
167 abort();
168
169 if (git_libgit2_opts(GIT_OPT_SET_PACK_MAX_OBJECTS, 10000000) < 0)
170 abort();
171
172 repo = fuzzer_repo_init();
173 return 0;
174 }
175
176 int LLVMFuzzerTestOneInput(const unsigned char *data, size_t size)
177 {
178 struct fuzzer_buffer buffer = { data, size };
179 git_remote_callbacks callbacks = GIT_REMOTE_CALLBACKS_INIT;
180 git_remote *remote;
181
182 if (git_remote_create_anonymous(&remote, repo, "fuzzer://remote-url") < 0)
183 fuzzer_git_abort("git_remote_create");
184
185 callbacks.transport = fuzzer_transport_cb;
186 callbacks.payload = &buffer;
187
188 if (git_remote_connect(remote, GIT_DIRECTION_FETCH,
189 &callbacks, NULL, NULL) < 0)
190 goto out;
191
192 git_remote_download(remote, NULL, NULL);
193
194 out:
195 git_remote_free(remote);
196
197 return 0;
198 }