414 lines · plain
1# Build for the AddressSanitizer runtime support library.2 3set(ASAN_SOURCES4 asan_aix.cpp5 asan_allocator.cpp6 asan_activation.cpp7 asan_debugging.cpp8 asan_descriptions.cpp9 asan_errors.cpp10 asan_fake_stack.cpp11 asan_flags.cpp12 asan_fuchsia.cpp13 asan_globals.cpp14 asan_globals_win.cpp15 asan_interceptors.cpp16 asan_interceptors_memintrinsics.cpp17 asan_linux.cpp18 asan_mac.cpp19 asan_malloc_linux.cpp20 asan_malloc_mac.cpp21 asan_malloc_win.cpp22 asan_memory_profile.cpp23 asan_poisoning.cpp24 asan_posix.cpp25 asan_premap_shadow.cpp26 asan_report.cpp27 asan_rtl.cpp28 asan_shadow_setup.cpp29 asan_stack.cpp30 asan_stats.cpp31 asan_suppressions.cpp32 asan_thread.cpp33 asan_win.cpp34 )35 36if(WIN32)37 set(ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES38 asan_globals_win.cpp39 asan_win_common_runtime_thunk.cpp40 asan_win_dynamic_runtime_thunk.cpp41 )42 set(ASAN_STATIC_RUNTIME_THUNK_SOURCES43 asan_globals_win.cpp44 asan_malloc_win_thunk.cpp45 asan_win_common_runtime_thunk.cpp46 asan_win_static_runtime_thunk.cpp47 )48endif()49 50if (NOT WIN32 AND NOT APPLE)51 list(APPEND ASAN_SOURCES52 asan_interceptors_vfork.S53 )54endif()55 56set(ASAN_CXX_SOURCES57 asan_new_delete.cpp58 )59 60set(ASAN_STATIC_SOURCES61 asan_rtl_static.cpp62 )63 64if ("x86_64" IN_LIST ASAN_SUPPORTED_ARCH AND NOT WIN32 AND NOT APPLE)65 list(APPEND ASAN_STATIC_SOURCES66 asan_rtl_x86_64.S67 )68endif()69 70set(ASAN_PREINIT_SOURCES71 asan_preinit.cpp72 )73 74SET(ASAN_HEADERS75 asan_activation.h76 asan_activation_flags.inc77 asan_allocator.h78 asan_descriptions.h79 asan_errors.h80 asan_fake_stack.h81 asan_flags.h82 asan_flags.inc83 asan_init_version.h84 asan_interceptors.h85 asan_interceptors_memintrinsics.h86 asan_interface.inc87 asan_interface_internal.h88 asan_internal.h89 asan_mapping.h90 asan_poisoning.h91 asan_premap_shadow.h92 asan_report.h93 asan_scariness_score.h94 asan_stack.h95 asan_stats.h96 asan_suppressions.h97 asan_thread.h98 )99 100include_directories(..)101if(MSVC)102 # asan on windows only supports the release dll version of the runtimes, in the interest of103 # only having one asan dll to support/test. Having asan statically linked104 # with the runtime might be possible, but it multiplies the number of scenerios to test.105 # the program USING sanitizers can use whatever version of the runtime it wants to.106 set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreadedDLL)107endif()108set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})109 110# Win/ASan relies on the runtime functions being hotpatchable. See111# https://github.com/llvm/llvm-project/pull/149444112if(MSVC)113 list(APPEND ASAN_CFLAGS /hotpatch)114endif()115 116append_list_if(MSVC /Zl ASAN_CFLAGS)117 118set(ASAN_COMMON_DEFINITIONS "")119 120append_rtti_flag(OFF ASAN_CFLAGS)121 122# Silence warnings in system headers with MSVC.123if(NOT CLANG_CL)124 append_list_if(COMPILER_RT_HAS_EXTERNAL_FLAG "/experimental:external;/external:W0;/external:anglebrackets" ASAN_CFLAGS)125endif()126 127# Too many existing bugs, needs cleanup.128append_list_if(COMPILER_RT_HAS_WNO_FORMAT -Wno-format ASAN_CFLAGS)129 130set(ASAN_DYNAMIC_LINK_FLAGS ${SANITIZER_COMMON_LINK_FLAGS})131 132if(ANDROID)133# Put most Sanitizer shared libraries in the global group. For more details, see134# android-changes-for-ndk-developers.md#changes-to-library-search-order135 if (COMPILER_RT_HAS_Z_GLOBAL)136 list(APPEND ASAN_DYNAMIC_LINK_FLAGS -Wl,-z,global)137 endif()138endif()139 140set(ASAN_DYNAMIC_DEFINITIONS141 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)142append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)143 144set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})145append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC146 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)147 148# LLVM turns /OPT:ICF back on when LLVM_ENABLE_PDBs is set149# we _REALLY_ need to turn it back off for ASAN, because the way150# asan emulates weak functions from DLLs requires NOICF151append_list_if(MSVC "LINKER:/DEBUG;LINKER:/OPT:NOICF" ASAN_DYNAMIC_LINK_FLAGS)152 153set(ASAN_DYNAMIC_LIBS154 ${COMPILER_RT_UNWINDER_LINK_LIBS}155 ${SANITIZER_CXX_ABI_LIBRARIES}156 ${SANITIZER_COMMON_LINK_LIBS})157 158append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)159append_list_if(COMPILER_RT_HAS_LIBRT rt ASAN_DYNAMIC_LIBS)160append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)161append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)162append_list_if(COMPILER_RT_HAS_LIBLOG log ASAN_DYNAMIC_LIBS)163append_list_if(MINGW "${MINGW_LIBRARIES}" ASAN_DYNAMIC_LIBS)164 165# Compile ASan sources into an object library.166 167add_compiler_rt_object_libraries(RTAsan_dynamic168 OS ${SANITIZER_COMMON_SUPPORTED_OS}169 ARCHS ${ASAN_SUPPORTED_ARCH}170 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}171 ADDITIONAL_HEADERS ${ASAN_HEADERS}172 CFLAGS ${ASAN_DYNAMIC_CFLAGS}173 DEFS ${ASAN_DYNAMIC_DEFINITIONS})174 175if(NOT APPLE)176 add_compiler_rt_object_libraries(RTAsan177 ARCHS ${ASAN_SUPPORTED_ARCH}178 SOURCES ${ASAN_SOURCES}179 ADDITIONAL_HEADERS ${ASAN_HEADERS}180 CFLAGS ${ASAN_CFLAGS}181 DEFS ${ASAN_COMMON_DEFINITIONS})182 add_compiler_rt_object_libraries(RTAsan_cxx183 ARCHS ${ASAN_SUPPORTED_ARCH}184 SOURCES ${ASAN_CXX_SOURCES}185 ADDITIONAL_HEADERS ${ASAN_HEADERS}186 CFLAGS ${ASAN_CFLAGS}187 DEFS ${ASAN_COMMON_DEFINITIONS})188 add_compiler_rt_object_libraries(RTAsan_static189 ARCHS ${ASAN_SUPPORTED_ARCH}190 SOURCES ${ASAN_STATIC_SOURCES}191 ADDITIONAL_HEADERS ${ASAN_HEADERS}192 CFLAGS ${ASAN_CFLAGS}193 DEFS ${ASAN_COMMON_DEFINITIONS})194 add_compiler_rt_object_libraries(RTAsan_preinit195 ARCHS ${ASAN_SUPPORTED_ARCH}196 SOURCES ${ASAN_PREINIT_SOURCES}197 ADDITIONAL_HEADERS ${ASAN_HEADERS}198 CFLAGS ${ASAN_CFLAGS}199 DEFS ${ASAN_COMMON_DEFINITIONS})200 201 file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp "")202 add_compiler_rt_object_libraries(RTAsan_dynamic_version_script_dummy203 ARCHS ${ASAN_SUPPORTED_ARCH}204 SOURCES ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp205 CFLAGS ${ASAN_DYNAMIC_CFLAGS}206 DEFS ${ASAN_DYNAMIC_DEFINITIONS})207endif()208 209# Build ASan runtimes shipped with Clang.210add_compiler_rt_component(asan)211 212if(APPLE)213 add_weak_symbols("asan" WEAK_SYMBOL_LINK_FLAGS)214 add_weak_symbols("lsan" WEAK_SYMBOL_LINK_FLAGS)215 add_weak_symbols("ubsan" WEAK_SYMBOL_LINK_FLAGS)216 add_weak_symbols("sanitizer_common" WEAK_SYMBOL_LINK_FLAGS)217 add_weak_symbols("xray" WEAK_SYMBOL_LINK_FLAGS)218 219 add_compiler_rt_runtime(clang_rt.asan220 SHARED221 OS ${SANITIZER_COMMON_SUPPORTED_OS}222 ARCHS ${ASAN_SUPPORTED_ARCH}223 OBJECT_LIBS RTAsan_dynamic224 RTInterception225 RTSanitizerCommon226 RTSanitizerCommonLibc227 RTSanitizerCommonCoverage228 RTSanitizerCommonSymbolizer229 RTLSanCommon230 RTUbsan231 CFLAGS ${ASAN_DYNAMIC_CFLAGS}232 LINK_FLAGS ${WEAK_SYMBOL_LINK_FLAGS}233 DEFS ${ASAN_DYNAMIC_DEFINITIONS}234 PARENT_TARGET asan)235 236 add_compiler_rt_runtime(clang_rt.asan_static237 STATIC238 ARCHS ${ASAN_SUPPORTED_ARCH}239 OBJECT_LIBS RTAsan_static240 CFLAGS ${ASAN_CFLAGS}241 DEFS ${ASAN_COMMON_DEFINITIONS}242 PARENT_TARGET asan)243else()244 # Build separate libraries for each target.245 246 set(ASAN_COMMON_RUNTIME_OBJECT_LIBS247 RTInterception248 RTSanitizerCommon249 RTSanitizerCommonLibc250 RTSanitizerCommonCoverage251 RTSanitizerCommonSymbolizer252 RTSanitizerCommonSymbolizerInternal253 RTLSanCommon254 RTUbsan)255 if (NOT WIN32)256 add_compiler_rt_runtime(clang_rt.asan257 STATIC258 ARCHS ${ASAN_SUPPORTED_ARCH}259 OBJECT_LIBS RTAsan_preinit260 RTAsan261 ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}262 CFLAGS ${ASAN_CFLAGS}263 DEFS ${ASAN_COMMON_DEFINITIONS}264 PARENT_TARGET asan)265 266 add_compiler_rt_runtime(clang_rt.asan_cxx267 STATIC268 ARCHS ${ASAN_SUPPORTED_ARCH}269 OBJECT_LIBS RTAsan_cxx270 CFLAGS ${ASAN_CFLAGS}271 DEFS ${ASAN_COMMON_DEFINITIONS}272 PARENT_TARGET asan)273 274 add_compiler_rt_runtime(clang_rt.asan_static275 STATIC276 ARCHS ${ASAN_SUPPORTED_ARCH}277 OBJECT_LIBS RTAsan_static278 CFLAGS ${ASAN_CFLAGS}279 DEFS ${ASAN_COMMON_DEFINITIONS}280 PARENT_TARGET asan)281 282 add_compiler_rt_runtime(clang_rt.asan-preinit283 STATIC284 ARCHS ${ASAN_SUPPORTED_ARCH}285 OBJECT_LIBS RTAsan_preinit286 CFLAGS ${ASAN_CFLAGS}287 DEFS ${ASAN_COMMON_DEFINITIONS}288 PARENT_TARGET asan)289 endif()290 291 # On AIX, we only need the static libraries.292 if (NOT "${CMAKE_SYSTEM_NAME}" MATCHES "AIX")293 foreach(arch ${ASAN_SUPPORTED_ARCH})294 if (COMPILER_RT_HAS_VERSION_SCRIPT)295 if(WIN32)296 set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch})297 else()298 set(SANITIZER_RT_VERSION_LIST_LIBS clang_rt.asan-${arch} clang_rt.asan_cxx-${arch})299 endif()300 add_sanitizer_rt_version_list(clang_rt.asan-dynamic-${arch}301 LIBS ${SANITIZER_RT_VERSION_LIST_LIBS}302 EXTRA asan.syms.extra)303 set(VERSION_SCRIPT_FLAG304 -Wl,--version-script,${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)305 # The Solaris 11.4 linker supports a subset of GNU ld version scripts,306 # but requires a special option to enable it.307 if (COMPILER_RT_HAS_GNU_VERSION_SCRIPT_COMPAT)308 list(APPEND VERSION_SCRIPT_FLAG -Wl,-z,gnu-version-script-compat)309 endif()310 set_property(SOURCE311 ${CMAKE_CURRENT_BINARY_DIR}/dummy.cpp312 APPEND PROPERTY313 OBJECT_DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/clang_rt.asan-dynamic-${arch}.vers)314 else()315 set(VERSION_SCRIPT_FLAG)316 endif()317 318 set(ASAN_DYNAMIC_WEAK_INTERCEPTION)319 add_compiler_rt_runtime(clang_rt.asan320 SHARED321 ARCHS ${arch}322 OBJECT_LIBS ${ASAN_COMMON_RUNTIME_OBJECT_LIBS}323 RTAsan_dynamic324 # The only purpose of RTAsan_dynamic_version_script_dummy is to325 # carry a dependency of the shared runtime on the version script.326 # Replacing it with a straightforward327 # add_dependencies(clang_rt.asan-dynamic-${arch} clang_rt.asan-dynamic-${arch}-version-list)328 # generates an order-only dependency in ninja.329 RTAsan_dynamic_version_script_dummy330 RTUbsan_cxx331 ${ASAN_DYNAMIC_WEAK_INTERCEPTION}332 CFLAGS ${ASAN_DYNAMIC_CFLAGS}333 LINK_FLAGS ${ASAN_DYNAMIC_LINK_FLAGS}334 ${VERSION_SCRIPT_FLAG}335 LINK_LIBS ${ASAN_DYNAMIC_LIBS}336 DEFS ${ASAN_DYNAMIC_DEFINITIONS}337 PARENT_TARGET asan)338 339 if (SANITIZER_USE_SYMBOLS AND NOT ${arch} STREQUAL "i386")340 add_sanitizer_rt_symbols(clang_rt.asan_cxx341 ARCHS ${arch})342 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)343 add_sanitizer_rt_symbols(clang_rt.asan344 ARCHS ${arch}345 EXTRA asan.syms.extra)346 add_dependencies(asan clang_rt.asan-${arch}-symbols)347 endif()348 349 if (WIN32)350 set(DYNAMIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_DYNAMIC_RUNTIME_THUNK")351 352 add_compiler_rt_object_libraries(AsanDynamicRuntimeThunk353 ${SANITIZER_COMMON_SUPPORTED_OS}354 ARCHS ${arch}355 SOURCES ${ASAN_DYNAMIC_RUNTIME_THUNK_SOURCES}356 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}357 DEFS ${ASAN_COMMON_DEFINITIONS})358 359 add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk360 STATIC361 ARCHS ${arch}362 OBJECT_LIBS AsanDynamicRuntimeThunk363 UbsanRuntimeThunk364 SancovRuntimeThunk365 SanitizerRuntimeThunk366 CFLAGS ${ASAN_CFLAGS} ${DYNAMIC_RUNTIME_THUNK_CFLAGS}367 DEFS ${ASAN_COMMON_DEFINITIONS}368 PARENT_TARGET asan)369 370 # mingw does not support static linkage of the CRT371 if(NOT MINGW)372 set(STATIC_RUNTIME_THUNK_CFLAGS "-DSANITIZER_STATIC_RUNTIME_THUNK")373 374 add_compiler_rt_object_libraries(AsanStaticRuntimeThunk375 ${SANITIZER_COMMON_SUPPORTED_OS}376 ARCHS ${arch}377 SOURCES ${ASAN_STATIC_RUNTIME_THUNK_SOURCES}378 CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}379 DEFS ${ASAN_DYNAMIC_DEFINITIONS})380 381 add_compiler_rt_runtime(clang_rt.asan_static_runtime_thunk382 STATIC383 ARCHS ${arch}384 OBJECT_LIBS AsanStaticRuntimeThunk385 UbsanRuntimeThunk386 SancovRuntimeThunk387 SanitizerRuntimeThunk388 CFLAGS ${ASAN_DYNAMIC_CFLAGS} ${STATIC_RUNTIME_THUNK_CFLAGS}389 DEFS ${ASAN_DYNAMIC_DEFINITIONS}390 PARENT_TARGET asan)391 endif()392 endif()393 endforeach()394 endif()395endif()396 397add_compiler_rt_resource_file(asan_ignorelist asan_ignorelist.txt asan)398 399# On AIX, static sanitizer libraries are not added to the DSO, so we need to put 400# asan.link_with_main_exec.txt and asan_cxx.link_with_main_exec.txt to the build401# and install dir for use in resolving undefined sanitizer symbols at runtime.402if ("${CMAKE_SYSTEM_NAME}" MATCHES "AIX")403 foreach(arch ${ASAN_SUPPORTED_ARCH})404 add_compiler_rt_cfg(asan_symbols_${arch} asan.link_with_main_exec.txt asan ${arch})405 add_compiler_rt_cfg(asan_cxx_symbols_${arch} asan_cxx.link_with_main_exec.txt asan ${arch})406 endforeach()407endif()408 409add_subdirectory(scripts)410 411if(COMPILER_RT_INCLUDE_TESTS)412 add_subdirectory(tests)413endif()414