Skip to content

Commit 2adfdfd

Browse files
committed
Makefile: run fuzzing corpora as normal unit tests in non-fuzzing mode.
This means we can make sure the compile and run in normal builds. Side note: various tests call common_setup(), which means we called it twice in unit testing mode, so we conditionalize those. Signed-off-by: Rusty Russell <[email protected]>
1 parent a6ea428 commit 2adfdfd

15 files changed

+104
-17
lines changed

Makefile

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,7 @@ include cln-grpc/Makefile
392392
endif
393393
include plugins/Makefile
394394
include tests/plugins/Makefile
395-
396-
ifneq ($(FUZZING),0)
397-
include tests/fuzz/Makefile
398-
endif
395+
include tests/fuzz/Makefile
399396

400397
ifneq ($V,1)
401398
MSGGEN_ARGS := -s
@@ -709,6 +706,7 @@ endif
709706
# We special case the fuzzing target binaries, as they need to link against libfuzzer,
710707
# which brings its own main().
711708
# FUZZER_LIB and LLVM_LDFLAGS are set by configure script on macOS
709+
ifneq ($(FUZZING),0)
712710
ifeq ($(OS),Darwin)
713711
ifneq ($(FUZZER_LIB),)
714712
FUZZ_LDFLAGS = $(FUZZER_LIB) $(LLVM_LDFLAGS)
@@ -718,9 +716,10 @@ endif
718716
else
719717
FUZZ_LDFLAGS = -fsanitize=fuzzer
720718
endif
719+
endif
721720

722721
$(ALL_FUZZ_TARGETS):
723-
@$(call VERBOSE, "ld $@", $(LINK.o) $(filter-out %.a,$^) $(LOADLIBES) $(EXTERNAL_LDLIBS) $(LDLIBS) libccan.a $(FUZZ_LDFLAGS) -o $@)
722+
@$(call VERBOSE, "ld $@", $(LINK.o) $(filter-out %.a,$^) libcommon.a libccan.a $(LOADLIBES) $(EXTERNAL_LDLIBS) $(LDLIBS) $(FUZZ_LDFLAGS) -o $@)
724723
ifeq ($(OS),Darwin)
725724
@$(call VERBOSE, "dsymutil $@", dsymutil $@)
726725
endif
@@ -841,6 +840,10 @@ update-mocks/%: % $(ALL_GEN_HEADERS) $(ALL_GEN_SOURCES)
841840
unittest/%: % bolt-precheck
842841
BOLTDIR=$(LOCAL_BOLTDIR) $(VG) $(VG_TEST_ARGS) $* > /dev/null
843842

843+
# FIXME: we don't do leak detection on fuzz tests, since they don't have a cleanup function.
844+
fuzzunittest/%: % bolt-precheck
845+
BOLTDIR=$(LOCAL_BOLTDIR) $(VG) $* > /dev/null
846+
844847
# Commands
845848
MKDIR_P = mkdir -p
846849
INSTALL = install

common/peer_failed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "config.h"
44
#include <ccan/compiler/compiler.h>
55
#include <ccan/short_types/short_types.h>
6+
#include <ccan/take/take.h>
67

78
struct channel_id;
89
struct per_peer_state;

tests/fuzz/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,13 @@ FUZZ_TARGETS_SRC := $(wildcard tests/fuzz/fuzz-*.c)
1313
FUZZ_TARGETS_OBJS := $(FUZZ_TARGETS_SRC:.c=.o)
1414
FUZZ_TARGETS_BIN := $(FUZZ_TARGETS_SRC:.c=)
1515

16-
$(FUZZ_TARGETS_OBJS): $(COMMON_HEADERS) $(WIRE_HEADERS) $(COMMON_SRC)
16+
$(FUZZ_TARGETS_OBJS): $(COMMON_HEADERS) $(WIRE_HEADERS) $(COMMON_SRC) tests/fuzz/libfuzz.h
1717
$(FUZZ_TARGETS_BIN): $(LIBFUZZ_OBJS) libcommon.a
1818

1919
ALL_C_SOURCES += $(FUZZ_TARGETS_SRC) $(LIBFUZZ_SRC)
2020
ALL_FUZZ_TARGETS += $(FUZZ_TARGETS_BIN)
21+
22+
# In non-fuzzing builds, these become normal tests.
23+
ifneq ($(FUZZING),1)
24+
check-units: $(FUZZ_TARGETS_BIN:%=fuzzunittest/%)
25+
endif

tests/fuzz/bolt12.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,11 @@ size_t LLVMFuzzerCustomCrossOver(const u8 *data1, size_t size1, const u8 *data2,
144144
return encoded_size;
145145
}
146146

147-
void init(int *argc, char ***argv) { common_setup("fuzzer"); }
147+
void init(int *argc, char ***argv)
148+
{
149+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
150+
if (!tmpctx)
151+
common_setup("fuzzer");
152+
}
148153

149154
#endif /* LIGHTNING_TESTS_FUZZ_BOLT12_H */

tests/fuzz/connectd_handshake.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ void init(int *argc, char ***argv)
7474
assert(devnull >= 0);
7575
status_setup_sync(devnull);
7676

77-
common_setup("fuzzer");
77+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
78+
if (!tmpctx)
79+
common_setup("fuzzer");
7880

7981
/* These keys are copied from BOLT 8 test vectors, though we use them in
8082
* a different setting.

tests/fuzz/fuzz-addr.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
void init(int *argc, char ***argv)
1010
{
1111
chainparams = chainparams_for_network("bitcoin");
12-
common_setup("fuzzer");
12+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
13+
if (!tmpctx)
14+
common_setup("fuzzer");
1315
}
1416

1517
void run(const uint8_t *data, size_t size)

tests/fuzz/fuzz-bolt11.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@ size_t LLVMFuzzerCustomCrossOver(const u8 *in1, size_t in1_size, const u8 *in2,
1818
size_t in2_size, u8 *out, size_t max_out_size,
1919
unsigned seed);
2020

21-
void init(int *argc, char ***argv) { common_setup("fuzzer"); }
21+
void init(int *argc, char ***argv)
22+
{
23+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
24+
if (!tmpctx)
25+
common_setup("fuzzer");
26+
}
2227

2328
// Encodes a dummy bolt11 invoice into `fuzz_data` and returns the size of the
2429
// encoded string.

tests/fuzz/fuzz-bolt12-bech32-decode.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
/* Include bolt12.c directly, to gain access to string_to_data(). */
99
#include "../../common/bolt12.c"
1010

11-
void init(int *argc, char ***argv) { common_setup("fuzzer"); }
11+
void init(int *argc, char ***argv)
12+
{
13+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
14+
if (!tmpctx)
15+
common_setup("fuzzer");
16+
}
1217

1318
void run(const u8 *data, size_t size)
1419
{

tests/fuzz/fuzz-channel_id.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010

1111
void init(int *argc, char ***argv)
1212
{
13-
common_setup("fuzzer");
13+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
14+
if (!tmpctx)
15+
common_setup("fuzzer");
1416
}
1517

1618
void run(const uint8_t *data, size_t size)

tests/fuzz/fuzz-close_tx.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
void init(int *argc, char ***argv)
1414
{
15-
common_setup("fuzzer");
15+
/* Don't call this if we're in unit-test mode, as libfuzz.c does it */
16+
if (!tmpctx)
17+
common_setup("fuzzer");
1618
chainparams = chainparams_for_network("bitcoin");
1719
}
1820

0 commit comments

Comments
 (0)