fuzzing_include_am.py 3.89 KB
Newer Older
1
#!/usr/bin/python
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

FUZZERS = """
	consensus
	descriptor
	diff
	diff-apply
	extrainfo
	hsdescv2
	http
	iptsv2
	microdesc
	vrs
"""


PREAMBLE = r"""
FUZZING_CPPFLAGS = \
	$(src_test_AM_CPPFLAGS) $(TEST_CPPFLAGS)
FUZZING_CFLAGS = \
	$(AM_CFLAGS) $(TEST_CFLAGS)
FUZZING_LDFLAG = \
	@TOR_LDFLAGS_zlib@ @TOR_LDFLAGS_openssl@ @TOR_LDFLAGS_libevent@
FUZZING_LIBS = \
	src/or/libtor-testing.a \
	src/common/libor-crypto-testing.a \
	$(LIBKECCAK_TINY) \
	$(LIBDONNA) \
	src/common/libor-testing.a \
	src/common/libor-ctime-testing.a \
	src/common/libor-event-testing.a \
	src/trunnel/libor-trunnel-testing.a \
33
	$(rust_ldadd) \
34
35
	@TOR_ZLIB_LIBS@ @TOR_LIB_MATH@ \
	@TOR_LIBEVENT_LIBS@ \
36
37
	@TOR_OPENSSL_LIBS@ @TOR_LIB_WS32@ @TOR_LIB_GDI@ @TOR_LIB_USERENV@ \
	@CURVE25519_LIBS@ \
38
39
	@TOR_SYSTEMD_LIBS@ \
	@TOR_LZMA_LIBS@ \
40
	@TOR_ZSTD_LIBS@
41
42
43
44
45
46
47
48
49
50
51
52
53
54

oss-fuzz-prereqs: \
	src/or/libtor-testing.a \
	src/common/libor-crypto-testing.a \
	$(LIBKECCAK_TINY) \
	$(LIBDONNA) \
	src/common/libor-testing.a \
	src/common/libor-ctime-testing.a \
	src/common/libor-event-testing.a \
	src/trunnel/libor-trunnel-testing.a

noinst_HEADERS += \
	src/test/fuzz/fuzzing.h

55
LIBFUZZER = -lFuzzer
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
LIBFUZZER_CPPFLAGS = $(FUZZING_CPPFLAGS) -DLLVM_FUZZ
LIBFUZZER_CFLAGS = $(FUZZING_CFLAGS)
LIBFUZZER_LDFLAG = $(FUZZING_LDFLAG)
LIBFUZZER_LIBS = $(FUZZING_LIBS) $(LIBFUZZER) -lstdc++

LIBOSS_FUZZ_CPPFLAGS = $(FUZZING_CPPFLAGS) -DLLVM_FUZZ
LIBOSS_FUZZ_CFLAGS = $(FUZZING_CFLAGS)
"""

POSTAMBLE = r"""
noinst_PROGRAMS += $(FUZZERS) $(LIBFUZZER_FUZZERS)
noinst_LIBRARIES += $(OSS_FUZZ_FUZZERS)
oss-fuzz-fuzzers:  oss-fuzz-prereqs $(OSS_FUZZ_FUZZERS)
fuzzers: $(FUZZERS) $(LIBFUZZER_FUZZERS)

test-fuzz-corpora: $(FUZZERS)
	$(top_srcdir)/src/test/fuzz_static_testcases.sh
"""

########### No user serviceable parts will follow.

PREAMBLE = PREAMBLE.strip()
POSTAMBLE = POSTAMBLE.strip()  # If I use it, it's a word!
FUZZERS = FUZZERS.split()
FUZZERS.sort()

WARNING = """
# This file was generated by fuzzing_include_am.py; do not hand-edit unless
# you enjoy having your changes erased.
""".strip()

print(WARNING)

print(PREAMBLE)

print("\n# ===== AFL fuzzers")

def get_id_name(s):
    return s.replace("-", "_")

for fuzzer in FUZZERS:
    idname = get_id_name(fuzzer)
    print("""\
src_test_fuzz_fuzz_{name}_SOURCES = \\
	src/test/fuzz/fuzzing_common.c \\
	src/test/fuzz/fuzz_{name}.c
src_test_fuzz_fuzz_{name}_CPPFLAGS = $(FUZZING_CPPFLAGS)
src_test_fuzz_fuzz_{name}_CFLAGS = $(FUZZING_CFLAGS)
src_test_fuzz_fuzz_{name}_LDFLAGS = $(FUZZING_LDFLAG)
src_test_fuzz_fuzz_{name}_LDADD = $(FUZZING_LIBS)
""".format(name=idname))

print("FUZZERS = \\")
print(" \\\n".join("\tsrc/test/fuzz/fuzz-{name}".format(name=fuzzer)
                   for fuzzer in FUZZERS))

print("\n# ===== libfuzzer")
print("\nif LIBFUZZER_ENABLED")

for fuzzer in FUZZERS:
    idname = get_id_name(fuzzer)
    print("""\
src_test_fuzz_lf_fuzz_{name}_SOURCES = \\
	$(src_test_fuzz_fuzz_{name}_SOURCES)
src_test_fuzz_lf_fuzz_{name}_CPPFLAGS = $(LIBFUZZER_CPPFLAGS)
src_test_fuzz_lf_fuzz_{name}_CFLAGS = $(LIBFUZZER_CFLAGS)
src_test_fuzz_lf_fuzz_{name}_LDFLAGS = $(LIBFUZZER_LDFLAG)
src_test_fuzz_lf_fuzz_{name}_LDADD = $(LIBFUZZER_LIBS)
""".format(name=idname))

print("LIBFUZZER_FUZZERS = \\")
print(" \\\n".join("\tsrc/test/fuzz/lf-fuzz-{name}".format(name=fuzzer)
                   for fuzzer in FUZZERS))

print("""
else
LIBFUZZER_FUZZERS =
endif""")

print("\n# ===== oss-fuzz\n")
print("if OSS_FUZZ_ENABLED")

for fuzzer in FUZZERS:
    idname = get_id_name(fuzzer)
    print("""\
src_test_fuzz_liboss_fuzz_{name}_a_SOURCES = \\
	$(src_test_fuzz_fuzz_{name}_SOURCES)
src_test_fuzz_liboss_fuzz_{name}_a_CPPFLAGS = $(LIBOSS_FUZZ_CPPFLAGS)
src_test_fuzz_liboss_fuzz_{name}_a_CFLAGS = $(LIBOSS_FUZZ_CFLAGS)
""".format(name=idname))

print("OSS_FUZZ_FUZZERS = \\")
print(" \\\n".join("\tsrc/test/fuzz/liboss-fuzz-{name}.a".format(name=fuzzer)
                   for fuzzer in FUZZERS))

print("""
else
OSS_FUZZ_FUZZERS =
endif""")

print("")

print(POSTAMBLE)