xref: /llvm-project/compiler-rt/lib/asan/CMakeLists.txt (revision 74496b03f7db43caae4c3bda79379623a4657a00)
1# Build for the AddressSanitizer runtime support library.
2
3set(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  )
34
35if(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    )
47endif()
48
49if (NOT WIN32 AND NOT APPLE)
50  list(APPEND ASAN_SOURCES
51    asan_interceptors_vfork.S
52    )
53endif()
54
55set(ASAN_CXX_SOURCES
56  asan_new_delete.cpp
57  )
58
59set(ASAN_STATIC_SOURCES
60  asan_rtl_static.cpp
61  )
62
63if ("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  )
67endif()
68
69set(ASAN_PREINIT_SOURCES
70  asan_preinit.cpp
71  )
72
73SET(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  )
98
99include_directories(..)
100if(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)
106endif()
107set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
108
109append_list_if(MSVC /Zl ASAN_CFLAGS)
110
111set(ASAN_COMMON_DEFINITIONS "")
112
113append_rtti_flag(OFF ASAN_CFLAGS)
114
115# Silence warnings in system headers with MSVC.
116if(NOT CLANG_CL)
117  append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" ASAN_CFLAGS)
118endif()
119
120# Too many existing bugs, needs cleanup.
121append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)
122
123set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
124
125if(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()
131endif()
132
133set(ASAN_DYNAMIC_DEFINITIONS
134  ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
135append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
136
137set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
138append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
139  -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
140
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
144append_list_if(MSVC "LINKER:/DEBUG;LINKER:/OPT:NOICF" ASAN_DYNAMIC_LINK_FLAGS)
145
146set(ASAN_DYNAMIC_LIBS
147  ${COMPILER_RT_UNWINDER_LINK_LIBS}
148  ${SANITIZER_CXX_ABI_LIBRARIES}
149  ${SANITIZER_COMMON_LINK_LIBS})
150
151append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
152append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)
153append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
154append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
155append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)
156append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)
157
158# Compile ASan sources into an object library.
159
160add_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})
167
168if(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})
193
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})
200endif()
201
202# Build ASan runtimes shipped with Clang.
203add_compiler_rt_component(asan)
204
205if(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)
211
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)
228
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)
236else()
237  # Build separate libraries for each target.
238
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)
258
259    add_compiler_rt_runtime(clang_rt.asan_cxx
260      STATIC
261      ARCHS ${ASAN_SUPPORTED_ARCH}
262      OBJECT_LIBS RTAsan_cxx
263      CFLAGS ${ASAN_CFLAGS}
264      DEFS ${ASAN_COMMON_DEFINITIONS}
265      PARENT_TARGET asan)
266
267    add_compiler_rt_runtime(clang_rt.asan_static
268      STATIC
269      ARCHS ${ASAN_SUPPORTED_ARCH}
270      OBJECT_LIBS RTAsan_static
271      CFLAGS ${ASAN_CFLAGS}
272      DEFS ${ASAN_COMMON_DEFINITIONS}
273      PARENT_TARGET asan)
274
275    add_compiler_rt_runtime(clang_rt.asan-preinit
276      STATIC
277      ARCHS ${ASAN_SUPPORTED_ARCH}
278      OBJECT_LIBS RTAsan_preinit
279      CFLAGS ${ASAN_CFLAGS}
280      DEFS ${ASAN_COMMON_DEFINITIONS}
281      PARENT_TARGET asan)
282  endif()
283
284  foreach(arch ${ASAN_SUPPORTED_ARCH})
285    if (COMPILER_RT_HAS_VERSION_SCRIPT)
286      if(WIN32)
287        set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch})
288      else()
289        set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch})
290      endif()
291      add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}
292                                    LIBS ${SANITIZER_RT_VERSION_LIST_LIBS}
293                                    EXTRA asan.syms.extra)
294      set(VERSION_SCRIPT_FLAG
295           -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
296      # The Solaris 11.4 linker supports a subset of GNU ld version scripts,
297      # but requires a special option to enable it.
298      if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)
299          list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)
300      endif()
301      set_property(SOURCE
302        ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
303        APPEND PROPERTY
304        OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)
305    else()
306      set(VERSION_SCRIPT_FLAG)
307    endif()
308
309    set(ASAN_DYNAMIC_WEAK_INTERCEPTION)
310    add_compiler_rt_runtime(clang_rt.asan
311      SHARED
312      ARCHS ${arch}
313      OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}
314              RTAsan_dynamic
315              # The only purpose of RTAsan_dynamic_version_script_dummy is to
316              # carry a dependency of the shared runtime on the version script.
317              # Replacing it with a straightforward
318              # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
319              # generates an order-only dependency in ninja.
320              RTAsan_dynamic_version_script_dummy
321              RTUbsan_cxx
322              ${ASAN_DYNAMIC_WEAK_INTERCEPTION}
323      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
324      LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}
325                ${VERSION_SCRIPT_FLAG}
326      LINK_LIBS ${ASAN_DYNAMIC_LIBS}
327      DEFS ${ASAN_DYNAMIC_DEFINITIONS}
328      PARENT_TARGET asan)
329
330    if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")
331      add_sanitizer_rt_symbols(clang_rt.asan_cxx
332        ARCHS ${arch})
333      add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
334      add_sanitizer_rt_symbols(clang_rt.asan
335        ARCHS ${arch}
336        EXTRA asan.syms.extra)
337      add_dependencies(asan clang_rt.asan-${arch}-symbols)
338    endif()
339
340    if (WIN32)
341      set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")
342
343      add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk
344        ${SANITIZER_COMMON_SUPPORTED_OS}
345        ARCHS ${arch}
346        SOURCES ${ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES}
347        CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
348        DEFS ${ASAN_COMMON_DEFINITIONS})
349
350      add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk
351        STATIC
352        ARCHS ${arch}
353        OBJECT_LIBS AsanDynamicRuntimeThunk
354                    UbsanRuntimeThunk
355                    SancovRuntimeThunk
356                    SanitizerRuntimeThunk
357        CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}
358        DEFS ${ASAN_COMMON_DEFINITIONS}
359        PARENT_TARGET asan)
360
361      # mingw does not support static linkage of the CRT
362      if(NOT MINGW)
363        set(STATIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_STATIC_RUNTIME_THUNK")
364
365        add_compiler_rt_object_libraries(AsanStaticRuntimeThunk
366          ${SANITIZER_COMMON_SUPPORTED_OS}
367          ARCHS ${arch}
368          SOURCES ${ASAN_STATIC_RUNTIME_THUNK_SOURCES}
369          CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
370          DEFS ${ASAN_DYNAMIC_DEFINITIONS})
371
372        add_compiler_rt_runtime(clang_rt.asan_static_runtime_thunk
373          STATIC
374          ARCHS ${arch}
375          OBJECT_LIBS AsanStaticRuntimeThunk
376                      UbsanRuntimeThunk
377                      SancovRuntimeThunk
378                      SanitizerRuntimeThunk
379          CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}
380          DEFS ${ASAN_DYNAMIC_DEFINITIONS}
381          PARENT_TARGET asan)
382      endif()
383    endif()
384  endforeach()
385endif()
386
387add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)
388
389add_subdirectory(scripts)
390
391if(COMPILER_RT_INCLUDE_TESTS)
392  add_subdirectory(tests)
393endif()
394