1# Build for the AddressSanitizer runtime support library.
2
3set(ASAN_SOURCES
4  asan_allocator.cc
5  asan_activation.cc
6  asan_debugging.cc
7  asan_fake_stack.cc
8  asan_flags.cc
9  asan_globals.cc
10  asan_interceptors.cc
11  asan_linux.cc
12  asan_mac.cc
13  asan_malloc_linux.cc
14  asan_malloc_mac.cc
15  asan_malloc_win.cc
16  asan_poisoning.cc
17  asan_posix.cc
18  asan_report.cc
19  asan_rtl.cc
20  asan_stack.cc
21  asan_stats.cc
22  asan_suppressions.cc
23  asan_thread.cc
24  asan_win.cc)
25
26set(ASAN_CXX_SOURCES
27  asan_new_delete.cc)
28
29set(ASAN_PREINIT_SOURCES
30  asan_preinit.cc)
31
32include_directories(..)
33
34set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
35append_no_rtti_flag(ASAN_CFLAGS)
36
37set(ASAN_COMMON_DEFINITIONS
38  ASAN_HAS_EXCEPTIONS=1)
39
40if(ANDROID)
41  list(APPEND ASAN_COMMON_DEFINITIONS
42    ASAN_LOW_MEMORY=1)
43endif()
44
45set(ASAN_DYNAMIC_DEFINITIONS
46  ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
47append_list_if(WIN32 INTERCEPTION_DYNAMIC_CRT ASAN_DYNAMIC_DEFINITIONS)
48
49set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
50append_list_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
51  -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
52append_list_if(MSVC /DEBUG ASAN_DYNAMIC_CFLAGS)
53
54append_list_if(COMPILER_RT_HAS_LIBC c ASAN_DYNAMIC_LIBS)
55append_list_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
56append_list_if(COMPILER_RT_HAS_LIBM m ASAN_DYNAMIC_LIBS)
57append_list_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
58append_list_if(COMPILER_RT_HAS_LIBSTDCXX stdc++ ASAN_DYNAMIC_LIBS)
59
60append_list_if(ANDROID log ASAN_DYNAMIC_LIBS)
61
62# Compile ASan sources into an object library.
63if(APPLE)
64  foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
65    add_compiler_rt_darwin_object_library(RTAsan ${os}
66      ARCH ${ASAN_SUPPORTED_ARCH}
67      SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
68      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
69      DEFS ${ASAN_DYNAMIC_DEFINITIONS})
70  endforeach()
71else()
72  foreach(arch ${ASAN_SUPPORTED_ARCH})
73    add_compiler_rt_object_library(RTAsan ${arch}
74      SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
75      DEFS ${ASAN_COMMON_DEFINITIONS})
76    add_compiler_rt_object_library(RTAsan_cxx ${arch}
77      SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
78      DEFS ${ASAN_COMMON_DEFINITIONS})
79    add_compiler_rt_object_library(RTAsan_preinit ${arch}
80      SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
81      DEFS ${ASAN_COMMON_DEFINITIONS})
82    add_compiler_rt_object_library(RTAsan_dynamic ${arch}
83      SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
84      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
85      DEFS ${ASAN_DYNAMIC_DEFINITIONS})
86  endforeach()
87endif()
88
89# Build ASan runtimes shipped with Clang.
90add_custom_target(asan)
91if(APPLE)
92  foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
93    add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
94      ARCH ${ASAN_SUPPORTED_ARCH}
95      SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
96              $<TARGET_OBJECTS:RTInterception.${os}>
97              $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
98              $<TARGET_OBJECTS:RTLSanCommon.${os}>
99              $<TARGET_OBJECTS:RTUbsan.${os}>
100      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
101      DEFS ${ASAN_DYNAMIC_DEFINITIONS})
102    add_dependencies(asan clang_rt.asan_${os}_dynamic)
103  endforeach()
104else()
105  # Build separate libraries for each target.
106  foreach(arch ${ASAN_SUPPORTED_ARCH})
107    set(ASAN_COMMON_RUNTIME_OBJECTS
108      $<TARGET_OBJECTS:RTInterception.${arch}>
109      $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
110      $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>
111      $<TARGET_OBJECTS:RTLSanCommon.${arch}>
112      $<TARGET_OBJECTS:RTUbsan.${arch}>)
113
114    add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
115      SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
116              $<TARGET_OBJECTS:RTAsan.${arch}>
117              ${ASAN_COMMON_RUNTIME_OBJECTS}
118      CFLAGS ${ASAN_CFLAGS}
119      DEFS ${ASAN_COMMON_DEFINITIONS})
120    add_dependencies(asan clang_rt.asan-${arch})
121
122    add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
123      SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
124              $<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
125      CFLAGS ${ASAN_CFLAGS}
126      DEFS ${ASAN_COMMON_DEFINITIONS})
127    add_dependencies(asan clang_rt.asan_cxx-${arch})
128
129    add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
130      SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
131      CFLAGS ${ASAN_CFLAGS}
132      DEFS ${ASAN_COMMON_DEFINITIONS})
133    add_dependencies(asan clang_rt.asan-preinit-${arch})
134
135    if (WIN32)
136      set(SHARED_ASAN_NAME clang_rt.asan_dynamic-${arch}${COMPILER_RT_OS_SUFFIX})
137    else()
138      set(SHARED_ASAN_NAME clang_rt.asan-${arch}${COMPILER_RT_OS_SUFFIX})
139    endif()
140    add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
141      OUTPUT_NAME ${SHARED_ASAN_NAME}
142      SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
143              $<TARGET_OBJECTS:RTUbsan_cxx.${arch}>
144              ${ASAN_COMMON_RUNTIME_OBJECTS}
145      CFLAGS ${ASAN_DYNAMIC_CFLAGS}
146      DEFS ${ASAN_DYNAMIC_DEFINITIONS})
147    target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
148    add_dependencies(asan clang_rt.asan-dynamic-${arch})
149
150    if (UNIX AND NOT ${arch} MATCHES "i386|i686")
151      add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
152      add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
153      add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
154      add_dependencies(asan clang_rt.asan-${arch}-symbols)
155    endif()
156
157    if (WIN32)
158      add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
159        SOURCES asan_win_dll_thunk.cc
160                $<TARGET_OBJECTS:RTInterception.${arch}>
161        CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
162        DEFS ${ASAN_COMMON_DEFINITIONS})
163      add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
164      add_compiler_rt_runtime(clang_rt.asan_dynamic_runtime_thunk-${arch} ${arch}
165        STATIC
166        SOURCES asan_win_dynamic_runtime_thunk.cc
167        CFLAGS ${ASAN_CFLAGS} -DASAN_DYNAMIC_RUNTIME_THUNK -Zl
168        DEFS ${ASAN_COMMON_DEFINITIONS})
169      add_dependencies(asan clang_rt.asan_dynamic_runtime_thunk-${arch})
170    endif()
171  endforeach()
172endif()
173
174add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
175add_dependencies(asan asan_blacklist)
176add_dependencies(compiler-rt asan)
177
178add_subdirectory(scripts)
179
180if(COMPILER_RT_INCLUDE_TESTS)
181  add_subdirectory(tests)
182endif()
183