blob: 447ee0b93bb70c93baeb32041f4e6bd7b6580424 [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)
73
74append_list_if(ANDROID log ASAN_DYNAMIC_LIBS)
Stephen Hines2d1fdb22014-05-28 23:58:16 -070075
Alexey Samsonov05fa3802013-09-16 15:50:53 +000076# Compile ASan sources into an object library.
77if(APPLE)
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070078 add_compiler_rt_object_libraries(RTAsan
79 OS ${SANITIZER_COMMON_SUPPORTED_OS}
80 ARCH ${ASAN_SUPPORTED_ARCH}
81 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
82 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
83 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
Alexey Samsonov05fa3802013-09-16 15:50:53 +000084else()
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -070085 add_compiler_rt_object_libraries(RTAsan
86 ARCH ${ASAN_SUPPORTED_ARCH}
87 SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
88 DEFS ${ASAN_COMMON_DEFINITIONS})
89 add_compiler_rt_object_libraries(RTAsan_cxx
90 ARCH ${ASAN_SUPPORTED_ARCH}
91 SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
92 DEFS ${ASAN_COMMON_DEFINITIONS})
93 add_compiler_rt_object_libraries(RTAsan_preinit
94 ARCH ${ASAN_SUPPORTED_ARCH}
95 SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
96 DEFS ${ASAN_COMMON_DEFINITIONS})
97 add_compiler_rt_object_libraries(RTAsan_dynamic
98 ARCH ${ASAN_SUPPORTED_ARCH}
99 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
100 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
101 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
102
103 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "")
104 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
105 ARCH ${ASAN_SUPPORTED_ARCH}
106 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
107 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
108 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000109endif()
110
111# Build ASan runtimes shipped with Clang.
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700112add_custom_target(asan)
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000113if(APPLE)
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700114 foreach (os ${SANITIZER_COMMON_SUPPORTED_OS})
Alexander Potapenko49496742013-11-07 10:08:19 +0000115 add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
116 ARCH ${ASAN_SUPPORTED_ARCH}
117 SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
118 $<TARGET_OBJECTS:RTInterception.${os}>
119 $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
120 $<TARGET_OBJECTS:RTLSanCommon.${os}>
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700121 $<TARGET_OBJECTS:RTUbsan.${os}>
Stephen Hines86277eb2015-03-23 12:06:32 -0700122 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
123 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700124 add_dependencies(asan clang_rt.asan_${os}_dynamic)
Alexander Potapenko49496742013-11-07 10:08:19 +0000125 endforeach()
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000126else()
Alexey Samsonov05fa3802013-09-16 15:50:53 +0000127 # Build separate libraries for each target.
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000128 foreach(arch ${ASAN_SUPPORTED_ARCH})
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700129 set(ASAN_COMMON_RUNTIME_OBJECTS
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000130 $<TARGET_OBJECTS:RTInterception.${arch}>
131 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
Pirama Arumuga Nainar7c915052015-04-08 08:58:29 -0700132 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700133 $<TARGET_OBJECTS:RTLSanCommon.${arch}>
134 $<TARGET_OBJECTS:RTUbsan.${arch}>)
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000135
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700136 add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
137 SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
138 $<TARGET_OBJECTS:RTAsan.${arch}>
139 ${ASAN_COMMON_RUNTIME_OBJECTS}
Alexey Samsonov2aad7c12013-01-21 08:12:20 +0000140 CFLAGS ${ASAN_CFLAGS}
Alexey Samsonove5fa2432013-08-27 15:08:02 +0000141 DEFS ${ASAN_COMMON_DEFINITIONS})
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700142 add_dependencies(asan clang_rt.asan-${arch})
143
144 add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
145 SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700146 $<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700147 CFLAGS ${ASAN_CFLAGS}
148 DEFS ${ASAN_COMMON_DEFINITIONS})
149 add_dependencies(asan clang_rt.asan_cxx-${arch})
150
Stephen Hines86277eb2015-03-23 12:06:32 -0700151 add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
152 SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
153 CFLAGS ${ASAN_CFLAGS}
154 DEFS ${ASAN_COMMON_DEFINITIONS})
155 add_dependencies(asan clang_rt.asan-preinit-${arch})
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700156
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700157 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
158 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
159 LIB clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
160 EXTRA asan.syms.extra)
161 set(VERSION_SCRIPT_FLAG
162 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
163 set_source_files_properties(
164 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
165 PROPERTIES
166 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
167 else()
168 set(VERSION_SCRIPT_FLAG)
169 endif()
170
Stephen Hines86277eb2015-03-23 12:06:32 -0700171 if (WIN32)
172 set(SHARED_ASAN_NAME clang_rt.asan_dynamic-${arch}${COMPILER_RT_OS_SUFFIX})
173 else()
174 set(SHARED_ASAN_NAME clang_rt.asan-${arch}${COMPILER_RT_OS_SUFFIX})
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700175 endif()
Stephen Hines86277eb2015-03-23 12:06:32 -0700176 add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
177 OUTPUT_NAME ${SHARED_ASAN_NAME}
178 SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
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)
183 $<TARGET_OBJECTS:RTAsan_dynamic_version_script_dummy.${arch}>
Pirama Arumuga Nainar259f7062015-05-06 11:49:53 -0700184 $<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
Stephen Hines86277eb2015-03-23 12:06:32 -0700185 ${ASAN_COMMON_RUNTIME_OBJECTS}
186 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
Pirama Arumuga Nainarcdce50b2015-07-01 12:26:56 -0700187 LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
188 ${VERSION_SCRIPT_FLAG}
Stephen Hines86277eb2015-03-23 12:06:32 -0700189 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
190 target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
191 add_dependencies(asan clang_rt.asan-dynamic-${arch})
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700192
Stephen Hines86277eb2015-03-23 12:06:32 -0700193 if (UNIX AND NOT ${arch} MATCHES "i386|i686")
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700194 add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
195 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
Alexey Samsonov544bdfb2013-08-28 08:07:04 +0000196 add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700197 add_dependencies(asan clang_rt.asan-${arch}-symbols)
Alexey Samsonov544bdfb2013-08-28 08:07:04 +0000198 endif()
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000199
200 if (WIN32)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700201 add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
Stephen Hines6d186232014-11-26 17:56:19 -0800202 SOURCES asan_win_dll_thunk.cc
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700203 $<TARGET_OBJECTS:RTInterception.${arch}>
204 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
205 DEFS ${ASAN_COMMON_DEFINITIONS})
206 add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
Stephen Hines6d186232014-11-26 17:56:19 -0800207 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk-${arch} ${arch}
208 STATIC
209 SOURCES asan_win_dynamic_runtime_thunk.cc
210 CFLAGS ${ASAN_CFLAGS} -DASAN_DYNAMIC_RUNTIME_THUNK -Zl
211 DEFS ${ASAN_COMMON_DEFINITIONS})
212 add_dependencies(asan clang_rt.asan_dynamic_runtime_thunk-${arch})
Hans Wennborgc1f1af72013-08-27 01:24:01 +0000213 endif()
Alexey Samsonov43b4b9c2013-01-18 16:51:07 +0000214 endforeach()
Alexey Samsonovfe51abb2012-08-10 14:45:52 +0000215endif()
Alexey Samsonov4d57f442012-07-25 11:15:00 +0000216
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000217add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700218add_dependencies(asan asan_blacklist)
219add_dependencies(compiler-rt asan)
Alexey Samsonovc1caace2013-05-21 13:48:27 +0000220
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700221add_subdirectory(scripts)
Alexey Samsonov8c6e4852013-11-14 12:24:12 +0000222
Stephen Hines2d1fdb22014-05-28 23:58:16 -0700223if(COMPILER_RT_INCLUDE_TESTS)
Chandler Carruth821f21b2012-06-25 12:57:43 +0000224 add_subdirectory(tests)
225endif()