1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
#include <fuzzer/FuzzedDataProvider.h>
#include <cstdio>
#include <memory>
#include <string>
#include "ada.cpp"
#include "ada.h"
bool is_valid_utf8_string(const char *buf, size_t len) {
const uint8_t *data = reinterpret_cast<const uint8_t *>(buf);
uint64_t pos = 0;
uint32_t code_point = 0;
while (pos < len) {
uint64_t next_pos = pos + 16;
if (next_pos <= len) { // if it is safe to read 16 more bytes, check that
// they are ascii
uint64_t v1;
std::memcpy(&v1, data + pos, sizeof(uint64_t));
uint64_t v2;
std::memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
uint64_t v{v1 | v2};
if ((v & 0x8080808080808080) == 0) {
pos = next_pos;
continue;
}
}
unsigned char byte = data[pos];
while (byte < 0b10000000) {
if (++pos == len) {
return true;
}
byte = data[pos];
}
if ((byte & 0b11100000) == 0b11000000) {
next_pos = pos + 2;
if (next_pos > len) {
return false;
}
if ((data[pos + 1] & 0b11000000) != 0b10000000) {
return false;
}
code_point = (byte & 0b00011111) << 6 | (data[pos + 1] & 0b00111111);
if ((code_point < 0x80) || (0x7ff < code_point)) {
return false;
}
} else if ((byte & 0b11110000) == 0b11100000) {
next_pos = pos + 3;
if (next_pos > len) {
return false;
}
if ((data[pos + 1] & 0b11000000) != 0b10000000) {
return false;
}
if ((data[pos + 2] & 0b11000000) != 0b10000000) {
return false;
}
code_point = (byte & 0b00001111) << 12 |
(data[pos + 1] & 0b00111111) << 6 |
(data[pos + 2] & 0b00111111);
if ((code_point < 0x800) || (0xffff < code_point) ||
(0xd7ff < code_point && code_point < 0xe000)) {
return false;
}
} else if ((byte & 0b11111000) == 0b11110000) { // 0b11110000
next_pos = pos + 4;
if (next_pos > len) {
return false;
}
if ((data[pos + 1] & 0b11000000) != 0b10000000) {
return false;
}
if ((data[pos + 2] & 0b11000000) != 0b10000000) {
return false;
}
if ((data[pos + 3] & 0b11000000) != 0b10000000) {
return false;
}
code_point =
(byte & 0b00000111) << 18 | (data[pos + 1] & 0b00111111) << 12 |
(data[pos + 2] & 0b00111111) << 6 | (data[pos + 3] & 0b00111111);
if (code_point <= 0xffff || 0x10ffff < code_point) {
return false;
}
} else {
return false;
}
pos = next_pos;
}
return true;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fdp(data, size);
std::string source = fdp.ConsumeRandomLengthString(256);
// volatile forces the compiler to store the results without undue
// optimizations
volatile size_t length = 0;
auto parse_url = ada::parse<ada::url>(source);
auto parse_url_aggregator = ada::parse<ada::url_aggregator>(source);
if (is_valid_utf8_string(source.data(), source.length())) {
if (parse_url.has_value() ^ parse_url_aggregator.has_value()) {
printf("Source used to parse: %s", source.c_str());
abort();
}
}
if (parse_url) {
length += parse_url->get_href().size();
length += parse_url->get_origin().size();
}
if (parse_url_aggregator) {
length += parse_url_aggregator->get_href().size();
length += parse_url_aggregator->get_origin().size();
volatile bool is_parse_url_aggregator_output_valid = false;
is_parse_url_aggregator_output_valid = parse_url_aggregator->validate();
assert(parse_url->get_protocol() == parse_url_aggregator->get_protocol());
assert(parse_url->get_href() == parse_url_aggregator->get_href());
parse_url->set_href(source);
parse_url_aggregator->set_href(source);
assert(parse_url->get_href() == parse_url_aggregator->get_href());
}
/**
* ada::parse<ada::url>
*/
auto out_url = ada::parse<ada::url>("https://www.ada-url.com");
if (out_url) {
out_url->set_protocol(source);
out_url->set_username(source);
out_url->set_password(source);
out_url->set_hostname(source);
out_url->set_host(source);
out_url->set_pathname(source);
out_url->set_search(source);
out_url->set_hash(source);
out_url->set_port(source);
// getters
length += out_url->get_protocol().size();
length += out_url->get_username().size();
length += out_url->get_password().size();
length += out_url->get_hostname().size();
length += out_url->get_host().size();
length += out_url->get_pathname().size();
length += out_url->get_search().size();
length += out_url->get_hash().size();
length += out_url->get_origin().size();
length += out_url->get_port().size();
length += out_url->to_string().size();
}
/**
* ada::parse<ada::url_aggregator>
*/
auto out_aggregator =
ada::parse<ada::url_aggregator>("https://www.ada-url.com");
if (out_aggregator) {
out_aggregator->set_protocol(source);
out_aggregator->set_username(source);
out_aggregator->set_password(source);
out_aggregator->set_hostname(source);
out_aggregator->set_host(source);
out_aggregator->set_pathname(source);
out_aggregator->set_search(source);
out_aggregator->set_hash(source);
out_aggregator->set_port(source);
// getters
length += out_aggregator->get_protocol().size();
length += out_aggregator->get_username().size();
length += out_aggregator->get_password().size();
length += out_aggregator->get_hostname().size();
length += out_aggregator->get_host().size();
length += out_aggregator->get_pathname().size();
length += out_aggregator->get_search().size();
length += out_aggregator->get_hash().size();
length += out_aggregator->get_origin().size();
length += out_aggregator->get_port().size();
length += out_aggregator->to_string().size();
volatile bool is_output_valid = false;
is_output_valid = out_aggregator->validate();
// Printing due to dead-code elimination
printf("diagram %s\n", out_aggregator->to_diagram().c_str());
// clear methods
out_aggregator->clear_port();
out_aggregator->clear_search();
out_aggregator->clear_hash();
}
/**
* Node.js specific
*/
length += ada::href_from_file(source).size();
/**
* Others
*/
bool is_valid = ada::checkers::verify_dns_length(source);
// Only used for avoiding dead-code elimination
if (is_valid) {
printf("dns length is valid\n");
}
// Only used for avoiding dead-code elimination
printf("length of url is %d\n", length);
return 0;
} // extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|