Skip to content

Commit 287c318

Browse files
committed
[CMake] Avoid accidental C++ standard library dependency in sanitizers
While sanitizers don't use C++ standard library, we could still end up accidentally including or linking it just by the virtue of using the C++ compiler. Pass -nostdinc++ and -nostdlib++ to avoid these accidental dependencies. Differential Revision: https://reviews.llvm.org/D88922
1 parent f7f2e42 commit 287c318

File tree

8 files changed

+36
-37
lines changed

8 files changed

+36
-37
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,13 @@ if("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
437437
list(APPEND SANITIZER_COMMON_LINK_LIBS zircon)
438438
endif()
439439

440+
# We don't use the C++ standard library, so avoid including it by mistake.
441+
append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ SANITIZER_COMMON_CFLAGS)
442+
append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SANITIZER_COMMON_LINK_FLAGS)
443+
444+
# Remove -stdlib= which is unused when passing -nostdinc++.
445+
string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
446+
440447
macro(append_libcxx_libs var)
441448
if (${var}_INTREE)
442449
if (SANITIZER_USE_STATIC_LLVM_UNWINDER AND (TARGET unwind_static OR HAVE_LIBUNWIND))

compiler-rt/cmake/Modules/CompilerRTUtils.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ function(list_replace input_list old new)
8080
set(${input_list} "${replaced_list}" PARENT_SCOPE)
8181
endfunction()
8282

83+
macro(list_filter list element)
84+
list(FIND ${list} ${element} index)
85+
if(NOT index EQUAL -1)
86+
list(REMOVE_AT ${list} ${index})
87+
endif()
88+
endmacro()
89+
8390
# Takes ${ARGN} and puts only supported architectures in @out_var list.
8491
function(filter_available_targets out_var)
8592
set(archs ${${out_var}})

compiler-rt/cmake/config-ix.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ check_symbol_exists(__func__ "" COMPILER_RT_HAS_FUNC_SYMBOL)
123123

124124
# Includes.
125125
check_cxx_compiler_flag(-nostdinc++ COMPILER_RT_HAS_NOSTDINCXX_FLAG)
126+
check_cxx_compiler_flag(-nostdlib++ COMPILER_RT_HAS_NOSTDLIBXX_FLAG)
126127
check_include_files("sys/auxv.h" COMPILER_RT_HAS_AUXV)
127128

128129
# Libraries.

compiler-rt/lib/asan/CMakeLists.txt

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
111111
append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
112112
append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
113113

114-
if (TARGET cxx-headers OR HAVE_LIBCXX)
115-
set(ASAN_DEPS cxx-headers)
116-
endif()
117-
118114
# Compile ASan sources into an object library.
119115

120116
add_compiler_rt_object_libraries(RTAsan_dynamic
@@ -123,39 +119,34 @@ add_compiler_rt_object_libraries(RTAsan_dynamic
123119
SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
124120
ADDITIONAL_HEADERS ${ASAN_HEADERS}
125121
CFLAGS ${ASAN_DYNAMIC_CFLAGS}
126-
DEFS ${ASAN_DYNAMIC_DEFINITIONS}
127-
DEPS ${ASAN_DEPS})
122+
DEFS ${ASAN_DYNAMIC_DEFINITIONS})
128123

129124
if(NOT APPLE)
130125
add_compiler_rt_object_libraries(RTAsan
131126
ARCHS ${ASAN_SUPPORTED_ARCH}
132127
SOURCES ${ASAN_SOURCES}
133128
ADDITIONAL_HEADERS ${ASAN_HEADERS}
134129
CFLAGS ${ASAN_CFLAGS}
135-
DEFS ${ASAN_COMMON_DEFINITIONS}
136-
DEPS ${ASAN_DEPS})
130+
DEFS ${ASAN_COMMON_DEFINITIONS})
137131
add_compiler_rt_object_libraries(RTAsan_cxx
138132
ARCHS ${ASAN_SUPPORTED_ARCH}
139133
SOURCES ${ASAN_CXX_SOURCES}
140134
ADDITIONAL_HEADERS ${ASAN_HEADERS}
141135
CFLAGS ${ASAN_CFLAGS}
142-
DEFS ${ASAN_COMMON_DEFINITIONS}
143-
DEPS ${ASAN_DEPS})
136+
DEFS ${ASAN_COMMON_DEFINITIONS})
144137
add_compiler_rt_object_libraries(RTAsan_preinit
145138
ARCHS ${ASAN_SUPPORTED_ARCH}
146139
SOURCES ${ASAN_PREINIT_SOURCES}
147140
ADDITIONAL_HEADERS ${ASAN_HEADERS}
148141
CFLAGS ${ASAN_CFLAGS}
149-
DEFS ${ASAN_COMMON_DEFINITIONS}
150-
DEPS ${ASAN_DEPS})
142+
DEFS ${ASAN_COMMON_DEFINITIONS})
151143

152144
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
153145
add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
154146
ARCHS ${ASAN_SUPPORTED_ARCH}
155147
SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
156148
CFLAGS ${ASAN_DYNAMIC_CFLAGS}
157-
DEFS ${ASAN_DYNAMIC_DEFINITIONS}
158-
DEPS ${ASAN_DEPS})
149+
DEFS ${ASAN_DYNAMIC_DEFINITIONS})
159150
endif()
160151

161152
# Build ASan runtimes shipped with Clang.
@@ -250,8 +241,7 @@ else()
250241
ARCHS ${arch}
251242
SOURCES asan_win_weak_interception.cpp
252243
CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
253-
DEFS ${ASAN_COMMON_DEFINITIONS}
254-
DEPS ${ASAN_DEPS})
244+
DEFS ${ASAN_COMMON_DEFINITIONS})
255245
set(ASAN_DYNAMIC_WEAK_INTERCEPTION
256246
AsanWeakInterception
257247
UbsanWeakInterception
@@ -296,8 +286,7 @@ else()
296286
SOURCES asan_globals_win.cpp
297287
asan_win_dll_thunk.cpp
298288
CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
299-
DEFS ${ASAN_COMMON_DEFINITIONS}
300-
DEPS ${ASAN_DEPS})
289+
DEFS ${ASAN_COMMON_DEFINITIONS})
301290

302291
add_compiler_rt_runtime(clang_rt.asan_dll_thunk
303292
STATIC
@@ -322,8 +311,7 @@ else()
322311
SOURCES asan_globals_win.cpp
323312
asan_win_dynamic_runtime_thunk.cpp
324313
CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
325-
DEFS ${ASAN_COMMON_DEFINITIONS}
326-
DEPS ${ASAN_DEPS})
314+
DEFS ${ASAN_COMMON_DEFINITIONS})
327315

328316
add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
329317
STATIC

compiler-rt/lib/fuzzer/CMakeLists.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,15 @@ set(LIBFUZZER_CFLAGS ${SANITIZER_COMMON_CFLAGS})
6060
if(OS_NAME MATCHES "Linux|Fuchsia" AND
6161
COMPILER_RT_LIBCXX_PATH AND
6262
COMPILER_RT_LIBCXXABI_PATH)
63-
list(APPEND LIBFUZZER_CFLAGS -nostdinc++ -D_LIBCPP_ABI_VERSION=Fuzzer)
63+
list(APPEND LIBFUZZER_CFLAGS -D_LIBCPP_ABI_VERSION=Fuzzer)
6464
# Remove -stdlib= which is unused when passing -nostdinc++.
6565
string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
66-
elseif(TARGET cxx-headers OR HAVE_LIBCXX)
67-
set(LIBFUZZER_DEPS cxx-headers)
66+
else()
67+
# libFuzzer uses C++ standard library headers so drop -nostdinc++.
68+
list_filter(LIBFUZZER_CFLAGS "-nostdinc++")
69+
if(TARGET cxx-headers OR HAVE_LIBCXX)
70+
set(LIBFUZZER_DEPS cxx-headers)
71+
endif()
6872
endif()
6973

7074
append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fno-omit-frame-pointer LIBFUZZER_CFLAGS)

compiler-rt/lib/hwasan/CMakeLists.txt

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,6 @@ append_list_if(COMPILER_RT_HAS_LIBRT rt HWASAN_DYNAMIC_LIBS)
7171
append_list_if(COMPILER_RT_HAS_LIBM m HWASAN_DYNAMIC_LIBS)
7272
append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread HWASAN_DYNAMIC_LIBS)
7373

74-
if (TARGET cxx-headers OR HAVE_LIBCXX)
75-
set(HWASAN_DEPS cxx-headers)
76-
endif()
77-
7874
# Static runtime library.
7975
add_compiler_rt_component(hwasan)
8076

@@ -83,30 +79,26 @@ add_compiler_rt_object_libraries(RTHwasan
8379
SOURCES ${HWASAN_RTL_SOURCES}
8480
ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
8581
CFLAGS ${HWASAN_RTL_CFLAGS}
86-
DEFS ${HWASAN_DEFINITIONS}
87-
DEPS ${HWASAN_DEPS})
82+
DEFS ${HWASAN_DEFINITIONS})
8883
add_compiler_rt_object_libraries(RTHwasan_cxx
8984
ARCHS ${HWASAN_SUPPORTED_ARCH}
9085
SOURCES ${HWASAN_RTL_CXX_SOURCES}
9186
ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
9287
CFLAGS ${HWASAN_RTL_CFLAGS}
93-
DEFS ${HWASAN_DEFINITIONS}
94-
DEPS ${HWASAN_DEPS})
88+
DEFS ${HWASAN_DEFINITIONS})
9589
add_compiler_rt_object_libraries(RTHwasan_dynamic
9690
ARCHS ${HWASAN_SUPPORTED_ARCH}
9791
SOURCES ${HWASAN_RTL_SOURCES} ${HWASAN_RTL_CXX_SOURCES}
9892
ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
9993
CFLAGS ${HWASAN_DYNAMIC_CFLAGS}
100-
DEFS ${HWASAN_DEFINITIONS}
101-
DEPS ${HWASAN_DEPS})
94+
DEFS ${HWASAN_DEFINITIONS})
10295

10396
file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
10497
add_compiler_rt_object_libraries(RTHwasan_dynamic_version_script_dummy
10598
ARCHS ${HWASAN_SUPPORTED_ARCH}
10699
SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
107100
CFLAGS ${HWASAN_DYNAMIC_CFLAGS}
108-
DEFS ${HWASAN_DEFINITIONS}
109-
DEPS ${HWASAN_DEPS})
101+
DEFS ${HWASAN_DEFINITIONS})
110102

111103
foreach(arch ${HWASAN_SUPPORTED_ARCH})
112104
add_compiler_rt_runtime(clang_rt.hwasan

compiler-rt/lib/profile/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ endif()
113113

114114
# We don't use the C++ Standard Library here, so avoid including it by mistake.
115115
append_list_if(COMPILER_RT_HAS_NOSTDINCXX_FLAG -nostdinc++ EXTRA_FLAGS)
116-
# Remove -stdlib= which is unused when passing -nostdinc++.
117-
string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
118116

119117
# This appears to be a C-only warning banning the use of locals in aggregate
120118
# initializers. All other compilers accept this, though.

compiler-rt/lib/xray/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ set(XRAY_COMMON_RUNTIME_OBJECT_LIBS
149149
RTSanitizerCommon
150150
RTSanitizerCommonLibc)
151151

152+
# XRay uses C++ standard library headers so drop -nostdinc++.
153+
list_filter(XRAY_CFLAGS "-nostdinc++")
152154
if (TARGET cxx-headers OR HAVE_LIBCXX)
153155
set(XRAY_DEPS cxx-headers)
154156
endif()

0 commit comments

Comments
 (0)