Bump version to 19.1.2
[llvm-project.git] / compiler-rt / lib / asan / CMakeLists.txt
blob463ea233b37aa4da49dadb092736eb1517e27721
1 # Build for the AddressSanitizer runtime support library.
3 set(ASAN_SOURCES
4   asan_allocator.cpp
5   asan_activation.cpp
6   asan_debugging.cpp
7   asan_descriptions.cpp
8   asan_errors.cpp
9   asan_fake_stack.cpp
10   asan_flags.cpp
11   asan_fuchsia.cpp
12   asan_globals.cpp
13   asan_globals_win.cpp
14   asan_interceptors.cpp
15   asan_interceptors_memintrinsics.cpp
16   asan_linux.cpp
17   asan_mac.cpp
18   asan_malloc_linux.cpp
19   asan_malloc_mac.cpp
20   asan_malloc_win.cpp
21   asan_memory_profile.cpp
22   asan_poisoning.cpp
23   asan_posix.cpp
24   asan_premap_shadow.cpp
25   asan_report.cpp
26   asan_rtl.cpp
27   asan_shadow_setup.cpp
28   asan_stack.cpp
29   asan_stats.cpp
30   asan_suppressions.cpp
31   asan_thread.cpp
32   asan_win.cpp
33   )
35 if (NOT WIN32 AND NOT APPLE)
36   list(APPEND ASAN_SOURCES
37     asan_interceptors_vfork.S
38     )
39 endif()
41 set(ASAN_CXX_SOURCES
42   asan_new_delete.cpp
43   )
45 set(ASAN_STATIC_SOURCES
46   asan_rtl_static.cpp
47   )
49 if ("x86_64" IN_LIST ASAN_SUPPORTED_ARCH AND NOT WIN32 AND NOT APPLE)
50   list(APPEND ASAN_STATIC_SOURCES
51     asan_rtl_x86_64.S
52   )
53 endif()
55 set(ASAN_PREINIT_SOURCES
56   asan_preinit.cpp
57   )
59 SET(ASAN_HEADERS
60   asan_activation.h
61   asan_activation_flags.inc
62   asan_allocator.h
63   asan_descriptions.h
64   asan_errors.h
65   asan_fake_stack.h
66   asan_flags.h
67   asan_flags.inc
68   asan_init_version.h
69   asan_interceptors.h
70   asan_interceptors_memintrinsics.h
71   asan_interface.inc
72   asan_interface_internal.h
73   asan_internal.h
74   asan_mapping.h
75   asan_poisoning.h
76   asan_premap_shadow.h
77   asan_report.h
78   asan_scariness_score.h
79   asan_stack.h
80   asan_stats.h
81   asan_suppressions.h
82   asan_thread.h
83   )
85 include_directories(..)
87 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
89 append_list_if(MSVC /Zl ASAN_CFLAGS)
91 set(ASAN_COMMON_DEFINITIONS "")
93 append_rtti_flag(OFF ASAN_CFLAGS)
95 # Silence warnings in system headers with MSVC.
96 if(NOT CLANG_CL)
97   append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" ASAN_CFLAGS)
98 endif()
100 # Too many existing bugs, needs cleanup.
101 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
103 set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
105 if(ANDROID)
106 # Put most Sanitizer shared libraries in the global group. For more details, see
107 # android-changes-for-ndk-developers.md#changes-to-library-search-order
108   if (COMPILER_RT_HAS_Z_GLOBAL)
109     list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
110   endif()
111 endif()
113 set(ASAN_DYNAMIC_DEFINITIONS
114   ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
115 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
117 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
118 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
119   -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
120 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
122 set(ASAN_DYNAMIC_LIBS
123   ${COMPILER_RT_UNWINDER_LINK_LIBS}
124   ${SANITIZER_CXX_ABI_LIBRARIES}
125   ${SANITIZER_COMMON_LINK_LIBS})
127 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
128 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
129 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
130 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
131 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
132 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
134 # Compile ASan sources into an object library.
136 add_compiler_rt_object_libraries(RTAsan_dynamic
137   OS ${SANITIZER_COMMON_SUPPORTED_OS}
138   ARCHS ${ASAN_SUPPORTED_ARCH}
139   SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
140   ADDITIONAL_HEADERS ${ASAN_HEADERS}
141   CFLAGS ${ASAN_DYNAMIC_CFLAGS}
142   DEFS ${ASAN_DYNAMIC_DEFINITIONS})
144 if(NOT APPLE)
145   add_compiler_rt_object_libraries(RTAsan
146     ARCHS ${ASAN_SUPPORTED_ARCH}
147     SOURCES ${ASAN_SOURCES}
148     ADDITIONAL_HEADERS ${ASAN_HEADERS}
149     CFLAGS ${ASAN_CFLAGS}
150     DEFS ${ASAN_COMMON_DEFINITIONS})
151   add_compiler_rt_object_libraries(RTAsan_cxx
152     ARCHS ${ASAN_SUPPORTED_ARCH}
153     SOURCES ${ASAN_CXX_SOURCES}
154     ADDITIONAL_HEADERS ${ASAN_HEADERS}
155     CFLAGS ${ASAN_CFLAGS}
156     DEFS ${ASAN_COMMON_DEFINITIONS})
157   add_compiler_rt_object_libraries(RTAsan_static
158     ARCHS ${ASAN_SUPPORTED_ARCH}
159     SOURCES ${ASAN_STATIC_SOURCES}
160     ADDITIONAL_HEADERS ${ASAN_HEADERS}
161     CFLAGS ${ASAN_CFLAGS}
162     DEFS ${ASAN_COMMON_DEFINITIONS})
163   add_compiler_rt_object_libraries(RTAsan_preinit
164     ARCHS ${ASAN_SUPPORTED_ARCH}
165     SOURCES ${ASAN_PREINIT_SOURCES}
166     ADDITIONAL_HEADERS ${ASAN_HEADERS}
167     CFLAGS ${ASAN_CFLAGS}
168     DEFS ${ASAN_COMMON_DEFINITIONS})
170   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
171   add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
172     ARCHS ${ASAN_SUPPORTED_ARCH}
173     SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
174     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
175     DEFS ${ASAN_DYNAMIC_DEFINITIONS})
176 endif()
178 # Build ASan runtimes shipped with Clang.
179 add_compiler_rt_component(asan)
181 if(APPLE)
182   add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
183   add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
184   add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
185   add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
186   add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
188   add_compiler_rt_runtime(clang_rt.asan
189     SHARED
190     OS ${SANITIZER_COMMON_SUPPORTED_OS}
191     ARCHS ${ASAN_SUPPORTED_ARCH}
192     OBJECT_LIBS RTAsan_dynamic
193                 RTInterception
194                 RTSanitizerCommon
195                 RTSanitizerCommonLibc
196                 RTSanitizerCommonCoverage
197                 RTSanitizerCommonSymbolizer
198                 RTLSanCommon
199                 RTUbsan
200     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
201     LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
202     DEFS ${ASAN_DYNAMIC_DEFINITIONS}
203     PARENT_TARGET asan)
205   add_compiler_rt_runtime(clang_rt.asan_static
206     STATIC
207     ARCHS ${ASAN_SUPPORTED_ARCH}
208     OBJECT_LIBS RTAsan_static
209     CFLAGS ${ASAN_CFLAGS}
210     DEFS ${ASAN_COMMON_DEFINITIONS}
211     PARENT_TARGET asan)
212 else()
213   # Build separate libraries for each target.
215   set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
216     RTInterception
217     RTSanitizerCommon
218     RTSanitizerCommonLibc
219     RTSanitizerCommonCoverage
220     RTSanitizerCommonSymbolizer
221     RTSanitizerCommonSymbolizerInternal
222     RTLSanCommon
223     RTUbsan)
225   add_compiler_rt_runtime(clang_rt.asan
226     STATIC
227     ARCHS ${ASAN_SUPPORTED_ARCH}
228     OBJECT_LIBS RTAsan_preinit
229                 RTAsan
230                 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
231     CFLAGS ${ASAN_CFLAGS}
232     DEFS ${ASAN_COMMON_DEFINITIONS}
233     PARENT_TARGET asan)
235   add_compiler_rt_runtime(clang_rt.asan_cxx
236     STATIC
237     ARCHS ${ASAN_SUPPORTED_ARCH}
238     OBJECT_LIBS RTAsan_cxx
239                 RTUbsan_cxx
240     CFLAGS ${ASAN_CFLAGS}
241     DEFS ${ASAN_COMMON_DEFINITIONS}
242     PARENT_TARGET asan)
244   add_compiler_rt_runtime(clang_rt.asan_static
245     STATIC
246     ARCHS ${ASAN_SUPPORTED_ARCH}
247     OBJECT_LIBS RTAsan_static
248     CFLAGS ${ASAN_CFLAGS}
249     DEFS ${ASAN_COMMON_DEFINITIONS}
250     PARENT_TARGET asan)
252   add_compiler_rt_runtime(clang_rt.asan-preinit
253     STATIC
254     ARCHS ${ASAN_SUPPORTED_ARCH}
255     OBJECT_LIBS RTAsan_preinit
256     CFLAGS ${ASAN_CFLAGS}
257     DEFS ${ASAN_COMMON_DEFINITIONS}
258     PARENT_TARGET asan)
260   foreach(arch ${ASAN_SUPPORTED_ARCH})
261     if (COMPILER_RT_HAS_VERSION_SCRIPT)
262       add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
263                                     LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
264                                     EXTRA asan.syms.extra)
265       set(VERSION_SCRIPT_FLAG
266            -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
267       # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
268       # but requires a special option to enable it.
269       if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
270           list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
271       endif()
272       set_property(SOURCE
273         ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
274         APPEND PROPERTY
275         OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
276     else()
277       set(VERSION_SCRIPT_FLAG)
278     endif()
280     set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
281     if (WIN32)
282       add_compiler_rt_object_libraries(AsanWeakInterception
283         ${SANITIZER_COMMON_SUPPORTED_OS}
284         ARCHS ${arch}
285         SOURCES
286           asan_win_weak_interception.cpp
287         CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
288         DEFS ${ASAN_COMMON_DEFINITIONS})
289       set(ASAN_DYNAMIC_WEAK_INTERCEPTION
290           AsanWeakInterception
291           UbsanWeakInterception
292           SancovWeakInterception
293           SanitizerCommonWeakInterception)
294     endif()
296     add_compiler_rt_runtime(clang_rt.asan
297       SHARED
298       ARCHS ${arch}
299       OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
300               RTAsan_dynamic
301               # The only purpose of RTAsan_dynamic_version_script_dummy is to
302               # carry a dependency of the shared runtime on the version script.
303               # Replacing it with a straightforward
304               # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
305               # generates an order-only dependency in ninja.
306               RTAsan_dynamic_version_script_dummy
307               RTUbsan_cxx
308               ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
309       CFLAGS ${ASAN_DYNAMIC_CFLAGS}
310       LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
311                 ${VERSION_SCRIPT_FLAG}
312       LINK_LIBS ${ASAN_DYNAMIC_LIBS}
313       DEFS ${ASAN_DYNAMIC_DEFINITIONS}
314       PARENT_TARGET asan)
316     if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
317       add_sanitizer_rt_symbols(clang_rt.asan_cxx
318         ARCHS ${arch})
319       add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
320       add_sanitizer_rt_symbols(clang_rt.asan
321         ARCHS ${arch}
322         EXTRA asan.syms.extra)
323       add_dependencies(asan clang_rt.asan-${arch}-symbols)
324     endif()
326     if (WIN32)
327       add_compiler_rt_object_libraries(AsanDllThunk
328         ${SANITIZER_COMMON_SUPPORTED_OS}
329         ARCHS ${arch}
330         SOURCES asan_globals_win.cpp
331                 asan_win_dll_thunk.cpp
332         CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
333         DEFS ${ASAN_COMMON_DEFINITIONS})
335       add_compiler_rt_runtime(clang_rt.asan_dll_thunk
336         STATIC
337         ARCHS ${arch}
338         OBJECT_LIBS AsanDllThunk
339                     UbsanDllThunk
340                     SancovDllThunk
341                     SanitizerCommonDllThunk
342         SOURCES $<TARGET_OBJECTS:RTInterception.${arch}>
343         PARENT_TARGET asan)
345       set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
346       if(MSVC)
347         list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
348       elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
349         list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
350       endif()
352       add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
353         ${SANITIZER_COMMON_SUPPORTED_OS}
354         ARCHS ${arch}
355         SOURCES asan_globals_win.cpp
356                 asan_win_dynamic_runtime_thunk.cpp
357         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
358         DEFS ${ASAN_COMMON_DEFINITIONS})
360       add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
361         STATIC
362         ARCHS ${arch}
363         OBJECT_LIBS AsanDynamicRuntimeThunk
364                     UbsanDynamicRuntimeThunk
365                     SancovDynamicRuntimeThunk
366                     SanitizerCommonDynamicRuntimeThunk
367         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
368         DEFS ${ASAN_COMMON_DEFINITIONS}
369         PARENT_TARGET asan)
370     endif()
371   endforeach()
372 endif()
374 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
376 add_subdirectory(scripts)
378 if(COMPILER_RT_INCLUDE_TESTS)
379   add_subdirectory(tests)
380 endif()