Debian Codegraph @

fuzz/dns_qp.c

raw
1 /*
2 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3 *
4 * SPDX-License-Identifier: MPL-2.0
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public
7 * License, v. 2.0. If a copy of the MPL was not distributed with this
8 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * See the COPYRIGHT file distributed with this work for additional
11 * information regarding copyright ownership.
12 */
13
14 #include <assert.h>
15 #include <err.h>
16 #include <stdbool.h>
17 #include <stdint.h>
18
19 #include <isc/random.h>
20 #include <isc/refcount.h>
21 #include <isc/rwlock.h>
22 #include <isc/urcu.h>
23 #include <isc/util.h>
24
25 #include <dns/qp.h>
26 #include <dns/types.h>
27
28 #include "fuzz.h"
29 #include "qp_p.h"
30
31 #include <tests/qp.h>
32
33 bool debug = false;
34
35 #if 0
36 #define TRACE(...) warnx(__VA_ARGS__)
37 #else
38 #define TRACE(...)
39 #endif
40
41 #if 0
42 #define ASSERT(p) \
43 do { \
44 warnx("%s:%d: %s (%s)", __func__, __LINE__, #p, \
45 (p) ? "OK" : "FAIL"); \
46 ok = ok && (p); \
47 } while (0)
48 #else
49 #define ASSERT(p) assert(p)
50 #endif
51
52 static struct {
53 uint32_t refcount;
54 bool exists;
55 uint8_t len;
56 dns_qpkey_t key;
57 dns_qpkey_t ascii;
58 } item[256 * 256 / 4];
59
60 static void
61 fuzz_attach(void *ctx, void *pval, uint32_t ival) {
62 assert(ctx == NULL);
63 assert(pval == &item[ival]);
64 item[ival].refcount++;
65 }
66
67 static void
68 fuzz_detach(void *ctx, void *pval, uint32_t ival) {
69 assert(ctx == NULL);
70 assert(pval == &item[ival]);
71 item[ival].refcount--;
72 }
73
74 static size_t
75 fuzz_makekey(dns_qpkey_t key, void *ctx, void *pval, uint32_t ival) {
76 assert(ctx == NULL);
77 assert(pval == &item[ival]);
78 memmove(key, item[ival].key, item[ival].len);
79 return item[ival].len;
80 }
81
82 static void
83 fuzz_triename(void *ctx, char *buf, size_t size) {
84 assert(ctx == NULL);
85 strlcpy(buf, "fuzz", size);
86 }
87
88 const dns_qpmethods_t fuzz_methods = {
89 fuzz_attach,
90 fuzz_detach,
91 fuzz_makekey,
92 fuzz_triename,
93 };
94
95 static uint8_t
96 random_byte(void) {
97 return isc_random_uniform(SHIFT_OFFSET - SHIFT_NOBYTE) + SHIFT_NOBYTE;
98 }
99
100 int
101 LLVMFuzzerInitialize(int *argc, char ***argv) {
102 UNUSED(argc);
103 UNUSED(argv);
104
105 for (size_t i = 0; i < ARRAY_SIZE(item); i++) {
106 size_t len = isc_random_uniform(100) + 16;
107 item[i].len = len;
108 for (size_t off = 0; off < len; off++) {
109 item[i].key[off] = random_byte();
110 }
111 memmove(item[i].ascii, item[i].key, len);
112 qp_test_keytoascii(item[i].ascii, len);
113 }
114
115 return 0;
116 }
117
118 int
119 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
120 isc_result_t result;
121
122 TRACE("------------------------------------------------");
123
124 isc_mem_t *mctx = NULL;
125 isc_mem_create(&mctx);
126 isc_mem_setdestroycheck(mctx, true);
127
128 dns_qp_t *qp = NULL;
129 dns_qp_create(mctx, &fuzz_methods, NULL, &qp);
130
131 /* avoid overrun */
132 size = size & ~1;
133
134 size_t count = 0;
135
136 for (size_t in = 0; in < size; in += 2) {
137 size_t what = data[in] + data[in + 1] * 256;
138 size_t i = (what / 4) % (count * 2 + 2);
139 bool exists = item[i].exists;
140 uint32_t refcount = item[i].refcount;
141 bool ok = true;
142 if (what & 2) {
143 void *pval = NULL;
144 uint32_t ival = ~0U;
145 result = dns_qp_getkey(qp, item[i].key, item[i].len,
146 &pval, &ival);
147 TRACE("count %zu get %s %zu >%s<", count,
148 isc_result_toid(result), i, item[i].ascii);
149 if (result == ISC_R_SUCCESS) {
150 ASSERT(pval == &item[i]);
151 ASSERT(ival == i);
152 ASSERT(item[i].refcount == 1);
153 ASSERT(item[i].exists == true);
154 } else if (result == ISC_R_NOTFOUND) {
155 ASSERT(pval == NULL);
156 ASSERT(ival == ~0U);
157 ASSERT(item[i].refcount == 0);
158 ASSERT(item[i].exists == false);
159 } else {
160 UNREACHABLE();
161 }
162 } else if (what & 1) {
163 result = dns_qp_insert(qp, &item[i], i);
164 TRACE("count %zu ins %s %zu >%s<", count,
165 isc_result_toid(result), i, item[i].ascii);
166 if (result == ISC_R_SUCCESS) {
167 item[i].exists = true;
168 ASSERT(exists == false);
169 ASSERT(refcount == 0);
170 ASSERT(item[i].refcount == 1);
171 count += 1;
172 ASSERT(qp->leaf_count == count);
173 } else if (result == ISC_R_EXISTS) {
174 ASSERT(exists == true);
175 ASSERT(refcount == 1);
176 ASSERT(item[i].refcount == 1);
177 ASSERT(qp->leaf_count == count);
178 } else {
179 UNREACHABLE();
180 }
181 } else {
182 result = dns_qp_deletekey(qp, item[i].key, item[i].len,
183 NULL, NULL);
184 TRACE("count %zu del %s %zu >%s<", count,
185 isc_result_toid(result), i, item[i].ascii);
186 if (result == ISC_R_SUCCESS) {
187 item[i].exists = false;
188 ASSERT(exists == true);
189 ASSERT(refcount == 1);
190 ASSERT(item[i].refcount == 0);
191 count -= 1;
192 ASSERT(qp->leaf_count == count);
193 } else if (result == ISC_R_NOTFOUND) {
194 ASSERT(exists == false);
195 ASSERT(refcount == 0);
196 ASSERT(item[i].refcount == 0);
197 ASSERT(qp->leaf_count == count);
198 } else {
199 UNREACHABLE();
200 }
201 }
202 if (!ok) {
203 qp_test_dumpqp(qp);
204 qp_test_dumptrie(qp);
205 }
206 assert(ok);
207 }
208
209 for (size_t i = 0; i < ARRAY_SIZE(item); i++) {
210 assert(item[i].exists == (item[i].refcount != 0));
211 }
212
213 dns_qp_destroy(&qp);
214 isc_mem_destroy(&mctx);
215 isc_mem_checkdestroyed(stderr);
216
217 for (size_t i = 0; i < ARRAY_SIZE(item); i++) {
218 item[i].exists = false;
219 assert(item[i].refcount == 0);
220 }
221
222 return 0;
223 }