1 # Build for the AddressSanitizer runtime support library.
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.
7 set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)
33 set(ASAN_PREINIT_SOURCES
36 include_directories(..)
39 include_directories(${COMPILER_RT_EXTRA_ANDROID_HEADERS})
42 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
43 append_no_rtti_flag(ASAN_CFLAGS)
45 set(ASAN_COMMON_DEFINITIONS
46 ASAN_HAS_EXCEPTIONS=1)
49 list(APPEND ASAN_COMMON_DEFINITIONS
53 set(ASAN_DYNAMIC_DEFINITIONS
54 ${ASAN_COMMON_DEFINITIONS} ASAN_DYNAMIC=1)
56 set(ASAN_DYNAMIC_CFLAGS ${ASAN_CFLAGS})
57 append_if(COMPILER_RT_HAS_FTLS_MODEL_INITIAL_EXEC
58 -ftls-model=initial-exec ASAN_DYNAMIC_CFLAGS)
60 set(ASAN_DYNAMIC_LIBS stdc++ m c)
61 append_if(COMPILER_RT_HAS_LIBPTHREAD pthread ASAN_DYNAMIC_LIBS)
62 append_if(COMPILER_RT_HAS_LIBDL dl ASAN_DYNAMIC_LIBS)
65 set(ASAN_ASM_SOURCES asan_asm_instrumentation.S)
66 set_source_files_properties(${ASAN_ASM_SOURCES} PROPERTIES LANGUAGE C)
67 list(APPEND ASAN_SOURCES ${ASAN_ASM_SOURCES})
70 # Compile ASan sources into an object library.
72 foreach(os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
73 add_compiler_rt_darwin_object_library(RTAsan ${os}
74 ARCH ${ASAN_SUPPORTED_ARCH}
75 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
77 DEFS ${ASAN_COMMON_DEFINITIONS})
80 add_library(RTAsan.arm.android OBJECT ${ASAN_SOURCES} ${ASAN_CXX_SOURCES})
81 set_target_compile_flags(RTAsan.arm.android ${ASAN_CFLAGS})
82 set_property(TARGET RTAsan.arm.android APPEND PROPERTY
83 COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
85 foreach(arch ${ASAN_SUPPORTED_ARCH})
86 add_compiler_rt_object_library(RTAsan ${arch}
87 SOURCES ${ASAN_SOURCES} CFLAGS ${ASAN_CFLAGS}
88 DEFS ${ASAN_COMMON_DEFINITIONS})
89 add_compiler_rt_object_library(RTAsan_cxx ${arch}
90 SOURCES ${ASAN_CXX_SOURCES} CFLAGS ${ASAN_CFLAGS}
91 DEFS ${ASAN_COMMON_DEFINITIONS})
92 add_compiler_rt_object_library(RTAsan_preinit ${arch}
93 SOURCES ${ASAN_PREINIT_SOURCES} CFLAGS ${ASAN_CFLAGS}
94 DEFS ${ASAN_COMMON_DEFINITIONS})
95 if (COMPILER_RT_BUILD_SHARED_ASAN)
96 add_compiler_rt_object_library(RTAsan_dynamic ${arch}
97 SOURCES ${ASAN_SOURCES} ${ASAN_CXX_SOURCES}
98 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
99 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
104 # Build ASan runtimes shipped with Clang.
105 add_custom_target(asan)
107 foreach (os ${SANITIZER_COMMON_SUPPORTED_DARWIN_OS})
108 add_compiler_rt_darwin_dynamic_runtime(clang_rt.asan_${os}_dynamic ${os}
109 ARCH ${ASAN_SUPPORTED_ARCH}
110 SOURCES $<TARGET_OBJECTS:RTAsan.${os}>
111 $<TARGET_OBJECTS:RTInterception.${os}>
112 $<TARGET_OBJECTS:RTSanitizerCommon.${os}>
113 $<TARGET_OBJECTS:RTLSanCommon.${os}>
114 CFLAGS ${ASAN_CFLAGS}
115 DEFS ${ASAN_COMMON_DEFINITIONS})
116 add_dependencies(asan clang_rt.asan_${os}_dynamic)
120 add_library(clang_rt.asan-arm-android SHARED
121 $<TARGET_OBJECTS:RTAsan.arm.android>
122 $<TARGET_OBJECTS:RTInterception.arm.android>
123 $<TARGET_OBJECTS:RTSanitizerCommon.arm.android>)
124 set_target_compile_flags(clang_rt.asan-arm-android
126 set_property(TARGET clang_rt.asan-arm-android APPEND PROPERTY
127 COMPILE_DEFINITIONS ${ASAN_COMMON_DEFINITIONS})
128 target_link_libraries(clang_rt.asan-arm-android dl log)
129 add_dependencies(asan clang_rt.asan-arm-android)
131 # Build separate libraries for each target.
132 foreach(arch ${ASAN_SUPPORTED_ARCH})
133 set(ASAN_COMMON_RUNTIME_OBJECTS
134 $<TARGET_OBJECTS:RTInterception.${arch}>
135 $<TARGET_OBJECTS:RTSanitizerCommon.${arch}>
136 $<TARGET_OBJECTS:RTSanitizerCommonLibc.${arch}>)
138 # We can't build Leak Sanitizer on Windows yet.
139 list(APPEND ASAN_COMMON_RUNTIME_OBJECTS
140 $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
143 add_compiler_rt_runtime(clang_rt.asan-${arch} ${arch} STATIC
144 SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
145 $<TARGET_OBJECTS:RTAsan.${arch}>
146 ${ASAN_COMMON_RUNTIME_OBJECTS}
147 CFLAGS ${ASAN_CFLAGS}
148 DEFS ${ASAN_COMMON_DEFINITIONS})
149 add_dependencies(asan clang_rt.asan-${arch})
151 add_compiler_rt_runtime(clang_rt.asan_cxx-${arch} ${arch} STATIC
152 SOURCES $<TARGET_OBJECTS:RTAsan_cxx.${arch}>
153 CFLAGS ${ASAN_CFLAGS}
154 DEFS ${ASAN_COMMON_DEFINITIONS})
155 add_dependencies(asan clang_rt.asan_cxx-${arch})
157 if (COMPILER_RT_BUILD_SHARED_ASAN)
158 add_compiler_rt_runtime(clang_rt.asan-preinit-${arch} ${arch} STATIC
159 SOURCES $<TARGET_OBJECTS:RTAsan_preinit.${arch}>
160 CFLAGS ${ASAN_CFLAGS}
161 DEFS ${ASAN_COMMON_DEFINITIONS})
162 add_dependencies(asan clang_rt.asan-preinit-${arch})
164 add_compiler_rt_runtime(clang_rt.asan-dynamic-${arch} ${arch} SHARED
165 OUTPUT_NAME clang_rt.asan-${arch}
166 SOURCES $<TARGET_OBJECTS:RTAsan_dynamic.${arch}>
167 ${ASAN_COMMON_RUNTIME_OBJECTS}
168 CFLAGS ${ASAN_DYNAMIC_CFLAGS}
169 DEFS ${ASAN_DYNAMIC_DEFINITIONS})
170 target_link_libraries(clang_rt.asan-dynamic-${arch} ${ASAN_DYNAMIC_LIBS})
171 add_dependencies(asan clang_rt.asan-dynamic-${arch})
174 if (UNIX AND NOT ${arch} STREQUAL "i386")
175 add_sanitizer_rt_symbols(clang_rt.asan_cxx-${arch})
176 add_dependencies(asan clang_rt.asan_cxx-${arch}-symbols)
177 add_sanitizer_rt_symbols(clang_rt.asan-${arch} asan.syms.extra)
178 add_dependencies(asan clang_rt.asan-${arch}-symbols)
182 add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
183 SOURCES asan_dll_thunk.cc
184 CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
185 DEFS ${ASAN_COMMON_DEFINITIONS})
186 add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
191 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
192 add_dependencies(asan asan_blacklist)
193 add_dependencies(compiler-rt asan)
195 add_subdirectory(scripts)
197 if(COMPILER_RT_INCLUDE_TESTS)
198 add_subdirectory(tests)