[ASan Win] Simplify and improve the way we forward ASan interface calls from DLLs
[blocksruntime.git] / lib / asan / CMakeLists.txt
blobea3c720958884ced7e2807c251add34d29ec9b21
1 # Build for the AddressSanitizer runtime support library.
3 if(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.
7 set(CMAKE_BUILD_WITH_INSTALL_RPATH OFF)
8 endif()
10 set(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)
30 set(ASAN_CXX_SOURCES
31   asan_new_delete.cc)
33 set(ASAN_PREINIT_SOURCES
34   asan_preinit.cc)
36 include_directories(..)
38 if(ANDROID)
39   include_directories(${COMPILER_RT_EXTRA_ANDROID_HEADERS})
40 endif()
42 set(ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS})
43 append_no_rtti_flag(ASAN_CFLAGS)
45 set(ASAN_COMMON_DEFINITIONS
46   ASAN_HAS_EXCEPTIONS=1)
48 if(ANDROID)
49   list(APPEND ASAN_COMMON_DEFINITIONS
50     ASAN_LOW_MEMORY=1)
51 endif()
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)
64 if (NOT MSVC)
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})
68 endif()
70 # Compile ASan sources into an object library.
71 if(APPLE)
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}
76       CFLAGS ${ASAN_CFLAGS}
77       DEFS ${ASAN_COMMON_DEFINITIONS})
78   endforeach()
79 elseif(ANDROID)
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})
84 else()
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})
100     endif()
101   endforeach()
102 endif()
104 # Build ASan runtimes shipped with Clang.
105 add_custom_target(asan)
106 if(APPLE)
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)
117   endforeach()
119 elseif(ANDROID)
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
125     ${ASAN_CFLAGS})
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)
130 else()
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}>)
137     if (NOT WIN32)
138       # We can't build Leak Sanitizer on Windows yet.
139       list(APPEND ASAN_COMMON_RUNTIME_OBJECTS
140            $<TARGET_OBJECTS:RTLSanCommon.${arch}>)
141     endif()
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})
172     endif()
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)
179     endif()
181     if (WIN32)
182       add_compiler_rt_runtime(clang_rt.asan_dll_thunk-${arch} ${arch} STATIC
183         SOURCES asan_dll_thunk.cc
184                 $<TARGET_OBJECTS:RTInterception.${arch}>
185         CFLAGS ${ASAN_CFLAGS} -DASAN_DLL_THUNK
186         DEFS ${ASAN_COMMON_DEFINITIONS})
187       add_dependencies(asan clang_rt.asan_dll_thunk-${arch})
188     endif()
189   endforeach()
190 endif()
192 add_compiler_rt_resource_file(asan_blacklist asan_blacklist.txt)
193 add_dependencies(asan asan_blacklist)
194 add_dependencies(compiler-rt asan)
196 add_subdirectory(scripts)
198 if(COMPILER_RT_INCLUDE_TESTS)
199   add_subdirectory(tests)
200 endif()