blob: 6716f48b22bd46466392fbb1c45a55ad7764dc0a [file] [log] [blame]
Chandler Carruthd51e0a02012-04-04 22:12:04 +00001# Build for the AddressSanitizer runtime support library.
2
Chandler Carruth479d47f2012-06-20 10:18:43 +00003set(ASAN_SOURCES
Stephen Hines86277eb2015-03-23 12:06:32 -07004 asan_allocator.cc
Stephen Hines2d1fdb22014-05-28 23:58:16 -07005 asan_activation.cc
Stephen Hines6d186232014-11-26 17:56:19 -08006 asan_debugging.cc
Kostya Serebryany2679f192012-12-10 14:19:15 +00007 asan_fake_stack.cc
Stephen Hines86277eb2015-03-23 12:06:32 -07008 asan_flags.cc
Chandler Carruth479d47f2012-06-20 10:18:43 +00009 asan_globals.cc
10 asan_interceptors.cc
11 asan_linux.cc
12 asan_mac.cc
13 asan_malloc_linux.cc
14 asan_malloc_mac.cc
15 asan_malloc_win.cc
Chandler Carruth479d47f2012-06-20 10:18:43 +000016 asan_poisoning.cc
17 asan_posix.cc
Alexey Samsonovf7c1d182012-08-09 08:15:46 +000018 asan_report.cc
Chandler Carruth479d47f2012-06-20 10:18:43 +000019 asan_rtl.cc
20 asan_stack.cc
21 asan_stats.cc
Stephen Hines86277eb2015-03-23 12:06:32 -070022 asan_suppressions.cc
Chandler Carruth479d47f2012-06-20 10:18:43 +000023 asan_thread.cc
Alexey Samsonovc3b30b32013-06-22 16:33:52 +000024 asan_win.cc)
Chandler Carruthd51e0a02012-04-04 22:12:04 +000025
Stephen Hines2d1fdb22014-05-28 23:58:16 -070026set(ASAN_CXX_SOURCES
27 asan_new_delete.cc)
28
29set(ASAN_PREINIT_SOURCES
30 asan_preinit.cc)
31
Chandler Carruth821f21b2012-06-25 12:57:43 +000032include_directories(..)
33
Stephen Hines2d1fdb22014-05-28 23:58:16 -070034set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
35append_no_rtti_flag(ASAN_CFLAGS)
36
Alexey Samsonov2aed0402013-01-21 08:24:19 +000037set(ASAN_COMMON_DEFINITIONS
Alexey Samsonov5e5be212013-01-22 07:21:24 +000038 ASAN_HAS_EXCEPTIONS=1)
Alexey Samsonov2aed0402013-01-21 08:24:19 +000039
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070040set(ASAN_DYNAMIC_LINK_FLAGS)
41
Evgeniy Stepanov34fc56c2012-09-11 11:55:45 +000042if(ANDROID)
Alexey Samsonov2aed0402013-01-21 08:24:19 +000043 list(APPEND ASAN_COMMON_DEFINITIONS
Alexey Samsonov2aed0402013-01-21 08:24:19 +000044 ASAN_LOW_MEMORY=1)
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070045# On Android, -z global does not do what it is documented to do.
46# On Android, -z global moves the library ahead in the lookup order,
47# placing it right after the LD_PRELOADs. This is used to compensate for the fact
48# that Android linker does not look at the dependencies of the main executable
49# that aren't dependencies of the current DSO when resolving symbols from said DSO.
50# As a net result, this allows running ASan executables without LD_PRELOAD-ing the
51# ASan runtime library.
52# The above is applicable to L MR1 or newer.
53 if (COMPILER_RT_HAS_Z_GLOBAL)
54 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
55 endif()
Evgeniy Stepanov34fc56c2012-09-11 11:55:45 +000056endif()
Alexander Potapenko0ef53102012-08-17 09:00:08 +000057
Stephen Hines2d1fdb22014-05-28 23:58:16 -070058set(ASAN_DYNAMIC_DEFINITIONS
59 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
Stephen Hines6d186232014-11-26 17:56:19 -080060append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070061
62set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
Stephen Hines6d186232014-11-26 17:56:19 -080063append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
Stephen Hines2d1fdb22014-05-28 23:58:16 -070064 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
Stephen Hines6d186232014-11-26 17:56:19 -080065append_list_if(MSVC /DEBUG ASAN_DYNAMIC_CFLAGS)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070066
Stephen Hines6d186232014-11-26 17:56:19 -080067append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
68append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070069append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
Stephen Hines6d186232014-11-26 17:56:19 -080070append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
71append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
72append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ ASAN_DYNAMIC_LIBS)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080073append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070074
Alexey Samsonov05fa3802013-09-16 15:50:53 +000075# Compile ASan sources into an object library.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080076
77add_compiler_rt_object_libraries(RTAsan_dynamic
78 OS ${SANITIZER_COMMON_SUPPORTED_OS}
79 ARCHS ${ASAN_SUPPORTED_ARCH}
80 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
81 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
82 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
83
84if(NOT APPLE)
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070085 add_compiler_rt_object_libraries(RTAsan
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080086 ARCHS ${ASAN_SUPPORTED_ARCH}
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070087 SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
88 DEFS ${ASAN_COMMON_DEFINITIONS})
89 add_compiler_rt_object_libraries(RTAsan_cxx
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080090 ARCHS ${ASAN_SUPPORTED_ARCH}
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070091 SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
92 DEFS ${ASAN_COMMON_DEFINITIONS})
93 add_compiler_rt_object_libraries(RTAsan_preinit
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -080094 ARCHS ${ASAN_SUPPORTED_ARCH}
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070095 SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
96 DEFS ${ASAN_COMMON_DEFINITIONS})
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070097
98 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "")
99 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800100 ARCHS ${ASAN_SUPPORTED_ARCH}
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700101 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
102 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
103 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000104endif()
105
106# Build ASan runtimes shipped with Clang.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700107add_custom_target(asan)
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000108if(APPLE)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800109 add_compiler_rt_runtime(clang_rt.asan
110 SHARED
111 OS ${SANITIZER_COMMON_SUPPORTED_OS}
112 ARCHS ${ASAN_SUPPORTED_ARCH}
113 OBJECT_LIBS RTAsan_dynamic
114 RTInterception
115 RTSanitizerCommon
116 RTSanitizerCommonLibc
117 RTLSanCommon
118 RTUbsan
119 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
120 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
121 PARENT_TARGET asan)
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000122else()
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000123 # Build separate libraries for each target.
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800124
125 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
126 RTInterception
127 RTSanitizerCommon
128 RTSanitizerCommonLibc
129 RTLSanCommon
130 RTUbsan)
131
132 add_compiler_rt_runtime(clang_rt.asan
133 STATIC
134 ARCHS ${ASAN_SUPPORTED_ARCH}
135 OBJECT_LIBS RTAsan_preinit
136 RTAsan
137 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
138 CFLAGS ${ASAN_CFLAGS}
139 DEFS ${ASAN_COMMON_DEFINITIONS}
140 PARENT_TARGET asan)
141
142 add_compiler_rt_runtime(clang_rt.asan_cxx
143 STATIC
144 ARCHS ${ASAN_SUPPORTED_ARCH}
145 OBJECT_LIBS RTAsan_cxx
146 RTUbsan_cxx
147 CFLAGS ${ASAN_CFLAGS}
148 DEFS ${ASAN_COMMON_DEFINITIONS}
149 PARENT_TARGET asan)
150
151 add_compiler_rt_runtime(clang_rt.asan-preinit
152 STATIC
153 ARCHS ${ASAN_SUPPORTED_ARCH}
154 OBJECT_LIBS RTAsan_preinit
155 CFLAGS ${ASAN_CFLAGS}
156 DEFS ${ASAN_COMMON_DEFINITIONS}
157 PARENT_TARGET asan)
158
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000159 foreach(arch ${ASAN_SUPPORTED_ARCH})
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700160 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
161 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800162 LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700163 EXTRA asan.syms.extra)
164 set(VERSION_SCRIPT_FLAG
165 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
166 set_source_files_properties(
167 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
168 PROPERTIES
169 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
170 else()
171 set(VERSION_SCRIPT_FLAG)
172 endif()
173
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800174 add_compiler_rt_runtime(clang_rt.asan
175 SHARED
176 ARCHS ${arch}
177 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
178 RTAsan_dynamic
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700179 # The only purpose of RTAsan_dynamic_version_script_dummy is to carry
180 # a dependency of the shared runtime on the version script. With CMake
181 # 3.1 or later it can be replaced with a straightforward
182 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800183 RTAsan_dynamic_version_script_dummy
184 RTUbsan_cxx
Stephen Hines86277eb2015-03-23 12:06:32 -0700185 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700186 LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
187 ${VERSION_SCRIPT_FLAG}
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800188 LINK_LIBS ${ASAN_DYNAMIC_LIBS}
189 DEFS ${ASAN_DYNAMIC_DEFINITIONS}
190 PARENT_TARGET asan)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700191
Stephen Hines86277eb2015-03-23 12:06:32 -0700192 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800193 add_sanitizer_rt_symbols(clang_rt.asan_cxx
194 ARCHS ${arch})
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700195 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800196 add_sanitizer_rt_symbols(clang_rt.asan
197 ARCHS ${arch}
198 EXTRA asan.syms.extra)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700199 add_dependencies(asan clang_rt.asan-${arch}-symbols)
Alexey Samsonov544bdfb2013-08-28 08:07:04 +0000200 endif()
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000201
202 if (WIN32)
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800203 add_compiler_rt_runtime(clang_rt.asan_dll_thunk
204 STATIC
205 ARCHS ${arch}
Stephen Hines6d186232014-11-26 17:56:19 -0800206 SOURCES asan_win_dll_thunk.cc
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700207 $<TARGET_OBJECTS:RTInterception.${arch}>
208 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800209 DEFS ${ASAN_COMMON_DEFINITIONS}
210 PARENT_TARGET asan)
211 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
Stephen Hines6d186232014-11-26 17:56:19 -0800212 STATIC
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800213 ARCHS ${arch}
Stephen Hines6d186232014-11-26 17:56:19 -0800214 SOURCES asan_win_dynamic_runtime_thunk.cc
215 CFLAGS ${ASAN_CFLAGS} -DASAN_DYNAMIC_RUNTIME_THUNK -Zl
Pirama Arumuga Nainar799172d2016-03-03 15:50:30 -0800216 DEFS ${ASAN_COMMON_DEFINITIONS}
217 PARENT_TARGET asan)
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000218 endif()
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000219 endforeach()
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000220endif()
Alexey Samsonov4d57f442012-07-25 11:15:00 +0000221
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000222add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700223add_dependencies(asan asan_blacklist)
224add_dependencies(compiler-rt asan)
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000225
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700226add_subdirectory(scripts)
Alexey Samsonov8c6e4852013-11-14 12:24:12 +0000227
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700228if(COMPILER_RT_INCLUDE_TESTS)
Chandler Carruth821f21b2012-06-25 12:57:43 +0000229 add_subdirectory(tests)
230endif()