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.
94 append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external /external:W0 /external:anglebrackets" ASAN_CFLAGS)
96 # Too many existing bugs, needs cleanup.
97 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
99 set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
102 # Put most Sanitizer shared libraries in the global group. For more details, see
103 # android-changes-for-ndk-developers.md#changes-to-library-search-order
104 if (COMPILER_RT_HAS_Z_GLOBAL)
105 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
109 set(ASAN_DYNAMIC_DEFINITIONS
110 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
111 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
113 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
114 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
115 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
116 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
118 set(ASAN_DYNAMIC_LIBS ${SANITIZER_CXX_ABI_LIBRARIES} ${SANITIZER_COMMON_LINK_LIBS})
120 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
121 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
122 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
123 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
124 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
125 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
127 # Compile ASan sources into an object library.
129 add_compiler_rt_object_libraries(RTAsan_dynamic
130 OS ${SANITIZER_COMMON_SUPPORTED_OS}
131 ARCHS ${ASAN_SUPPORTED_ARCH}
132 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
133 ADDITIONAL_HEADERS ${ASAN_HEADERS}
134 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
135 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
138 add_compiler_rt_object_libraries(RTAsan
139 ARCHS ${ASAN_SUPPORTED_ARCH}
140 SOURCES ${ASAN_SOURCES}
141 ADDITIONAL_HEADERS ${ASAN_HEADERS}
142 CFLAGS ${ASAN_CFLAGS}
143 DEFS ${ASAN_COMMON_DEFINITIONS})
144 add_compiler_rt_object_libraries(RTAsan_cxx
145 ARCHS ${ASAN_SUPPORTED_ARCH}
146 SOURCES ${ASAN_CXX_SOURCES}
147 ADDITIONAL_HEADERS ${ASAN_HEADERS}
148 CFLAGS ${ASAN_CFLAGS}
149 DEFS ${ASAN_COMMON_DEFINITIONS})
150 add_compiler_rt_object_libraries(RTAsan_static
151 ARCHS ${ASAN_SUPPORTED_ARCH}
152 SOURCES ${ASAN_STATIC_SOURCES}
153 ADDITIONAL_HEADERS ${ASAN_HEADERS}
154 CFLAGS ${ASAN_CFLAGS}
155 DEFS ${ASAN_COMMON_DEFINITIONS})
156 add_compiler_rt_object_libraries(RTAsan_preinit
157 ARCHS ${ASAN_SUPPORTED_ARCH}
158 SOURCES ${ASAN_PREINIT_SOURCES}
159 ADDITIONAL_HEADERS ${ASAN_HEADERS}
160 CFLAGS ${ASAN_CFLAGS}
161 DEFS ${ASAN_COMMON_DEFINITIONS})
163 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
164 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
165 ARCHS ${ASAN_SUPPORTED_ARCH}
166 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
167 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
168 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
171 # Build ASan runtimes shipped with Clang.
172 add_compiler_rt_component(asan)
175 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
176 add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
177 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
178 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
179 add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
181 add_compiler_rt_runtime(clang_rt.asan
183 OS ${SANITIZER_COMMON_SUPPORTED_OS}
184 ARCHS ${ASAN_SUPPORTED_ARCH}
185 OBJECT_LIBS RTAsan_dynamic
188 RTSanitizerCommonLibc
189 RTSanitizerCommonCoverage
190 RTSanitizerCommonSymbolizer
193 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
194 LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
195 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
198 add_compiler_rt_runtime(clang_rt.asan_static
200 ARCHS ${ASAN_SUPPORTED_ARCH}
201 OBJECT_LIBS RTAsan_static
202 CFLAGS ${ASAN_CFLAGS}
203 DEFS ${ASAN_COMMON_DEFINITIONS}
206 # Build separate libraries for each target.
208 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
211 RTSanitizerCommonLibc
212 RTSanitizerCommonCoverage
213 RTSanitizerCommonSymbolizer
217 add_compiler_rt_runtime(clang_rt.asan
219 ARCHS ${ASAN_SUPPORTED_ARCH}
220 OBJECT_LIBS RTAsan_preinit
222 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
223 CFLAGS ${ASAN_CFLAGS}
224 DEFS ${ASAN_COMMON_DEFINITIONS}
227 add_compiler_rt_runtime(clang_rt.asan_cxx
229 ARCHS ${ASAN_SUPPORTED_ARCH}
230 OBJECT_LIBS RTAsan_cxx
232 CFLAGS ${ASAN_CFLAGS}
233 DEFS ${ASAN_COMMON_DEFINITIONS}
236 add_compiler_rt_runtime(clang_rt.asan_static
238 ARCHS ${ASAN_SUPPORTED_ARCH}
239 OBJECT_LIBS RTAsan_static
240 CFLAGS ${ASAN_CFLAGS}
241 DEFS ${ASAN_COMMON_DEFINITIONS}
244 add_compiler_rt_runtime(clang_rt.asan-preinit
246 ARCHS ${ASAN_SUPPORTED_ARCH}
247 OBJECT_LIBS RTAsan_preinit
248 CFLAGS ${ASAN_CFLAGS}
249 DEFS ${ASAN_COMMON_DEFINITIONS}
252 foreach(arch ${ASAN_SUPPORTED_ARCH})
253 if (COMPILER_RT_HAS_VERSION_SCRIPT)
254 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
255 LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
256 EXTRA asan.syms.extra)
257 set(VERSION_SCRIPT_FLAG
258 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
259 # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
260 # but requires a special option to enable it.
261 if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
262 list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
265 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
267 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
269 set(VERSION_SCRIPT_FLAG)
272 set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
274 add_compiler_rt_object_libraries(AsanWeakInterception
275 ${SANITIZER_COMMON_SUPPORTED_OS}
277 SOURCES asan_win_weak_interception.cpp
278 CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
279 DEFS ${ASAN_COMMON_DEFINITIONS})
280 set(ASAN_DYNAMIC_WEAK_INTERCEPTION
282 UbsanWeakInterception
283 SancovWeakInterception
284 SanitizerCommonWeakInterception)
287 add_compiler_rt_runtime(clang_rt.asan
290 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
292 # The only purpose of RTAsan_dynamic_version_script_dummy is to
293 # carry a dependency of the shared runtime on the version script.
294 # Replacing it with a straightforward
295 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
296 # generates an order-only dependency in ninja.
297 RTAsan_dynamic_version_script_dummy
299 ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
300 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
301 LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
302 ${VERSION_SCRIPT_FLAG}
303 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
304 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
307 if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
308 add_sanitizer_rt_symbols(clang_rt.asan_cxx
310 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
311 add_sanitizer_rt_symbols(clang_rt.asan
313 EXTRA asan.syms.extra)
314 add_dependencies(asan clang_rt.asan-${arch}-symbols)
318 add_compiler_rt_object_libraries(AsanDllThunk
319 ${SANITIZER_COMMON_SUPPORTED_OS}
321 SOURCES asan_globals_win.cpp
322 asan_win_dll_thunk.cpp
323 CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
324 DEFS ${ASAN_COMMON_DEFINITIONS})
326 add_compiler_rt_runtime(clang_rt.asan_dll_thunk
329 OBJECT_LIBS AsanDllThunk
332 SanitizerCommonDllThunk
333 SOURCES $<TARGET_OBJECTS:RTInterception.${arch}>
336 set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
338 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
339 elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
340 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
343 add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
344 ${SANITIZER_COMMON_SUPPORTED_OS}
346 SOURCES asan_globals_win.cpp
347 asan_win_dynamic_runtime_thunk.cpp
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
355 UbsanDynamicRuntimeThunk
356 SancovDynamicRuntimeThunk
357 SanitizerCommonDynamicRuntimeThunk
358 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
359 DEFS ${ASAN_COMMON_DEFINITIONS}
365 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
367 add_subdirectory(scripts)
369 if(COMPILER_RT_INCLUDE_TESTS)
370 add_subdirectory(tests)