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 (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 ${SANITIZER_CXX_ABI_LIBRARIES} ${SANITIZER_COMMON_LINK_LIBS})
122 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
123 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
124 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
125 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
126 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
127 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
129 # Compile ASan sources into an object library.
131 add_compiler_rt_object_libraries(RTAsan_dynamic
132 OS ${SANITIZER_COMMON_SUPPORTED_OS}
133 ARCHS ${ASAN_SUPPORTED_ARCH}
134 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
135 ADDITIONAL_HEADERS ${ASAN_HEADERS}
136 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
137 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
140 add_compiler_rt_object_libraries(RTAsan
141 ARCHS ${ASAN_SUPPORTED_ARCH}
142 SOURCES ${ASAN_SOURCES}
143 ADDITIONAL_HEADERS ${ASAN_HEADERS}
144 CFLAGS ${ASAN_CFLAGS}
145 DEFS ${ASAN_COMMON_DEFINITIONS})
146 add_compiler_rt_object_libraries(RTAsan_cxx
147 ARCHS ${ASAN_SUPPORTED_ARCH}
148 SOURCES ${ASAN_CXX_SOURCES}
149 ADDITIONAL_HEADERS ${ASAN_HEADERS}
150 CFLAGS ${ASAN_CFLAGS}
151 DEFS ${ASAN_COMMON_DEFINITIONS})
152 add_compiler_rt_object_libraries(RTAsan_static
153 ARCHS ${ASAN_SUPPORTED_ARCH}
154 SOURCES ${ASAN_STATIC_SOURCES}
155 ADDITIONAL_HEADERS ${ASAN_HEADERS}
156 CFLAGS ${ASAN_CFLAGS}
157 DEFS ${ASAN_COMMON_DEFINITIONS})
158 add_compiler_rt_object_libraries(RTAsan_preinit
159 ARCHS ${ASAN_SUPPORTED_ARCH}
160 SOURCES ${ASAN_PREINIT_SOURCES}
161 ADDITIONAL_HEADERS ${ASAN_HEADERS}
162 CFLAGS ${ASAN_CFLAGS}
163 DEFS ${ASAN_COMMON_DEFINITIONS})
165 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
166 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
167 ARCHS ${ASAN_SUPPORTED_ARCH}
168 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
169 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
170 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
173 # Build ASan runtimes shipped with Clang.
174 add_compiler_rt_component(asan)
177 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
178 add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
179 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
180 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
181 add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
183 add_compiler_rt_runtime(clang_rt.asan
185 OS ${SANITIZER_COMMON_SUPPORTED_OS}
186 ARCHS ${ASAN_SUPPORTED_ARCH}
187 OBJECT_LIBS RTAsan_dynamic
190 RTSanitizerCommonLibc
191 RTSanitizerCommonCoverage
192 RTSanitizerCommonSymbolizer
195 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
196 LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
197 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
200 add_compiler_rt_runtime(clang_rt.asan_static
202 ARCHS ${ASAN_SUPPORTED_ARCH}
203 OBJECT_LIBS RTAsan_static
204 CFLAGS ${ASAN_CFLAGS}
205 DEFS ${ASAN_COMMON_DEFINITIONS}
208 # Build separate libraries for each target.
210 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
213 RTSanitizerCommonLibc
214 RTSanitizerCommonCoverage
215 RTSanitizerCommonSymbolizer
219 add_compiler_rt_runtime(clang_rt.asan
221 ARCHS ${ASAN_SUPPORTED_ARCH}
222 OBJECT_LIBS RTAsan_preinit
224 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
225 CFLAGS ${ASAN_CFLAGS}
226 DEFS ${ASAN_COMMON_DEFINITIONS}
229 add_compiler_rt_runtime(clang_rt.asan_cxx
231 ARCHS ${ASAN_SUPPORTED_ARCH}
232 OBJECT_LIBS RTAsan_cxx
234 CFLAGS ${ASAN_CFLAGS}
235 DEFS ${ASAN_COMMON_DEFINITIONS}
238 add_compiler_rt_runtime(clang_rt.asan_static
240 ARCHS ${ASAN_SUPPORTED_ARCH}
241 OBJECT_LIBS RTAsan_static
242 CFLAGS ${ASAN_CFLAGS}
243 DEFS ${ASAN_COMMON_DEFINITIONS}
246 add_compiler_rt_runtime(clang_rt.asan-preinit
248 ARCHS ${ASAN_SUPPORTED_ARCH}
249 OBJECT_LIBS RTAsan_preinit
250 CFLAGS ${ASAN_CFLAGS}
251 DEFS ${ASAN_COMMON_DEFINITIONS}
254 foreach(arch ${ASAN_SUPPORTED_ARCH})
255 if (COMPILER_RT_HAS_VERSION_SCRIPT)
256 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
257 LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
258 EXTRA asan.syms.extra)
259 set(VERSION_SCRIPT_FLAG
260 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
261 # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
262 # but requires a special option to enable it.
263 if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
264 list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
267 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
269 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
271 set(VERSION_SCRIPT_FLAG)
274 set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
276 add_compiler_rt_object_libraries(AsanWeakInterception
277 ${SANITIZER_COMMON_SUPPORTED_OS}
280 asan_win_weak_interception.cpp
281 CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
282 DEFS ${ASAN_COMMON_DEFINITIONS})
283 set(ASAN_DYNAMIC_WEAK_INTERCEPTION
285 UbsanWeakInterception
286 SancovWeakInterception
287 SanitizerCommonWeakInterception)
290 add_compiler_rt_runtime(clang_rt.asan
293 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
295 # The only purpose of RTAsan_dynamic_version_script_dummy is to
296 # carry a dependency of the shared runtime on the version script.
297 # Replacing it with a straightforward
298 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
299 # generates an order-only dependency in ninja.
300 RTAsan_dynamic_version_script_dummy
302 ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
303 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
304 LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
305 ${VERSION_SCRIPT_FLAG}
306 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
307 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
310 if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
311 add_sanitizer_rt_symbols(clang_rt.asan_cxx
313 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
314 add_sanitizer_rt_symbols(clang_rt.asan
316 EXTRA asan.syms.extra)
317 add_dependencies(asan clang_rt.asan-${arch}-symbols)
321 add_compiler_rt_object_libraries(AsanDllThunk
322 ${SANITIZER_COMMON_SUPPORTED_OS}
324 SOURCES asan_globals_win.cpp
325 asan_win_dll_thunk.cpp
326 CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
327 DEFS ${ASAN_COMMON_DEFINITIONS})
329 add_compiler_rt_runtime(clang_rt.asan_dll_thunk
332 OBJECT_LIBS AsanDllThunk
335 SanitizerCommonDllThunk
336 SOURCES $<TARGET_OBJECTS:RTInterception.${arch}>
339 set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
341 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
342 elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
343 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
346 add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
347 ${SANITIZER_COMMON_SUPPORTED_OS}
349 SOURCES asan_globals_win.cpp
350 asan_win_dynamic_runtime_thunk.cpp
351 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
352 DEFS ${ASAN_COMMON_DEFINITIONS})
354 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
357 OBJECT_LIBS AsanDynamicRuntimeThunk
358 UbsanDynamicRuntimeThunk
359 SancovDynamicRuntimeThunk
360 SanitizerCommonDynamicRuntimeThunk
361 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
362 DEFS ${ASAN_COMMON_DEFINITIONS}
368 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
370 add_subdirectory(scripts)
372 if(COMPILER_RT_INCLUDE_TESTS)
373 add_subdirectory(tests)