Coverage Report

Created: 2026-12-01 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ada-url/fuzz/simple_absolute.cc
Line
Count
Source
1
#include <fuzzer/FuzzedDataProvider.h>
2
3
#include <cstdio>
4
#include <string>
5
#include <string_view>
6
7
#include "ada.cpp"
8
#include "ada.h"
9
10
static constexpr const char* kSchemes[] = {
11
    "http://", "https://", "HTTP://", "Https://",
12
    "http:",   "https:",   "htTP://", "HTTPS://"};
13
14
static constexpr const char* kHosts[] = {
15
    "example.com",
16
    "www.example.com",
17
    "WWW.Example.COM",
18
    "a",
19
    "a.b.c",
20
    "maps.google.com",
21
    "192.168.0.1",
22
    "0x7f.1",
23
    "127.1",
24
    "0",
25
    "3232235777",
26
    "xn--nxasmq6b.com",
27
    "xn--a",
28
    "XN--A",
29
    "user@example.com",
30
    "user:pass@example.com",
31
    "example.com:8080",
32
    "example.com:0",
33
    "example.com:65535",
34
    "",
35
};
36
37
static constexpr const char* kPaths[] = {
38
    "",
39
    "/",
40
    "/path",
41
    "/path/file.js",
42
    "/a/./b/../c",
43
    "/foo/%2e",
44
    "/foo/%2e%2e",
45
    "/foo/%2E%2E/bar",
46
    "/path with space",
47
    "/path\twith\ttab",
48
    "\\path",
49
    "/continue=https%3A%2F%2Fexample.com%2F",
50
    "/imghp",
51
    "/./",
52
    "/../",
53
    "/%2e%2e%2f",
54
};
55
56
static constexpr const char* kQueries[] = {
57
    "",
58
    "?",
59
    "?q=1",
60
    "?hl=en&tab=wi",
61
    "?continue=https%3A%2F%2Fexample.com%2F",
62
    "?a=1&b=2&c=3",
63
};
64
65
static constexpr const char* kFrags[] = {
66
    "", "#", "#frag", "#x%20y", "#/",
67
};
68
69
template <size_t N>
70
47.6k
static const char* pick(FuzzedDataProvider& fdp, const char* const (&arr)[N]) {
71
47.6k
  return arr[fdp.ConsumeIntegralInRange<size_t>(0, N - 1)];
72
47.6k
}
simple_absolute.cc:char const* pick<8ul>(FuzzedDataProvider&, char const* const (&) [8ul])
Line
Count
Source
70
9.53k
static const char* pick(FuzzedDataProvider& fdp, const char* const (&arr)[N]) {
71
9.53k
  return arr[fdp.ConsumeIntegralInRange<size_t>(0, N - 1)];
72
9.53k
}
simple_absolute.cc:char const* pick<20ul>(FuzzedDataProvider&, char const* const (&) [20ul])
Line
Count
Source
70
9.53k
static const char* pick(FuzzedDataProvider& fdp, const char* const (&arr)[N]) {
71
9.53k
  return arr[fdp.ConsumeIntegralInRange<size_t>(0, N - 1)];
72
9.53k
}
simple_absolute.cc:char const* pick<16ul>(FuzzedDataProvider&, char const* const (&) [16ul])
Line
Count
Source
70
9.53k
static const char* pick(FuzzedDataProvider& fdp, const char* const (&arr)[N]) {
71
9.53k
  return arr[fdp.ConsumeIntegralInRange<size_t>(0, N - 1)];
72
9.53k
}
simple_absolute.cc:char const* pick<6ul>(FuzzedDataProvider&, char const* const (&) [6ul])
Line
Count
Source
70
9.53k
static const char* pick(FuzzedDataProvider& fdp, const char* const (&arr)[N]) {
71
9.53k
  return arr[fdp.ConsumeIntegralInRange<size_t>(0, N - 1)];
72
9.53k
}
simple_absolute.cc:char const* pick<5ul>(FuzzedDataProvider&, char const* const (&) [5ul])
Line
Count
Source
70
9.53k
static const char* pick(FuzzedDataProvider& fdp, const char* const (&arr)[N]) {
71
9.53k
  return arr[fdp.ConsumeIntegralInRange<size_t>(0, N - 1)];
72
9.53k
}
73
74
9.53k
static std::string make_candidate(FuzzedDataProvider& fdp) {
75
9.53k
  std::string out;
76
9.53k
  out += pick(fdp, kSchemes);
77
9.53k
  out += pick(fdp, kHosts);
78
9.53k
  out += pick(fdp, kPaths);
79
9.53k
  out += pick(fdp, kQueries);
80
9.53k
  out += pick(fdp, kFrags);
81
82
9.53k
  if (fdp.ConsumeBool() && !out.empty()) {
83
3.59k
    std::string mid = fdp.ConsumeRandomLengthString(24);
84
3.59k
    size_t pos = fdp.ConsumeIntegralInRange<size_t>(0, out.size());
85
3.59k
    out.insert(pos, mid);
86
3.59k
  }
87
9.53k
  if (fdp.ConsumeBool() && !out.empty()) {
88
2.26k
    size_t i = fdp.ConsumeIntegralInRange<size_t>(0, out.size() - 1);
89
2.26k
    out[i] = static_cast<char>(fdp.ConsumeIntegral<uint8_t>());
90
2.26k
  }
91
9.53k
  return out;
92
9.53k
}
93
94
404k
static void check_href_size(const ada::url& u, std::string_view input) {
95
404k
  if (u.get_href_size() != u.get_href().size()) {
96
0
    printf(
97
0
        "get_href_size mismatch (url)\n"
98
0
        "  input: %.*s\n"
99
0
        "  size:  %zu href.size: %zu\n"
100
0
        "  href:  %s\n",
101
0
        static_cast<int>(input.size()), input.data(), u.get_href_size(),
102
0
        u.get_href().size(), u.get_href().c_str());
103
0
    abort();
104
0
  }
105
404k
}
106
107
static void check_href_size(const ada::url_aggregator& u,
108
404k
                            std::string_view input) {
109
404k
  if (u.get_href_size() != u.get_href().size()) {
110
0
    printf(
111
0
        "get_href_size mismatch (aggregator)\n"
112
0
        "  input: %.*s\n"
113
0
        "  size:  %zu href.size: %zu\n"
114
0
        "  href:  %s\n",
115
0
        static_cast<int>(input.size()), input.data(), u.get_href_size(),
116
0
        u.get_href().size(), std::string(u.get_href()).c_str());
117
0
    abort();
118
0
  }
119
404k
}
120
121
template <class Result>
122
static void check_reparse_idempotent(const Result& parsed,
123
404k
                                     std::string_view input) {
124
404k
  const std::string href = std::string(parsed.get_href());
125
404k
  auto again = ada::parse<Result>(href);
126
404k
  if (!again) {
127
0
    printf(
128
0
        "re-parse of href failed\n"
129
0
        "  input: %.*s\n"
130
0
        "  href:  %s\n",
131
0
        static_cast<int>(input.size()), input.data(), href.c_str());
132
0
    abort();
133
0
  }
134
404k
  if (std::string(again->get_href()) != href) {
135
0
    printf(
136
0
        "href not idempotent\n"
137
0
        "  input: %.*s\n"
138
0
        "  href1: %s\n"
139
0
        "  href2: %s\n",
140
0
        static_cast<int>(input.size()), input.data(), href.c_str(),
141
0
        std::string(again->get_href()).c_str());
142
0
    abort();
143
0
  }
144
404k
}
simple_absolute.cc:void check_reparse_idempotent<ada::url>(ada::url const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Line
Count
Source
123
202k
                                     std::string_view input) {
124
202k
  const std::string href = std::string(parsed.get_href());
125
202k
  auto again = ada::parse<Result>(href);
126
202k
  if (!again) {
127
0
    printf(
128
0
        "re-parse of href failed\n"
129
0
        "  input: %.*s\n"
130
0
        "  href:  %s\n",
131
0
        static_cast<int>(input.size()), input.data(), href.c_str());
132
0
    abort();
133
0
  }
134
202k
  if (std::string(again->get_href()) != href) {
135
0
    printf(
136
0
        "href not idempotent\n"
137
0
        "  input: %.*s\n"
138
0
        "  href1: %s\n"
139
0
        "  href2: %s\n",
140
0
        static_cast<int>(input.size()), input.data(), href.c_str(),
141
0
        std::string(again->get_href()).c_str());
142
0
    abort();
143
0
  }
144
202k
}
simple_absolute.cc:void check_reparse_idempotent<ada::url_aggregator>(ada::url_aggregator const&, std::__1::basic_string_view<char, std::__1::char_traits<char> >)
Line
Count
Source
123
202k
                                     std::string_view input) {
124
202k
  const std::string href = std::string(parsed.get_href());
125
202k
  auto again = ada::parse<Result>(href);
126
202k
  if (!again) {
127
0
    printf(
128
0
        "re-parse of href failed\n"
129
0
        "  input: %.*s\n"
130
0
        "  href:  %s\n",
131
0
        static_cast<int>(input.size()), input.data(), href.c_str());
132
0
    abort();
133
0
  }
134
202k
  if (std::string(again->get_href()) != href) {
135
0
    printf(
136
0
        "href not idempotent\n"
137
0
        "  input: %.*s\n"
138
0
        "  href1: %s\n"
139
0
        "  href2: %s\n",
140
0
        static_cast<int>(input.size()), input.data(), href.c_str(),
141
0
        std::string(again->get_href()).c_str());
142
0
    abort();
143
0
  }
144
202k
}
145
146
static void check_components_agree(const ada::url& u,
147
                                   const ada::url_aggregator& a,
148
202k
                                   std::string_view input) {
149
202k
  if (u.get_protocol() != a.get_protocol() ||
150
202k
      u.get_href() != std::string(a.get_href()) ||
151
202k
      std::string(u.get_hostname()) != std::string(a.get_hostname()) ||
152
202k
      std::string(u.get_pathname()) != std::string(a.get_pathname()) ||
153
202k
      std::string(u.get_search()) != std::string(a.get_search()) ||
154
202k
      std::string(u.get_hash()) != std::string(a.get_hash()) ||
155
202k
      std::string(u.get_port()) != std::string(a.get_port()) ||
156
202k
      u.get_username() != std::string(a.get_username()) ||
157
202k
      u.get_password() != std::string(a.get_password()) ||
158
202k
      std::string(u.get_host()) != std::string(a.get_host())) {
159
0
    printf(
160
0
        "url vs aggregator component mismatch\n"
161
0
        "  input: %.*s\n"
162
0
        "  url href: %s\n"
163
0
        "  agg href: %s\n",
164
0
        static_cast<int>(input.size()), input.data(), u.get_href().c_str(),
165
0
        std::string(a.get_href()).c_str());
166
0
    fflush(stdout);
167
0
    abort();
168
0
  }
169
202k
}
170
171
224k
static void fuzz_one_input(std::string_view input) {
172
224k
  auto url = ada::parse<ada::url>(input);
173
224k
  auto agg = ada::parse<ada::url_aggregator>(input);
174
175
224k
  if (url.has_value() != agg.has_value()) {
176
0
    printf(
177
0
        "parse agreement failure\n"
178
0
        "  input: %.*s\n"
179
0
        "  url: %d aggregator: %d\n",
180
0
        static_cast<int>(input.size()), input.data(), url.has_value(),
181
0
        agg.has_value());
182
0
    abort();
183
0
  }
184
185
224k
  if (!url) {
186
21.9k
    return;
187
21.9k
  }
188
189
202k
  check_components_agree(*url, *agg, input);
190
202k
  check_href_size(*url, input);
191
202k
  check_href_size(*agg, input);
192
202k
  check_reparse_idempotent(*url, input);
193
202k
  check_reparse_idempotent(*agg, input);
194
195
202k
  const std::string href = url->get_href();
196
202k
  url->set_href(href);
197
202k
  agg->set_href(href);
198
202k
  if (url->get_href() != std::string(agg->get_href())) {
199
0
    printf(
200
0
        "set_href agreement failure\n"
201
0
        "  href: %s\n",
202
0
        href.c_str());
203
0
    abort();
204
0
  }
205
202k
  check_href_size(*url, href);
206
202k
  check_href_size(*agg, href);
207
208
202k
  volatile bool valid = agg->validate();
209
202k
  (void)valid;
210
202k
}
211
212
9.53k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
213
9.53k
  FuzzedDataProvider fdp(data, size);
