Skip to content

Commit 8578d6c

Browse files
sangbidarustyrussell
authored andcommitted
makefile: enable fuzzing support on macOS
Enable fuzzing support on macOS by configuring the build system to use Homebrew LLVM toolchain and handle macOS-specific linking requirements. The `make check-fuzz` command was failing on macOS because: - System clang lacks fuzzer runtime library support - Linking issues with fuzzer targets - Test script attempts to execute debug symbol files This PR adds macOS-specific configuration to: - Use Homebrew LLVM toolchain for fuzzer support - Explicitly link fuzzer libraries - Exclude `.dSYM` directories from test discovery All 76 fuzzer targets now build and pass on macOS.
1 parent e522aed commit 8578d6c

File tree

3 files changed

+73
-3
lines changed

3 files changed

+73
-3
lines changed

Makefile

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ BOLTVERSION := $(DEFAULT_BOLTVERSION)
3232

3333
-include config.vars
3434

35+
# Use Homebrew LLVM toolchain for fuzzing support on macOS
36+
ifeq ($(OS),Darwin)
37+
export PATH := /opt/homebrew/opt/llvm/bin:$(PATH)
38+
export DYLD_LIBRARY_PATH := /opt/homebrew/opt/llvm/lib:$(DYLD_LIBRARY_PATH)
39+
endif
40+
41+
# Define EXTERNAL_LDLIBS for linking external libraries
42+
EXTERNAL_LDLIBS=$(SODIUM_LDLIBS) $(SQLITE3_LDLIBS) $(POSTGRES_LDLIBS)
43+
3544
SORT=LC_ALL=C sort
3645

3746

@@ -254,8 +263,8 @@ man8dir = $(mandir)/man8
254263
ifeq ("$(OS)-$(ARCH)", "Darwin-arm64")
255264
CPATH := /opt/homebrew/include
256265
LIBRARY_PATH := /opt/homebrew/lib
257-
LDFLAGS := -L/opt/homebrew/opt/sqlite/lib
258-
CPPFLAGS := -I/opt/homebrew/opt/sqlite/include
266+
LDFLAGS := -L/opt/homebrew/opt/sqlite/lib -L/opt/homebrew/opt/openssl@3/lib
267+
CPPFLAGS := -I/opt/homebrew/opt/sqlite/include -I/opt/homebrew/opt/openssl@3/include
259268
PKG_CONFIG_PATH=/opt/homebrew/opt/sqlite/lib/pkgconfig
260269
else
261270
CPATH := /usr/local/include
@@ -698,7 +707,17 @@ endif
698707

699708
# We special case the fuzzing target binaries, as they need to link against libfuzzer,
700709
# which brings its own main().
710+
# FUZZER_LIB and LLVM_LDFLAGS are set by configure script on macOS
711+
ifeq ($(OS),Darwin)
712+
ifneq ($(FUZZER_LIB),)
713+
FUZZ_LDFLAGS = $(FUZZER_LIB) $(LLVM_LDFLAGS)
714+
else
701715
FUZZ_LDFLAGS = -fsanitize=fuzzer
716+
endif
717+
else
718+
FUZZ_LDFLAGS = -fsanitize=fuzzer
719+
endif
720+
702721
$(ALL_FUZZ_TARGETS):
703722
@$(call VERBOSE, "ld $@", $(LINK.o) $(filter-out %.a,$^) $(LOADLIBES) $(EXTERNAL_LDLIBS) $(LDLIBS) libccan.a $(FUZZ_LDFLAGS) -o $@)
704723
ifeq ($(OS),Darwin)

configure

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ set_defaults()
170170
if [ "$(uname -s)" = "Darwin" ]; then
171171
# Always override to avoid DWARF 5
172172
CDEBUGFLAGS="-std=gnu11 -g -gdwarf-4 -fno-standalone-debug -fstack-protector-strong"
173+
# Set SDKROOT for macOS
174+
SDKROOT="$(xcrun --sdk macosx --show-sdk-path)"
173175

174176
# Optional: confirm dsymutil is available
175177
if ! command -v dsymutil >/dev/null 2>&1; then
@@ -538,6 +540,45 @@ if ! check_command 'jq' jq; then
538540
exit 1
539541
fi
540542

