Skip to content

Commit eac2b47

Browse files
committed
Reland "[libFuzzer] Support using libc++"
This is needed in case the users of libFuzzer use libc++ in their code, which the fuzz target (libFuzzer) will be linked against. When libc++ source is available, we build a private version of it and link it against libFuzzer which allows using the same static library against codebases which use both libc++ and libstdc++. Differential Revision: https://reviews.llvm.org/D37631 llvm-svn: 322755
1 parent 12e6e70 commit eac2b47

File tree

8 files changed

+126
-36
lines changed

8 files changed

+126
-36
lines changed

compiler-rt/lib/fuzzer/CMakeLists.txt

+36
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ CHECK_CXX_SOURCE_COMPILES("
3333

3434
set(LIBFUZZER_CFLAGS ${SANITIZER_COMMON_CFLAGS})
3535

36+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND COMPILER_RT_LIBCXX_PATH)
37+
list(APPEND LIBFUZZER_CFLAGS -nostdinc++ -D_LIBCPP_ABI_VERSION=__Fuzzer)
38+
endif()
39+
3640
append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fno-omit-frame-pointer LIBFUZZER_CFLAGS)
3741

3842
if (CMAKE_CXX_FLAGS MATCHES "fsanitize-coverage")
@@ -75,6 +79,38 @@ add_compiler_rt_runtime(clang_rt.fuzzer_no_main
7579
CFLAGS ${LIBFUZZER_CFLAGS}
7680
PARENT_TARGET fuzzer)
7781

82+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND COMPILER_RT_LIBCXX_PATH)
83+
macro(partially_link_libcxx name dir arch)
84+
set(cxx_${arch}_merge_dir "${CMAKE_CURRENT_BINARY_DIR}/cxx_${arch}_merge.dir")
85+
file(MAKE_DIRECTORY ${cxx_${arch}_merge_dir})
86+
add_custom_command(TARGET clang_rt.${name}-${arch} POST_BUILD
87+
COMMAND ${CMAKE_LINKER} --whole-archive "$<TARGET_LINKER_FILE:clang_rt.${name}-${arch}>" --no-whole-archive ${dir}/lib/libc++.a -r -o ${name}.o
88+
COMMAND ${CMAKE_OBJCOPY} --localize-hidden ${name}.o
89+
COMMAND ${CMAKE_COMMAND} -E remove "$<TARGET_LINKER_FILE:clang_rt.${name}-${arch}>"
90+
COMMAND ${CMAKE_AR} qcs "$<TARGET_LINKER_FILE:clang_rt.${name}-${arch}>" ${name}.o
91+
WORKING_DIRECTORY ${cxx_${arch}_merge_dir}
92+
)
93+
endmacro()
94+
95+
foreach(arch ${FUZZER_SUPPORTED_ARCH})
96+
get_target_flags_for_arch(${arch} TARGET_CFLAGS)
97+
set(LIBCXX_${arch}_PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libcxx_fuzzer_${arch})
98+
add_custom_libcxx(libcxx_fuzzer_${arch} ${LIBCXX_${arch}_PREFIX}
99+
CFLAGS ${TARGET_CFLAGS}
100+
-D_LIBCPP_ABI_VERSION=__Fuzzer
101+
-D_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=1
102+
-fvisibility=hidden
103+
CMAKE_ARGS -DLIBCXX_ENABLE_EXCEPTIONS=OFF
104+
-DLIBCXX_CXX_ABI=none)
105+
target_compile_options(RTfuzzer.${arch} PRIVATE -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
106+
add_dependencies(RTfuzzer.${arch} libcxx_fuzzer_${arch})
107+
target_compile_options(RTfuzzer_main.${arch} PRIVATE -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
108+
add_dependencies(RTfuzzer_main.${arch} libcxx_fuzzer_${arch})
109+
partially_link_libcxx(fuzzer_no_main ${LIBCXX_${arch}_PREFIX} ${arch})
110+
partially_link_libcxx(fuzzer ${LIBCXX_${arch}_PREFIX} ${arch})
111+
endforeach()
112+
endif()
113+
78114
if(COMPILER_RT_INCLUDE_TESTS)
79115
add_subdirectory(tests)
80116
endif()

compiler-rt/lib/fuzzer/FuzzerInterface.h

+13-9
Original file line numberDiff line numberDiff line change
@@ -30,35 +30,39 @@ extern "C" {
3030
// Executes the code under test with [Data, Data+Size) as the input.
3131
// libFuzzer will invoke this function *many* times with different inputs.
3232
// Must return 0.
33-
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
33+
__attribute__((visibility("default"))) int
34+
LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
3435

3536
// Optional user-provided initialization function.
3637
// If provided, this function will be called by libFuzzer once at startup.
3738
// It may read and modify argc/argv.
3839
// Must return 0.
39-
int LLVMFuzzerInitialize(int *argc, char ***argv);
40+
__attribute__((visibility("default"))) int LLVMFuzzerInitialize(int *argc,
41+
char ***argv);
4042

4143
// Optional user-provided custom mutator.
4244
// Mutates raw data in [Data, Data+Size) inplace.
4345
// Returns the new size, which is not greater than MaxSize.
4446
// Given the same Seed produces the same mutation.
45-
size_t LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize,
46-
unsigned int Seed);
47+
__attribute__((visibility("default"))) size_t
48+
LLVMFuzzerCustomMutator(uint8_t *Data, size_t Size, size_t MaxSize,
49+
unsigned int Seed);
4750

4851
// Optional user-provided custom cross-over function.
4952
// Combines pieces of Data1 & Data2 together into Out.
5053
// Returns the new size, which is not greater than MaxOutSize.
5154
// Should produce the same mutation given the same Seed.
52-
size_t LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1,
53-
const uint8_t *Data2, size_t Size2,
54-
uint8_t *Out, size_t MaxOutSize,
55-
unsigned int Seed);
55+
__attribute__((visibility("default"))) size_t
56+
LLVMFuzzerCustomCrossOver(const uint8_t *Data1, size_t Size1,
57+
const uint8_t *Data2, size_t Size2, uint8_t *Out,
58+
size_t MaxOutSize, unsigned int Seed);
5659

5760
// Experimental, may go away in future.
5861
// libFuzzer-provided function to be used inside LLVMFuzzerCustomMutator.
5962
// Mutates raw data in [Data, Data+Size) inplace.
6063
// Returns the new size, which is not greater than MaxSize.
61-
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
64+
__attribute__((visibility("default"))) size_t
65+
LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize);
6266

