/src/ada-url/fuzz/unicode.cc
Line | Count | Source |
1 | | #include <fuzzer/FuzzedDataProvider.h> |
2 | | |
3 | | #include <cassert> |
4 | | #include <cstdio> |
5 | | #include <string> |
6 | | |
7 | | #include "ada.cpp" |
8 | | #include "ada.h" |
9 | | |
10 | | // ============================================================ |
11 | | // Fuzzer for low-level unicode, checker, and helper utilities. |
12 | | // |
13 | | // These functions are exercised indirectly through the URL |
14 | | // parsing pipeline but are never targeted directly. Fuzzing |
15 | | // them in isolation lets the engine discover edge cases |
16 | | // without requiring a structurally valid URL as input. |
17 | | // ============================================================ |
18 | | |
19 | 3.13k | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
20 | 3.13k | FuzzedDataProvider fdp(data, size); |
21 | 3.13k | std::string input = fdp.ConsumeRandomLengthString(128); |
22 | 3.13k | std::string_view sv(input.data(), input.size()); |
23 | | |
24 | | // ===== Per-character classification ===== |
25 | | // Drive the fuzzer over every byte value so all lookup-table branches are |
26 | | // reachable from the corpus without relying on URL structure. |
27 | 9.99k | for (char c : input) { |
28 | | // forbidden_host ⊆ forbidden_domain (every host-forbidden code point must |
29 | | // also be domain-forbidden). |
30 | 9.99k | bool is_host = ada::unicode::is_forbidden_host_code_point(c); |
31 | 9.99k | bool is_domain = ada::unicode::is_forbidden_domain_code_point(c); |
32 | 9.99k | if (is_host && !is_domain) { |
33 | 0 | printf( |
34 | 0 | "is_forbidden_host_code_point implies is_forbidden_domain_code_point" |
35 | 0 | " but got inconsistent results for char 0x%02x\n", |
36 | 0 | (unsigned char)c); |
37 | 0 | abort(); |
38 | 0 | } |
39 | | |
40 | | // ascii_digit ⊆ ascii_hex_digit |
41 | 9.99k | bool is_digit = ada::unicode::is_ascii_digit(c); |
42 | 9.99k | bool is_hex = ada::unicode::is_ascii_hex_digit(c); |
43 | 9.99k | if (is_digit && !is_hex) { |
44 | 0 | printf( |
45 | 0 | "is_ascii_digit implies is_ascii_hex_digit" |
46 | 0 | " but got inconsistent results for char 0x%02x\n", |
47 | 0 | (unsigned char)c); |
48 | 0 | abort(); |
49 | 0 | } |
50 | | |
51 | | // lowercase_hex ⊆ ascii_hex_digit |
52 | 9.99k | bool is_lhex = ada::unicode::is_lowercase_hex(c); |
53 | 9.99k | if (is_lhex && !is_hex) { |
54 | 0 | printf( |
55 | 0 | "is_lowercase_hex implies is_ascii_hex_digit" |
56 | 0 | " but got inconsistent results for char 0x%02x\n", |
57 | 0 | (unsigned char)c); |
58 | 0 | abort(); |
59 | 0 | } |
60 | | |
61 | | // convert_hex_to_binary result must be in [0, 15] for valid hex digits. |
62 | 9.99k | if (is_hex) { |
63 | 1.04k | unsigned val = ada::unicode::convert_hex_to_binary(c); |
64 | 1.04k | if (val > 15) { |
65 | 0 | printf( |
66 | 0 | "convert_hex_to_binary returned %u (> 15) for hex digit 0x%02x\n", |
67 | 0 | val, (unsigned char)c); |
68 | 0 | abort(); |
69 | 0 | } |
70 | 1.04k | } |
71 | | |
72 | | // Other classification functions – must not crash. |
73 | 9.99k | volatile bool alnum = ada::unicode::is_alnum_plus(c); |
74 | 9.99k | (void)alnum; |
75 | 9.99k | volatile bool c0 = ada::unicode::is_c0_control_or_space(c); |
76 | 9.99k | (void)c0; |
77 | 9.99k | volatile bool tab_nl = ada::unicode::is_ascii_tab_or_newline(c); |
78 | 9.99k | (void)tab_nl; |
79 | 9.99k | volatile bool ascii32 = ada::unicode::is_ascii( |
80 | 9.99k | static_cast<char32_t>(static_cast<unsigned char>(c))); |
81 | 9.99k | (void)ascii32; |
82 | | |
83 | | // ada::checkers per-character functions. |
84 | 9.99k | volatile bool alpha = ada::checkers::is_alpha(c); |
85 | 9.99k | (void)alpha; |
86 | 9.99k | volatile bool digit2 = ada::checkers::is_digit(c); |
87 | 9.99k | (void)digit2; |
88 | 9.99k | volatile char lower = ada::checkers::to_lower(c); |
89 | 9.99k | (void)lower; |
90 | 9.99k | } |
91 | | |
92 | | // ===== String-level classification ===== |
93 | | |
94 | | // has_tabs_or_newline: any string is valid input. |
95 | 3.13k | volatile bool has_tn = ada::unicode::has_tabs_or_newline(sv); |
96 | 3.13k | (void)has_tn; |
97 | | |
98 | | // is_double_dot and is_single_dot are mutually exclusive. |
99 | 3.13k | { |
100 | 3.13k | bool double_dot = ada::unicode::is_double_dot_path_segment(sv); |
101 | 3.13k | bool single_dot = ada::unicode::is_single_dot_path_segment(sv); |
102 | 3.13k | if (double_dot && single_dot) { |
103 | 0 | printf( |
104 | 0 | "is_double_dot_path_segment and is_single_dot_path_segment are" |
105 | 0 | " mutually exclusive but both true for '%s'\n", |
106 | 0 | input.c_str()); |
107 | 0 | abort(); |
108 | 0 | } |
109 | 3.13k | } |
110 | | |
111 | | // contains_forbidden_domain_code_point: must not crash. |
112 | 3.13k | volatile bool has_forbidden = |
113 | 3.13k | ada::unicode::contains_forbidden_domain_code_point(input.data(), |
114 | 3.13k | input.size()); |
115 | 3.13k | (void)has_forbidden; |
116 | | |
117 | | // contains_forbidden_domain_code_point_or_upper: bit 0 = forbidden, bit 1 = |
118 | | // upper. |
119 | 3.13k | volatile uint8_t forbidden_or_upper = |
120 | 3.13k | ada::unicode::contains_forbidden_domain_code_point_or_upper(input.data(), |
121 | 3.13k | input.size()); |
122 | 3.13k | (void)forbidden_or_upper; |
123 | | |
124 | | // to_lower_ascii: in-place; result must have the same length. |
125 | 3.13k | { |
126 | 3.13k | std::string lower_copy = input; |
127 | 3.13k | volatile bool all_ascii = |
128 | 3.13k | ada::unicode::to_lower_ascii(lower_copy.data(), lower_copy.size()); |
129 | 3.13k | (void)all_ascii; |
130 | 3.13k | assert(lower_copy.size() == input.size()); |
131 | 3.13k | } |
132 | | |
133 | | // ===== Helper functions ===== |
134 | | |
135 | | // prune_hash: the invariant is that the returned fragment (if any) is |
136 | | // shorter than the original input (the '#' itself is consumed). |
137 | 3.13k | { |
138 | 3.13k | std::string_view pruned = sv; |
139 | 3.13k | auto frag = ada::helpers::prune_hash(pruned); |
140 | | // The prefix + separator + suffix must not exceed original length. |
141 | 3.13k | assert(pruned.size() <= sv.size()); |
142 | 3.13k | if (frag.has_value()) { |
143 | 28 | assert(pruned.size() + 1 + frag->size() <= sv.size() + 1); |
144 | 28 | } |
145 | 3.13k | } |
146 | | |
147 | | // trim_c0_whitespace: result must be a substring, so never longer. |
148 | 3.13k | { |
149 | 3.13k | std::string_view trimmed = sv; |
150 | 3.13k | ada::helpers::trim_c0_whitespace(trimmed); |
151 | 3.13k | assert(trimmed.size() <= sv.size()); |
152 | 3.13k | } |
153 | | |
154 | | // remove_ascii_tab_or_newline: must remove all \t / \n / \r characters. |
155 | 3.13k | { |
156 | 3.13k | std::string cleaned = input; |
157 | 3.13k | ada::helpers::remove_ascii_tab_or_newline(cleaned); |
158 | 8.61k | for (char c : cleaned) { |
159 | 8.61k | if (c == '\t' || c == '\n' || c == '\r') { |
160 | 0 | printf( |
161 | 0 | "remove_ascii_tab_or_newline left tab/newline in output for" |
162 | 0 | " input '%s'\n", |
163 | 0 | input.c_str()); |
164 | 0 | abort(); |
165 | 0 | } |
166 | 8.61k | } |
167 | | // Removing characters cannot make the string longer. |
168 | 3.13k | assert(cleaned.size() <= input.size()); |
169 | 3.13k | } |
170 | | |
171 | | // find_authority_delimiter and find_authority_delimiter_special: result must |
172 | | // be within [0, input.size()]. |
173 | 3.13k | { |
174 | 3.13k | size_t delim = ada::helpers::find_authority_delimiter(sv); |
175 | 3.13k | assert(delim <= sv.size()); |
176 | 3.13k | size_t delim_special = ada::helpers::find_authority_delimiter_special(sv); |
177 | 3.13k | assert(delim_special <= sv.size()); |
178 | 3.13k | } |
179 | | |
180 | | // get_host_delimiter_location: result position must be within [0, |
181 | | // view.size()]. |
182 | 3.13k | { |
183 | 3.13k | std::string_view view = sv; |
184 | 3.13k | auto [pos, found_colon] = |
185 | 3.13k | ada::helpers::get_host_delimiter_location(true, view); |
186 | 3.13k | (void)found_colon; |
187 | | // pos is relative to the original sv, not the possibly-trimmed view. |
188 | 3.13k | assert(pos <= sv.size()); |
189 | 3.13k | } |
190 | 3.13k | { |
191 | 3.13k | std::string_view view = sv; |
192 | 3.13k | auto [pos, found_colon] = |
193 | 3.13k | ada::helpers::get_host_delimiter_location(false, view); |
194 | 3.13k | (void)found_colon; |
195 | 3.13k | assert(pos <= sv.size()); |
196 | 3.13k | } |
197 | | |
198 | | // ===== unicode::to_ascii (internal IDNA entry point) ===== |
199 | | // This is the internal function called by the URL parser's host processing. |
200 | | // It takes an optional<string> output buffer, the input domain, and the |
201 | | // position of the first percent character. |
202 | 3.13k | { |
203 | 3.13k | std::string input2 = fdp.ConsumeRandomLengthString(64); |
204 | 3.13k | size_t first_pct = input2.find('%'); |
205 | 3.13k | if (first_pct == std::string::npos) first_pct = input2.size(); |
206 | 3.13k | std::optional<std::string> out; |
207 | 3.13k | volatile bool ok = ada::unicode::to_ascii(out, input2, first_pct); |
208 | 3.13k | (void)ok; |
209 | 3.13k | if (out.has_value()) { |
210 | | // A successfully converted domain must always be parseable as a URL host. |
211 | 1.04k | volatile size_t olen = out->size(); |
212 | 1.04k | (void)olen; |
213 | 1.04k | } |
214 | 3.13k | } |
215 | | |
216 | 3.13k | return 0; |
217 | 3.13k | } |