1 # Build for the AddressSanitizer runtime support library.
15 asan_interceptors_memintrinsics.cpp
21 asan_memory_profile.cpp
24 asan_premap_shadow.cpp
35 if (NOT WIN32 AND NOT APPLE)
36 list(APPEND ASAN_SOURCES
37 asan_interceptors_vfork.S
45 set(ASAN_STATIC_SOURCES
49 if (ASAN_SUPPORTED_ARCH STREQUAL "x86_64" AND NOT WIN32 AND NOT APPLE)
50 list(APPEND ASAN_STATIC_SOURCES
55 set(ASAN_PREINIT_SOURCES
61 asan_activation_flags.inc
70 asan_interceptors_memintrinsics.h
72 asan_interface_internal.h
79 asan_scariness_score.h
86 include_directories(..)
88 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
89 set(ASAN_COMMON_DEFINITIONS ${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION})
91 append_rtti_flag(OFF ASAN_CFLAGS)
93 # Silence warnings in system headers with MSVC.
95 append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" ASAN_CFLAGS)
98 # Too many existing bugs, needs cleanup.
99 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
101 set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
104 # Put most Sanitizer shared libraries in the global group. For more details, see
105 # android-changes-for-ndk-developers.md#changes-to-library-search-order
106 if (COMPILER_RT_HAS_Z_GLOBAL)
107 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
111 set(ASAN_DYNAMIC_DEFINITIONS
112 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
113 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
115 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
116 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
117 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
118 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
120 set(ASAN_DYNAMIC_LIBS
121 ${COMPILER_RT_UNWINDER_LINK_LIBS}
122 ${SANITIZER_CXX_ABI_LIBRARIES}
123 ${SANITIZER_COMMON_LINK_LIBS})
125 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
126 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
127 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
128 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
129 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
130 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
132 # Compile ASan sources into an object library.
134 add_compiler_rt_object_libraries(RTAsan_dynamic
135 OS ${SANITIZER_COMMON_SUPPORTED_OS}
136 ARCHS ${ASAN_SUPPORTED_ARCH}
137 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
138 ADDITIONAL_HEADERS ${ASAN_HEADERS}
139 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
140 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
143 add_compiler_rt_object_libraries(RTAsan
144 ARCHS ${ASAN_SUPPORTED_ARCH}
145 SOURCES ${ASAN_SOURCES}
146 ADDITIONAL_HEADERS ${ASAN_HEADERS}
147 CFLAGS ${ASAN_CFLAGS}
148 DEFS ${ASAN_COMMON_DEFINITIONS})
149 add_compiler_rt_object_libraries(RTAsan_cxx
150 ARCHS ${ASAN_SUPPORTED_ARCH}
151 SOURCES ${ASAN_CXX_SOURCES}
152 ADDITIONAL_HEADERS ${ASAN_HEADERS}
153 CFLAGS ${ASAN_CFLAGS}
154 DEFS ${ASAN_COMMON_DEFINITIONS})
155 add_compiler_rt_object_libraries(RTAsan_static
156 ARCHS ${ASAN_SUPPORTED_ARCH}
157 SOURCES ${ASAN_STATIC_SOURCES}
158 ADDITIONAL_HEADERS ${ASAN_HEADERS}
159 CFLAGS ${ASAN_CFLAGS}
160 DEFS ${ASAN_COMMON_DEFINITIONS})
161 add_compiler_rt_object_libraries(RTAsan_preinit
162 ARCHS ${ASAN_SUPPORTED_ARCH}
163 SOURCES ${ASAN_PREINIT_SOURCES}
164 ADDITIONAL_HEADERS ${ASAN_HEADERS}
165 CFLAGS ${ASAN_CFLAGS}
166 DEFS ${ASAN_COMMON_DEFINITIONS})
168 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
169 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
170 ARCHS ${ASAN_SUPPORTED_ARCH}
171 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
172 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
173 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
176 # Build ASan runtimes shipped with Clang.
177 add_compiler_rt_component(asan)
180 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
181 add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
182 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
183 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
184 add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
186 add_compiler_rt_runtime(clang_rt.asan
188 OS ${SANITIZER_COMMON_SUPPORTED_OS}
189 ARCHS ${ASAN_SUPPORTED_ARCH}
190 OBJECT_LIBS RTAsan_dynamic
193 RTSanitizerCommonLibc
194 RTSanitizerCommonCoverage
195 RTSanitizerCommonSymbolizer
198 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
199 LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
200 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
203 add_compiler_rt_runtime(clang_rt.asan_static
205 ARCHS ${ASAN_SUPPORTED_ARCH}
206 OBJECT_LIBS RTAsan_static
207 CFLAGS ${ASAN_CFLAGS}
208 DEFS ${ASAN_COMMON_DEFINITIONS}
211 # Build separate libraries for each target.
213 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
216 RTSanitizerCommonLibc
217 RTSanitizerCommonCoverage
218 RTSanitizerCommonSymbolizer
219 RTSanitizerCommonSymbolizerInternal
223 add_compiler_rt_runtime(clang_rt.asan
225 ARCHS ${ASAN_SUPPORTED_ARCH}
226 OBJECT_LIBS RTAsan_preinit
228 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
229 CFLAGS ${ASAN_CFLAGS}
230 DEFS ${ASAN_COMMON_DEFINITIONS}
233 add_compiler_rt_runtime(clang_rt.asan_cxx
235 ARCHS ${ASAN_SUPPORTED_ARCH}
236 OBJECT_LIBS RTAsan_cxx
238 CFLAGS ${ASAN_CFLAGS}
239 DEFS ${ASAN_COMMON_DEFINITIONS}
242 add_compiler_rt_runtime(clang_rt.asan_static
244 ARCHS ${ASAN_SUPPORTED_ARCH}
245 OBJECT_LIBS RTAsan_static
246 CFLAGS ${ASAN_CFLAGS}
247 DEFS ${ASAN_COMMON_DEFINITIONS}
250 add_compiler_rt_runtime(clang_rt.asan-preinit
252 ARCHS ${ASAN_SUPPORTED_ARCH}
253 OBJECT_LIBS RTAsan_preinit
254 CFLAGS ${ASAN_CFLAGS}
255 DEFS ${ASAN_COMMON_DEFINITIONS}
258 foreach(arch ${ASAN_SUPPORTED_ARCH})
259 if (COMPILER_RT_HAS_VERSION_SCRIPT)
260 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
261 LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
262 EXTRA asan.syms.extra)
263 set(VERSION_SCRIPT_FLAG
264 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
265 # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
266 # but requires a special option to enable it.
267 if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
268 list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
271 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
273 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
275 set(VERSION_SCRIPT_FLAG)
278 set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
280 add_compiler_rt_object_libraries(AsanWeakInterception
281 ${SANITIZER_COMMON_SUPPORTED_OS}
284 asan_win_weak_interception.cpp
285 CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
286 DEFS ${ASAN_COMMON_DEFINITIONS})
287 set(ASAN_DYNAMIC_WEAK_INTERCEPTION
289 UbsanWeakInterception
290 SancovWeakInterception
291 SanitizerCommonWeakInterception)
294 add_compiler_rt_runtime(clang_rt.asan
297 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
299 # The only purpose of RTAsan_dynamic_version_script_dummy is to
300 # carry a dependency of the shared runtime on the version script.
301 # Replacing it with a straightforward
302 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
303 # generates an order-only dependency in ninja.
304 RTAsan_dynamic_version_script_dummy
306 ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
307 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
308 LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
309 ${VERSION_SCRIPT_FLAG}
310 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
311 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
314 if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
315 add_sanitizer_rt_symbols(clang_rt.asan_cxx
317 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
318 add_sanitizer_rt_symbols(clang_rt.asan
320 EXTRA asan.syms.extra)
321 add_dependencies(asan clang_rt.asan-${arch}-symbols)
325 add_compiler_rt_object_libraries(AsanDllThunk
326 ${SANITIZER_COMMON_SUPPORTED_OS}
328 SOURCES asan_globals_win.cpp
329 asan_win_dll_thunk.cpp
330 CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
331 DEFS ${ASAN_COMMON_DEFINITIONS})
333 add_compiler_rt_runtime(clang_rt.asan_dll_thunk
336 OBJECT_LIBS AsanDllThunk
339 SanitizerCommonDllThunk
340 SOURCES $<TARGET_OBJECTS:RTInterception.${arch}>
343 set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
345 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
346 elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
347 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
350 add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
351 ${SANITIZER_COMMON_SUPPORTED_OS}
353 SOURCES asan_globals_win.cpp
354 asan_win_dynamic_runtime_thunk.cpp
355 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
356 DEFS ${ASAN_COMMON_DEFINITIONS})
358 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
361 OBJECT_LIBS AsanDynamicRuntimeThunk
362 UbsanDynamicRuntimeThunk
363 SancovDynamicRuntimeThunk
364 SanitizerCommonDynamicRuntimeThunk
365 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
366 DEFS ${ASAN_COMMON_DEFINITIONS}
372 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
374 add_subdirectory(scripts)
376 if(COMPILER_RT_INCLUDE_TESTS)
377 add_subdirectory(tests)