[memprof] Remove verifyIndexedMemProfRecord and verifyFunctionProfileData (#117412)
[llvm-project.git] / compiler-rt / lib / asan / CMakeLists.txt
blobfb3d74283a61e0cc7264c6c42db38aa4591b8866
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(WIN32)
36   set(ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES
37     asan_globals_win.cpp
38     asan_win_common_runtime_thunk.cpp
39     asan_win_dynamic_runtime_thunk.cpp
40     )
41   set(ASAN_STATIC_RUNTIME_THUNK_SOURCES
42     asan_globals_win.cpp
43     asan_malloc_win_thunk.cpp
44     asan_win_common_runtime_thunk.cpp
45     asan_win_static_runtime_thunk.cpp
46     )
47 endif()
49 if (NOT WIN32 AND NOT APPLE)
50   list(APPEND ASAN_SOURCES
51     asan_interceptors_vfork.S
52     )
53 endif()
55 set(ASAN_CXX_SOURCES
56   asan_new_delete.cpp
57   )
59 set(ASAN_STATIC_SOURCES
60   asan_rtl_static.cpp
61   )
63 if ("x86_64" IN_LIST ASAN_SUPPORTED_ARCH AND NOT WIN32 AND NOT APPLE)
64   list(APPEND ASAN_STATIC_SOURCES
65     asan_rtl_x86_64.S
66   )
67 endif()
69 set(ASAN_PREINIT_SOURCES
70   asan_preinit.cpp
71   )
73 SET(ASAN_HEADERS
74   asan_activation.h
75   asan_activation_flags.inc
76   asan_allocator.h
77   asan_descriptions.h
78   asan_errors.h
79   asan_fake_stack.h
80   asan_flags.h
81   asan_flags.inc
82   asan_init_version.h
83   asan_interceptors.h
84   asan_interceptors_memintrinsics.h
85   asan_interface.inc
86   asan_interface_internal.h
87   asan_internal.h
88   asan_mapping.h
89   asan_poisoning.h
90   asan_premap_shadow.h
91   asan_report.h
92   asan_scariness_score.h
93   asan_stack.h
94   asan_stats.h
95   asan_suppressions.h
96   asan_thread.h
97   )
99 include_directories(..)
100 if(MSVC)
101   # asan on windows only supports the release dll version of the runtimes, in the interest of
102   # only having one asan dll to support/test. Having asan statically linked
103   # with the runtime might be possible, but it multiplies the number of scenerios to test.
104   # the program USING sanitizers can use whatever version of the runtime it wants to.
105   set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)
106 endif()
107 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
109 append_list_if(MSVC /Zl ASAN_CFLAGS)
111 set(ASAN_COMMON_DEFINITIONS "")
113 append_rtti_flag(OFF ASAN_CFLAGS)
115 # Silence warnings in system headers with MSVC.
116 if(NOT CLANG_CL)
117   append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" ASAN_CFLAGS)
118 endif()
120 # Too many existing bugs, needs cleanup.
121 append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
123 set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
125 if(ANDROID)
126 # Put most Sanitizer shared libraries in the global group. For more details, see
127 # android-changes-for-ndk-developers.md#changes-to-library-search-order
128   if (COMPILER_RT_HAS_Z_GLOBAL)
129     list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
130   endif()
131 endif()
133 set(ASAN_DYNAMIC_DEFINITIONS
134   ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
135 append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
137 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
138 append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
139   -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
141 # LLVM turns /OPT:ICF back on when LLVM_ENABLE_PDBs is set
142 # we _REALLY_ need to turn it back off for ASAN, because the way
143 # asan emulates weak functions from DLLs requires NOICF
144 append_list_if(MSVC "/DEBUG;/OPT:NOICF" ASAN_DYNAMIC_LINK_FLAGS)
146 set(ASAN_DYNAMIC_LIBS
147   ${COMPILER_RT_UNWINDER_LINK_LIBS}
148   ${SANITIZER_CXX_ABI_LIBRARIES}
149   ${SANITIZER_COMMON_LINK_LIBS})
151 append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
152 append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
153 append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
154 append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
155 append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
156 append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
158 # Compile ASan sources into an object library.
160 add_compiler_rt_object_libraries(RTAsan_dynamic
161   OS ${SANITIZER_COMMON_SUPPORTED_OS}
162   ARCHS ${ASAN_SUPPORTED_ARCH}
163   SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
164   ADDITIONAL_HEADERS ${ASAN_HEADERS}
165   CFLAGS ${ASAN_DYNAMIC_CFLAGS}
166   DEFS ${ASAN_DYNAMIC_DEFINITIONS})
168 if(NOT APPLE)
169   add_compiler_rt_object_libraries(RTAsan
170     ARCHS ${ASAN_SUPPORTED_ARCH}
171     SOURCES ${ASAN_SOURCES}
172     ADDITIONAL_HEADERS ${ASAN_HEADERS}
173     CFLAGS ${ASAN_CFLAGS}
174     DEFS ${ASAN_COMMON_DEFINITIONS})
175   add_compiler_rt_object_libraries(RTAsan_cxx
176     ARCHS ${ASAN_SUPPORTED_ARCH}
177     SOURCES ${ASAN_CXX_SOURCES}
178     ADDITIONAL_HEADERS ${ASAN_HEADERS}
179     CFLAGS ${ASAN_CFLAGS}
180     DEFS ${ASAN_COMMON_DEFINITIONS})
181   add_compiler_rt_object_libraries(RTAsan_static
182     ARCHS ${ASAN_SUPPORTED_ARCH}
183     SOURCES ${ASAN_STATIC_SOURCES}
184     ADDITIONAL_HEADERS ${ASAN_HEADERS}
185     CFLAGS ${ASAN_CFLAGS}
186     DEFS ${ASAN_COMMON_DEFINITIONS})
187   add_compiler_rt_object_libraries(RTAsan_preinit
188     ARCHS ${ASAN_SUPPORTED_ARCH}
189     SOURCES ${ASAN_PREINIT_SOURCES}
190     ADDITIONAL_HEADERS ${ASAN_HEADERS}
191     CFLAGS ${ASAN_CFLAGS}
192     DEFS ${ASAN_COMMON_DEFINITIONS})
194   file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
195   add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy
196     ARCHS ${ASAN_SUPPORTED_ARCH}
197     SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
198     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
199     DEFS ${ASAN_DYNAMIC_DEFINITIONS})
200 endif()
202 # Build ASan runtimes shipped with Clang.
203 add_compiler_rt_component(asan)
205 if(APPLE)
206   add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)
207   add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)
208   add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)
209   add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)
210   add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)
212   add_compiler_rt_runtime(clang_rt.asan
213     SHARED
214     OS ${SANITIZER_COMMON_SUPPORTED_OS}
215     ARCHS ${ASAN_SUPPORTED_ARCH}
216     OBJECT_LIBS RTAsan_dynamic
217                 RTInterception
218                 RTSanitizerCommon
219                 RTSanitizerCommonLibc
220                 RTSanitizerCommonCoverage
221                 RTSanitizerCommonSymbolizer
222                 RTLSanCommon
223                 RTUbsan
224     CFLAGS ${ASAN_DYNAMIC_CFLAGS}
225     LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}
226     DEFS ${ASAN_DYNAMIC_DEFINITIONS}
227     PARENT_TARGET asan)
229   add_compiler_rt_runtime(clang_rt.asan_static
230     STATIC
231     ARCHS ${ASAN_SUPPORTED_ARCH}
232     OBJECT_LIBS RTAsan_static
233     CFLAGS ${ASAN_CFLAGS}
234     DEFS ${ASAN_COMMON_DEFINITIONS}
235     PARENT_TARGET asan)
236 else()
237   # Build separate libraries for each target.
239   set(ASAN_COMMON_RUNTIME_OBJECT_LIBS
240     RTInterception
241     RTSanitizerCommon
242     RTSanitizerCommonLibc
243     RTSanitizerCommonCoverage
244     RTSanitizerCommonSymbolizer
245     RTSanitizerCommonSymbolizerInternal
246     RTLSanCommon
247     RTUbsan)
248   if (NOT WIN32)
249     add_compiler_rt_runtime(clang_rt.asan
250       STATIC
251       ARCHS ${ASAN_SUPPORTED_ARCH}
252       OBJECT_LIBS RTAsan_preinit
253                   RTAsan
254                   ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
255       CFLAGS ${ASAN_CFLAGS}
256       DEFS ${ASAN_COMMON_DEFINITIONS}
257       PARENT_TARGET asan)
259     add_compiler_rt_runtime(clang_rt.asan_cxx
260       STATIC
261       ARCHS ${ASAN_SUPPORTED_ARCH}
262       OBJECT_LIBS RTAsan_cxx
263                   RTUbsan_cxx
264       CFLAGS ${ASAN_CFLAGS}
265       DEFS ${ASAN_COMMON_DEFINITIONS}
266       PARENT_TARGET asan)
268     add_compiler_rt_runtime(clang_rt.asan_static
269       STATIC
270       ARCHS ${ASAN_SUPPORTED_ARCH}
271       OBJECT_LIBS RTAsan_static
272       CFLAGS ${ASAN_CFLAGS}
273       DEFS ${ASAN_COMMON_DEFINITIONS}
274       PARENT_TARGET asan)
276     add_compiler_rt_runtime(clang_rt.asan-preinit
277       STATIC
278       ARCHS ${ASAN_SUPPORTED_ARCH}
279       OBJECT_LIBS RTAsan_preinit
280       CFLAGS ${ASAN_CFLAGS}
281       DEFS ${ASAN_COMMON_DEFINITIONS}
282       PARENT_TARGET asan)
283   endif()
285   foreach(arch ${ASAN_SUPPORTED_ARCH})
286     if (COMPILER_RT_HAS_VERSION_SCRIPT)
287       if(WIN32)
288         set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch})
289       else()
290         set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch})
291       endif()
292       add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
293                                     LIBS ${SANITIZER_RT_VERSION_LIST_LIBS}
294                                     EXTRA asan.syms.extra)
295       set(VERSION_SCRIPT_FLAG
296            -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
297       # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
298       # but requires a special option to enable it.
299       if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
300           list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
301       endif()
302       set_property(SOURCE
303         ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
304         APPEND PROPERTY
305         OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
306     else()
307       set(VERSION_SCRIPT_FLAG)
308     endif()
310     set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
311     add_compiler_rt_runtime(clang_rt.asan
312       SHARED
313       ARCHS ${arch}
314       OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
315               RTAsan_dynamic
316               # The only purpose of RTAsan_dynamic_version_script_dummy is to
317               # carry a dependency of the shared runtime on the version script.
318               # Replacing it with a straightforward
319               # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
320               # generates an order-only dependency in ninja.
321               RTAsan_dynamic_version_script_dummy
322               RTUbsan_cxx
323               ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
324       CFLAGS ${ASAN_DYNAMIC_CFLAGS}
325       LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
326                 ${VERSION_SCRIPT_FLAG}
327       LINK_LIBS ${ASAN_DYNAMIC_LIBS}
328       DEFS ${ASAN_DYNAMIC_DEFINITIONS}
329       PARENT_TARGET asan)
331     if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
332       add_sanitizer_rt_symbols(clang_rt.asan_cxx
333         ARCHS ${arch})
334       add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
335       add_sanitizer_rt_symbols(clang_rt.asan
336         ARCHS ${arch}
337         EXTRA asan.syms.extra)
338       add_dependencies(asan clang_rt.asan-${arch}-symbols)
339     endif()
341     if (WIN32)
342       set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
344       add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
345         ${SANITIZER_COMMON_SUPPORTED_OS}
346         ARCHS ${arch}
347         SOURCES ${ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES}
348         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
349         DEFS ${ASAN_COMMON_DEFINITIONS})
351       add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
352         STATIC
353         ARCHS ${arch}
354         OBJECT_LIBS AsanDynamicRuntimeThunk
355                     UbsanRuntimeThunk
356                     SancovRuntimeThunk
357                     SanitizerRuntimeThunk
358         CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
359         DEFS ${ASAN_COMMON_DEFINITIONS}
360         PARENT_TARGET asan)
362       # mingw does not support static linkage of the CRT
363       if(NOT MINGW)
364         set(STATIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_STATIC_RUNTIME_THUNK")
366         add_compiler_rt_object_libraries(AsanStaticRuntimeThunk
367           ${SANITIZER_COMMON_SUPPORTED_OS}
368           ARCHS ${arch}
369           SOURCES ${ASAN_STATIC_RUNTIME_THUNK_SOURCES}
370           CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
371           DEFS ${ASAN_DYNAMIC_DEFINITIONS})
373         add_compiler_rt_runtime(clang_rt.asan_static_runtime_thunk
374           STATIC
375           ARCHS ${arch}
376           OBJECT_LIBS AsanStaticRuntimeThunk
377                       UbsanRuntimeThunk
378                       SancovRuntimeThunk
379                       SanitizerRuntimeThunk
380           CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
381           DEFS ${ASAN_DYNAMIC_DEFINITIONS}
382           PARENT_TARGET asan)
383       endif()
384     endif()
385   endforeach()
386 endif()
388 add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
390 add_subdirectory(scripts)
392 if(COMPILER_RT_INCLUDE_TESTS)
393   add_subdirectory(tests)
394 endif()