ladybird/Userland/Utilities/test-fuzz.cpp

130 lines
3.9 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2021, Ben Wiederhake <BenWiederhake.GitHub@gmx.de>
* Copyright (c) 2023, Tim Schumacher <timschumi@gmx.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/ArgsParser.h>
#include <LibCore/File.h>
#include <LibMain/Main.h>
#include <unistd.h>
2023-05-22 22:34:01 +00:00
// TODO: Look into generating this from the authoritative list of fuzzing targets in fuzzer.cmake.
#define ENUMERATE_TARGETS(T) \
2023-05-22 22:34:01 +00:00
T(ASN1) \
T(BMPLoader) \
2023-05-22 22:34:01 +00:00
T(Brotli) \
T(CSSParser) \
T(CyrillicDecoder) \
2023-07-17 21:36:08 +00:00
T(DDSLoader) \
2023-05-22 22:34:01 +00:00
T(DeflateCompression) \
T(DeflateDecompression) \
T(ELF) \
2023-05-22 22:34:01 +00:00
T(FlacLoader) \
T(Gemini) \
T(GIFLoader) \
2023-05-22 22:34:01 +00:00
T(GzipCompression) \
T(GzipDecompression) \
T(HebrewDecoder) \
T(HttpRequest) \
2023-05-22 22:34:01 +00:00
T(ICCProfile) \
T(ICOLoader) \
T(ILBMLoader) \
2023-05-22 22:34:01 +00:00
T(IMAPParser) \
T(JPEGLoader) \
T(Js) \
2023-05-22 22:34:01 +00:00
T(Latin1Decoder) \
T(Latin2Decoder) \
T(LzmaDecompression) \
T(LzmaRoundtrip) \
T(Markdown) \
2023-05-22 22:34:01 +00:00
T(MatroskaReader) \
T(MD5) \
T(MP3Loader) \
T(PBMLoader) \
2023-05-22 22:34:01 +00:00
T(PDF) \
T(PEM) \
T(PGMLoader) \
T(PNGLoader) \
2023-05-22 22:34:01 +00:00
T(Poly1305) \
T(PPMLoader) \
2023-05-22 22:34:01 +00:00
T(QOALoader) \
2021-12-21 11:11:00 +00:00
T(QOILoader) \
2023-05-22 22:34:01 +00:00
T(QuotedPrintableParser) \
T(RegexECMA262) \
2023-05-22 22:34:01 +00:00
T(RegexPosixBasic) \
T(RegexPosixExtended) \
2023-05-22 22:34:01 +00:00
T(RSAKeyParsing) \
T(SHA1) \
T(SHA256) \
T(SHA384) \
T(SHA512) \
T(Shell) \
2023-05-22 22:34:01 +00:00
T(ShellPosix) \
T(SQLParser) \
T(Tar) \
T(TGALoader) \
T(TTF) \
2023-07-17 21:33:53 +00:00
T(TinyVGLoader) \
2023-05-22 22:34:01 +00:00
T(URL) \
T(UTF16BEDecoder) \
T(VP9Decoder) \
T(WasmParser) \
T(WAVLoader) \
T(WebPLoader) \
T(WOFF) \
T(XML) \
T(Zip) \
T(ZlibDecompression)
#undef __ENUMERATE_TARGET
2022-04-01 17:58:27 +00:00
#define __ENUMERATE_TARGET(x) extern "C" int Test##x(uint8_t const*, size_t);
ENUMERATE_TARGETS(__ENUMERATE_TARGET)
#undef __ENUMERATE_TARGET
static auto parse_target_name(StringView name)
{
if (name == "list"sv) {
outln("The following targets are included:");
#undef __ENUMERATE_TARGET
#define __ENUMERATE_TARGET(x) outln(#x);
ENUMERATE_TARGETS(__ENUMERATE_TARGET)
#undef __ENUMERATE_TARGET
exit(0);
}
#undef __ENUMERATE_TARGET
#define __ENUMERATE_TARGET(x) \
if (name == #x) \
return Test##x;
ENUMERATE_TARGETS(__ENUMERATE_TARGET)
#undef __ENUMERATE_TARGET
warnln("Unknown fuzzing target \"{}\". Try \"list\" to get a full list.", name);
exit(1);
}
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
StringView type;
StringView filename;
Core::ArgsParser args_parser;
args_parser.add_positional_argument(type, "Type of fuzzing target to run (use \"list\" to list all existing)", "target-kind");
args_parser.add_positional_argument(filename, "Input file", "filename", Core::ArgsParser::Required::No);
args_parser.parse(arguments);
if (arguments.strings.size() <= 2 && arguments.strings[1] != "list"sv) {
args_parser.print_usage_terminal(stderr, arguments.strings[0]);
return 0;
}
auto fn = parse_target_name(type);
auto file = TRY(Core::File::open(filename, Core::File::OpenMode::Read));
auto input = TRY(file->read_until_eof());
return fn(input.data(), input.size());
}