/src/bind9/fuzz/dns_name_fromwire.c
Line | Count | Source |
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 <stddef.h> |
15 | | #include <stdlib.h> |
16 | | #include <unistd.h> |
17 | | |
18 | | #include <isc/ascii.h> |
19 | | #include <isc/buffer.h> |
20 | | #include <isc/util.h> |
21 | | |
22 | | #include <dns/compress.h> |
23 | | #include <dns/fixedname.h> |
24 | | #include <dns/name.h> |
25 | | |
26 | | #include "fuzz.h" |
27 | | #include "old.h" |
28 | | |
29 | | bool debug = false; |
30 | | |
31 | | int |
32 | 18 | LLVMFuzzerInitialize(int *argc ISC_ATTR_UNUSED, char ***argv ISC_ATTR_UNUSED) { |
33 | 18 | return 0; |
34 | 18 | } |
35 | | |
36 | | int |
37 | 176 | LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { |
38 | 176 | isc_result_t new_result; |
39 | 176 | isc_result_t old_result; |
40 | 176 | dns_fixedname_t new_fixed; |
41 | 176 | dns_fixedname_t old_fixed; |
42 | 176 | dns_name_t *new_name = dns_fixedname_initname(&new_fixed); |
43 | 176 | dns_name_t *old_name = dns_fixedname_initname(&old_fixed); |
44 | 176 | dns_decompress_t dctx = DNS_DECOMPRESS_PERMITTED; |
45 | 176 | isc_buffer_t new_buf; |
46 | 176 | isc_buffer_t old_buf; |
47 | | |
48 | | /* |
49 | | * Output buffers may be partially used or undersized. |
50 | | */ |
51 | 176 | if (size > 0) { |
52 | 176 | uint8_t add = *data++; |
53 | 176 | size--; |
54 | 176 | isc_buffer_add(&new_fixed.buffer, add); |
55 | 176 | isc_buffer_add(&old_fixed.buffer, add); |
56 | 176 | } |
57 | | |
58 | | /* |
59 | | * timeout faster if we hit a pointer loop |
60 | | */ |
61 | 176 | alarm(1); |
62 | | |
63 | | /* |
64 | | * We shift forward by half the input data to make an area |
65 | | * that pointers can refer back to. |
66 | | */ |
67 | | |
68 | 176 | isc_buffer_constinit(&new_buf, data, size); |
69 | 176 | isc_buffer_add(&new_buf, size); |
70 | 176 | isc_buffer_setactive(&new_buf, size); |
71 | 176 | isc_buffer_forward(&new_buf, size / 2); |
72 | 176 | new_result = dns_name_fromwire(new_name, &new_buf, dctx, NULL); |
73 | | |
74 | 176 | isc_buffer_constinit(&old_buf, data, size); |
75 | 176 | isc_buffer_add(&old_buf, size); |
76 | 176 | isc_buffer_setactive(&old_buf, size); |
77 | 176 | isc_buffer_forward(&old_buf, size / 2); |
78 | 176 | old_result = old_name_fromwire(old_name, &old_buf, dctx, 0, NULL); |
79 | | |
80 | 176 | REQUIRE(new_result == old_result); |
81 | 176 | REQUIRE(dns_name_equal(new_name, old_name)); |
82 | | |
83 | 176 | REQUIRE(new_fixed.buffer.current == old_fixed.buffer.current); |
84 | 176 | REQUIRE(new_fixed.buffer.active == old_fixed.buffer.active); |
85 | 176 | REQUIRE(new_fixed.buffer.used == old_fixed.buffer.used); |
86 | 176 | REQUIRE(new_fixed.buffer.length == old_fixed.buffer.length); |
87 | | |
88 | 176 | REQUIRE(new_buf.base == old_buf.base); |
89 | 176 | REQUIRE(new_buf.current == old_buf.current); |
90 | 176 | REQUIRE(new_buf.active == old_buf.active); |
91 | 176 | REQUIRE(new_buf.used == old_buf.used); |
92 | 176 | REQUIRE(new_buf.length == old_buf.length); |
93 | | |
94 | 176 | return 0; |
95 | 176 | } |