Skip to content

Commit 7eddb4f

Browse files
committed
Fix fuzzer builds for reworked AST.
When generating fuzzer binaries we previously would generate parser and linker code in two invocations, one for the module's C++ code with `-c` and one for the linker code with `-l` As of 2843729 this generates code which often fails to link due to missing symbols (this seems to be due to us not including code from the `filter` module in the single-file C++ output anymore). This patch reworks to code to switch from using two separate `spicyc` invocations with `-c` and `-l` to emitting all sources at once with `-x`. This requires some changes to the CMake function generating the targets as we need to statically name all inputs to `add_executable` which generates code for each module and the linker file. We also rename the binaries in the process.
1 parent 852797d commit 7eddb4f

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

.cmake-format.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
{
22
"parse": {
33
"additional_commands": {
4-
"spicy_add_analyzer": {
4+
"fuzz_parser": {
55
"kwargs": {
6-
"NAME": "*",
7-
"PACKAGE_NAME": "*",
6+
"PARSER": "1",
87
"SOURCES": "*",
9-
"SCRIPTS": "*"
8+
"MODULES": "*"
109
}
1110
}
1211
}

ci/fuzz/CMakeLists.txt

+44-29
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,59 @@
11
# Copyright (c) 2020-2023 by the Zeek Project. See LICENSE for details.
22

33
# Declares a new fuzzer target.
4-
function (fuzz_parser Name SpicyInput Parser)
5-
string(REPLACE ":" "_" parser ${Parser})
6-
set(name ${Name}-${parser})
7-
add_custom_command(
8-
OUTPUT "${name}.cc"
9-
COMMAND ${CMAKE_BINARY_DIR}/bin/spicyc -c -o "${name}.cc" "${SpicyInput}"
10-
DEPENDS spicyc
11-
COMMENT "Generating C++ code for ${Parser}")
4+
function (fuzz_parser)
5+
set(options)
6+
set(oneValueArg PARSER)
7+
set(multiValueArgs MODULES SOURCES)
8+
9+
cmake_parse_arguments(PARSE_ARGV 0 FUZZ "${options}" "${oneValueArg}" "${multiValueArgs}")
10+
11+
if (NOT DEFINED FUZZ_PARSER)
12+
message(FATAL_ERROR "PARSER" is required)
13+
endif ()
14+
15+
string(REPLACE "::" "_" _parser ${FUZZ_PARSER})
16+
17+
list(TRANSFORM FUZZ_MODULES PREPEND ${_parser}_ OUTPUT_VARIABLE _generated_sources)
18+
list(TRANSFORM _generated_sources APPEND ".cc" OUTPUT_VARIABLE _generated_sources)
19+
list(APPEND _generated_sources "${_parser}___linker__.cc")
1220

1321
add_custom_command(
14-
OUTPUT "${name}_link.cc"
15-
COMMAND ${CMAKE_BINARY_DIR}/bin/spicyc -l -o "${name}_link.cc" "${SpicyInput}"
22+
OUTPUT ${_generated_sources}
23+
COMMAND ${CMAKE_BINARY_DIR}/bin/spicyc -x ${CMAKE_CURRENT_BINARY_DIR}/${_parser}
24+
"${FUZZ_SOURCES}"
1625
DEPENDS spicyc
17-
COMMENT "Generating C++ linker code for ${Parser}")
26+
COMMENT "Generating C++ code for ${FUZZ_PARSER}")
1827

19-
add_executable(fuzz-${name} fuzz.cc "${name}.cc" "${name}_link.cc")
20-
target_compile_definitions(fuzz-${name} PRIVATE SPICY_FUZZ_PARSER="${Parser}")
21-
target_compile_options(fuzz-${name} PRIVATE -fsanitize=fuzzer-no-link)
22-
target_link_options(fuzz-${name} PRIVATE -fsanitize=fuzzer-no-link)
28+
add_executable(fuzz-${_parser} fuzz.cc ${_generated_sources})
29+
target_compile_definitions(fuzz-${_parser} PRIVATE SPICY_FUZZ_PARSER="${_parser}")
30+
target_compile_options(fuzz-${_parser} PRIVATE -fsanitize=fuzzer-no-link)
31+
target_link_options(fuzz-${_parser} PRIVATE -fsanitize=fuzzer-no-link)
2332

24-
set(LIBFUZZER_LIB $ENV{LIBFUZZER_LIB})
25-
if ("${LIBFUZZER_LIB}" STREQUAL "")
33+
set(_libfuzzer_lib $ENV{LIBFUZZER_LIB})
34+
if ("${_libfuzzer_lib}" STREQUAL "")
2635
message(FATAL_ERROR "When building fuzzers the environment variable LIBFUZZER_LIB "
2736
"must contain the path to libclang_rt.fuzzer_no_main-<arch>.a")
2837
endif ()
29-
if (NOT EXISTS ${LIBFUZZER_LIB})
30-
message(FATAL_ERROR "Configured LIBFUZZER_LIB ${LIBFUZZER_LIB} does not exist")
38+
if (NOT EXISTS ${_libfuzzer_lib})
39+
message(FATAL_ERROR "Configured LIBFUZZER_LIB ${_libfuzzer_lib} does not exist")
3140
endif ()
3241

33-
target_link_libraries(fuzz-${name} spicy-rt hilti-rt "${LIBFUZZER_LIB}")
42+
target_link_libraries(fuzz-${_parser} spicy-rt hilti-rt "${_libfuzzer_lib}")
3443
endfunction ()
3544

36-
fuzz_parser(dhcp ${CMAKE_SOURCE_DIR}/spicy-dhcp/analyzer/analyzer.spicy "dhcp::Message")
37-
fuzz_parser(tftp ${CMAKE_SOURCE_DIR}/spicy-tftp/analyzer/tftp.spicy "TFTP::Packet")
38-
fuzz_parser(pe ${CMAKE_SOURCE_DIR}/spicy-pe/analyzer/analyzer.spicy "pe::ImageFile")
39-
fuzz_parser(png ${CMAKE_SOURCE_DIR}/spicy-png/analyzer/analyzer.spicy "PNG::File")
40-
fuzz_parser(dns ${CMAKE_SOURCE_DIR}/spicy-dns/analyzer/analyzer.spicy "dns::Message")
41-
fuzz_parser(http ${CMAKE_SOURCE_DIR}/spicy-http/analyzer/analyzer.spicy "HTTP::Request")
42-
fuzz_parser(http ${CMAKE_SOURCE_DIR}/spicy-http/analyzer/analyzer.spicy "HTTP::Requests")
43-
fuzz_parser(http ${CMAKE_SOURCE_DIR}/spicy-http/analyzer/analyzer.spicy "HTTP::Reply")
44-
fuzz_parser(http ${CMAKE_SOURCE_DIR}/spicy-http/analyzer/analyzer.spicy "HTTP::Replies")
45+
fuzz_parser(PARSER "DHCP::Message" SOURCES ${CMAKE_SOURCE_DIR}/spicy-dhcp/analyzer/analyzer.spicy
46+
MODULES DHCP)
47+
fuzz_parser(PARSER "TFTP::Packet" SOURCES ${CMAKE_SOURCE_DIR}/spicy-tftp/analyzer/tftp.spicy
48+
MODULES TFTP)
49+
fuzz_parser(PARSER "pe::ImageFile" SOURCES ${CMAKE_SOURCE_DIR}/spicy-pe/analyzer/analyzer.spicy
50+
MODULES pe)
51+
fuzz_parser(PARSER "PNG::File" SOURCES ${CMAKE_SOURCE_DIR}/spicy-png/analyzer/analyzer.spicy
52+
MODULES PNG)
53+
fuzz_parser(PARSER "dns::Message" SOURCES ${CMAKE_SOURCE_DIR}/spicy-dns/analyzer/analyzer.spicy
54+
MODULES dns)
55+
56+
foreach (P IN ITEMS Request Requests Reply Replies)
57+
fuzz_parser(PARSER "HTTP::${P}" SOURCES ${CMAKE_SOURCE_DIR}/spicy-http/analyzer/analyzer.spicy
58+
MODULES HTTP filter)
59+
endforeach ()

0 commit comments

Comments
 (0)