Add libFuzzer support
This adds support for fuzzing tree-sitter grammars with libFuzzer. This currently only works on Linux because of linking issues on macOS. Breifly, the AddressSanitizer library is dynamically linked into the fuzzer binary and cannot be found at runtime if built with a compiler that wasn't provided by Xcode(?). The runtime library is statically linked on Linux so this isn't a problem.
This commit is contained in:
parent
69500c9dd7
commit
798ef5e4dc
8 changed files with 205 additions and 0 deletions
27
test/fuzz/fuzzer.cc
Normal file
27
test/fuzz/fuzzer.cc
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#include <string.h>
|
||||
#include "tree_sitter/runtime.h"
|
||||
|
||||
void test_log(void *payload, TSLogType type, const char *string) { }
|
||||
|
||||
TSLogger logger = {
|
||||
.log = test_log,
|
||||
};
|
||||
|
||||
extern "C" const TSLanguage *TSLANG();
|
||||
|
||||
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||
const char *str = reinterpret_cast<const char *>(data);
|
||||
|
||||
TSDocument *document = ts_document_new();
|
||||
ts_document_set_language(document, TSLANG());
|
||||
ts_document_set_input_string_with_length(document, str, size);
|
||||
|
||||
TSParseOptions options = {};
|
||||
options.halt_on_error = false;
|
||||
ts_document_parse_with_options(document, options);
|
||||
|
||||
TSNode root_node = ts_document_root_node(document);
|
||||
ts_document_free(document);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue