1 # Build for the AddressSanitizer runtime support library.
16 asan_memory_profile.cc
30 set(ASAN_PREINIT_SOURCES
33 include_directories(..)
35 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
36 append_rtti_flag(OFF ASAN_CFLAGS)
38 set(ASAN_COMMON_DEFINITIONS
39 ASAN_HAS_EXCEPTIONS=1)
41 set(ASAN_DYNAMIC_LINK_FLAGS)
44 list(APPEND ASAN_COMMON_DEFINITIONS
46 # On Android, -z global does not do what it is documented to do.
47 # On Android, -z global moves the library ahead in the lookup order,
48 # placing it right after the LD_PRELOADs. This is used to compensate for the fact
49 # that Android linker does not look at the dependencies of the main executable
50 # that aren't dependencies of the current DSO when resolving symbols from said DSO.
51 # As a net result, this allows running ASan executables without LD_PRELOAD-ing the
52 # ASan runtime library.
53 # The above is applicable to L MR1 or newer.
54 if (COMPILER_RT_HAS_Z_GLOBAL)
55 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
59 set(ASAN_DYNAMIC_DEFINITIONS
60 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
61 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
63 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
64 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
65 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
66 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
68 append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
69 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
70 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
71 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
72 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
73 append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ ASAN_DYNAMIC_LIBS)
74 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
76 # Compile ASan sources into an object library.
78 add_compiler_rt_object_libraries(RTAsan_dynamic
79 OS ${SANITIZER_COMMON_SUPPORTED_OS}
80 ARCHS ${ASAN_SUPPORTED_ARCH}
81 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
82 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
83 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
86 add_compiler_rt_object_libraries(RTAsan
87 ARCHS ${ASAN_SUPPORTED_ARCH}
88 SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
89 DEFS ${ASAN_COMMON_DEFINITIONS})
90 add_compiler_rt_object_libraries(RTAsan_cxx
91 ARCHS ${ASAN_SUPPORTED_ARCH}
92 SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
93 DEFS ${ASAN_COMMON_DEFINITIONS})
94 add_compiler_rt_object_libraries(RTAsan_preinit
95 ARCHS ${ASAN_SUPPORTED_ARCH}
96 SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
97 DEFS ${ASAN_COMMON_DEFINITIONS})
99 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "")
100 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
101 ARCHS ${ASAN_SUPPORTED_ARCH}
102 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
103 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
104 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
107 # Build ASan runtimes shipped with Clang.
108 add_custom_target(asan)
109 set_target_properties(asan PROPERTIES FOLDER "Compiler-RT Misc")
112 add_compiler_rt_runtime(clang_rt.asan
114 OS ${SANITIZER_COMMON_SUPPORTED_OS}
115 ARCHS ${ASAN_SUPPORTED_ARCH}
116 OBJECT_LIBS RTAsan_dynamic
119 RTSanitizerCommonLibc
122 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
123 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
126 # Build separate libraries for each target.
128 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
131 RTSanitizerCommonLibc
135 add_compiler_rt_runtime(clang_rt.asan
137 ARCHS ${ASAN_SUPPORTED_ARCH}
138 OBJECT_LIBS RTAsan_preinit
140 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
141 CFLAGS ${ASAN_CFLAGS}
142 DEFS ${ASAN_COMMON_DEFINITIONS}
145 add_compiler_rt_runtime(clang_rt.asan_cxx
147 ARCHS ${ASAN_SUPPORTED_ARCH}
148 OBJECT_LIBS RTAsan_cxx
150 CFLAGS ${ASAN_CFLAGS}
151 DEFS ${ASAN_COMMON_DEFINITIONS}
154 add_compiler_rt_runtime(clang_rt.asan-preinit
156 ARCHS ${ASAN_SUPPORTED_ARCH}
157 OBJECT_LIBS RTAsan_preinit
158 CFLAGS ${ASAN_CFLAGS}
159 DEFS ${ASAN_COMMON_DEFINITIONS}
162 foreach(arch ${ASAN_SUPPORTED_ARCH})
163 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
164 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
165 LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
166 EXTRA asan.syms.extra)
167 set(VERSION_SCRIPT_FLAG
168 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
169 set_source_files_properties(
170 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
172 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
174 set(VERSION_SCRIPT_FLAG)
177 add_compiler_rt_runtime(clang_rt.asan
180 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
182 # The only purpose of RTAsan_dynamic_version_script_dummy is to carry
183 # a dependency of the shared runtime on the version script. With CMake
184 # 3.1 or later it can be replaced with a straightforward
185 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
186 RTAsan_dynamic_version_script_dummy
188 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
189 LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
190 ${VERSION_SCRIPT_FLAG}
191 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
192 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
195 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
196 add_sanitizer_rt_symbols(clang_rt.asan_cxx
198 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
199 add_sanitizer_rt_symbols(clang_rt.asan
201 EXTRA asan.syms.extra)
202 add_dependencies(asan clang_rt.asan-${arch}-symbols)
206 add_compiler_rt_runtime(clang_rt.asan_dll_thunk
209 SOURCES asan_win_dll_thunk.cc
210 $<TARGET_OBJECTS:RTInterception.${arch}>
211 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
212 DEFS ${ASAN_COMMON_DEFINITIONS}
214 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
217 SOURCES asan_win_dynamic_runtime_thunk.cc
218 CFLAGS ${ASAN_CFLAGS} -DASAN_DYNAMIC_RUNTIME_THUNK -Zl
219 DEFS ${ASAN_COMMON_DEFINITIONS}
225 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt asan)
226 add_dependencies(compiler-rt asan)
228 add_subdirectory(scripts)
230 if(COMPILER_RT_INCLUDE_TESTS)
231 add_subdirectory(tests)