Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
unicode.cc
Go to the documentation of this file.
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
19extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
20 FuzzedDataProvider fdp(data, size);
21 std::string input = fdp.ConsumeRandomLengthString(128);
22 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 for (char c : input) {
28 // forbidden_host ⊆ forbidden_domain (every host-forbidden code point must
29 // also be domain-forbidden).
30 bool is_host = ada::unicode::is_forbidden_host_code_point(c);
31 bool is_domain = ada::unicode::is_forbidden_domain_code_point(c);
32 if (is_host && !is_domain) {
33 printf(
34 "is_forbidden_host_code_point implies is_forbidden_domain_code_point"
35 " but got inconsistent results for char 0x%02x\n",
36 (unsigned char)c);
37 abort();
38 }
39
40 // ascii_digit ⊆ ascii_hex_digit
41 bool is_digit = ada::unicode::is_ascii_digit(c);
42 bool is_hex = ada::unicode::is_ascii_hex_digit(c);
43 if (is_digit && !is_hex) {
44 printf(
45 "is_ascii_digit implies is_ascii_hex_digit"
46 " but got inconsistent results for char 0x%02x\n",
47 (unsigned char)c);
48 abort();
49 }
50
51 // lowercase_hex ⊆ ascii_hex_digit
52 bool is_lhex = ada::unicode::is_lowercase_hex(c);
53 if (is_lhex && !is_hex) {
54 printf(
55 "is_lowercase_hex implies is_ascii_hex_digit"
56 " but got inconsistent results for char 0x%02x\n",
57 (unsigned char)c);
58 abort();
59 }
60
61 // convert_hex_to_binary result must be in [0, 15] for valid hex digits.
62 if (is_hex) {
63 unsigned val = ada::unicode::convert_hex_to_binary(c);
64 if (val > 15) {
65 printf(
66 "convert_hex_to_binary returned %u (> 15) for hex digit 0x%02x\n",
67 val, (unsigned char)c);
68 abort();
69 }
70 }
71
72 // Other classification functions – must not crash.
73 volatile bool alnum = ada::unicode::is_alnum_plus(c);
74 (void)alnum;
75 volatile bool c0 = ada::unicode::is_c0_control_or_space(c);
76 (void)c0;
77 volatile bool tab_nl = ada::unicode::is_ascii_tab_or_newline(c);
78 (void)tab_nl;
79 volatile bool ascii32 = ada::unicode::is_ascii(
80 static_cast<char32_t>(static_cast<unsigned char>(c)));
81 (void)ascii32;
82
83 // ada::checkers per-character functions.
84 volatile bool alpha = ada::checkers::is_alpha(c);
85 (void)alpha;
86 volatile bool digit2 = ada::checkers::is_digit(c);
87 (void)digit2;
88 volatile char lower = ada::checkers::to_lower(c);
89 (void)lower;
90 }
91
92 // ===== String-level classification =====
93
94 // has_tabs_or_newline: any string is valid input.
95 volatile bool has_tn = ada::unicode::has_tabs_or_newline(sv);
96 (void)has_tn;
97
98 // is_double_dot and is_single_dot are mutually exclusive.
99 {
100 bool double_dot = ada::unicode::is_double_dot_path_segment(sv);
101 bool single_dot = ada::unicode::is_single_dot_path_segment(sv);
102 if (double_dot && single_dot) {
103 printf(
104 "is_double_dot_path_segment and is_single_dot_path_segment are"
105 " mutually exclusive but both true for '%s'\n",
106 input.c_str());
107 abort();
108 }
109 }
110
111 // contains_forbidden_domain_code_point: must not crash.
112 volatile bool has_forbidden =
113 ada::unicode::contains_forbidden_domain_code_point(input.data(),
114 input.size());
115 (void)has_forbidden;
116
117 // contains_forbidden_domain_code_point_or_upper: bit 0 = forbidden, bit 1 =
118 // upper.
119 volatile uint8_t forbidden_or_upper =
120 ada::unicode::contains_forbidden_domain_code_point_or_upper(input.data(),
121 input.size());
122 (void)forbidden_or_upper;
123
124 // to_lower_ascii: in-place; result must have the same length.
125 {
126 std::string lower_copy = input;
127 volatile bool all_ascii =
128 ada::unicode::to_lower_ascii(lower_copy.data(), lower_copy.size());
129 (void)all_ascii;
130 assert(lower_copy.size() == input.size());
131 }
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 {
138 std::string_view pruned = sv;
139 auto frag = ada::helpers::prune_hash(pruned);
140 // The prefix + separator + suffix must not exceed original length.
141 assert(pruned.size() <= sv.size());
142 if (frag.has_value()) {
143 assert(pruned.size() + 1 + frag->size() <= sv.size() + 1);
144 }
145 }
146
147 // trim_c0_whitespace: result must be a substring, so never longer.
148 {
149 std::string_view trimmed = sv;
150 ada::helpers::trim_c0_whitespace(trimmed);
151 assert(trimmed.size() <= sv.size());
152 }
153
154 // remove_ascii_tab_or_newline: must remove all \t / \n / \r characters.
155 {
156 std::string cleaned = input;
157 ada::helpers::remove_ascii_tab_or_newline(cleaned);
158 for (char c : cleaned) {
159 if (c == '\t' || c == '\n' || c == '\r') {
160 printf(
161 "remove_ascii_tab_or_newline left tab/newline in output for"
162 " input '%s'\n",
163 input.c_str());
164 abort();
165 }
166 }
167 // Removing characters cannot make the string longer.
168 assert(cleaned.size() <= input.size());
169 }
170
171 // find_authority_delimiter and find_authority_delimiter_special: result must
172 // be within [0, input.size()].
173 {
174 size_t delim = ada::helpers::find_authority_delimiter(sv);
175 assert(delim <= sv.size());
176 size_t delim_special = ada::helpers::find_authority_delimiter_special(sv);
177 assert(delim_special <= sv.size());
178 }
179
180 // get_host_delimiter_location: result position must be within [0,
181 // view.size()].
182 {
183 std::string_view view = sv;
184 auto [pos, found_colon] =
185 ada::helpers::get_host_delimiter_location(true, view);
186 (void)found_colon;
187 // pos is relative to the original sv, not the possibly-trimmed view.
188 assert(pos <= sv.size());
189 }
190 {
191 std::string_view view = sv;
192 auto [pos, found_colon] =
193 ada::helpers::get_host_delimiter_location(false, view);
194 (void)found_colon;
195 assert(pos <= sv.size());
196 }
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 {
203 std::string input2 = fdp.ConsumeRandomLengthString(64);
204 size_t first_pct = input2.find('%');
205 if (first_pct == std::string::npos) first_pct = input2.size();
206 std::optional<std::string> out;
207 volatile bool ok = ada::unicode::to_ascii(out, input2, first_pct);
208 (void)ok;
209 if (out.has_value()) {
210 // A successfully converted domain must always be parseable as a URL host.
211 volatile size_t olen = out->size();
212 (void)olen;
213 }
214 }
215
216 return 0;
217}
Main header for the Ada URL parser library.
constexpr char to_lower(char x) noexcept
constexpr bool is_alpha(char x) noexcept
constexpr bool is_digit(char x) noexcept
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
Definition unicode.cc:19