1add_compiler_rt_component(gwp_asan)
2
3include_directories(..)
4
5set(GWP_ASAN_SOURCES
6  common.cpp
7  crash_handler.cpp
8  platform_specific/common_posix.cpp
9  platform_specific/guarded_pool_allocator_posix.cpp
10  platform_specific/mutex_posix.cpp
11  platform_specific/utilities_posix.cpp
12  guarded_pool_allocator.cpp
13  random.cpp
14  stack_trace_compressor.cpp
15)
16
17set(GWP_ASAN_HEADERS
18  common.h
19  crash_handler.h
20  definitions.h
21  guarded_pool_allocator.h
22  mutex.h
23  options.h
24  options.inc
25  random.h
26  stack_trace_compressor.h
27  utilities.h
28)
29
30# Ensure that GWP-ASan meets the delegated requirements of some supporting
31# allocators. Some supporting allocators (e.g. scudo standalone) cannot use any
32# parts of the C++ standard library.
33set(GWP_ASAN_CFLAGS ${SANITIZER_COMMON_CFLAGS} -fno-rtti -fno-exceptions
34    -nostdinc++ -pthread)
35append_list_if(COMPILER_RT_HAS_FPIC_FLAG -fPIC GWP_ASAN_CFLAGS)
36append_list_if(COMPILER_RT_HAS_OMIT_FRAME_POINTER_FLAG -fno-omit-frame-pointer
37               GWP_ASAN_CFLAGS)
38
39# Remove -stdlib= which is unused when passing -nostdinc++.
40string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
41
42# Options parsing support is optional. GwpAsan is totally independent of
43# sanitizer_common, the options parser is not. This is an optional library
44# that can be used by an allocator to automatically parse GwpAsan options from
45# the environment variable GWP_ASAN_FLAGS, but the allocator can choose to
46# implement its own options parsing and populate the Options struct itself.
47set(GWP_ASAN_OPTIONS_PARSER_SOURCES
48  optional/options_parser.cpp
49)
50set(GWP_ASAN_OPTIONS_PARSER_HEADERS
51  optional/options_parser.h
52  options.h
53  options.inc
54)
55set(GWP_ASAN_BACKTRACE_HEADERS
56  optional/backtrace.h
57  options.h
58  options.inc
59)
60set(GWP_ASAN_SEGV_HANDLER_HEADERS
61  optional/segv_handler.h
62  options.h)
63
64set(GWP_ASAN_OPTIONS_PARSER_CFLAGS
65    ${GWP_ASAN_CFLAGS}
66    ${SANITIZER_COMMON_CFLAGS})
67set(GWP_ASAN_OPTIONS_PARSER_OBJECT_LIBS
68    RTSanitizerCommon
69    RTSanitizerCommonNoLibc)
70
71if (COMPILER_RT_HAS_GWP_ASAN)
72  foreach(arch ${GWP_ASAN_SUPPORTED_ARCH})
73    add_compiler_rt_runtime(
74      clang_rt.gwp_asan
75      STATIC
76      ARCHS ${arch}
77      SOURCES ${GWP_ASAN_SOURCES}
78      ADDITIONAL_HEADERS ${GWP_ASAN_HEADERS}
79      CFLAGS ${GWP_ASAN_CFLAGS}
80      PARENT_TARGET gwp_asan
81    )
82  endforeach()
83
84  add_compiler_rt_object_libraries(RTGwpAsan
85      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
86      SOURCES ${GWP_ASAN_SOURCES}
87      ADDITIONAL_HEADERS ${GWP_ASAN_HEADERS}
88      CFLAGS ${GWP_ASAN_CFLAGS})
89
90  # Note: If you choose to add this as an object library, ensure you also
91  # include the sanitizer_common flag parsing object lib (generally
92  # 'RTSanitizerCommonNoTermination'). Also, you'll need to either implement
93  # your own backtrace support (see optional/backtrace.h), or choose between one
94  # of the pre-implemented backtrace support options (see below).
95  add_compiler_rt_object_libraries(RTGwpAsanOptionsParser
96      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
97      SOURCES ${GWP_ASAN_OPTIONS_PARSER_SOURCES}
98      ADDITIONAL_HEADERS ${GWP_ASAN_OPTIONS_PARSER_HEADERS}
99      CFLAGS ${GWP_ASAN_OPTIONS_PARSER_CFLAGS})
100
101  # As above, build the pre-implemented optional backtrace support libraries.
102  add_compiler_rt_object_libraries(RTGwpAsanBacktraceLibc
103      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
104      SOURCES optional/backtrace_linux_libc.cpp
105      ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
106      CFLAGS ${GWP_ASAN_CFLAGS})
107  add_compiler_rt_object_libraries(RTGwpAsanSegvHandler
108      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
109      SOURCES optional/segv_handler_posix.cpp
110      ADDITIONAL_HEADERS ${GWP_ASAN_SEGV_HANDLER_HEADERS}
111      CFLAGS ${GWP_ASAN_CFLAGS})
112  add_compiler_rt_object_libraries(RTGwpAsanBacktraceSanitizerCommon
113      ARCHS ${GWP_ASAN_SUPPORTED_ARCH}
114      SOURCES optional/backtrace_sanitizer_common.cpp
115      ADDITIONAL_HEADERS ${GWP_ASAN_BACKTRACE_HEADERS}
116      CFLAGS ${GWP_ASAN_CFLAGS} ${SANITIZER_COMMON_CFLAGS})
117endif()
118
119if(COMPILER_RT_INCLUDE_TESTS)
120  add_subdirectory(tests)
121endif()
122