6367
#ifdef __cplusplus
6468
} // extern "C"

compiler-rt/lib/fuzzer/FuzzerLoop.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -826,13 +826,15 @@ void Fuzzer::AnnounceOutput(const uint8_t *Data, size_t Size) {
826826

827827
extern "C" {
828828

829-
size_t LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
829+
__attribute__((visibility("default"))) size_t
830+
LLVMFuzzerMutate(uint8_t *Data, size_t Size, size_t MaxSize) {
830831
assert(fuzzer::F);
831832
return fuzzer::F->GetMD().DefaultMutate(Data, Size, MaxSize);
832833
}
833834

834835
// Experimental
835-
void LLVMFuzzerAnnounceOutput(const uint8_t *Data, size_t Size) {
836+
__attribute__((visibility("default"))) void
837+
LLVMFuzzerAnnounceOutput(const uint8_t *Data, size_t Size) {
836838
assert(fuzzer::F);
837839
fuzzer::F->AnnounceOutput(Data, Size);
838840
}

compiler-rt/lib/fuzzer/FuzzerMain.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ extern "C" {
1616
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size);
1717
} // extern "C"
1818

19-
int main(int argc, char **argv) {
19+
__attribute__((visibility("default"))) int main(int argc, char **argv) {
2020
return fuzzer::FuzzerDriver(&argc, &argv, LLVMFuzzerTestOneInput);
2121
}

compiler-rt/lib/fuzzer/tests/CMakeLists.txt

+13-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ else()
1818
list(APPEND LIBFUZZER_UNITTEST_LINK_FLAGS -lstdc++ -lpthread)
1919
endif()
2020

21+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND COMPILER_RT_LIBCXX_PATH)
22+
list(APPEND LIBFUZZER_UNITTEST_CFLAGS -nostdinc++ -D_LIBCPP_ABI_VERSION=__Fuzzer)
23+
endif()
24+
2125
foreach(arch ${FUZZER_SUPPORTED_ARCH})
2226
set(LIBFUZZER_TEST_RUNTIME RTFuzzerTest.${arch})
2327
if(APPLE)
@@ -33,14 +37,20 @@ foreach(arch ${FUZZER_SUPPORTED_ARCH})
3337
ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
3438
FOLDER "Compiler-RT Runtime tests")
3539

40+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux" AND COMPILER_RT_LIBCXX_PATH)
41+
set(LIBFUZZER_TEST_RUNTIME_DEPS libcxx_fuzzer_${arch})
42+
set(LIBFUZZER_TEST_RUNTIME_CFLAGS -isystem ${LIBCXX_${arch}_PREFIX}/include/c++/v1)
43+
set(LIBFUZZER_TEST_RUNTIME_LINK_FLAGS ${LIBCXX_${arch}_PREFIX}/lib/libc++.a)
44+
endif()
45+
3646
set(FuzzerTestObjects)
3747
generate_compiler_rt_tests(FuzzerTestObjects
3848
FuzzerUnitTests "Fuzzer-${arch}-Test" ${arch}
3949
SOURCES FuzzerUnittest.cpp ${COMPILER_RT_GTEST_SOURCE}
4050
RUNTIME ${LIBFUZZER_TEST_RUNTIME}
41-
DEPS gtest
42-
CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS}
43-
LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS})
51+
DEPS gtest ${LIBFUZZER_TEST_RUNTIME_DEPS}
52+
CFLAGS ${LIBFUZZER_UNITTEST_CFLAGS} ${LIBFUZZER_TEST_RUNTIME_CFLAGS}
53+
LINK_FLAGS ${LIBFUZZER_UNITTEST_LINK_FLAGS} ${LIBFUZZER_TEST_RUNTIME_LINK_FLAGS})
4454
set_target_properties(FuzzerUnitTests PROPERTIES
4555
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
4656
endforeach()

compiler-rt/test/fuzzer/CMakeLists.txt

+48-19
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,65 @@ if(COMPILER_RT_INCLUDE_TESTS)
88
list(APPEND LIBFUZZER_TEST_DEPS FuzzerUnitTests)
99
endif()
1010

11-
set(LIBFUZZER_TESTSUITES)
11+
set(EXCLUDE_FROM_ALL ON)
1212

13+
add_custom_target(check-fuzzer)
1314

1415
if(COMPILER_RT_INCLUDE_TESTS)
1516
# libFuzzer unit tests.
1617
configure_lit_site_cfg(
1718
${CMAKE_CURRENT_SOURCE_DIR}/unit/lit.site.cfg.in
1819
${CMAKE_CURRENT_BINARY_DIR}/unit/lit.site.cfg)
19-
list(APPEND LIBFUZZER_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/unit)
20+
add_lit_testsuite(check-fuzzer-unit "Running Fuzzer unit tests"
21+
${CMAKE_CURRENT_BINARY_DIR}/unit
22+
DEPENDS ${LIBFUZZER_TEST_DEPS})
23+
set_target_properties(check-fuzzer-unit PROPERTIES FOLDER "Compiler-RT Tests")
24+
add_dependencies(check-fuzzer check-fuzzer-unit)
2025
endif()
2126

22-
foreach(arch ${FUZZER_SUPPORTED_ARCH})
23-
set(LIBFUZZER_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
24-
get_test_cc_for_arch(${arch} LIBFUZZER_TEST_COMPILER LIBFUZZER_TEST_FLAGS)
27+
macro(test_fuzzer stdlib)
28+
cmake_parse_arguments(TEST "" "" "DEPS" ${ARGN})
29+
string(REPLACE "+" "x" stdlib_name ${stdlib})
30+
string(REPLACE "-" ";" stdlib_list ${stdlib_name})
31+
set(STDLIB_CAPITALIZED "")
32+
foreach(part IN LISTS stdlib_list)
33+
string(SUBSTRING ${part} 0 1 first_letter)
34+
string(TOUPPER ${first_letter} first_letter)
35+
string(REGEX REPLACE "^.(.*)" "${first_letter}\\1" part "${part}")
36+
set(STDLIB_CAPITALIZED "${STDLIB_CAPITALIZED}${part}")
37+
endforeach()
38+
foreach(arch ${FUZZER_SUPPORTED_ARCH})
39+
set(LIBFUZZER_TEST_COMPILER ${COMPILER_RT_TEST_COMPILER})
40+
get_test_cc_for_arch(${arch} LIBFUZZER_TEST_COMPILER LIBFUZZER_TEST_FLAGS)
2541

26-
string(TOUPPER ${arch} ARCH_UPPER_CASE)
27-
set(CONFIG_NAME ${ARCH_UPPER_CASE}${OS_NAME}Config)
42+
set(LIBFUZZER_TEST_STDLIB ${stdlib})
2843

29-
# LIT-based libFuzzer tests.
30-
configure_lit_site_cfg(
31-
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
32-
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg
33-
)
34-
list(APPEND LIBFUZZER_TESTSUITES ${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME})
44+
string(TOUPPER ${arch} ARCH_UPPER_CASE)
45+
set(CONFIG_NAME ${ARCH_UPPER_CASE}${STDLIB_CAPITALIZED}${OS_NAME}Config)
3546

36-
endforeach()
47+
# LIT-based libFuzzer tests.
48+
configure_lit_site_cfg(
49+
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in
50+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}/lit.site.cfg
51+
)
3752

38-
set(EXCLUDE_FROM_ALL ON)
53+
add_lit_testsuite(check-fuzzer-${stdlib_name} "Running Fuzzer ${stdlib} tests"
54+
${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_NAME}
55+
DEPENDS ${LIBFUZZER_TEST_DEPS})
56+
if(TEST_DEPS)
57+
add_dependencies(check-fuzzer-${stdlib_name} ${TEST_DEPS})
58+
endif()
59+
set_target_properties(check-fuzzer-${stdlib_name} PROPERTIES FOLDER "Compiler-RT Tests")
60+
add_dependencies(check-fuzzer check-fuzzer-${stdlib_name})
61+
endforeach()
62+
endmacro()
3963

40-
add_lit_testsuite(check-fuzzer "Running Fuzzer tests"
41-
${LIBFUZZER_TESTSUITES}
42-
DEPENDS ${LIBFUZZER_TEST_DEPS})
43-
set_target_properties(check-fuzzer PROPERTIES FOLDER "Compiler-RT Tests")
64+
test_fuzzer("default")
65+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
66+
if(TARGET cxx_shared)
67+
test_fuzzer("libc++" DEPS cxx_shared)
68+
endif()
69+
if(TARGET cxx_static)
70+
test_fuzzer("static-libc++" DEPS cxx_static)
71+
endif()
72+
endif()

compiler-rt/test/fuzzer/lit.cfg

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ config.test_format = lit.formats.ShTest(True)
77
config.suffixes = ['.test']
88
config.test_source_root = os.path.dirname(__file__)
99

10+
config.environment['LD_LIBRARY_PATH'] = config.llvm_library_dir
11+
1012
# Choose between lit's internal shell pipeline runner and a real shell. If
1113
# LIT_USE_INTERNAL_SHELL is in the environment, we use that as an override.
1214
use_lit_shell = os.environ.get("LIT_USE_INTERNAL_SHELL")
@@ -51,8 +53,13 @@ config.substitutions.append(('%libfuzzer_src', libfuzzer_src_root))
5153

5254
def generate_compiler_cmd(is_cpp=True, fuzzer_enabled=True):
5355
compiler_cmd = config.c_compiler
54-
link_cmd = '-lc++' if any(x in config.target_triple for x in ('darwin', 'freebsd')) else '-lstdc++'
55-
std_cmd = '-std=c++11' if is_cpp else ''
56+
if config.clang and config.stdlib == 'libc++':
57+
link_cmd = '-stdlib=libc++'
58+
elif config.clang and config.stdlib == 'static-libc++':
59+
link_cmd = '-stdlib=libc++ -lc++abi -static-libstdc++'
60+
else:
61+
link_cmd = '-lc++' if any(x in config.target_triple for x in ('darwin', 'freebsd')) else '-lstdc++'
62+
std_cmd = '--driver-mode=g++ -std=c++11' if is_cpp else ''
5663
sanitizers = ['address']
5764
if fuzzer_enabled:
5865
sanitizers.append('fuzzer')

compiler-rt/test/fuzzer/lit.site.cfg.in

+2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ config.test_exec_root = "@CMAKE_CURRENT_BINARY_DIR@"
55
config.cpp_compiler = "@LIBFUZZER_TEST_COMPILER@"
66
config.target_flags = "@LIBFUZZER_TEST_FLAGS@"
77
config.c_compiler = "@LIBFUZZER_TEST_COMPILER@"
8+
config.stdlib = "@LIBFUZZER_TEST_STDLIB@"
89

910
config.osx_sysroot_flag = "@OSX_SYSROOT_FLAG@"
1011
config.cmake_binary_dir = "@CMAKE_BINARY_DIR@"
12+
config.llvm_library_dir = "@LLVM_LIBRARY_DIR@"
1113
config.target_triple = "@TARGET_TRIPLE@"
1214

1315
# Load common config for all compiler-rt lit tests.

0 commit comments

Comments
 (0)