blob: 2b182f5e7efc062586e6ee1c10fbec6c8ba3fa38 [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 -07008my_global_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -07009my_global_sanitize_diag :=
Dan Willemsenf0638392018-09-04 22:25:22 -070010ifdef LOCAL_IS_HOST_MODULE
11 ifneq ($($(my_prefix)OS),windows)
12 my_global_sanitize := $(strip $(SANITIZE_HOST))
Dan Albert4c401412015-08-19 20:13:33 -070013
Dan Willemsenf0638392018-09-04 22:25:22 -070014 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
15 my_global_sanitize := $(subst true,address,$(my_global_sanitize))
Dan Albert4c401412015-08-19 20:13:33 -070016 endif
Dan Willemsenf0638392018-09-04 22:25:22 -070017else
18 my_global_sanitize := $(strip $(SANITIZE_TARGET))
19 my_global_sanitize_diag := $(strip $(SANITIZE_TARGET_DIAG))
Dan Albert4c401412015-08-19 20:13:33 -070020endif
21
Ivan Lozanob4749cb2017-07-21 10:33:32 -070022# Disable global integer_overflow in excluded paths.
23ifneq ($(filter integer_overflow, $(my_global_sanitize)),)
24 combined_exclude_paths := $(INTEGER_OVERFLOW_EXCLUDE_PATHS) \
25 $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS)
26
27 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
28 $(filter $(dir)%,$(LOCAL_PATH)))),)
29 my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize))
30 my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag))
31 endif
32endif
33
Ivan Lozano702e8bd2018-03-15 14:49:20 -070034# Global integer sanitization doesn't support static modules.
35ifeq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
36 my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize))
37 my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag))
38endif
39ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
40 my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize))
41 my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag))
42endif
43
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -070044# Disable global CFI in excluded paths
45ifneq ($(filter cfi, $(my_global_sanitize)),)
46 combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \
47 $(PRODUCT_CFI_EXCLUDE_PATHS)
48
49 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
50 $(filter $(dir)%,$(LOCAL_PATH)))),)
51 my_global_sanitize := $(filter-out cfi,$(my_global_sanitize))
52 my_global_sanitize_diag := $(filter-out cfi,$(my_global_sanitize_diag))
53 endif
54endif
55
Dan Albert4c401412015-08-19 20:13:33 -070056ifneq ($(my_global_sanitize),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -070057 my_sanitize := $(my_global_sanitize) $(my_sanitize)
Dan Albert4c401412015-08-19 20:13:33 -070058endif
Ivan Lozano4a363732017-06-28 09:11:26 -070059ifneq ($(my_global_sanitize_diag),)
60 my_sanitize_diag := $(my_global_sanitize_diag) $(my_sanitize_diag)
61endif
Dan Albert4c401412015-08-19 20:13:33 -070062
Andreas Gampe6b30d772016-06-27 15:15:31 -070063# The sanitizer specified in the product configuration wins over the previous.
64ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),)
65 my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
66 ifeq ($(my_sanitize),never)
67 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070068 my_sanitize_diag :=
Andreas Gampe6b30d772016-06-27 15:15:31 -070069 endif
70endif
71
Colin Cross23618422016-11-02 15:05:21 -070072ifndef LOCAL_IS_HOST_MODULE
73 # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden)
74 SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH)
75 ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
76 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070077 my_sanitize_diag :=
Colin Cross23618422016-11-02 15:05:21 -070078 endif
Andreas Gampecd257402016-06-20 17:36:49 -070079endif
80
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070081# Add a filter point based on module owner (to lighten the burden). The format is a space- or
82# colon-separated list of owner names.
83ifneq (,$(SANITIZE_NEVER_BY_OWNER))
84 ifneq (,$(LOCAL_MODULE_OWNER))
85 ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
86 $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
87 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070088 my_sanitize_diag :=
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070089 endif
90 endif
91endif
92
Dan Albert08cca282014-12-11 18:56:26 -080093# Don't apply sanitizers to NDK code.
94ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -070095 my_sanitize :=
Dan Willemsenf761c0f2016-06-28 16:47:43 -070096 my_global_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070097 my_sanitize_diag :=
Dan Albert27ccb752015-04-16 16:21:02 -070098endif
99
Dan Albert4c401412015-08-19 20:13:33 -0700100# Never always wins.
101ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -0800102 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -0700103 my_sanitize_diag :=
Dan Albert08cca282014-12-11 18:56:26 -0800104endif
105
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700106# Enable CFI in included paths (for Arm64 only).
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700107ifeq ($(filter cfi, $(my_sanitize)),)
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700108 ifneq ($(filter arm64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
109 combined_include_paths := $(CFI_INCLUDE_PATHS) \
110 $(PRODUCT_CFI_INCLUDE_PATHS)
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700111
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700112 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\
113 $(filter $(dir)%,$(LOCAL_PATH)))),)
114 my_sanitize := cfi $(my_sanitize)
115 my_sanitize_diag := cfi $(my_sanitize_diag)
116 endif
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700117 endif
118endif
119
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800120# If CFI is disabled globally, remove it from my_sanitize.
Vishwath Mohan45665b42017-01-24 13:20:28 -0800121ifeq ($(strip $(ENABLE_CFI)),false)
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800122 my_sanitize := $(filter-out cfi,$(my_sanitize))
123 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
124endif
125
Vishwath Mohana2046062017-02-07 20:28:07 -0800126# Disable CFI for arm32 (b/35157333).
127ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
128 my_sanitize := $(filter-out cfi,$(my_sanitize))
129 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
130endif
131
Vishwath Mohanc026f6d2017-04-20 07:39:13 -0700132# Also disable CFI if ASAN is enabled.
133ifneq ($(filter address,$(my_sanitize)),)
134 my_sanitize := $(filter-out cfi,$(my_sanitize))
135 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
136endif
137
Evgenii Stepanov8c50e3c2017-01-31 17:08:33 -0800138# CFI needs gold linker, and mips toolchain does not have one.
139ifneq ($(filter mips mips64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
140 my_sanitize := $(filter-out cfi,$(my_sanitize))
141 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
142endif
143
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700144# Disable sanitizers which need the UBSan runtime for host targets.
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800145ifdef LOCAL_IS_HOST_MODULE
146 my_sanitize := $(filter-out cfi,$(my_sanitize))
147 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700148 my_sanitize := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize))
149 my_sanitize_diag := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize_diag))
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800150endif
151
Ivan Lozano4a363732017-06-28 09:11:26 -0700152# Support for local sanitize blacklist paths.
153ifneq ($(my_sanitize)$(my_global_sanitize),)
154 ifneq ($(LOCAL_SANITIZE_BLACKLIST),)
155 my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLACKLIST)
156 endif
157endif
158
Ivan Lozanob4749cb2017-07-21 10:33:32 -0700159# Disable integer_overflow if LOCAL_NOSANITIZE=integer.
Ivan Lozano4a363732017-06-28 09:11:26 -0700160ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),)
161 ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),)
162 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
163 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
164 endif
165endif
166
Evgenii Stepanov42823662016-05-12 13:07:17 -0700167my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
168ifneq ($(my_nosanitize),)
169 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
170endif
171
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700172ifneq ($(filter arm x86 x86_64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
173 my_sanitize := $(filter-out hwaddress,$(my_sanitize))
174endif
175
176ifneq ($(filter hwaddress,$(my_sanitize)),)
177 my_sanitize := $(filter-out address,$(my_sanitize))
178 my_sanitize := $(filter-out thread,$(my_sanitize))
Evgenii Stepanov88a95a32018-12-04 17:06:45 -0800179 my_sanitize := $(filter-out cfi,$(my_sanitize))
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700180endif
181
182ifneq ($(filter hwaddress,$(my_sanitize)),)
183 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_RUNTIME_LIBRARY)
Evgenii Stepanoved907462018-11-01 15:43:14 -0700184 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700185 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
186 my_static_libraries := $(my_static_libraries) $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_STATIC_LIBRARY)
187 endif
188 endif
189endif
190
Dan Alberta6311b72015-07-30 10:17:33 -0700191# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
192# its use an error. For multilib cases, don't use it for the 32-bit case.
193ifneq ($(filter thread,$(my_sanitize)),)
194 ifeq ($(my_32_64_bit_suffix),32)
195 ifeq ($(my_module_multilib),both)
196 my_sanitize := $(filter-out thread,$(my_sanitize))
197 else
198 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
199 endif
Yabin Cuie77c32e2017-10-19 14:33:58 -0700200 else
201 my_shared_libraries += $(TSAN_RUNTIME_LIBRARY)
Dan Alberta6311b72015-07-30 10:17:33 -0700202 endif
203endif
204
Evgenii Stepanov7dcb8b82016-05-06 18:15:57 -0700205ifneq ($(filter safe-stack,$(my_sanitize)),)
206 ifeq ($(my_32_64_bit_suffix),32)
207 my_sanitize := $(filter-out safe-stack,$(my_sanitize))
208 endif
209endif
210
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700211# Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700212ifneq ($(filter address thread hwaddress,$(my_sanitize)),)
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700213 my_sanitize := $(filter-out scudo,$(my_sanitize))
214endif
215
Kostya Kortchinsky02732402019-02-01 09:06:42 -0800216# Or if disabled globally.
217ifeq ($(strip $(PRODUCT_DISABLE_SCUDO)),true)
218 my_sanitize := $(filter-out scudo,$(my_sanitize))
219endif
220
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700221# Undefined symbols can occur if a non-sanitized library links
222# sanitized static libraries. That's OK, because the executable
223# always depends on the ASan runtime library, which defines these
224# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -0700225ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700226 ifndef LOCAL_IS_HOST_MODULE
227 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
228 ifeq ($(my_sanitize),)
229 my_allow_undefined_symbols := true
230 endif
231 endif
232 endif
233endif
234
Dan Albertb5b2ffe2015-04-16 18:07:07 -0700235ifneq ($(filter default-ub,$(my_sanitize)),)
236 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -0800237endif
238
Ivan Krasin74b32b82015-09-18 11:54:43 -0700239ifneq ($(filter coverage,$(my_sanitize)),)
240 ifeq ($(filter address,$(my_sanitize)),)
241 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
242 endif
Zach Rigglebe0811f2017-08-21 17:07:42 -0400243 my_cflags += -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp
Ivan Krasin74b32b82015-09-18 11:54:43 -0700244 my_sanitize := $(filter-out coverage,$(my_sanitize))
245endif
246
Ivan Lozano4a363732017-06-28 09:11:26 -0700247ifneq ($(filter integer_overflow,$(my_sanitize)),)
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700248 # Respect LOCAL_NOSANITIZE for integer-overflow flags.
249 ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
250 my_sanitize += signed-integer-overflow
251 endif
252 ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
253 my_sanitize += unsigned-integer-overflow
254 endif
255 my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS)
Ivan Lozano4a363732017-06-28 09:11:26 -0700256
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700257 # Check for diagnostics mode.
258 ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
259 ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
260 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
Ivan Lozano911cb992018-02-21 13:41:05 -0800261 my_sanitize_diag += signed-integer-overflow
262 my_sanitize_diag += unsigned-integer-overflow
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700263 else
264 $(call pretty-error,Make cannot apply integer overflow diagnostics to static binary.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700265 endif
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700266 else
267 $(call pretty-error,Make cannot apply integer overflow diagnostics to static library.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700268 endif
269 endif
270 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
271endif
272
273# Makes sure integer_overflow diagnostics is removed from the diagnostics list
274# even if integer_overflow is not set for some reason.
275ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
276 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
277endif
278
Dan Albert08cca282014-12-11 18:56:26 -0800279ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800280 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800281 my_cflags += -fsanitize=$(fsanitize_arg)
Evgenii Stepanov9b82b3f2018-08-31 12:57:26 -0700282 my_asflags += -fsanitize=$(fsanitize_arg)
Dan Albert08cca282014-12-11 18:56:26 -0800283
284 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700285 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800286 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albertabf4bc92015-06-16 23:27:34 -0700287 else
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700288 my_cflags += -fsanitize-trap=all
289 my_cflags += -ftrap-function=abort
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700290 ifneq ($(filter address thread,$(my_sanitize)),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700291 my_cflags += -fno-sanitize-trap=address,thread
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700292 my_shared_libraries += libdl
293 endif
Dan Albert08cca282014-12-11 18:56:26 -0800294 endif
295endif
296
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700297ifneq ($(filter cfi,$(my_sanitize)),)
Evgenii Stepanov81bea1b2017-01-20 14:12:08 -0800298 # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp).
299 # LLVM is not set up to do this on a function basis, so force Thumb on the
300 # entire module.
301 LOCAL_ARM_MODE := thumb
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800302 my_cflags += $(CFI_EXTRA_CFLAGS)
Evgenii Stepanov9b82b3f2018-08-31 12:57:26 -0700303 my_asflags += $(CFI_EXTRA_ASFLAGS)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000304 # Only append the default visibility flag if -fvisibility has not already been
305 # set to hidden.
306 ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),)
307 my_cflags += -fvisibility=default
308 endif
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800309 my_ldflags += $(CFI_EXTRA_LDFLAGS)
Evgenii Stepanove1b96f32017-01-23 16:57:38 -0800310 my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so
Vishwath Mohan85f72442017-11-01 09:21:20 +0000311
312 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
313 my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags))
314 my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags))
315 else
316 # Apply the version script to non-static executables
317 my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map
318 LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map
319 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700320endif
321
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800322# If local or global modules need ASAN, add linker flags.
323ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700324 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
325 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800326 # -nodefaultlibs (provided with libc++) prevents the driver from linking
327 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albert1f0d5302015-04-28 14:55:50 -0700328 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700329 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800330 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700331 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800332 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
333 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
334 $(my_shared_libraries)
335 endif
336 ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
337 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
338 endif
339
340 # Do not add unnecessary dependency in shared libraries.
341 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
342 my_ldflags += -Wl,--as-needed
343 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700344
Mikhail Naganovaa73cef2018-12-20 15:55:08 -0800345 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
Colin Crossd08699e2016-07-17 15:28:07 -0700346 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
347 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
348 # Make sure linker_asan get installed.
Logan Chienc6d2cf82019-01-31 17:07:50 +0800349 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER_FILE)
Colin Crossd08699e2016-07-17 15:28:07 -0700350 endif
351 endif
Dan Albert08cca282014-12-11 18:56:26 -0800352 endif
353endif
354
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800355# If local module needs ASAN, add compiler flags.
356ifneq ($(filter address,$(my_sanitize)),)
357 # Frame pointer based unwinder in ASan requires ARM frame setup.
358 LOCAL_ARM_MODE := arm
359 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
360 ifndef LOCAL_IS_HOST_MODULE
361 my_cflags += -mllvm -asan-globals=0
362 endif
363endif
364
Evgenii Stepanovaec1ffc2018-08-28 13:52:08 -0700365# If local module needs HWASAN, add compiler flags.
366ifneq ($(filter hwaddress,$(my_sanitize)),)
367 my_cflags += $(HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
368endif
369
Alexey Polyudove98e5622018-03-27 15:28:12 -0700370# Use minimal diagnostics when integer overflow is enabled; never do it for HOST or AUX modules
371ifeq ($(LOCAL_IS_HOST_MODULE)$(LOCAL_IS_AUX_MODULE),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800372 # Pre-emptively add UBSAN minimal runtime incase a static library dependency requires it
373 ifeq ($(filter STATIC_LIBRARIES,$(LOCAL_MODULE_CLASS)),)
374 ifndef LOCAL_SDK_VERSION
375 my_static_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY)
Ivan Lozanoe5081692018-05-11 14:09:36 -0700376 my_ldflags += -Wl,--exclude-libs,$($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY).a
Ivan Lozano911cb992018-02-21 13:41:05 -0800377 endif
378 endif
379 ifneq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize)),)
Kostya Kortchinsky47c10eb2018-10-11 08:56:12 -0700380 ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize_diag)),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800381 ifeq ($(filter cfi,$(my_sanitize_diag)),)
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700382 ifeq ($(filter address hwaddress,$(my_sanitize)),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800383 my_cflags += -fsanitize-minimal-runtime
384 my_cflags += -fno-sanitize-trap=integer
385 my_cflags += -fno-sanitize-recover=integer
386 endif
387 endif
388 endif
389 endif
390endif
391
Kostya Kortchinsky47c10eb2018-10-11 08:56:12 -0700392# For Scudo, we opt for the minimal runtime, unless some diagnostics are enabled.
393ifneq ($(filter scudo,$(my_sanitize)),)
394 ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer cfi,$(my_sanitize_diag)),)
395 my_cflags += -fsanitize-minimal-runtime
396 endif
397 ifneq ($(filter -fsanitize-minimal-runtime,$(my_cflags)),)
398 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_MINIMAL_RUNTIME_LIBRARY)
399 else
400 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_RUNTIME_LIBRARY)
401 endif
402endif
403
Dan Albert9f176552015-04-28 11:26:45 -0700404ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
405 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700406 my_cflags += -fsanitize-recover=$(recover_arg)
407endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700408
Ivan Lozano5fb2de72018-12-12 10:04:34 -0800409ifneq ($(strip $(LOCAL_SANITIZE_NO_RECOVER)),)
410 no_recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_NO_RECOVER)),
411 my_cflags += -fno-sanitize-recover=$(no_recover_arg)
412endif
413
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800414ifneq ($(my_sanitize_diag),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000415 # TODO(vishwath): Add diagnostic support for static executables once
416 # we switch to clang-4393122 (which adds the static ubsan runtime
417 # that this depends on)
418 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
419 notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
420 my_cflags += -fno-sanitize-trap=$(notrap_arg)
421 # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700422 ifeq ($(filter address thread scudo hwaddress,$(my_sanitize)),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000423 # Does not have to be the first DT_NEEDED unlike ASan.
424 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
425 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700426 endif
427endif
Chih-Hung Hsieh18710622018-11-17 20:18:08 -0800428
429# http://b/119329758, Android core does not boot up with this sanitizer yet.
430# Previously sanitized modules might not pass new implicit-integer-sign-change check.
431# Disable this check unless it has been explicitly specified.
432ifneq ($(findstring fsanitize,$(my_cflags)),)
433 ifneq ($(findstring integer,$(my_cflags)),)
434 ifeq ($(findstring sanitize=implicit-integer-sign-change,$(my_cflags)),)
435 my_cflags += -fno-sanitize=implicit-integer-sign-change
436 endif
437 endif
438endif