Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / asan / CMakeLists.txt
blob48d0e91052d73bb642892263e40dc65b9e2e5fbb
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 (ASAN_SUPPORTED_ARCH STREQUAL "x86_64" 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_lock.h
75   asan_mapping.h
76   asan_poisoning.h
77   asan_premap_shadow.h
78   asan_report.h
79   asan_scariness_score.h
80   asan_stack.h
81   asan_stats.h
82   asan_suppressions.h
83   asan_thread.h
84   )
86 include_directories(..)
88 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
89 set(ASAN_COMMON_DEFINITIONS ${COMPILER_RT_ASAN_SHADOW_SCALE_DEFINITION})
91 append_rtti_flag(OFF ASAN_CFLAGS)
93 # Silence warnings in system headers with MSVC.
94 if(NOT CLANG_CL)
95   append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" ASAN_CFLAGS)
96 endif()
98 # Too many existing bugs, needs cleanup.
99 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
101 set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
103 if(ANDROID)
104 # Put most Sanitizer shared libraries in the global group. For more details, see
105 # android-changes-for-ndk-developers.md#changes-to-library-search-order
106   if (COMPILER_RT_HAS_Z_GLOBAL)
107     list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
108   endif()
109 endif()
111 set(ASAN_DYNAMIC_DEFINITIONS
112   ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
113 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
115 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
116 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
117   -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
118 append_list_if(MSVC /DEBUG ASAN_DYNAMIC_LINK_FLAGS)
120 set(ASAN_DYNAMIC_LIBS
121   ${COMPILER_RT_UNWINDER_LINK_LIBS}
122   ${SANITIZER_CXX_ABI_LIBRARIES}
123   ${SANITIZER_COMMON_LINK_LIBS})
125 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
126 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
127 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
128 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
129 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
130 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
132 # Compile ASan sources into an object library.
134 add_compiler_rt_object_libraries(RTAsan_dynamic
135   OS ${SANITIZER_COMMON_SUPPORTED_OS}
136   ARCHS ${ASAN_SUPPORTED_ARCH}
137   SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
138   ADDITIONAL_HEADERS ${ASAN_HEADERS}
139   CFLAGS ${ASAN_DYNAMIC_CFLAGS}
140   DEFS ${ASAN_DYNAMIC_DEFINITIONS})
142 if(NOT APPLE)
143   add_compiler_rt_object_libraries(RTAsan
144     ARCHS ${ASAN_SUPPORTED_ARCH}
145     SOURCES ${ASAN_SOURCES}
146     ADDITIONAL_HEADERS ${ASAN_HEADERS}
147     CFLAGS ${ASAN_CFLAGS}
148     DEFS ${ASAN_COMMON_DEFINITIONS})
149   add_compiler_rt_object_libraries(RTAsan_cxx
150     ARCHS ${ASAN_SUPPORTED_ARCH}
151     SOURCES ${ASAN_CXX_SOURCES}
152     ADDITIONAL_HEADERS ${ASAN_HEADERS}
153     CFLAGS ${ASAN_CFLAGS}
154     DEFS ${ASAN_COMMON_DEFINITIONS})
155   add_compiler_rt_object_libraries(RTAsan_static
156     ARCHS ${ASAN_SUPPORTED_ARCH}
157     SOURCES ${ASAN_STATIC_SOURCES}
158     ADDITIONAL_HEADERS ${ASAN_HEADERS}
159     CFLAGS ${ASAN_CFLAGS}
160     DEFS ${ASAN_COMMON_DEFINITIONS})
161   add_compiler_rt_object_libraries(RTAsan_preinit
162     ARCHS ${ASAN_SUPPORTED_ARCH}
163     SOURCES ${ASAN_PREINIT_SOURCES}
164     ADDITIONAL_HEADERS ${ASAN_HEADERS}
165     CFLAGS ${ASAN_CFLAGS}
166     DEFS ${ASAN_COMMON_DEFINITIONS})
168   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
169   add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
170     ARCHS ${ASAN_SUPPORTED_ARCH}
171     SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
172     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
173     DEFS ${ASAN_DYNAMIC_DEFINITIONS})
174 endif()
176 # Build ASan runtimes shipped with Clang.
177 add_compiler_rt_component(asan)
179 if(APPLE)
180   add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
181   add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
182   add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
183   add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
184   add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
186   add_compiler_rt_runtime(clang_rt.asan
187     SHARED
188     OS ${SANITIZER_COMMON_SUPPORTED_OS}
189     ARCHS ${ASAN_SUPPORTED_ARCH}
190     OBJECT_LIBS RTAsan_dynamic
191                 RTInterception
192                 RTSanitizerCommon
193                 RTSanitizerCommonLibc
194                 RTSanitizerCommonCoverage
195                 RTSanitizerCommonSymbolizer
196                 RTLSanCommon
197                 RTUbsan
198     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
199     LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
200     DEFS ${ASAN_DYNAMIC_DEFINITIONS}
201     PARENT_TARGET asan)
203   add_compiler_rt_runtime(clang_rt.asan_static
204     STATIC
205     ARCHS ${ASAN_SUPPORTED_ARCH}
206     OBJECT_LIBS RTAsan_static
207     CFLAGS ${ASAN_CFLAGS}
208     DEFS ${ASAN_COMMON_DEFINITIONS}
209     PARENT_TARGET asan)
210 else()
211   # Build separate libraries for each target.
213   set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
214     RTInterception
215     RTSanitizerCommon
216     RTSanitizerCommonLibc
217     RTSanitizerCommonCoverage
218     RTSanitizerCommonSymbolizer
219     RTSanitizerCommonSymbolizerInternal
220     RTLSanCommon
221     RTUbsan)
223   add_compiler_rt_runtime(clang_rt.asan
224     STATIC
225     ARCHS ${ASAN_SUPPORTED_ARCH}
226     OBJECT_LIBS RTAsan_preinit
227                 RTAsan
228                 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
229     CFLAGS ${ASAN_CFLAGS}
230     DEFS ${ASAN_COMMON_DEFINITIONS}
231     PARENT_TARGET asan)
233   add_compiler_rt_runtime(clang_rt.asan_cxx
234     STATIC
235     ARCHS ${ASAN_SUPPORTED_ARCH}
236     OBJECT_LIBS RTAsan_cxx
237                 RTUbsan_cxx
238     CFLAGS ${ASAN_CFLAGS}
239     DEFS ${ASAN_COMMON_DEFINITIONS}
240     PARENT_TARGET asan)
242   add_compiler_rt_runtime(clang_rt.asan_static
243     STATIC
244     ARCHS ${ASAN_SUPPORTED_ARCH}
245     OBJECT_LIBS RTAsan_static
246     CFLAGS ${ASAN_CFLAGS}
247     DEFS ${ASAN_COMMON_DEFINITIONS}
248     PARENT_TARGET asan)
250   add_compiler_rt_runtime(clang_rt.asan-preinit
251     STATIC
252     ARCHS ${ASAN_SUPPORTED_ARCH}
253     OBJECT_LIBS RTAsan_preinit
254     CFLAGS ${ASAN_CFLAGS}
255     DEFS ${ASAN_COMMON_DEFINITIONS}
256     PARENT_TARGET asan)
258   foreach(arch ${ASAN_SUPPORTED_ARCH})
259     if (COMPILER_RT_HAS_VERSION_SCRIPT)
260       add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
261                                     LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch}
262                                     EXTRA asan.syms.extra)
263       set(VERSION_SCRIPT_FLAG
264            -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
265       # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
266       # but requires a special option to enable it.
267       if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
268           list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
269       endif()
270       set_property(SOURCE
271         ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
272         APPEND PROPERTY
273         OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
274     else()
275       set(VERSION_SCRIPT_FLAG)
276     endif()
278     set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
279     if (WIN32)
280       add_compiler_rt_object_libraries(AsanWeakInterception
281         ${SANITIZER_COMMON_SUPPORTED_OS}
282         ARCHS ${arch}
283         SOURCES
284           asan_win_weak_interception.cpp
285         CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DYNAMIC
286         DEFS ${ASAN_COMMON_DEFINITIONS})
287       set(ASAN_DYNAMIC_WEAK_INTERCEPTION
288           AsanWeakInterception
289           UbsanWeakInterception
290           SancovWeakInterception
291           SanitizerCommonWeakInterception)
292     endif()
294     add_compiler_rt_runtime(clang_rt.asan
295       SHARED
296       ARCHS ${arch}
297       OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
298               RTAsan_dynamic
299               # The only purpose of RTAsan_dynamic_version_script_dummy is to
300               # carry a dependency of the shared runtime on the version script.
301               # Replacing it with a straightforward
302               # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
303               # generates an order-only dependency in ninja.
304               RTAsan_dynamic_version_script_dummy
305               RTUbsan_cxx
306               ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
307       CFLAGS ${ASAN_DYNAMIC_CFLAGS}
308       LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
309                 ${VERSION_SCRIPT_FLAG}
310       LINK_LIBS ${ASAN_DYNAMIC_LIBS}
311       DEFS ${ASAN_DYNAMIC_DEFINITIONS}
312       PARENT_TARGET asan)
314     if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
315       add_sanitizer_rt_symbols(clang_rt.asan_cxx
316         ARCHS ${arch})
317       add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
318       add_sanitizer_rt_symbols(clang_rt.asan
319         ARCHS ${arch}
320         EXTRA asan.syms.extra)
321       add_dependencies(asan clang_rt.asan-${arch}-symbols)
322     endif()
324     if (WIN32)
325       add_compiler_rt_object_libraries(AsanDllThunk
326         ${SANITIZER_COMMON_SUPPORTED_OS}
327         ARCHS ${arch}
328         SOURCES asan_globals_win.cpp
329                 asan_win_dll_thunk.cpp
330         CFLAGS ${ASAN_CFLAGS} -DSANITIZER_DLL_THUNK
331         DEFS ${ASAN_COMMON_DEFINITIONS})
333       add_compiler_rt_runtime(clang_rt.asan_dll_thunk
334         STATIC
335         ARCHS ${arch}
336         OBJECT_LIBS AsanDllThunk
337                     UbsanDllThunk
338                     SancovDllThunk
339                     SanitizerCommonDllThunk
340         SOURCES $<TARGET_OBJECTS:RTInterception.${arch}>
341         PARENT_TARGET asan)
343       set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
344       if(MSVC)
345         list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-Zl")
346       elseif(CMAKE_C_COMPILER_ID MATCHES Clang)
347         list(APPEND DYNAMIC_RUNTIME_THUNK_CFLAGS "-nodefaultlibs")
348       endif()
350       add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
351         ${SANITIZER_COMMON_SUPPORTED_OS}
352         ARCHS ${arch}
353         SOURCES asan_globals_win.cpp
354                 asan_win_dynamic_runtime_thunk.cpp
355         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
356         DEFS ${ASAN_COMMON_DEFINITIONS})
358       add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
359         STATIC
360         ARCHS ${arch}
361         OBJECT_LIBS AsanDynamicRuntimeThunk
362                     UbsanDynamicRuntimeThunk
363                     SancovDynamicRuntimeThunk
364                     SanitizerCommonDynamicRuntimeThunk
365         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
366         DEFS ${ASAN_COMMON_DEFINITIONS}
367         PARENT_TARGET asan)
368     endif()
369   endforeach()
370 endif()
372 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
374 add_subdirectory(scripts)
376 if(COMPILER_RT_INCLUDE_TESTS)
377   add_subdirectory(tests)
378 endif()