Skip to content

Instantly share code, notes, and snippets.

@lemire
Last active May 16, 2024 01:40
Show Gist options
  • Save lemire/f501b3b2bb8c33673de4f0a0674a6112 to your computer and use it in GitHub Desktop.
Save lemire/f501b3b2bb8c33673de4f0a0674a6112 to your computer and use it in GitHub Desktop.
ada fuzz
#include <limits>
#include "fuzzer/FuzzedDataProvider.h" // see https://raw.githubusercontent.com/llvm/llvm-project/main/compiler-rt/include/fuzzer/FuzzedDataProvider.h
#include <memory>
#include <string>
// enables if needed
//#define ADA_LOGGING 1
#define ADA_DEVELOPMENT_CHECKS 1
#include "ada.cpp"
#include "ada.h"
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
FuzzedDataProvider fdp(data, size);
std::string source = fdp.ConsumeRandomLengthString(256);
std::cout << "source: " << source << std::endl;
std::string base_source = fdp.ConsumeRandomLengthString(256);
std::cout << "base_source: " << base_source << std::endl;
/**
* ada::parse<ada::url>
*/
auto out_url = ada::parse<ada::url>(source);
if (out_url) {
std::cout << "out_url: " << out_url->to_string() << std::endl;
std::string input = fdp.ConsumeRandomLengthString(256);
out_url->set_protocol(input);
out_url->set_username(input);
out_url->set_password(input);
out_url->set_hostname(input);
out_url->set_host(input);
out_url->set_pathname(input);
out_url->set_search(input);
out_url->set_hash(input);
out_url->set_port(input);
// volatile forces the compiler to store the results without undue
// optimizations
volatile size_t length = 0;
// 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();
out_url->to_string();
}
/**
* ada::parse<ada::url_aggregator>
*/
auto out_aggregator = ada::parse<ada::url_aggregator>(source);
if (out_aggregator) {
std::cout << "out_aggregator: " << out_aggregator->to_string() << std::endl;
std::string input = fdp.ConsumeRandomLengthString(256);
out_aggregator->set_protocol(input);
out_aggregator->set_username(input);
out_aggregator->set_password(input);
out_aggregator->set_hostname(input);
out_aggregator->set_host(input);
out_aggregator->set_pathname(input);
out_aggregator->set_search(input);
out_aggregator->set_hash(input);
out_aggregator->set_port(input);
// volatile forces the compiler to store the results without undue
// optimizations
volatile size_t length = 0;
// 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();
// to_string() and to_diagram()
std::cout << out_aggregator->to_string() << std::endl;
std::cout << out_aggregator->to_diagram() << std::endl;
// clear methods
out_aggregator->clear_port();
out_aggregator->clear_search();
out_aggregator->clear_hash();
}
/**
* Node.js specific
*/
ada::href_from_file(source);
return 0;
}
int main() {
const uint8_t data[] = "blob:/?\\x\\E\\|//..";
size_t size = strlen((const char*)data);
LLVMFuzzerTestOneInput(data, size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment