1 # Build for the AddressSanitizer runtime support library.
15 asan_interceptors_memintrinsics.cpp
21 asan_memory_profile.cpp
24 asan_premap_shadow.cpp
36 set(ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES
38 asan_win_common_runtime_thunk.cpp
39 asan_win_dynamic_runtime_thunk.cpp
41 set(ASAN_STATIC_RUNTIME_THUNK_SOURCES
43 asan_malloc_win_thunk.cpp
44 asan_win_common_runtime_thunk.cpp
45 asan_win_static_runtime_thunk.cpp
49 if (NOT WIN32 AND NOT APPLE)
50 list(APPEND ASAN_SOURCES
51 asan_interceptors_vfork.S
59 set(ASAN_STATIC_SOURCES
63 if ("x86_64" IN_LIST ASAN_SUPPORTED_ARCH AND NOT WIN32 AND NOT APPLE)
64 list(APPEND ASAN_STATIC_SOURCES
69 set(ASAN_PREINIT_SOURCES
75 asan_activation_flags.inc
84 asan_interceptors_memintrinsics.h
86 asan_interface_internal.h
92 asan_scariness_score.h
99 include_directories(..)
101 # asan on windows only supports the release dll version of the runtimes, in the interest of
102 # only having one asan dll to support/test. Having asan statically linked
103 # with the runtime might be possible, but it multiplies the number of scenerios to test.
104 # the program USING sanitizers can use whatever version of the runtime it wants to.
105 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
107 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
109 append_list_if(MSVC /Zl ASAN_CFLAGS)
111 set(ASAN_COMMON_DEFINITIONS "")
113 append_rtti_flag(OFF ASAN_CFLAGS)
115 # Silence warnings in system headers with MSVC.
117 append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" ASAN_CFLAGS)
120 # Too many existing bugs, needs cleanup.
121 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
123 set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
126 # Put most Sanitizer shared libraries in the global group. For more details, see
127 # android-changes-for-ndk-developers.md#changes-to-library-search-order
128 if (COMPILER_RT_HAS_Z_GLOBAL)
129 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
133 set(ASAN_DYNAMIC_DEFINITIONS
134 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
135 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
137 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
138 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
139 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
141 # LLVM turns /OPT:ICF back on when LLVM_ENABLE_PDBs is set
142 # we _REALLY_ need to turn it back off for ASAN, because the way
143 # asan emulates weak functions from DLLs requires NOICF
144 append_list_if(MSVC "/DEBUG;/OPT:NOICF" ASAN_DYNAMIC_LINK_FLAGS)
146 set(ASAN_DYNAMIC_LIBS
147 ${COMPILER_RT_UNWINDER_LINK_LIBS}
148 ${SANITIZER_CXX_ABI_LIBRARIES}
149 ${SANITIZER_COMMON_LINK_LIBS})
151 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
152 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
153 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
154 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
155 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
156 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
158 # Compile ASan sources into an object library.
160 add_compiler_rt_object_libraries(RTAsan_dynamic
161 OS ${SANITIZER_COMMON_SUPPORTED_OS}
162 ARCHS ${ASAN_SUPPORTED_ARCH}
163 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
164 ADDITIONAL_HEADERS ${ASAN_HEADERS}
165 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
166 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
169 add_compiler_rt_object_libraries(RTAsan
170 ARCHS ${ASAN_SUPPORTED_ARCH}
171 SOURCES ${ASAN_SOURCES}
172 ADDITIONAL_HEADERS ${ASAN_HEADERS}
173 CFLAGS ${ASAN_CFLAGS}
174 DEFS ${ASAN_COMMON_DEFINITIONS})
175 add_compiler_rt_object_libraries(RTAsan_cxx
176 ARCHS ${ASAN_SUPPORTED_ARCH}
177 SOURCES ${ASAN_CXX_SOURCES}
178 ADDITIONAL_HEADERS ${ASAN_HEADERS}
179 CFLAGS ${ASAN_CFLAGS}
180 DEFS ${ASAN_COMMON_DEFINITIONS})
181 add_compiler_rt_object_libraries(RTAsan_static
182 ARCHS ${ASAN_SUPPORTED_ARCH}
183 SOURCES ${ASAN_STATIC_SOURCES}
184 ADDITIONAL_HEADERS ${ASAN_HEADERS}
185 CFLAGS ${ASAN_CFLAGS}
186 DEFS ${ASAN_COMMON_DEFINITIONS})
187 add_compiler_rt_object_libraries(RTAsan_preinit
188 ARCHS ${ASAN_SUPPORTED_ARCH}
189 SOURCES ${ASAN_PREINIT_SOURCES}
190 ADDITIONAL_HEADERS ${ASAN_HEADERS}
191 CFLAGS ${ASAN_CFLAGS}
192 DEFS ${ASAN_COMMON_DEFINITIONS})
194 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
195 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
196 ARCHS ${ASAN_SUPPORTED_ARCH}
197 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
198 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
199 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
202 # Build ASan runtimes shipped with Clang.
203 add_compiler_rt_component(asan)
206 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
207 add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
208 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
209 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
210 add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
212 add_compiler_rt_runtime(clang_rt.asan
214 OS ${SANITIZER_COMMON_SUPPORTED_OS}
215 ARCHS ${ASAN_SUPPORTED_ARCH}
216 OBJECT_LIBS RTAsan_dynamic
219 RTSanitizerCommonLibc
220 RTSanitizerCommonCoverage
221 RTSanitizerCommonSymbolizer
224 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
225 LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
226 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
229 add_compiler_rt_runtime(clang_rt.asan_static
231 ARCHS ${ASAN_SUPPORTED_ARCH}
232 OBJECT_LIBS RTAsan_static
233 CFLAGS ${ASAN_CFLAGS}
234 DEFS ${ASAN_COMMON_DEFINITIONS}
237 # Build separate libraries for each target.
239 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
242 RTSanitizerCommonLibc
243 RTSanitizerCommonCoverage
244 RTSanitizerCommonSymbolizer
245 RTSanitizerCommonSymbolizerInternal
249 add_compiler_rt_runtime(clang_rt.asan
251 ARCHS ${ASAN_SUPPORTED_ARCH}
252 OBJECT_LIBS RTAsan_preinit
254 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
255 CFLAGS ${ASAN_CFLAGS}
256 DEFS ${ASAN_COMMON_DEFINITIONS}
259 add_compiler_rt_runtime(clang_rt.asan_cxx
261 ARCHS ${ASAN_SUPPORTED_ARCH}
262 OBJECT_LIBS RTAsan_cxx
264 CFLAGS ${ASAN_CFLAGS}
265 DEFS ${ASAN_COMMON_DEFINITIONS}
268 add_compiler_rt_runtime(clang_rt.asan_static
270 ARCHS ${ASAN_SUPPORTED_ARCH}
271 OBJECT_LIBS RTAsan_static
272 CFLAGS ${ASAN_CFLAGS}
273 DEFS ${ASAN_COMMON_DEFINITIONS}
276 add_compiler_rt_runtime(clang_rt.asan-preinit
278 ARCHS ${ASAN_SUPPORTED_ARCH}
279 OBJECT_LIBS RTAsan_preinit
280 CFLAGS ${ASAN_CFLAGS}
281 DEFS ${ASAN_COMMON_DEFINITIONS}
285 foreach(arch ${ASAN_SUPPORTED_ARCH})
286 if (COMPILER_RT_HAS_VERSION_SCRIPT)
288 set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch})
290 set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch})
292 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
293 LIBS ${SANITIZER_RT_VERSION_LIST_LIBS}
294 EXTRA asan.syms.extra)
295 set(VERSION_SCRIPT_FLAG
296 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
297 # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
298 # but requires a special option to enable it.
299 if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
300 list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
303 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
305 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
307 set(VERSION_SCRIPT_FLAG)
310 set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
311 add_compiler_rt_runtime(clang_rt.asan
314 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
316 # The only purpose of RTAsan_dynamic_version_script_dummy is to
317 # carry a dependency of the shared runtime on the version script.
318 # Replacing it with a straightforward
319 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
320 # generates an order-only dependency in ninja.
321 RTAsan_dynamic_version_script_dummy
323 ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
324 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
325 LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
326 ${VERSION_SCRIPT_FLAG}
327 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
328 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
331 if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
332 add_sanitizer_rt_symbols(clang_rt.asan_cxx
334 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
335 add_sanitizer_rt_symbols(clang_rt.asan
337 EXTRA asan.syms.extra)
338 add_dependencies(asan clang_rt.asan-${arch}-symbols)
342 set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
344 add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
345 ${SANITIZER_COMMON_SUPPORTED_OS}
347 SOURCES ${ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES}
348 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
349 DEFS ${ASAN_COMMON_DEFINITIONS})
351 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
354 OBJECT_LIBS AsanDynamicRuntimeThunk
357 SanitizerRuntimeThunk
358 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
359 DEFS ${ASAN_COMMON_DEFINITIONS}
362 # mingw does not support static linkage of the CRT
364 set(STATIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_STATIC_RUNTIME_THUNK")
366 add_compiler_rt_object_libraries(AsanStaticRuntimeThunk
367 ${SANITIZER_COMMON_SUPPORTED_OS}
369 SOURCES ${ASAN_STATIC_RUNTIME_THUNK_SOURCES}
370 CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
371 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
373 add_compiler_rt_runtime(clang_rt.asan_static_runtime_thunk
376 OBJECT_LIBS AsanStaticRuntimeThunk
379 SanitizerRuntimeThunk
380 CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
381 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
388 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
390 add_subdirectory(scripts)
392 if(COMPILER_RT_INCLUDE_TESTS)
393 add_subdirectory(tests)