Skip to content

Commit

Permalink
fix(fuzz): fix compilation of fuzzers
Browse files Browse the repository at this point in the history
* Fix misspellings
* Don't compile with -fsanitizer=fuzzer to avoid PCH issues due to
  different compilation flags
* Fix code using older APIs
  • Loading branch information
strager committed Dec 29, 2023
1 parent 1c4a8bc commit c96f1a6
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 29 deletions.
2 changes: 1 addition & 1 deletion fuzz/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ option(QUICK_LINT_JS_ENABLE_LLVM_LIBFUZZER_TESTS "" OFF)
if (QUICK_LINT_JS_ENABLE_LLVM_LIBFUZZER_TESTS)
function (quick_lint_js_add_fuzz_executable TARGET)
quick_lint_js_add_executable("${TARGET}" ${ARGN})
target_compile_options("${TARGET}" PRIVATE -fsanitize=fuzzer)
target_compile_options("${TARGET}" PRIVATE -fsanitize=fuzzer-no-link)
target_link_libraries("${TARGET}" PRIVATE -fsanitize=fuzzer)
endfunction ()

Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz-configuration-json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
using namespace quick_lint_js;

Padded_String json(String8(reinterpret_cast<const Char8 *>(data), size));
configuration c;
c.load_from_json(&json, &null_diag_reporter::instance);
Configuration c;
c.load_from_json(&json, &Null_Diag_Reporter::instance);

