blob: 967df2d9d17ccc79c975238b0e794c0a8f8b6c87 [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 :=
12ifeq ($(my_clang),true)
13 ifdef LOCAL_IS_HOST_MODULE
14 my_global_sanitize := $(strip $(SANITIZE_HOST))
15
16 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
17 my_global_sanitize := $(subst true,address,$(my_global_sanitize))
18 else
19 my_global_sanitize := $(strip $(SANITIZE_TARGET))
20 endif
21endif
22
Dan Albert4c401412015-08-19 20:13:33 -070023ifneq ($(my_global_sanitize),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -070024 my_sanitize := $(my_global_sanitize) $(my_sanitize)
Dan Albert4c401412015-08-19 20:13:33 -070025endif
26
Andreas Gampe6b30d772016-06-27 15:15:31 -070027# The sanitizer specified in the product configuration wins over the previous.
28ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),)
29 my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
30 ifeq ($(my_sanitize),never)
31 my_sanitize :=
32 endif
33endif
34
Colin Cross23618422016-11-02 15:05:21 -070035ifndef LOCAL_IS_HOST_MODULE
36 # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden)
37 SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH)
38 ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
39 my_sanitize :=
40 endif
Andreas Gampecd257402016-06-20 17:36:49 -070041endif
42
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070043# Add a filter point based on module owner (to lighten the burden). The format is a space- or
44# colon-separated list of owner names.
45ifneq (,$(SANITIZE_NEVER_BY_OWNER))
46 ifneq (,$(LOCAL_MODULE_OWNER))
47 ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
48 $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
49 my_sanitize :=
50 endif
51 endif
52endif
53
Dan Albert08cca282014-12-11 18:56:26 -080054# Don't apply sanitizers to NDK code.
55ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -070056 my_sanitize :=
Dan Willemsenf761c0f2016-06-28 16:47:43 -070057 my_global_sanitize :=
Dan Albert27ccb752015-04-16 16:21:02 -070058endif
59
Dan Albert4c401412015-08-19 20:13:33 -070060# Never always wins.
61ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -080062 my_sanitize :=
63endif
64
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -080065# If CFI is disabled globally, remove it from my_sanitize.
Vishwath Mohan45665b42017-01-24 13:20:28 -080066ifeq ($(strip $(ENABLE_CFI)),false)
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -080067 my_sanitize := $(filter-out cfi,$(my_sanitize))
68 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
69endif
70
Vishwath Mohana2046062017-02-07 20:28:07 -080071# Disable CFI for arm32 (b/35157333).
72ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
73 my_sanitize := $(filter-out cfi,$(my_sanitize))
74 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
75endif
76
Vishwath Mohanc026f6d2017-04-20 07:39:13 -070077# Also disable CFI if ASAN is enabled.
78ifneq ($(filter address,$(my_sanitize)),)
79 my_sanitize := $(filter-out cfi,$(my_sanitize))
80 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
81endif
82
Evgenii Stepanov8c50e3c2017-01-31 17:08:33 -080083# CFI needs gold linker, and mips toolchain does not have one.
84ifneq ($(filter mips mips64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
85 my_sanitize := $(filter-out cfi,$(my_sanitize))
86 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
87endif
88
Evgenii Stepanov42823662016-05-12 13:07:17 -070089my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
90ifneq ($(my_nosanitize),)
91 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
92endif
93
Dan Alberta6311b72015-07-30 10:17:33 -070094# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
95# its use an error. For multilib cases, don't use it for the 32-bit case.
96ifneq ($(filter thread,$(my_sanitize)),)
97 ifeq ($(my_32_64_bit_suffix),32)
98 ifeq ($(my_module_multilib),both)
99 my_sanitize := $(filter-out thread,$(my_sanitize))
100 else
101 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
102 endif
103 endif
104endif
105
Evgenii Stepanov7dcb8b82016-05-06 18:15:57 -0700106ifneq ($(filter safe-stack,$(my_sanitize)),)
107 ifeq ($(my_32_64_bit_suffix),32)
108 my_sanitize := $(filter-out safe-stack,$(my_sanitize))
109 endif
110endif
111
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700112# Undefined symbols can occur if a non-sanitized library links
113# sanitized static libraries. That's OK, because the executable
114# always depends on the ASan runtime library, which defines these
115# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -0700116ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700117 ifndef LOCAL_IS_HOST_MODULE
118 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
119 ifeq ($(my_sanitize),)
120 my_allow_undefined_symbols := true
121 endif
122 endif
123 endif
124endif
125
Dan Albert94b57912015-04-17 09:48:33 -0700126# Sanitizers can only be used with clang.
127ifneq ($(my_clang),true)
128 ifneq ($(my_sanitize),)
129 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
130 endif
131endif
132
Dan Albertb5b2ffe2015-04-16 18:07:07 -0700133ifneq ($(filter default-ub,$(my_sanitize)),)
134 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -0800135endif
136
Ivan Krasin74b32b82015-09-18 11:54:43 -0700137ifneq ($(filter coverage,$(my_sanitize)),)
138 ifeq ($(filter address,$(my_sanitize)),)
139 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
140 endif
Dan Austin9978b522017-06-28 15:26:38 -0700141 my_cflags += -fsanitize-coverage=trace-pc-guard
Ivan Krasin74b32b82015-09-18 11:54:43 -0700142 my_sanitize := $(filter-out coverage,$(my_sanitize))
143endif
144
Dan Albert08cca282014-12-11 18:56:26 -0800145ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800146 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800147 my_cflags += -fsanitize=$(fsanitize_arg)
148
149 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700150 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800151 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albert29224112015-08-13 17:25:10 -0700152 my_ldlibs += -lrt -ldl
Dan Albertabf4bc92015-06-16 23:27:34 -0700153 else
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700154 my_cflags += -fsanitize-trap=all
155 my_cflags += -ftrap-function=abort
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700156 ifneq ($(filter address thread,$(my_sanitize)),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700157 my_cflags += -fno-sanitize-trap=address,thread
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700158 my_shared_libraries += libdl
159 endif
Dan Albert08cca282014-12-11 18:56:26 -0800160 endif
161endif
162
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700163ifneq ($(filter cfi,$(my_sanitize)),)
Evgenii Stepanov81bea1b2017-01-20 14:12:08 -0800164 # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp).
165 # LLVM is not set up to do this on a function basis, so force Thumb on the
166 # entire module.
167 LOCAL_ARM_MODE := thumb
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800168 my_cflags += $(CFI_EXTRA_CFLAGS)
169 my_ldflags += $(CFI_EXTRA_LDFLAGS)
Evgenii Stepanove1b96f32017-01-23 16:57:38 -0800170 my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so
Evgenii Stepanov8c50e3c2017-01-31 17:08:33 -0800171 # Workaround for b/33678192. CFI jumptables need Thumb2 codegen. Revert when
172 # Clang is updated past r290384.
173 ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
174 my_ldflags += -march=armv7-a
175 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700176endif
177
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800178# If local or global modules need ASAN, add linker flags.
179ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700180 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
181 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800182 # -nodefaultlibs (provided with libc++) prevents the driver from linking
183 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albertabf4bc92015-06-16 23:27:34 -0700184 my_ldlibs += -lm -lpthread
Dan Albert1f0d5302015-04-28 14:55:50 -0700185 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700186 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800187 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700188 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800189 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
190 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
191 $(my_shared_libraries)
192 endif
193 ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
194 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
195 endif
196
197 # Do not add unnecessary dependency in shared libraries.
198 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
199 my_ldflags += -Wl,--as-needed
200 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700201
Colin Crossd08699e2016-07-17 15:28:07 -0700202 ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES)
203 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
204 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
205 # Make sure linker_asan get installed.
206 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
207 endif
208 endif
Dan Albert08cca282014-12-11 18:56:26 -0800209 endif
210endif
211
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800212# If local module needs ASAN, add compiler flags.
213ifneq ($(filter address,$(my_sanitize)),)
214 # Frame pointer based unwinder in ASan requires ARM frame setup.
215 LOCAL_ARM_MODE := arm
216 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
217 ifndef LOCAL_IS_HOST_MODULE
218 my_cflags += -mllvm -asan-globals=0
219 endif
220endif
221
Dan Albert08cca282014-12-11 18:56:26 -0800222ifneq ($(filter undefined,$(my_sanitize)),)
Dan Albertabf4bc92015-06-16 23:27:34 -0700223 ifndef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800224 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700225 endif
226endif
Dan Albert4111d482015-04-16 18:08:44 -0700227
Dan Albert9f176552015-04-28 11:26:45 -0700228ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
229 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700230 my_cflags += -fsanitize-recover=$(recover_arg)
231endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700232
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800233ifneq ($(my_sanitize_diag),)
234 notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700235 my_cflags += -fno-sanitize-trap=$(notrap_arg)
236 # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
237 ifeq ($(filter address thread,$(my_sanitize)),)
238 # Does not have to be the first DT_NEEDED unlike ASan.
239 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
240 endif
241endif