blob: 960262600785b1501cbe4885295f2ec8a43a8d5f [file] [log] [blame]
Dan Albert4ae5d4b2014-10-31 16:23:08 -07001##############################################
2## Perform configuration steps for sanitizers.
3##############################################
4
Dan Albert27ccb752015-04-16 16:21:02 -07005my_sanitize := $(strip $(LOCAL_SANITIZE))
Dan Albert08cca282014-12-11 18:56:26 -08006
Dan Albert4c401412015-08-19 20:13:33 -07007# SANITIZE_HOST is only in effect if the module is already using clang (host
8# modules that haven't set `LOCAL_CLANG := false` and device modules that
9# have set `LOCAL_CLANG := true`.
10my_global_sanitize :=
11ifeq ($(my_clang),true)
12 ifdef LOCAL_IS_HOST_MODULE
13 my_global_sanitize := $(strip $(SANITIZE_HOST))
14
15 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
16 my_global_sanitize := $(subst true,address,$(my_global_sanitize))
17 else
18 my_global_sanitize := $(strip $(SANITIZE_TARGET))
19 endif
20endif
21
Dan Albert4c401412015-08-19 20:13:33 -070022ifneq ($(my_global_sanitize),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -070023 my_sanitize := $(my_global_sanitize) $(my_sanitize)
Dan Albert4c401412015-08-19 20:13:33 -070024endif
25
Andreas Gampe6b30d772016-06-27 15:15:31 -070026# The sanitizer specified in the product configuration wins over the previous.
27ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),)
28 my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
29 ifeq ($(my_sanitize),never)
30 my_sanitize :=
31 endif
32endif
33
Andreas Gampecd257402016-06-20 17:36:49 -070034# Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden).
35SANITIZE_ARCH ?= 32 64
36ifeq ($(filter $(SANITIZE_ARCH),$(my_32_64_bit_suffix)),)
37 my_sanitize :=
38endif
39
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070040# Add a filter point based on module owner (to lighten the burden). The format is a space- or
41# colon-separated list of owner names.
42ifneq (,$(SANITIZE_NEVER_BY_OWNER))
43 ifneq (,$(LOCAL_MODULE_OWNER))
44 ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
45 $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
46 my_sanitize :=
47 endif
48 endif
49endif
50
Dan Albert08cca282014-12-11 18:56:26 -080051# Don't apply sanitizers to NDK code.
52ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -070053 my_sanitize :=
Dan Albert27ccb752015-04-16 16:21:02 -070054endif
55
Dan Albert4c401412015-08-19 20:13:33 -070056# Never always wins.
57ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -080058 my_sanitize :=
59endif
60
Evgenii Stepanov42823662016-05-12 13:07:17 -070061my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
62ifneq ($(my_nosanitize),)
63 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
64endif
65
Dan Alberta6311b72015-07-30 10:17:33 -070066# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
67# its use an error. For multilib cases, don't use it for the 32-bit case.
68ifneq ($(filter thread,$(my_sanitize)),)
69 ifeq ($(my_32_64_bit_suffix),32)
70 ifeq ($(my_module_multilib),both)
71 my_sanitize := $(filter-out thread,$(my_sanitize))
72 else
73 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
74 endif
75 endif
76endif
77
Evgenii Stepanov7dcb8b82016-05-06 18:15:57 -070078ifneq ($(filter safe-stack,$(my_sanitize)),)
79 ifeq ($(my_32_64_bit_suffix),32)
80 my_sanitize := $(filter-out safe-stack,$(my_sanitize))
81 endif
82endif
83
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070084# Undefined symbols can occur if a non-sanitized library links
85# sanitized static libraries. That's OK, because the executable
86# always depends on the ASan runtime library, which defines these
87# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -070088ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070089 ifndef LOCAL_IS_HOST_MODULE
90 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
91 ifeq ($(my_sanitize),)
92 my_allow_undefined_symbols := true
93 endif
94 endif
95 endif
96endif
97
Dan Albert94b57912015-04-17 09:48:33 -070098# Sanitizers can only be used with clang.
99ifneq ($(my_clang),true)
100 ifneq ($(my_sanitize),)
101 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
102 endif
103endif
104
Dan Albertb5b2ffe2015-04-16 18:07:07 -0700105ifneq ($(filter default-ub,$(my_sanitize)),)
106 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -0800107endif
108
Ivan Krasin74b32b82015-09-18 11:54:43 -0700109ifneq ($(filter coverage,$(my_sanitize)),)
110 ifeq ($(filter address,$(my_sanitize)),)
111 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
112 endif
113 my_cflags += -fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp
114 my_sanitize := $(filter-out coverage,$(my_sanitize))
115endif
116
Dan Albert08cca282014-12-11 18:56:26 -0800117ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800118 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800119 my_cflags += -fsanitize=$(fsanitize_arg)
120
121 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700122 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800123 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albert29224112015-08-13 17:25:10 -0700124 my_ldlibs += -lrt -ldl
Dan Albertabf4bc92015-06-16 23:27:34 -0700125 else
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700126 my_cflags += -fsanitize-trap=all
127 my_cflags += -ftrap-function=abort
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700128 ifneq ($(filter address thread,$(my_sanitize)),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700129 my_cflags += -fno-sanitize-trap=address,thread
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700130 my_shared_libraries += libdl
131 endif
Dan Albert08cca282014-12-11 18:56:26 -0800132 endif
133endif
134
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800135# If local or global modules need ASAN, add linker flags.
136ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700137 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
138 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800139 # -nodefaultlibs (provided with libc++) prevents the driver from linking
140 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albertabf4bc92015-06-16 23:27:34 -0700141 my_ldlibs += -lm -lpthread
Dan Albert1f0d5302015-04-28 14:55:50 -0700142 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700143 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800144 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700145 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800146 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
147 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
148 $(my_shared_libraries)
149 endif
150 ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
151 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
152 endif
153
154 # Do not add unnecessary dependency in shared libraries.
155 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
156 my_ldflags += -Wl,--as-needed
157 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700158
Evgenii Stepanov8f5e67a2015-07-10 18:06:51 -0700159 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
Ying Wanga05e2222015-08-17 16:13:24 -0700160 # Make sure linker_asan get installed.
161 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
Dan Albert08cca282014-12-11 18:56:26 -0800162 endif
163endif
164
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800165# If local module needs ASAN, add compiler flags.
166ifneq ($(filter address,$(my_sanitize)),)
167 # Frame pointer based unwinder in ASan requires ARM frame setup.
168 LOCAL_ARM_MODE := arm
169 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
170 ifndef LOCAL_IS_HOST_MODULE
171 my_cflags += -mllvm -asan-globals=0
172 endif
173endif
174
Dan Albert08cca282014-12-11 18:56:26 -0800175ifneq ($(filter undefined,$(my_sanitize)),)
Dan Albertabf4bc92015-06-16 23:27:34 -0700176 ifndef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800177 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700178 endif
179endif
Dan Albert4111d482015-04-16 18:08:44 -0700180
Dan Albert9f176552015-04-28 11:26:45 -0700181ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
182 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700183 my_cflags += -fsanitize-recover=$(recover_arg)
184endif