1 # Build for the AddressSanitizer runtime support library.
19 asan_memory_profile.cc
33 set(ASAN_PREINIT_SOURCES
36 include_directories(..)
38 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
39 append_rtti_flag(OFF ASAN_CFLAGS)
41 set(ASAN_DYNAMIC_LINK_FLAGS)
44 # On Android, -z global does not do what it is documented to do.
45 # On Android, -z global moves the library ahead in the lookup order,
46 # placing it right after the LD_PRELOADs. This is used to compensate for the fact
47 # that Android linker does not look at the dependencies of the main executable
48 # that aren't dependencies of the current DSO when resolving symbols from said DSO.
49 # As a net result, this allows running ASan executables without LD_PRELOAD-ing the
50 # ASan runtime library.
51 # The above is applicable to L MR1 or newer.
52 if (COMPILER_RT_HAS_Z_GLOBAL)
53 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
57 set(ASAN_DYNAMIC_DEFINITIONS
58 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
59 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
61 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
62 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
63 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
64 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
66 append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
67 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
68 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
69 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
70 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
71 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ ASAN_DYNAMIC_LIBS)
72 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
74 # Compile ASan sources into an object library.
76 add_compiler_rt_object_libraries(RTAsan_dynamic
77 OS ${SANITIZER_COMMON_SUPPORTED_OS}
78 ARCHS ${ASAN_SUPPORTED_ARCH}
79 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
80 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
81 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
84 add_compiler_rt_object_libraries(RTAsan
85 ARCHS ${ASAN_SUPPORTED_ARCH}
86 SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
87 DEFS ${ASAN_COMMON_DEFINITIONS})
88 add_compiler_rt_object_libraries(RTAsan_cxx
89 ARCHS ${ASAN_SUPPORTED_ARCH}
90 SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
91 DEFS ${ASAN_COMMON_DEFINITIONS})
92 add_compiler_rt_object_libraries(RTAsan_preinit
93 ARCHS ${ASAN_SUPPORTED_ARCH}
94 SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
95 DEFS ${ASAN_COMMON_DEFINITIONS})
97 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "")
98 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
99 ARCHS ${ASAN_SUPPORTED_ARCH}
100 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
101 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
102 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
105 # Build ASan runtimes shipped with Clang.
106 add_compiler_rt_component(asan)
109 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
110 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
111 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
113 add_compiler_rt_runtime(clang_rt.asan
115 OS ${SANITIZER_COMMON_SUPPORTED_OS}
116 ARCHS ${ASAN_SUPPORTED_ARCH}
117 OBJECT_LIBS RTAsan_dynamic
120 RTSanitizerCommonLibc
123 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
124 LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
125 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
128 # Build separate libraries for each target.
130 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
133 RTSanitizerCommonLibc
137 add_compiler_rt_runtime(clang_rt.asan
139 ARCHS ${ASAN_SUPPORTED_ARCH}
140 OBJECT_LIBS RTAsan_preinit
142 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
143 CFLAGS ${ASAN_CFLAGS}
144 DEFS ${ASAN_COMMON_DEFINITIONS}
147 add_compiler_rt_runtime(clang_rt.asan_cxx
149 ARCHS ${ASAN_SUPPORTED_ARCH}
150 OBJECT_LIBS RTAsan_cxx
152 CFLAGS ${ASAN_CFLAGS}
153 DEFS ${ASAN_COMMON_DEFINITIONS}
156 add_compiler_rt_runtime(clang_rt.asan-preinit
158 ARCHS ${ASAN_SUPPORTED_ARCH}
159 OBJECT_LIBS RTAsan_preinit
160 CFLAGS ${ASAN_CFLAGS}
161 DEFS ${ASAN_COMMON_DEFINITIONS}
164 foreach(arch ${ASAN_SUPPORTED_ARCH})
165 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
166 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
167 LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
168 EXTRA asan.syms.extra)
169 set(VERSION_SCRIPT_FLAG
170 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
171 set_source_files_properties(
172 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
174 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
176 set(VERSION_SCRIPT_FLAG)
179 add_compiler_rt_runtime(clang_rt.asan
182 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
184 # The only purpose of RTAsan_dynamic_version_script_dummy is to carry
185 # a dependency of the shared runtime on the version script. With CMake
186 # 3.1 or later it can be replaced with a straightforward
187 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
188 RTAsan_dynamic_version_script_dummy
190 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
191 LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
192 ${VERSION_SCRIPT_FLAG}
193 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
194 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
197 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
198 add_sanitizer_rt_symbols(clang_rt.asan_cxx
200 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
201 add_sanitizer_rt_symbols(clang_rt.asan
203 EXTRA asan.syms.extra)
204 add_dependencies(asan clang_rt.asan-${arch}-symbols)
208 add_compiler_rt_runtime(clang_rt.asan_dll_thunk
211 SOURCES asan_win_dll_thunk.cc
213 $<TARGET_OBJECTS:RTInterception.${arch}>
214 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
215 DEFS ${ASAN_COMMON_DEFINITIONS}
218 set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DASAN_DYNAMIC_RUNTIME_THUNK")
220 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
221 elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
222 list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
225 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
228 SOURCES asan_win_dynamic_runtime_thunk.cc
230 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
231 DEFS ${ASAN_COMMON_DEFINITIONS}
237 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt asan)
239 add_subdirectory(scripts)
241 if(COMPILER_RT_INCLUDE_TESTS)
242 add_subdirectory(tests)