]> git.proxmox.com Git - libgit2.git/blob - fuzzers/packfile_fuzzer.c
New upstream version 1.5.0+ds
[libgit2.git] / fuzzers / packfile_fuzzer.c
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 if (git_odb_new(&odb) < 0) {
40 fprintf(stderr, "Failed to create the odb\n");
41 abort();
42 }
43 if (git_mempack_new(&mempack) < 0) {
44 fprintf(stderr, "Failed to create the mempack\n");
45 abort();
46 }
47 if (git_odb_add_backend(odb, mempack, 999) < 0) {
48 fprintf(stderr, "Failed to add the mempack\n");
49 abort();
50 }
51 return 0;
52 }
53
54 int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
55 {
56 git_indexer_progress stats = {0, 0};
57 git_indexer *indexer = NULL;
58 git_str path = GIT_STR_INIT;
59 git_oid oid;
60 bool append_hash = false;
61
62 if (size == 0)
63 return 0;
64
65 if (!odb || !mempack) {
66 fprintf(stderr, "Global state not initialized\n");
67 abort();
68 }
69 git_mempack_reset(mempack);
70
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");
73 abort();
74 }
75
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);
79 abort();
80 }
81
82 /*
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.
85 */
86 append_hash = *data & 0x80;
87 ++data;
88 --size;
89
90 if (git_indexer_append(indexer, data, size, &stats) < 0)
91 goto cleanup;
92 if (append_hash) {
93 if (git_odb_hash(&oid, data, size, GIT_OBJECT_BLOB) < 0) {
94 fprintf(stderr, "Failed to compute the SHA1 hash\n");
95 abort();
96 }
97 if (git_indexer_append(indexer, &oid.id, GIT_OID_RAWSZ, &stats) < 0) {
98 goto cleanup;
99 }
100 }
101 if (git_indexer_commit(indexer, &stats) < 0)
102 goto cleanup;
103
104 if (git_str_printf(&path, "pack-%s.idx", git_indexer_name(indexer)) < 0)
105 goto cleanup;
106 p_unlink(git_str_cstr(&path));
107
108 git_str_clear(&path);
109
110 if (git_str_printf(&path, "pack-%s.pack", git_indexer_name(indexer)) < 0)
111 goto cleanup;
112 p_unlink(git_str_cstr(&path));
113
114 cleanup:
115 git_mempack_reset(mempack);
116 git_indexer_free(indexer);
117 git_str_dispose(&path);
118 return 0;
119 }