build(fuzz): link migrated Rust archive into fuzz targets
The fuzz Makefile compiles libzstd sources directly, so C compatibility shims previously left migrated symbols unresolved. Build and link a configuration matched Cargo archive after the C objects for every fuzz target. Track forced HUF mode changes with a C-object stamp to prevent a stale C decoder from being mixed with another Rust archive. A -m32 invocation now also selects Cargo's i686 target rather than attempting to link native Rust objects into a 32-bit fuzzer. Test Plan: - build and run huf_round_trip and dictionary_round_trip on LICENSE - transition default to forced X1 and back without cleaning, then run input - build and run huf_round_trip with C/C++/linker -m32 flags - cargo clippy, cargo clippy --benches, cargo clippy --tests, and nightly fmt Refs: tests/fuzz/Makefile Rust archive configuration
This commit is contained in:
+88
-1
@@ -16,6 +16,7 @@ ASFLAGS ?=
|
||||
LDFLAGS ?=
|
||||
ARFLAGS ?=
|
||||
LIB_FUZZING_ENGINE ?= libregression.a
|
||||
FUZZING_ENGINE := $(LIB_FUZZING_ENGINE)
|
||||
PYTHON ?= python
|
||||
ifeq ($(shell sh -c 'MSYSTEM="MSYS" uname') , Darwin)
|
||||
DOWNLOAD?=curl -L -o
|
||||
@@ -33,6 +34,85 @@ default: all
|
||||
|
||||
include $(LIBZSTD_MK_DIR)/libzstd.mk
|
||||
|
||||
# The fuzzers compile the C source list directly. Keep their Rust archive
|
||||
# configuration in lockstep with the C HUF selection, and link it after all C
|
||||
# objects so migrated C shims resolve exactly as they do in the normal tests.
|
||||
CARGO ?= cargo
|
||||
RUST_DIR := ../../rust
|
||||
RUST_MANIFEST := $(RUST_DIR)/Cargo.toml
|
||||
RUST_SOURCES := $(RUST_MANIFEST) $(RUST_DIR)/Cargo.lock \
|
||||
$(shell find $(RUST_DIR)/src -type f -name '*.rs' -print)
|
||||
|
||||
RUST_HUF_FORCE_X1 := $(HUF_FORCE_DECOMPRESS_X1)
|
||||
RUST_HUF_FORCE_X2 := $(HUF_FORCE_DECOMPRESS_X2)
|
||||
ifneq ($(filter -DHUF_FORCE_DECOMPRESS_X1 -DHUF_FORCE_DECOMPRESS_X1=%,$(CPPFLAGS) $(CFLAGS) $(MOREFLAGS)),)
|
||||
RUST_HUF_FORCE_X1 := 1
|
||||
endif
|
||||
ifneq ($(filter -DHUF_FORCE_DECOMPRESS_X2 -DHUF_FORCE_DECOMPRESS_X2=%,$(CPPFLAGS) $(CFLAGS) $(MOREFLAGS)),)
|
||||
RUST_HUF_FORCE_X2 := 1
|
||||
endif
|
||||
ifneq ($(RUST_HUF_FORCE_X1),0)
|
||||
ifneq ($(RUST_HUF_FORCE_X2),0)
|
||||
$(error HUF_FORCE_DECOMPRESS_X1 and HUF_FORCE_DECOMPRESS_X2 are mutually exclusive)
|
||||
endif
|
||||
endif
|
||||
|
||||
RUST_HUF_FEATURE :=
|
||||
RUST_BUILD_CONFIG := default
|
||||
ifneq ($(RUST_HUF_FORCE_X1),0)
|
||||
RUST_HUF_FEATURE := huf-force-decompress-x1
|
||||
RUST_BUILD_CONFIG := huf-force-decompress-x1
|
||||
endif
|
||||
ifneq ($(RUST_HUF_FORCE_X2),0)
|
||||
RUST_HUF_FEATURE := huf-force-decompress-x2
|
||||
RUST_BUILD_CONFIG := huf-force-decompress-x2
|
||||
endif
|
||||
|
||||
RUST_TARGET_DIR := $(RUST_DIR)/target/fuzz/$(RUST_BUILD_CONFIG)
|
||||
RUST_CARGO_FLAGS := --manifest-path $(RUST_MANIFEST) --release \
|
||||
--target-dir $(RUST_TARGET_DIR)
|
||||
ifneq ($(RUST_HUF_FEATURE),)
|
||||
RUST_CARGO_FLAGS += --features $(RUST_HUF_FEATURE)
|
||||
endif
|
||||
|
||||
# Keep a 32-bit C/C++ fuzz invocation from linking a native-width Cargo
|
||||
# archive. Cross builds may select any supported Cargo target explicitly.
|
||||
RUST_TARGET ?=
|
||||
ifeq ($(strip $(RUST_TARGET)),)
|
||||
ifneq ($(filter -m32,$(CC) $(CXX) $(CPPFLAGS) $(CFLAGS) $(CXXFLAGS) $(LDFLAGS) $(MOREFLAGS)),)
|
||||
RUST_TARGET := i686-unknown-linux-gnu
|
||||
endif
|
||||
endif
|
||||
RUST_TARGET_COMPONENT :=
|
||||
ifneq ($(strip $(RUST_TARGET)),)
|
||||
RUST_CARGO_FLAGS += --target $(RUST_TARGET)
|
||||
RUST_TARGET_COMPONENT := /$(RUST_TARGET)
|
||||
endif
|
||||
RUST_STATICLIB := $(RUST_TARGET_DIR)$(RUST_TARGET_COMPONENT)/release/libzstd_rs.a
|
||||
|
||||
$(RUST_STATICLIB): $(RUST_SOURCES)
|
||||
$(CARGO) build $(RUST_CARGO_FLAGS)
|
||||
|
||||
# `rt_` and `d_` object names are flat. Rebuild them whenever a forced HUF
|
||||
# mode changes instead of mixing a stale C decoder with another Rust archive.
|
||||
RUST_HUF_C_MODE_FILE := $(RUST_DIR)/target/.huf-c-mode-fuzz
|
||||
RUST_HUF_C_MODE_STAMP := $(RUST_DIR)/target/.huf-c-mode-fuzz.a
|
||||
.PHONY: FORCE_RUST_HUF_C_MODE
|
||||
FORCE_RUST_HUF_C_MODE:
|
||||
$(RUST_HUF_C_MODE_STAMP): FORCE_RUST_HUF_C_MODE
|
||||
@mkdir -p $(@D)
|
||||
@if test ! -r $(RUST_HUF_C_MODE_FILE) \
|
||||
|| test "$$(cat $(RUST_HUF_C_MODE_FILE))" != "$(RUST_BUILD_CONFIG)" \
|
||||
|| test ! -f $@; then \
|
||||
printf '%s\n' "$(RUST_BUILD_CONFIG)" > $(RUST_HUF_C_MODE_FILE); \
|
||||
$(AR) rcs $@; \
|
||||
touch $@; \
|
||||
fi
|
||||
|
||||
# Recipes below already append LIB_FUZZING_ENGINE. Preserve user-provided
|
||||
# engines but place the Rust archive ahead of them and after C objects.
|
||||
LIB_FUZZING_ENGINE := $(RUST_STATICLIB) $(FUZZING_ENGINE)
|
||||
|
||||
PRGDIR = ../../programs
|
||||
CONTRIBDIR = ../../contrib
|
||||
|
||||
@@ -104,6 +184,8 @@ FUZZ_RT_OBJ9 := $(FUZZ_RT_OBJ8:.c=.o)
|
||||
FUZZ_RT_OBJ10 := $(THIRD_PARTY_SEQ_PROD_OBJ) $(FUZZ_RT_OBJ9)
|
||||
FUZZ_ROUND_TRIP_OBJ := $(FUZZ_RT_OBJ10:.S=.o)
|
||||
|
||||
$(FUZZ_DECOMPRESS_OBJ) $(FUZZ_ROUND_TRIP_OBJ): $(RUST_HUF_C_MODE_STAMP)
|
||||
|
||||
FUZZ_TARGETS := \
|
||||
simple_round_trip \
|
||||
stream_round_trip \
|
||||
@@ -130,6 +212,11 @@ FUZZ_TARGETS := \
|
||||
.PHONY: all clean cleanall
|
||||
all: libregression.a $(FUZZ_TARGETS)
|
||||
|
||||
$(FUZZ_TARGETS): $(RUST_STATICLIB) $(RUST_HUF_C_MODE_STAMP)
|
||||
ifneq ($(filter libregression.a,$(FUZZING_ENGINE)),)
|
||||
$(FUZZ_TARGETS): libregression.a
|
||||
endif
|
||||
|
||||
rt_lib_common_%.o: $(LIB_SRCDIR)/common/%.c
|
||||
$(CC) $(FUZZ_CPPFLAGS) $(FUZZ_CFLAGS) $(FUZZ_ROUND_TRIP_FLAGS) $< -c -o $@
|
||||
|
||||
@@ -269,7 +356,7 @@ regressiontest: corpora
|
||||
$(PYTHON) ./fuzz.py regression $(REGRESSION_TARGET)
|
||||
|
||||
clean:
|
||||
@$(RM) *.a *.o $(FUZZ_TARGETS)
|
||||
@$(RM) *.a *.o $(FUZZ_TARGETS) $(RUST_HUF_C_MODE_FILE) $(RUST_HUF_C_MODE_STAMP)
|
||||
@echo Cleaning completed
|
||||
|
||||
cleanall:
|
||||
|
||||
Reference in New Issue
Block a user