xref: /llvm-project/compiler-rt/lib/hwasan/CMakeLists.txt (revision 74496b03f7db43caae4c3bda79379623a4657a00)
1include_directories(..)
2
3# Runtime library sources and build flags.
4set(HWASAN_RTL_SOURCES
5  hwasan.cpp
6  hwasan_allocator.cpp
7  hwasan_allocation_functions.cpp
8  hwasan_dynamic_shadow.cpp
9  hwasan_exceptions.cpp
10  hwasan_fuchsia.cpp
11  hwasan_globals.cpp
12  hwasan_interceptors.cpp
13  hwasan_interceptors_vfork.S
14  hwasan_linux.cpp
15  hwasan_memintrinsics.cpp
16  hwasan_poisoning.cpp
17  hwasan_report.cpp
18  hwasan_thread.cpp
19  hwasan_thread_list.cpp
20  hwasan_type_test.cpp
21  )
22
23foreach(arch ${HWASAN_SUPPORTED_ARCH})
24  if(${arch} MATCHES "aarch64")
25    list(APPEND HWASAN_RTL_SOURCES
26      hwasan_setjmp_aarch64.S
27      hwasan_tag_mismatch_aarch64.S
28      )
29  endif()
30  if(${arch} MATCHES "riscv64")
31    list(APPEND HWASAN_RTL_SOURCES
32      hwasan_setjmp_riscv64.S
33      hwasan_tag_mismatch_riscv64.S
34      )
35  endif()
36  if(${arch} MATCHES "x86_64")
37    list(APPEND HWASAN_RTL_SOURCES
38      hwasan_setjmp_x86_64.S
39      )
40  endif()
41endforeach()
42
43set(HWASAN_RTL_CXX_SOURCES
44  hwasan_new_delete.cpp
45  )
46
47set(HWASAN_RTL_PREINIT_SOURCES
48  hwasan_preinit.cpp
49  )
50
51set(HWASAN_RTL_HEADERS
52  hwasan.h
53  hwasan_allocator.h
54  hwasan_dynamic_shadow.h
55  hwasan_flags.h
56  hwasan_flags.inc
57  hwasan_globals.h
58  hwasan_interface_internal.h
59  hwasan_malloc_bisect.h
60  hwasan_mapping.h
61  hwasan_poisoning.h
62  hwasan_report.h
63  hwasan_thread.h
64  hwasan_thread_list.h
65  )
66
67set(HWASAN_DEFINITIONS)
68append_list_if(COMPILER_RT_HWASAN_WITH_INTERCEPTORS HWASAN_WITH_INTERCEPTORS=1 HWASAN_DEFINITIONS)
69
70if(FUCHSIA)
71  # Set this explicitly on Fuchsia, otherwise the default value is set to HWASAN_WITH_INTERCEPTORS.
72  list(APPEND HWASAN_DEFINITIONS HWASAN_REPLACE_OPERATORS_NEW_AND_DELETE=1)
73endif()
74
75set(HWASAN_RTL_CFLAGS ${SANITIZER_COMMON_CFLAGS})
76append_rtti_flag(OFF HWASAN_RTL_CFLAGS)
77append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC HWASAN_RTL_CFLAGS)
78# Prevent clang from generating libc calls.
79append_list_if(COMPILER_RT_HAS_FFREESTANDING_FLAG -ffreestanding HWASAN_RTL_CFLAGS)
80
81# Too many existing bugs, needs cleanup.
82append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format HWASAN_RTL_CFLAGS)
83
84set(HWASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})
85
86if(ANDROID)
87# Put most Sanitizer shared libraries in the global group. For more details, see
88# android-changes-for-ndk-developers.md#changes-to-library-search-order
89  if (COMPILER_RT_HAS_Z_GLOBAL)
90    list(APPEND HWASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)
91  endif()
92endif()
93
94set(HWASAN_DYNAMIC_CFLAGS ${HWASAN_RTL_CFLAGS})
95append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
96  -ftls-model=initial-exec HWASAN_DYNAMIC_CFLAGS)
97append_list_if(MSVC /DEBUG HWASAN_DYNAMIC_LINK_FLAGS)
98
99set(HWASAN_DYNAMIC_LIBS
100  ${COMPILER_RT_UNWINDER_LINK_LIBS}
101  ${SANITIZER_CXX_ABI_LIBRARIES}
102  ${SANITIZER_COMMON_LINK_LIBS})
103
104append_list_if(COMPILER_RT_HAS_LIBDL dl HWASAN_DYNAMIC_LIBS)
105append_list_if(COMPILER_RT_HAS_LIBRT rt HWASAN_DYNAMIC_LIBS)
106append_list_if(COMPILER_RT_HAS_LIBM m HWASAN_DYNAMIC_LIBS)
107append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread HWASAN_DYNAMIC_LIBS)
108
109# Static runtime library.
110add_compiler_rt_component(hwasan)
111
112add_compiler_rt_object_libraries(RTHwasan
113  ARCHS ${HWASAN_SUPPORTED_ARCH}
114  SOURCES ${HWASAN_RTL_SOURCES}
115  ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
116  CFLAGS ${HWASAN_RTL_CFLAGS}
117  DEFS ${HWASAN_DEFINITIONS})
118add_compiler_rt_object_libraries(RTHwasan_cxx
119  ARCHS ${HWASAN_SUPPORTED_ARCH}
120  SOURCES ${HWASAN_RTL_CXX_SOURCES}
121  ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
122  CFLAGS ${HWASAN_RTL_CFLAGS}
123  DEFS ${HWASAN_DEFINITIONS})
124add_compiler_rt_object_libraries(RTHwasan_dynamic
125  ARCHS ${HWASAN_SUPPORTED_ARCH}
126  SOURCES ${HWASAN_RTL_SOURCES} ${HWASAN_RTL_CXX_SOURCES}
127  ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
128  CFLAGS ${HWASAN_DYNAMIC_CFLAGS}
129  DEFS ${HWASAN_DEFINITIONS})
130add_compiler_rt_object_libraries(RTHwasan_preinit
131  ARCHS ${HWASAN_SUPPORTED_ARCH}
132  SOURCES ${HWASAN_RTL_PREINIT_SOURCES}
133  ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
134  CFLAGS ${HWASAN_RTL_CFLAGS}
135  DEFS ${HWASAN_DEFINITIONS})
136
137file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")
138add_compiler_rt_object_libraries(RTHwasan_dynamic_version_script_dummy
139  ARCHS ${HWASAN_SUPPORTED_ARCH}
140  SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
141  CFLAGS ${HWASAN_DYNAMIC_CFLAGS}
142  DEFS ${HWASAN_DEFINITIONS})
143
144# If use_aliases is TRUE, adds the HWASan runtime built with alias support.
145# Otherwise adds the runtime without alias support.
146function(add_hwasan_runtimes arch use_aliases)
147  set(hwasan_object_lib RTHwasan)
148  set(hwasan_object_dyn_lib RTHwasan_dynamic)
149  set(hwasan_runtime clang_rt.hwasan)
150  set(hwasan_rtl_flags ${HWASAN_RTL_CFLAGS})
151  set(hwasan_dyn_flags ${HWASAN_DYNAMIC_CFLAGS})
152  if(use_aliases)
153    list(APPEND hwasan_rtl_flags -DHWASAN_ALIASING_MODE)
154    list(APPEND hwasan_dyn_flags -DHWASAN_ALIASING_MODE)
155    add_compiler_rt_object_libraries(RTHwasanAliases
156      ARCHS ${arch}
157      SOURCES ${HWASAN_RTL_SOURCES}
158      ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
159      CFLAGS ${hwasan_rtl_flags}
160      DEFS ${HWASAN_DEFINITIONS})
161    add_compiler_rt_object_libraries(RTHwasanAliases_dynamic
162      ARCHS ${arch}
163      SOURCES ${HWASAN_RTL_SOURCES} ${HWASAN_RTL_CXX_SOURCES}
164      ADDITIONAL_HEADERS ${HWASAN_RTL_HEADERS}
165      CFLAGS ${hwasan_dyn_flags}
166      DEFS ${HWASAN_DEFINITIONS})
167
168    set(hwasan_object_lib RTHwasanAliases)
169    set(hwasan_object_dyn_lib RTHwasanAliases_dynamic)
170    set(hwasan_runtime clang_rt.hwasan_aliases)
171  endif()
172  add_compiler_rt_runtime(${hwasan_runtime}
173    STATIC
174    ARCHS ${arch}
175    OBJECT_LIBS ${hwasan_object_lib}
176                RTHwasan_preinit
177                RTInterception
178                RTSanitizerCommon
179                RTSanitizerCommonLibc
180                RTSanitizerCommonCoverage
181                RTSanitizerCommonSymbolizer
182                RTSanitizerCommonSymbolizerInternal
183                RTLSanCommon
184                RTUbsan
185    CFLAGS ${hwasan_rtl_flags}
186    PARENT_TARGET hwasan)
187  add_compiler_rt_runtime(${hwasan_runtime}_cxx
188    STATIC
189    ARCHS ${arch}
190    OBJECT_LIBS RTHwasan_cxx
191    CFLAGS ${hwasan_rtl_flags}
192    PARENT_TARGET hwasan)
193
194  if (UNIX)
195    add_sanitizer_rt_version_list(${hwasan_runtime}-dynamic-${arch}
196                                  LIBS ${hwasan_runtime}-${arch} ${hwasan_runtime}_cxx-${arch}
197                                  EXTRA hwasan.syms.extra)
198    set(VERSION_SCRIPT_FLAG
199         -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/${hwasan_runtime}-dynamic-${arch}.vers)
200    set_property(SOURCE
201      ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp
202      APPEND PROPERTY
203      OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${hwasan_runtime}-dynamic-${arch}.vers)
204  else()
205    set(VERSION_SCRIPT_FLAG)
206  endif()
207
208
209  add_compiler_rt_runtime(${hwasan_runtime}
210    SHARED
211    ARCHS ${arch}
212    OBJECT_LIBS
213            ${hwasan_object_dyn_lib}
214            RTInterception
215            RTSanitizerCommon
216            RTSanitizerCommonLibc
217            RTSanitizerCommonCoverage
218            RTSanitizerCommonSymbolizer
219            RTSanitizerCommonSymbolizerInternal
220            RTLSanCommon
221            RTUbsan
222            RTUbsan_cxx
223            # The only purpose of RTHWAsan_dynamic_version_script_dummy is to
224            # carry a dependency of the shared runtime on the version script.
225            # Replacing it with a straightforward
226            # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)
227            # generates an order-only dependency in ninja.
228            RTHwasan_dynamic_version_script_dummy
229    CFLAGS ${hwasan_dyn_flags}
230    LINK_FLAGS ${HWASAN_DYNAMIC_LINK_FLAGS}
231              ${VERSION_SCRIPT_FLAG}
232    LINK_LIBS ${HWASAN_DYNAMIC_LIBS}
233    DEFS ${ASAN_DYNAMIC_DEFINITIONS}
234    PARENT_TARGET hwasan)
235
236  if(SANITIZER_USE_SYMBOLS)
237    add_sanitizer_rt_symbols(${hwasan_runtime}
238      ARCHS ${arch}
239      EXTRA hwasan.syms.extra)
240    add_sanitizer_rt_symbols(${hwasan_runtime}_cxx
241      ARCHS ${arch}
242      EXTRA hwasan.syms.extra)
243    add_dependencies(hwasan ${hwasan_runtime}-${arch}-symbols
244                          ${hwasan_runtime}_cxx-${arch}-symbols)
245  endif()
246endfunction()
247
248foreach(arch ${HWASAN_SUPPORTED_ARCH})
249  add_hwasan_runtimes(${arch} FALSE)
250  if(${arch} MATCHES "x86_64")
251    add_hwasan_runtimes(${arch} TRUE)
252  endif()
253endforeach()
254
255add_compiler_rt_runtime(clang_rt.hwasan-preinit
256  STATIC
257  ARCHS ${HWASAN_SUPPORTED_ARCH}
258  OBJECT_LIBS RTHwasan_preinit
259  CFLAGS ${HWASAN_RTL_CFLAGS}
260  PARENT_TARGET hwasan)
261
262add_compiler_rt_resource_file(hwasan_ignorelist hwasan_ignorelist.txt hwasan)
263
264add_subdirectory("scripts")
265
266# if(COMPILER_RT_INCLUDE_TESTS)
267#   add_subdirectory(tests)
268# endif()
269