214
215
9.53k
  fuzz_one_input(make_candidate(fdp));
216
217
9.53k
  if (fdp.remaining_bytes() > 0) {
218
4.96k
    std::string raw = fdp.ConsumeRemainingBytesAsString();
219
4.96k
    if (raw.size() > 512) {
220
18
      raw.resize(512);
221
18
    }
222
4.96k
    if (!raw.empty() && (static_cast<unsigned char>(raw[0]) & 1u)) {
223
2.25k
      raw = std::string("https://") + raw;
224
2.71k
    } else if (!raw.empty()) {
225
2.71k
      raw = std::string("http://") + raw;
226
2.71k
    }
227
4.96k
    fuzz_one_input(raw);
228
4.96k
  }
229
230
9.53k
  static constexpr const char* kAnchors[] = {
231
9.53k
      "https://example.com",
232
9.53k
      "https://example.com/",
233
9.53k
      "https://example.com?q=1",
234
9.53k
      "https://example.com#frag",
235
9.53k
      "https://example.com/?q=1#frag",
236
9.53k
      "http://www.example.com/path/file.js",
237
9.53k
      "http://WWW.Example.COM/file.js",
238
9.53k
      "https://www.google.com/imghp?hl=en&tab=wi",
239
9.53k
      "http://192.168.0.1/x",
240
9.53k
      "http://0x7f.1/",
241
9.53k
      "https://user:pass@example.com/x",
242
9.53k
      "https://example.com:8080/x",
243
9.53k
      "https://example.com/a/./b/../c",
244
9.53k
      "https://example.com/foo/%2e%2e",
245
9.53k
      "https://xn--nxasmq6b.com/",
246
9.53k
      "https://xn--a/",
247
9.53k
      "http://foo.0xffffffff/",
248
9.53k
      "http://example.0x/",
249
9.53k
      "https://example.com:0080/x",
250
9.53k
      "http://@19%2E68.1.10.",
251
9.53k
      "http://19%2E68.1.10.",
252
9.53k
      "http://0xffffffff.",
253
9.53k
  };
254
209k
  for (const char* seed : kAnchors) {
255
209k
    fuzz_one_input(seed);
256
209k
  }
257
258
9.53k
  return 0;
259
9.53k
}