1# Build for the AddressSanitizer runtime support library.
2
3if(APPLE)
4# Don't set rpath for the ASan libraries. Developers are encouraged to ship
5# their binaries together with the corresponding ASan runtime libraries,
6# so they'll anyway need to fix the rpath and the install name.
7set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)
8endif()
9
10set(ASAN_SOURCES
11  asan_allocator2.cc
12  asan_activation.cc
13  asan_fake_stack.cc
14  asan_globals.cc
15  asan_interceptors.cc
16  asan_linux.cc
17  asan_mac.cc
18  asan_malloc_linux.cc
19  asan_malloc_mac.cc
20  asan_malloc_win.cc
21  asan_poisoning.cc
22  asan_posix.cc
23  asan_report.cc
24  asan_rtl.cc
25  asan_stack.cc
26  asan_stats.cc
27  asan_thread.cc
28  asan_win.cc)
29
30set(ASAN_CXX_SOURCES
31  asan_new_delete.cc)
32
33set(ASAN_PREINIT_SOURCES
34  asan_preinit.cc)
35
36include_directories(..)
37
38if(ANDROID)
39  include_directories(${COMPILER_RT_EXTRA_ANDROID_HEADERS})
40endif()
41
42set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
43append_no_rtti_flag(ASAN_CFLAGS)
44
45set(ASAN_COMMON_DEFINITIONS
46  ASAN_HAS_EXCEPTIONS=1)
47
48if(ANDROID)
49  list(APPEND ASAN_COMMON_DEFINITIONS
50    ASAN_LOW_MEMORY=1)
51endif()
52
53set(ASAN_DYNAMIC_DEFINITIONS
54  ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
55
56set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
57append_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
58  -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
59
60set(ASAN_DYNAMIC_LIBS stdc++ m c)
61append_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
62append_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
63
64# Compile ASan sources into an object library.
65if(APPLE)
66  foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
67    add_compiler_rt_darwin_object_library(RTAsan ${os}
68      ARCH ${ASAN_SUPPORTED_ARCH}
69      SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
70      CFLAGS ${ASAN_CFLAGS}
71      DEFS ${ASAN_COMMON_DEFINITIONS})
72  endforeach()
73elseif(ANDROID)
74  add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES} ${ASAN_CXX_SOURCES})
75  set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
76  set_property(TARGET RTAsan.arm.android APPEND PROPERTY
77    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
78else()
79  foreach(arch ${ASAN_SUPPORTED_ARCH})
80    add_compiler_rt_object_library(RTAsan ${arch}
81      SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
82      DEFS ${ASAN_COMMON_DEFINITIONS})
83    add_compiler_rt_object_library(RTAsan_cxx ${arch}
84      SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
85      DEFS ${ASAN_COMMON_DEFINITIONS})
86    add_compiler_rt_object_library(RTAsan_preinit ${arch}
87      SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
88      DEFS ${ASAN_COMMON_DEFINITIONS})
89    if (COMPILER_RT_BUILD_SHARED_ASAN)
90      add_compiler_rt_object_library(RTAsan_dynamic ${arch}
91        SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
92        CFLAGS ${ASAN_DYNAMIC_CFLAGS}
93        DEFS ${ASAN_DYNAMIC_DEFINITIONS})
94    endif()
95  endforeach()
96endif()
97
98# Build ASan runtimes shipped with Clang.
99add_custom_target(asan)
100if(APPLE)
101  foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
102    add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
103      ARCH ${ASAN_SUPPORTED_ARCH}
104      SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
105              $<TARGET_OBJECTS:RTInterception.${os}>
106              $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
107              $<TARGET_OBJECTS:RTLSanCommon.${os}>
108      CFLAGS ${ASAN_CFLAGS}
109      DEFS ${ASAN_COMMON_DEFINITIONS})
110    add_dependencies(asan clang_rt.asan_${os}_dynamic)
111  endforeach()
112
113elseif(ANDROID)
114  add_library(clang_rt.asan-arm-android SHARED
115    $<TARGET_OBJECTS:RTAsan.arm.android>
116    $<TARGET_OBJECTS:RTInterception.arm.android>
117    $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
118  set_target_compile_flags(clang_rt.asan-arm-android
119    ${ASAN_CFLAGS})
120  set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
121    COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
122  target_link_libraries(clang_rt.asan-arm-android dl log)
123  add_dependencies(asan clang_rt.asan-arm-android)
124  install(TARGETS clang_rt.asan-arm-android
125          ARCHIVE DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR}
126          LIBRARY DESTINATION ${COMPILER_RT_LIBRARY_INSTALL_DIR})
127else()
128  # Build separate libraries for each target.
129  foreach(arch ${ASAN_SUPPORTED_ARCH})
130    set(ASAN_COMMON_RUNTIME_OBJECTS
131      $<TARGET_OBJECTS:RTInterception.${arch}>
132      $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
133      $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
134    if (NOT WIN32)
135      # We can't build Leak Sanitizer on Windows yet.
136      list(APPEND ASAN_COMMON_RUNTIME_OBJECTS
137           $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
138    endif()
139
140    add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
141      SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
142              $<TARGET_OBJECTS:RTAsan.${arch}>
143              ${ASAN_COMMON_RUNTIME_OBJECTS}
144      CFLAGS ${ASAN_CFLAGS}
145      DEFS ${ASAN_COMMON_DEFINITIONS})
146    add_dependencies(asan clang_rt.asan-${arch})
147
148    add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
149      SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
150      CFLAGS ${ASAN_CFLAGS}
151      DEFS ${ASAN_COMMON_DEFINITIONS})
152    add_dependencies(asan clang_rt.asan_cxx-${arch})
153
154    if (COMPILER_RT_BUILD_SHARED_ASAN)
155      add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
156        SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
157        CFLAGS ${ASAN_CFLAGS}
158        DEFS ${ASAN_COMMON_DEFINITIONS})
159      add_dependencies(asan clang_rt.asan-preinit-${arch})
160
161      add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
162        OUTPUT_NAME clang_rt.asan-${arch}
163        SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
164                ${ASAN_COMMON_RUNTIME_OBJECTS}
165        CFLAGS ${ASAN_DYNAMIC_CFLAGS}
166        DEFS ${ASAN_DYNAMIC_DEFINITIONS})
167      target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
168      add_dependencies(asan clang_rt.asan-dynamic-${arch})
169    endif()
170
171    if (UNIX AND NOT ${arch} STREQUAL "i386")
172      add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
173      add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
174      add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
175      add_dependencies(asan clang_rt.asan-${arch}-symbols)
176    endif()
177
178    if (WIN32)
179      add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
180        SOURCES asan_dll_thunk.cc
181                $<TARGET_OBJECTS:RTInterception.${arch}>
182        CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
183        DEFS ${ASAN_COMMON_DEFINITIONS})
184      add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
185    endif()
186  endforeach()
187endif()
188
189add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
190add_dependencies(asan asan_blacklist)
191add_dependencies(compiler-rt asan)
192
193add_subdirectory(scripts)
194
195if(COMPILER_RT_INCLUDE_TESTS)
196  add_subdirectory(tests)
197endif()
198