blob: 0d182e9e1737f0181b01ff53b56151cddef14034 [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))
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -08006my_sanitize_diag := $(strip $(LOCAL_SANITIZE_DIAG))
Dan Albert08cca282014-12-11 18:56:26 -08007
Dan Albert4c401412015-08-19 20:13:33 -07008# SANITIZE_HOST is only in effect if the module is already using clang (host
9# modules that haven't set `LOCAL_CLANG := false` and device modules that
10# have set `LOCAL_CLANG := true`.
11my_global_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070012my_global_sanitize_diag :=
Dan Albert4c401412015-08-19 20:13:33 -070013ifeq ($(my_clang),true)
14 ifdef LOCAL_IS_HOST_MODULE
15 my_global_sanitize := $(strip $(SANITIZE_HOST))
16
17 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
18 my_global_sanitize := $(subst true,address,$(my_global_sanitize))
19 else
20 my_global_sanitize := $(strip $(SANITIZE_TARGET))
Ivan Lozano4a363732017-06-28 09:11:26 -070021 my_global_sanitize_diag := $(strip $(SANITIZE_TARGET_DIAG))
Dan Albert4c401412015-08-19 20:13:33 -070022 endif
23endif
24
Dan Albert4c401412015-08-19 20:13:33 -070025ifneq ($(my_global_sanitize),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -070026 my_sanitize := $(my_global_sanitize) $(my_sanitize)
Dan Albert4c401412015-08-19 20:13:33 -070027endif
Ivan Lozano4a363732017-06-28 09:11:26 -070028ifneq ($(my_global_sanitize_diag),)
29 my_sanitize_diag := $(my_global_sanitize_diag) $(my_sanitize_diag)
30endif
Dan Albert4c401412015-08-19 20:13:33 -070031
Andreas Gampe6b30d772016-06-27 15:15:31 -070032# The sanitizer specified in the product configuration wins over the previous.
33ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),)
34 my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
35 ifeq ($(my_sanitize),never)
36 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070037 my_sanitize_diag :=
Andreas Gampe6b30d772016-06-27 15:15:31 -070038 endif
39endif
40
Colin Cross23618422016-11-02 15:05:21 -070041ifndef LOCAL_IS_HOST_MODULE
42 # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden)
43 SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH)
44 ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
45 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070046 my_sanitize_diag :=
Colin Cross23618422016-11-02 15:05:21 -070047 endif
Andreas Gampecd257402016-06-20 17:36:49 -070048endif
49
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070050# Add a filter point based on module owner (to lighten the burden). The format is a space- or
51# colon-separated list of owner names.
52ifneq (,$(SANITIZE_NEVER_BY_OWNER))
53 ifneq (,$(LOCAL_MODULE_OWNER))
54 ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
55 $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
56 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070057 my_sanitize_diag :=
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070058 endif
59 endif
60endif
61
Dan Albert08cca282014-12-11 18:56:26 -080062# Don't apply sanitizers to NDK code.
63ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -070064 my_sanitize :=
Dan Willemsenf761c0f2016-06-28 16:47:43 -070065 my_global_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070066 my_sanitize_diag :=
Dan Albert27ccb752015-04-16 16:21:02 -070067endif
68
Dan Albert4c401412015-08-19 20:13:33 -070069# Never always wins.
70ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -080071 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070072 my_sanitize_diag :=
Dan Albert08cca282014-12-11 18:56:26 -080073endif
74
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -080075# If CFI is disabled globally, remove it from my_sanitize.
Vishwath Mohan45665b42017-01-24 13:20:28 -080076ifeq ($(strip $(ENABLE_CFI)),false)
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -080077 my_sanitize := $(filter-out cfi,$(my_sanitize))
78 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
79endif
80
Vishwath Mohana2046062017-02-07 20:28:07 -080081# Disable CFI for arm32 (b/35157333).
82ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
83 my_sanitize := $(filter-out cfi,$(my_sanitize))
84 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
85endif
86
Vishwath Mohanc026f6d2017-04-20 07:39:13 -070087# Also disable CFI if ASAN is enabled.
88ifneq ($(filter address,$(my_sanitize)),)
89 my_sanitize := $(filter-out cfi,$(my_sanitize))
90 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
91endif
92
Evgenii Stepanov8c50e3c2017-01-31 17:08:33 -080093# CFI needs gold linker, and mips toolchain does not have one.
94ifneq ($(filter mips mips64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
95 my_sanitize := $(filter-out cfi,$(my_sanitize))
96 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
97endif
98
Ivan Lozano4a363732017-06-28 09:11:26 -070099# Support for local sanitize blacklist paths.
100ifneq ($(my_sanitize)$(my_global_sanitize),)
101 ifneq ($(LOCAL_SANITIZE_BLACKLIST),)
102 my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLACKLIST)
103 endif
104endif
105
Ivan Lozano4a363732017-06-28 09:11:26 -0700106ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),)
Ivan Lozano9a82bfd2017-07-13 14:49:12 -0700107 # Disable integer_overflow in excluded paths.
108 combined_exclude_paths := $(INTEGER_OVERFLOW_EXCLUDE_PATHS) \
109 $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS)
110
111 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
112 $(filter $(dir)%,$(LOCAL_PATH)))),)
113 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
114 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
115 endif
116 # Disable integer_overflow if LOCAL_NOSANITIZE=integer.
Ivan Lozano4a363732017-06-28 09:11:26 -0700117 ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),)
118 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
119 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
120 endif
121endif
122
Evgenii Stepanov42823662016-05-12 13:07:17 -0700123my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
124ifneq ($(my_nosanitize),)
125 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
126endif
127
Dan Alberta6311b72015-07-30 10:17:33 -0700128# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
129# its use an error. For multilib cases, don't use it for the 32-bit case.
130ifneq ($(filter thread,$(my_sanitize)),)
131 ifeq ($(my_32_64_bit_suffix),32)
132 ifeq ($(my_module_multilib),both)
133 my_sanitize := $(filter-out thread,$(my_sanitize))
134 else
135 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
136 endif
137 endif
138endif
139
Evgenii Stepanov7dcb8b82016-05-06 18:15:57 -0700140ifneq ($(filter safe-stack,$(my_sanitize)),)
141 ifeq ($(my_32_64_bit_suffix),32)
142 my_sanitize := $(filter-out safe-stack,$(my_sanitize))
143 endif
144endif
145
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700146# Undefined symbols can occur if a non-sanitized library links
147# sanitized static libraries. That's OK, because the executable
148# always depends on the ASan runtime library, which defines these
149# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -0700150ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700151 ifndef LOCAL_IS_HOST_MODULE
152 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
153 ifeq ($(my_sanitize),)
154 my_allow_undefined_symbols := true
155 endif
156 endif
157 endif
158endif
159
Dan Albert94b57912015-04-17 09:48:33 -0700160# Sanitizers can only be used with clang.
161ifneq ($(my_clang),true)
162 ifneq ($(my_sanitize),)
163 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
164 endif
165endif
166
Dan Albertb5b2ffe2015-04-16 18:07:07 -0700167ifneq ($(filter default-ub,$(my_sanitize)),)
168 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -0800169endif
170
Ivan Krasin74b32b82015-09-18 11:54:43 -0700171ifneq ($(filter coverage,$(my_sanitize)),)
172 ifeq ($(filter address,$(my_sanitize)),)
173 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
174 endif
Dan Austin9978b522017-06-28 15:26:38 -0700175 my_cflags += -fsanitize-coverage=trace-pc-guard
Ivan Krasin74b32b82015-09-18 11:54:43 -0700176 my_sanitize := $(filter-out coverage,$(my_sanitize))
177endif
178
Ivan Lozano4a363732017-06-28 09:11:26 -0700179ifneq ($(filter integer_overflow,$(my_sanitize)),)
180 ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
181 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
182
183 # Respect LOCAL_NOSANITIZE for integer-overflow flags.
184 ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
185 my_cflags += -fsanitize=signed-integer-overflow
186 endif
187 ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
188 my_cflags += -fsanitize=unsigned-integer-overflow
189 endif
190 my_cflags += -fsanitize-trap=all
191 my_cflags += -ftrap-function=abort
192 my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS)
193
194 # Check for diagnostics mode (on by default).
195 ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
196 my_cflags += -fno-sanitize-trap=signed-integer-overflow,unsigned-integer-overflow
197 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY) $(my_shared_libraries)
198 endif
199 endif
200 endif
201 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
202endif
203
204# Makes sure integer_overflow diagnostics is removed from the diagnostics list
205# even if integer_overflow is not set for some reason.
206ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
207 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
208endif
209
Dan Albert08cca282014-12-11 18:56:26 -0800210ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800211 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800212 my_cflags += -fsanitize=$(fsanitize_arg)
213
214 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700215 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800216 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albert29224112015-08-13 17:25:10 -0700217 my_ldlibs += -lrt -ldl
Dan Albertabf4bc92015-06-16 23:27:34 -0700218 else
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700219 my_cflags += -fsanitize-trap=all
220 my_cflags += -ftrap-function=abort
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700221 ifneq ($(filter address thread,$(my_sanitize)),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700222 my_cflags += -fno-sanitize-trap=address,thread
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700223 my_shared_libraries += libdl
224 endif
Dan Albert08cca282014-12-11 18:56:26 -0800225 endif
226endif
227
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700228ifneq ($(filter cfi,$(my_sanitize)),)
Evgenii Stepanov81bea1b2017-01-20 14:12:08 -0800229 # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp).
230 # LLVM is not set up to do this on a function basis, so force Thumb on the
231 # entire module.
232 LOCAL_ARM_MODE := thumb
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800233 my_cflags += $(CFI_EXTRA_CFLAGS)
234 my_ldflags += $(CFI_EXTRA_LDFLAGS)
Evgenii Stepanove1b96f32017-01-23 16:57:38 -0800235 my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so
Evgenii Stepanov8c50e3c2017-01-31 17:08:33 -0800236 # Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when
237 # Clang is updated past r290384.
238 ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
239 my_ldflags += -march=armv7-a
240 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700241endif
242
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800243# If local or global modules need ASAN, add linker flags.
244ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700245 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
246 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800247 # -nodefaultlibs (provided with libc++) prevents the driver from linking
248 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albertabf4bc92015-06-16 23:27:34 -0700249 my_ldlibs += -lm -lpthread
Dan Albert1f0d5302015-04-28 14:55:50 -0700250 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700251 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800252 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700253 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800254 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
255 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
256 $(my_shared_libraries)
257 endif
258 ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
259 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
260 endif
261
262 # Do not add unnecessary dependency in shared libraries.
263 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
264 my_ldflags += -Wl,--as-needed
265 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700266
Colin Crossd08699e2016-07-17 15:28:07 -0700267 ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES)
268 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
269 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
270 # Make sure linker_asan get installed.
271 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
272 endif
273 endif
Dan Albert08cca282014-12-11 18:56:26 -0800274 endif
275endif
276
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800277# If local module needs ASAN, add compiler flags.
278ifneq ($(filter address,$(my_sanitize)),)
279 # Frame pointer based unwinder in ASan requires ARM frame setup.
280 LOCAL_ARM_MODE := arm
281 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
282 ifndef LOCAL_IS_HOST_MODULE
283 my_cflags += -mllvm -asan-globals=0
284 endif
285endif
286
Dan Albert08cca282014-12-11 18:56:26 -0800287ifneq ($(filter undefined,$(my_sanitize)),)
Dan Albertabf4bc92015-06-16 23:27:34 -0700288 ifndef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800289 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700290 endif
291endif
Dan Albert4111d482015-04-16 18:08:44 -0700292
Dan Albert9f176552015-04-28 11:26:45 -0700293ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
294 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700295 my_cflags += -fsanitize-recover=$(recover_arg)
296endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700297
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800298ifneq ($(my_sanitize_diag),)
299 notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700300 my_cflags += -fno-sanitize-trap=$(notrap_arg)
301 # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
302 ifeq ($(filter address thread,$(my_sanitize)),)
303 # Does not have to be the first DT_NEEDED unlike ASan.
304 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
305 endif
306endif