return 0;
}
Expand Down
4 changes: 2 additions & 2 deletions fuzz/fuzz-lex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ extern "C" {
int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
quick_lint_js::Padded_String source(quick_lint_js::String8(
reinterpret_cast<const quick_lint_js::Char8 *>(data), size));
quick_lint_js::lexer l(&source, &quick_lint_js::null_diag_reporter::instance);
while (l.peek().type != quick_lint_js::token_type::end_of_file) {
quick_lint_js::Lexer l(&source, &quick_lint_js::Null_Diag_Reporter::instance);
while (l.peek().type != quick_lint_js::Token_Type::end_of_file) {
l.skip();
}

Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz-lsp-message-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
using namespace quick_lint_js;

namespace {
class fuzz_lsp_message_parser
: public lsp_message_parser<fuzz_lsp_message_parser> {
class Fuzz_LSP_Message_Parser
: public LSP_Message_Parser<Fuzz_LSP_Message_Parser> {
public:
void message_parsed([[maybe_unused]] String8_View message_content) {
// TODO(strager): Ensure message_content is valid.
Expand All @@ -21,7 +21,7 @@ class fuzz_lsp_message_parser

extern "C" {
int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
fuzz_lsp_message_parser parser;
Fuzz_LSP_Message_Parser parser;
std::size_t i = 0;
auto size_remaining = [&]() -> std::size_t { return size - i; };
for (;;) {
Expand Down
10 changes: 5 additions & 5 deletions fuzz/fuzz-lsp-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

namespace quick_lint_js {
namespace {
class null_lsp_endpoint_remote {
class Null_LSP_Endpoint_Remote {
public:
void send_message(const Byte_Buffer&) {}
};

class null_configuration_filesystem : public Configuration_Filesystem {
class Null_Configuration_Filesystem : public Configuration_Filesystem {
public:
Result<Canonical_Path_Result, Canonicalize_Path_IO_Error> canonicalize_path(
const std::string& path) override {
Expand Down Expand Up @@ -46,9 +46,9 @@ int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
using namespace quick_lint_js;

Null_Configuration_Filesystem fs;
LSP_Javascript_Linter linter;
LSP_Endpoint<Linting_LSP_Server_Handler, Null_LSP_Endpoint_Remote> server(
std::forward_as_tuple(&fs, &linter), std::forward_as_tuple());
LSP_JavaScript_Linter linter;
Linting_LSP_Server_Handler handler(&fs, &linter);
LSP_JSON_RPC_Message_Parser server(&handler);

std::size_t i = 0;
auto size_remaining = [&]() -> std::size_t { return size - i; };
Expand Down
8 changes: 5 additions & 3 deletions fuzz/fuzz-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#include <cstddef>
#include <cstring>
#include <quick-lint-js/cli/options.h>
#include <quick-lint-js/util/narrow-cast.h>
#include <quick-lint-js/container/monotonic-allocator.h>
#include <quick-lint-js/util/cast.h>
#include <vector>

extern "C" {
Expand All @@ -26,8 +27,9 @@ int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
}
QLJS_ASSERT(!argv.empty());

quick_lint_js::options o = quick_lint_js::parse_options(
quick_lint_js::narrow_cast<int>(argv.size()), argv.data());
quick_lint_js::Monotonic_Allocator allocator("fuzz-options");
quick_lint_js::Options o = quick_lint_js::parse_options(
quick_lint_js::narrow_cast<int>(argv.size()), argv.data(), &allocator);
static_cast<void>(o);

return 0;
Expand Down
15 changes: 7 additions & 8 deletions fuzz/fuzz-parse-lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@

#include <cstddef>
#include <cstdint>
#include <quick-lint-js/container/padded-string.h>
#include <quick-lint-js/diag/diag-reporter.h>
#include <quick-lint-js/fe/parse.h>
#include <quick-lint-js/fe/variable-analyzer.h>
#include <quick-lint-js/fe/global-declared-variable-set.h>
#include <quick-lint-js/fe/linter.h>
#include <quick-lint-js/port/char8.h>

extern "C" {
int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
quick_lint_js::Padded_String source(quick_lint_js::String8(
reinterpret_cast<const quick_lint_js::Char8 *>(data), size));
quick_lint_js::parser p(&source,
&quick_lint_js::null_diag_reporter::instance);
quick_lint_js::variable_analyzer l;
[[maybe_unused]] bool ok =
p.parse_and_visit_module_catching_fatal_parse_errors(l);

quick_lint_js::Global_Declared_Variable_Set globals;
quick_lint_js::parse_and_lint(&source,
quick_lint_js::Null_Diag_Reporter::instance,
globals, quick_lint_js::Linter_Options());
return 0;
}
}
Expand Down
6 changes: 3 additions & 3 deletions fuzz/fuzz-parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ extern "C" {
int LLVMFuzzerTestOneInput(const std::uint8_t *data, std::size_t size) {
quick_lint_js::Padded_String source(quick_lint_js::String8(
reinterpret_cast<const quick_lint_js::Char8 *>(data), size));
quick_lint_js::parser p(&source,
&quick_lint_js::null_diag_reporter::instance);
quick_lint_js::null_visitor visitor;
quick_lint_js::Parser p(&source, &quick_lint_js::Null_Diag_Reporter::instance,
quick_lint_js::Parser_Options());
quick_lint_js::Null_Visitor visitor;
[[maybe_unused]] bool ok =
p.parse_and_visit_module_catching_fatal_parse_errors(visitor);

Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz-utf-8-decode-encode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {

const Char8* c = input.data();
while (c != input.null_terminator()) {
decode_utf_8_result r =
Decode_UTF8_Result r =
decode_utf_8(Padded_String_View(c, input.null_terminator()));

if (r.ok) {
Expand Down
2 changes: 1 addition & 1 deletion fuzz/fuzz-utf-8-lsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ int LLVMFuzzerTestOneInput(const std::uint8_t* data, std::size_t size) {
bool ok = true;
if (counted_characters == character_count - 1) {
// Code unit to count is possibly the second of a UTF-16 surrogate pair.
decode_utf_8_result result =
Decode_UTF8_Result result =
decode_utf_8(Padded_String_View(c, input_end));
bool character_needs_utf_16_surrogate_pair =
result.ok && result.code_point >= 0x10000;
Expand Down

0 comments on commit c96f1a6

Please sign in to comment.