543+
# Detect LLVM paths for fuzzing on macOS
544+
LLVM_LIBDIR=""
545+
FUZZER_LIB=""
546+
LLVM_LDFLAGS=""
547+
if [ "$OS" = "Darwin" ] && [ "$FUZZING" = "1" ]; then
548+
echo -n "Detecting LLVM paths for fuzzing... "
549+
550+
# Try to find LLVM using Homebrew
551+
if command -v brew >/dev/null 2>&1; then
552+
LLVM_PREFIX=$(brew --prefix llvm 2>/dev/null || echo "")
553+
if [ -n "$LLVM_PREFIX" ]; then
554+
LLVM_LIBDIR="$LLVM_PREFIX/lib"
555+
556+
# Find the fuzzer library
557+
# Look for libclang_rt.fuzzer_osx.a in the clang lib directories
558+
CLANG_VERSION=$(ls -1 "$LLVM_LIBDIR/clang" 2>/dev/null | sort -V | tail -n1)
559+
if [ -n "$CLANG_VERSION" ]; then
560+
FUZZER_LIB="$LLVM_LIBDIR/clang/$CLANG_VERSION/lib/darwin/libclang_rt.fuzzer_osx.a"
561+
if [ ! -f "$FUZZER_LIB" ]; then
562+
echo "Warning: Could not find fuzzer library at $FUZZER_LIB" >&2
563+
FUZZER_LIB=""
564+
fi
565+
fi
566+
567+
# Set LLVM C++ library path
568+
if [ -d "$LLVM_PREFIX/lib/c++" ]; then
569+
LLVM_LDFLAGS="-L$LLVM_PREFIX/lib/c++ -lc++"
570+
fi
571+
572+
echo "found at $LLVM_PREFIX"
573+
else
574+
echo "not found"
575+
echo "Warning: LLVM not found via Homebrew. Fuzzing may not work." >&2
576+
fi
577+
else
578+
echo "not found (Homebrew not available)"
579+
fi
580+
fi
581+
541582
# Now we can finally set our warning flags
542583
if [ -z ${CWARNFLAGS+x} ]; then
543584
CWARNFLAGS=$(default_cwarnflags "$COPTFLAGS" \
@@ -551,8 +592,13 @@ add_var CONFIGURATOR_CC "$CONFIGURATOR_CC"
551592
add_var CWARNFLAGS "$CWARNFLAGS"
552593
add_var CDEBUGFLAGS "$CDEBUGFLAGS"
553594
add_var COPTFLAGS "$COPTFLAGS"
595+
if [ -n "${SDKROOT:-}" ]; then
596+
add_var SDKROOT "$SDKROOT"
597+
fi
554598
add_var CSANFLAGS "$CSANFLAGS"
555599
add_var FUZZFLAGS "$FUZZFLAGS"
600+
add_var FUZZER_LIB "$FUZZER_LIB"
601+
add_var LLVM_LDFLAGS "$LLVM_LDFLAGS"
556602
add_var SQLITE3_CFLAGS "$SQLITE3_CFLAGS"
557603
add_var SQLITE3_LDLIBS "$SQLITE3_LDLIBS"
558604
add_var POSTGRES_INCLUDE "$POSTGRES_INCLUDE"

tests/fuzz/check-fuzz.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
# Runs each fuzz target on its seed corpus and prints any failures.
44
FUZZ_DIR=$(dirname "$0")
55
readonly FUZZ_DIR
6-
TARGETS=$(find "${FUZZ_DIR}" -type f -name "fuzz-*" ! -name "*.*")
6+
# On macOS, exclude debug symbol files from fuzzer target discovery
7+
if [[ "$OSTYPE" == "darwin"* ]]; then
8+
TARGETS=$(find "${FUZZ_DIR}" -type f -name "fuzz-*" ! -name "*.*" ! -path "*.dSYM/*")
9+
else
10+
TARGETS=$(find "${FUZZ_DIR}" -type f -name "fuzz-*" ! -name "*.*")
11+
fi
712
readonly TARGETS
813

914
export UBSAN_OPTIONS="halt_on_error=1:print_stacktrace=1"

0 commit comments

Comments
 (0)