1# Build for the AddressSanitizer runtime support library.
2
3set(ASAN_SOURCES
4  asan_allocator.cc
5  asan_activation.cc
6  asan_debugging.cc
7  asan_fake_stack.cc
8  asan_flags.cc
9  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
16  asan_memory_profile.cc
17  asan_poisoning.cc
18  asan_posix.cc
19  asan_report.cc
20  asan_rtl.cc
21  asan_stack.cc
22  asan_stats.cc
23  asan_suppressions.cc
24  asan_thread.cc
25  asan_win.cc)
26
27set(ASAN_CXX_SOURCES
28  asan_new_delete.cc)
29
30set(ASAN_PREINIT_SOURCES
31  asan_preinit.cc)
32
33include_directories(..)
34
35set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
36append_rtti_flag(OFF ASAN_CFLAGS)
37
38set(ASAN_COMMON_DEFINITIONS
39  ASAN_HAS_EXCEPTIONS=1)
40
41set(ASAN_DYNAMIC_LINK_FLAGS)
42
43if(ANDROID)
44  list(APPEND ASAN_COMMON_DEFINITIONS
45    ASAN_LOW_MEMORY=1)
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)
56  endif()
57endif()
58
59set(ASAN_DYNAMIC_DEFINITIONS
60  ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
61append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
62
63set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
64append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
65  -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
66append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
67
68append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
69append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
70append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
71append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
72append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
73append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ ASAN_DYNAMIC_LIBS)
74append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
75
76# Compile ASan sources into an object library.
77
78add_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})
84
85if(NOT APPLE)
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})
98
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})
105endif()
106
107# Build ASan runtimes shipped with Clang.
108add_custom_target(asan)
109set_target_properties(asan PROPERTIES FOLDER "Compiler-RT Misc")
110
111if(APPLE)
112  add_compiler_rt_runtime(clang_rt.asan
113    SHARED
114    OS ${SANITIZER_COMMON_SUPPORTED_OS}
115    ARCHS ${ASAN_SUPPORTED_ARCH}
116    OBJECT_LIBS RTAsan_dynamic
117                RTInterception
118                RTSanitizerCommon
119                RTSanitizerCommonLibc
120                RTLSanCommon
121                RTUbsan
122    CFLAGS ${ASAN_DYNAMIC_CFLAGS}
123    DEFS ${ASAN_DYNAMIC_DEFINITIONS}
124    PARENT_TARGET asan)
125else()
126  # Build separate libraries for each target.
127
128  set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
129    RTInterception
130    RTSanitizerCommon
131    RTSanitizerCommonLibc
132    RTLSanCommon
133    RTUbsan)
134
135  add_compiler_rt_runtime(clang_rt.asan
136    STATIC
137    ARCHS ${ASAN_SUPPORTED_ARCH}
138    OBJECT_LIBS RTAsan_preinit
139                RTAsan
140                ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
141    CFLAGS ${ASAN_CFLAGS}
142    DEFS ${ASAN_COMMON_DEFINITIONS}
143    PARENT_TARGET asan)
144
145  add_compiler_rt_runtime(clang_rt.asan_cxx
146    STATIC
147    ARCHS ${ASAN_SUPPORTED_ARCH}
148    OBJECT_LIBS RTAsan_cxx
149                RTUbsan_cxx
150    CFLAGS ${ASAN_CFLAGS}
151    DEFS ${ASAN_COMMON_DEFINITIONS}
152    PARENT_TARGET asan)
153
154  add_compiler_rt_runtime(clang_rt.asan-preinit
155    STATIC
156    ARCHS ${ASAN_SUPPORTED_ARCH}
157    OBJECT_LIBS RTAsan_preinit
158    CFLAGS ${ASAN_CFLAGS}
159    DEFS ${ASAN_COMMON_DEFINITIONS}
160    PARENT_TARGET asan)
161
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
171        PROPERTIES
172        OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
173    else()
174      set(VERSION_SCRIPT_FLAG)
175    endif()
176
177    add_compiler_rt_runtime(clang_rt.asan
178      SHARED
179      ARCHS ${arch}
180      OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
181              RTAsan_dynamic
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
187              RTUbsan_cxx
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}
193      PARENT_TARGET asan)
194
195    if (UNIX AND NOT ${arch} MATCHES "i386|i686")
196      add_sanitizer_rt_symbols(clang_rt.asan_cxx
197        ARCHS ${arch})
198      add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
199      add_sanitizer_rt_symbols(clang_rt.asan
200        ARCHS ${arch}
201        EXTRA asan.syms.extra)
202      add_dependencies(asan clang_rt.asan-${arch}-symbols)
203    endif()
204
205    if (WIN32)
206      add_compiler_rt_runtime(clang_rt.asan_dll_thunk
207        STATIC
208        ARCHS ${arch}
209        SOURCES asan_win_dll_thunk.cc
210                $<TARGET_OBJECTS:RTInterception.${arch}>
211        CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
212        DEFS ${ASAN_COMMON_DEFINITIONS}
213        PARENT_TARGET asan)
214      add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
215        STATIC
216        ARCHS ${arch}
217        SOURCES asan_win_dynamic_runtime_thunk.cc
218        CFLAGS ${ASAN_CFLAGS} -DASAN_DYNAMIC_RUNTIME_THUNK -Zl
219        DEFS ${ASAN_COMMON_DEFINITIONS}
220        PARENT_TARGET asan)
221    endif()
222  endforeach()
223endif()
224
225add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt asan)
226add_dependencies(compiler-rt asan)
227
228add_subdirectory(scripts)
229
230if(COMPILER_RT_INCLUDE_TESTS)
231  add_subdirectory(tests)
232endif()
233