• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_poisoning.cc
17  asan_posix.cc
18  asan_report.cc
19  asan_rtl.cc
20  asan_stack.cc
21  asan_stats.cc
22  asan_suppressions.cc
23  asan_thread.cc
24  asan_win.cc)
25
26set(ASAN_CXX_SOURCES
27  asan_new_delete.cc)
28
29set(ASAN_PREINIT_SOURCES
30  asan_preinit.cc)
31
32include_directories(..)
33
34set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
35append_no_rtti_flag(ASAN_CFLAGS)
36
37set(ASAN_COMMON_DEFINITIONS
38  ASAN_HAS_EXCEPTIONS=1)
39
40set(ASAN_DYNAMIC_LINK_FLAGS)
41
42if(ANDROID)
43  list(APPEND ASAN_COMMON_DEFINITIONS
44    ASAN_LOW_MEMORY=1)
45# 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()
56endif()
57
58set(ASAN_DYNAMIC_DEFINITIONS
59  ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
60append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
61
62set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
63append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
64  -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
65append_list_if(MSVC /DEBUG ASAN_DYNAMIC_CFLAGS)
66
67append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
68append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
69append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
70append_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)
73append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
74
75# Compile ASan sources into an object library.
76
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)
85  add_compiler_rt_object_libraries(RTAsan
86    ARCHS ${ASAN_SUPPORTED_ARCH}
87    SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
88    DEFS ${ASAN_COMMON_DEFINITIONS})
89  add_compiler_rt_object_libraries(RTAsan_cxx
90    ARCHS ${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    ARCHS ${ASAN_SUPPORTED_ARCH}
95    SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
96    DEFS ${ASAN_COMMON_DEFINITIONS})
97
98  file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc "")
99  add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
100    ARCHS ${ASAN_SUPPORTED_ARCH}
101    SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cc
102    CFLAGS ${ASAN_DYNAMIC_CFLAGS}
103    DEFS ${ASAN_DYNAMIC_DEFINITIONS})
104endif()
105
106# Build ASan runtimes shipped with Clang.
107add_custom_target(asan)
108if(APPLE)
109  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)
122else()
123  # Build separate libraries for each target.
124
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
159  foreach(arch ${ASAN_SUPPORTED_ARCH})
160    if (UNIX AND NOT ${arch} MATCHES "i386|i686")
161      add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
162                                    LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
163                                    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
174    add_compiler_rt_runtime(clang_rt.asan
175      SHARED
176      ARCHS ${arch}
177      OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
178              RTAsan_dynamic
179              # 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              RTAsan_dynamic_version_script_dummy
184              RTUbsan_cxx
185      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
186      LINKFLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
187                ${VERSION_SCRIPT_FLAG}
188      LINK_LIBS ${ASAN_DYNAMIC_LIBS}
189      DEFS ${ASAN_DYNAMIC_DEFINITIONS}
190      PARENT_TARGET asan)
191
192    if (UNIX AND NOT ${arch} MATCHES "i386|i686")
193      add_sanitizer_rt_symbols(clang_rt.asan_cxx
194        ARCHS ${arch})
195      add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
196      add_sanitizer_rt_symbols(clang_rt.asan
197        ARCHS ${arch}
198        EXTRA asan.syms.extra)
199      add_dependencies(asan clang_rt.asan-${arch}-symbols)
200    endif()
201
202    if (WIN32)
203      add_compiler_rt_runtime(clang_rt.asan_dll_thunk
204        STATIC
205        ARCHS ${arch}
206        SOURCES asan_win_dll_thunk.cc
207                $<TARGET_OBJECTS:RTInterception.${arch}>
208        CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
209        DEFS ${ASAN_COMMON_DEFINITIONS}
210        PARENT_TARGET asan)
211      add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
212        STATIC
213        ARCHS ${arch}
214        SOURCES asan_win_dynamic_runtime_thunk.cc
215        CFLAGS ${ASAN_CFLAGS} -DASAN_DYNAMIC_RUNTIME_THUNK -Zl
216        DEFS ${ASAN_COMMON_DEFINITIONS}
217        PARENT_TARGET asan)
218    endif()
219  endforeach()
220endif()
221
222add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
223add_dependencies(asan asan_blacklist)
224add_dependencies(compiler-rt asan)
225
226add_subdirectory(scripts)
227
228if(COMPILER_RT_INCLUDE_TESTS)
229  add_subdirectory(tests)
230endif()
231