blob: f387f053a3e1f36d44f61626a2edc88605e99d71 [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
22# The sanitizer specified by the environment wins over the module.
23ifneq ($(my_global_sanitize),)
24 my_sanitize := $(my_global_sanitize)
25endif
26
Andreas Gampecd257402016-06-20 17:36:49 -070027# Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden).
28SANITIZE_ARCH ?= 32 64
29ifeq ($(filter $(SANITIZE_ARCH),$(my_32_64_bit_suffix)),)
30 my_sanitize :=
31endif
32
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070033# Add a filter point based on module owner (to lighten the burden). The format is a space- or
34# colon-separated list of owner names.
35ifneq (,$(SANITIZE_NEVER_BY_OWNER))
36 ifneq (,$(LOCAL_MODULE_OWNER))
37 ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
38 $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
39 my_sanitize :=
40 endif
41 endif
42endif
43
Dan Albert08cca282014-12-11 18:56:26 -080044# Don't apply sanitizers to NDK code.
45ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -070046 my_sanitize :=
Dan Albert27ccb752015-04-16 16:21:02 -070047endif
48
Dan Albert4c401412015-08-19 20:13:33 -070049# Never always wins.
50ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -080051 my_sanitize :=
52endif
53
Evgenii Stepanov42823662016-05-12 13:07:17 -070054my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
55ifneq ($(my_nosanitize),)
56 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
57endif
58
Dan Alberta6311b72015-07-30 10:17:33 -070059# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
60# its use an error. For multilib cases, don't use it for the 32-bit case.
61ifneq ($(filter thread,$(my_sanitize)),)
62 ifeq ($(my_32_64_bit_suffix),32)
63 ifeq ($(my_module_multilib),both)
64 my_sanitize := $(filter-out thread,$(my_sanitize))
65 else
66 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
67 endif
68 endif
69endif
70
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070071# Undefined symbols can occur if a non-sanitized library links
72# sanitized static libraries. That's OK, because the executable
73# always depends on the ASan runtime library, which defines these
74# symbols.
75ifneq ($(strip $(SANITIZE_TARGET)),)
76 ifndef LOCAL_IS_HOST_MODULE
77 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
78 ifeq ($(my_sanitize),)
79 my_allow_undefined_symbols := true
80 endif
81 endif
Evgenii Stepanov4b396e42015-07-20 16:32:53 -070082 # Workaround for a bug in AddressSanitizer that breaks stack unwinding.
83 # https://code.google.com/p/address-sanitizer/issues/detail?id=387
84 # Revert when external/compiler-rt is updated past r236014.
85 LOCAL_PACK_MODULE_RELOCATIONS := false
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070086 endif
87endif
88
Dan Albert94b57912015-04-17 09:48:33 -070089# Sanitizers can only be used with clang.
90ifneq ($(my_clang),true)
91 ifneq ($(my_sanitize),)
92 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
93 endif
94endif
95
Dan Albertb5b2ffe2015-04-16 18:07:07 -070096ifneq ($(filter default-ub,$(my_sanitize)),)
97 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -080098endif
99
Ivan Krasin74b32b82015-09-18 11:54:43 -0700100ifneq ($(filter coverage,$(my_sanitize)),)
101 ifeq ($(filter address,$(my_sanitize)),)
102 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
103 endif
104 my_cflags += -fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp
105 my_sanitize := $(filter-out coverage,$(my_sanitize))
106endif
107
Dan Albert08cca282014-12-11 18:56:26 -0800108ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800109 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800110 my_cflags += -fsanitize=$(fsanitize_arg)
111
112 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700113 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800114 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albert29224112015-08-13 17:25:10 -0700115 my_ldlibs += -lrt -ldl
Dan Albertabf4bc92015-06-16 23:27:34 -0700116 else
Stephen Hinese8119e92015-11-09 16:32:11 -0800117 ifeq ($(filter address,$(my_sanitize)),)
118 my_cflags += -fsanitize-trap=all
119 my_cflags += -ftrap-function=abort
120 endif
Dan Albertabf4bc92015-06-16 23:27:34 -0700121 my_shared_libraries += libdl
Dan Albert08cca282014-12-11 18:56:26 -0800122 endif
123endif
124
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800125# If local or global modules need ASAN, add linker flags.
126ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700127 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
128 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800129 # -nodefaultlibs (provided with libc++) prevents the driver from linking
130 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albertabf4bc92015-06-16 23:27:34 -0700131 my_ldlibs += -lm -lpthread
Dan Albert1f0d5302015-04-28 14:55:50 -0700132 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700133 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800134 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700135 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800136 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
137 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
138 $(my_shared_libraries)
139 endif
140 ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
141 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
142 endif
143
144 # Do not add unnecessary dependency in shared libraries.
145 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
146 my_ldflags += -Wl,--as-needed
147 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700148
Evgenii Stepanov8f5e67a2015-07-10 18:06:51 -0700149 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
Ying Wanga05e2222015-08-17 16:13:24 -0700150 # Make sure linker_asan get installed.
151 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
Dan Albert08cca282014-12-11 18:56:26 -0800152 endif
153endif
154
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800155# If local module needs ASAN, add compiler flags.
156ifneq ($(filter address,$(my_sanitize)),)
157 # Frame pointer based unwinder in ASan requires ARM frame setup.
158 LOCAL_ARM_MODE := arm
159 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
160 ifndef LOCAL_IS_HOST_MODULE
161 my_cflags += -mllvm -asan-globals=0
162 endif
163endif
164
Dan Albert08cca282014-12-11 18:56:26 -0800165ifneq ($(filter undefined,$(my_sanitize)),)
Dan Albertabf4bc92015-06-16 23:27:34 -0700166 ifndef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800167 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700168 endif
169endif
Dan Albert4111d482015-04-16 18:08:44 -0700170
Dan Albert9f176552015-04-28 11:26:45 -0700171ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
172 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700173 my_cflags += -fsanitize-recover=$(recover_arg)
174endif