-
Notifications
You must be signed in to change notification settings - Fork 6
/
fuzzy.sh
82 lines (66 loc) · 1.59 KB
/
fuzzy.sh
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
#!/bin/sh
set -eu
ROOT_DIR="fuzzer"
LANG=$1
TIME=$2
CPP=$3
# if scanner = scanner.cc then XFLAG = c++ else XFLAG = c
if [ "$CPP" = "cpp" ]; then
SCANNER="scanner.cc"
XFLAG="c++"
else
SCANNER="scanner.c"
XFLAG="c"
fi
shift 3
export PATH="/root/.cargo/bin:$PATH"
export CFLAGS="$(pkg-config --cflags --libs tree-sitter) -O0 -g -Wall"
JQ_FILTER='.. | if .type? == "STRING" or (.type? == "ALIAS" and .named? == false) then .value else null end'
build_dict() {
jq "$JQ_FILTER" <src/grammar.json |
grep -v "\\\\" | grep -v null >"$ROOT_DIR/dict"
}
build_fuzzer() {
cat <<END | clang -fsanitize=fuzzer,address $CFLAGS -lstdc++ -g -x $XFLAG - src/$SCANNER src/parser.c $@ -o $ROOT_DIR/fuzzer
#include <stdio.h>
#include <stdlib.h>
#include <tree_sitter/api.h>
#ifdef __cplusplus
extern "C"
#endif
TSLanguage *tree_sitter_$LANG();
#ifdef __cplusplus
extern "C"
#endif
int LLVMFuzzerTestOneInput(const uint8_t * data, const size_t len) {
// Create a parser.
TSParser *parser = ts_parser_new();
// Set the parser's language.
ts_parser_set_language(parser, tree_sitter_$LANG());
// Build a syntax tree based on source code stored in a string.
TSTree *tree = ts_parser_parse_string(
parser,
NULL,
(const char *)data,
len
);
// Free all of the heap-allocated memory.
ts_tree_delete(tree);
ts_parser_delete(parser);
return 0;
}
END
}
generate_fuzzer() {
tree-sitter generate
}
makedirs() {
mkdir -p "$ROOT_DIR"
mkdir -p "$ROOT_DIR/out"
}
makedirs
generate_fuzzer
build_dict
build_fuzzer $@
cd "$ROOT_DIR"
./fuzzer -detect_leaks=0 -dict=dict -timeout=2 -max_total_time=$TIME out/