blob: 82b17beab370a667ff5481df34ecba541acff5ad [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
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -080056# Disable global memtag_heap in excluded paths
57ifneq ($(filter memtag_heap, $(my_global_sanitize)),)
58 combined_exclude_paths := $(MEMTAG_HEAP_EXCLUDE_PATHS) \
59 $(PRODUCT_MEMTAG_HEAP_EXCLUDE_PATHS)
60
61 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
62 $(filter $(dir)%,$(LOCAL_PATH)))),)
63 my_global_sanitize := $(filter-out memtag_heap,$(my_global_sanitize))
64 my_global_sanitize_diag := $(filter-out memtag_heap,$(my_global_sanitize_diag))
65 endif
66endif
67
Dan Albert4c401412015-08-19 20:13:33 -070068ifneq ($(my_global_sanitize),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -070069 my_sanitize := $(my_global_sanitize) $(my_sanitize)
Dan Albert4c401412015-08-19 20:13:33 -070070endif
Ivan Lozano4a363732017-06-28 09:11:26 -070071ifneq ($(my_global_sanitize_diag),)
72 my_sanitize_diag := $(my_global_sanitize_diag) $(my_sanitize_diag)
73endif
Dan Albert4c401412015-08-19 20:13:33 -070074
Andreas Gampe6b30d772016-06-27 15:15:31 -070075# The sanitizer specified in the product configuration wins over the previous.
76ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),)
77 my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
78 ifeq ($(my_sanitize),never)
79 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070080 my_sanitize_diag :=
Andreas Gampe6b30d772016-06-27 15:15:31 -070081 endif
82endif
83
Colin Cross23618422016-11-02 15:05:21 -070084ifndef LOCAL_IS_HOST_MODULE
85 # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden)
86 SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH)
87 ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
88 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070089 my_sanitize_diag :=
Colin Cross23618422016-11-02 15:05:21 -070090 endif
Andreas Gampecd257402016-06-20 17:36:49 -070091endif
92
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070093# Add a filter point based on module owner (to lighten the burden). The format is a space- or
94# colon-separated list of owner names.
95ifneq (,$(SANITIZE_NEVER_BY_OWNER))
96 ifneq (,$(LOCAL_MODULE_OWNER))
97 ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
98 $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
99 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -0700100 my_sanitize_diag :=
Andreas Gampe3d3b0c92016-06-20 17:46:29 -0700101 endif
102 endif
103endif
104
Dan Albert08cca282014-12-11 18:56:26 -0800105# Don't apply sanitizers to NDK code.
106ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -0700107 my_sanitize :=
Dan Willemsenf761c0f2016-06-28 16:47:43 -0700108 my_global_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -0700109 my_sanitize_diag :=
Dan Albert27ccb752015-04-16 16:21:02 -0700110endif
111
Dan Albert4c401412015-08-19 20:13:33 -0700112# Never always wins.
113ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -0800114 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -0700115 my_sanitize_diag :=
Dan Albert08cca282014-12-11 18:56:26 -0800116endif
117
Elvis Chien064d91c2021-04-14 17:02:29 +0800118# Enable CFI in included paths.
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700119ifeq ($(filter cfi, $(my_sanitize)),)
Elvis Chien064d91c2021-04-14 17:02:29 +0800120 combined_include_paths := $(CFI_INCLUDE_PATHS) \
121 $(PRODUCT_CFI_INCLUDE_PATHS)
122 combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \
123 $(PRODUCT_CFI_EXCLUDE_PATHS)
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700124
Elvis Chien064d91c2021-04-14 17:02:29 +0800125 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\
126 $(filter $(dir)%,$(LOCAL_PATH)))),)
127 ifeq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
128 $(filter $(dir)%,$(LOCAL_PATH)))),)
129 my_sanitize := cfi $(my_sanitize)
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700130 endif
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700131 endif
132endif
133
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800134# Enable memtag_heap in included paths (for Arm64 only).
135ifeq ($(filter memtag_heap, $(my_sanitize)),)
136 ifneq ($(filter arm64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
137 combined_sync_include_paths := $(MEMTAG_HEAP_SYNC_INCLUDE_PATHS) \
138 $(PRODUCT_MEMTAG_HEAP_SYNC_INCLUDE_PATHS)
139 combined_async_include_paths := $(MEMTAG_HEAP_ASYNC_INCLUDE_PATHS) \
140 $(PRODUCT_MEMTAG_HEAP_ASYNC_INCLUDE_PATHS)
Evgenii Stepanov13bc2272021-04-09 14:52:33 -0700141 combined_exclude_paths := $(MEMTAG_HEAP_EXCLUDE_PATHS) \
142 $(PRODUCT_MEMTAG_HEAP_EXCLUDE_PATHS)
Evgenii Stepanov80b53b82023-06-08 15:40:39 -0700143 ifneq ($(PRODUCT_MEMTAG_HEAP_SKIP_DEFAULT_PATHS),true)
144 combined_sync_include_paths += $(PRODUCT_MEMTAG_HEAP_SYNC_DEFAULT_INCLUDE_PATHS)
145 combined_async_include_paths += $(PRODUCT_MEMTAG_HEAP_ASYNC_DEFAULT_INCLUDE_PATHS)
146 endif
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800147
Evgenii Stepanov13bc2272021-04-09 14:52:33 -0700148 ifeq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
149 $(filter $(dir)%,$(LOCAL_PATH)))),)
150 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_sync_include_paths)),\
151 $(filter $(dir)%,$(LOCAL_PATH)))),)
152 my_sanitize := memtag_heap $(my_sanitize)
153 my_sanitize_diag := memtag_heap $(my_sanitize_diag)
154 else ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_async_include_paths)),\
155 $(filter $(dir)%,$(LOCAL_PATH)))),)
156 my_sanitize := memtag_heap $(my_sanitize)
157 endif
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800158 endif
159 endif
160endif
161
Hang Lue235ded2022-08-29 14:42:34 +0800162# Enable HWASan in included paths.
163ifeq ($(filter hwaddress, $(my_sanitize)),)
164 combined_include_paths := $(HWASAN_INCLUDE_PATHS) \
165 $(PRODUCT_HWASAN_INCLUDE_PATHS)
166
167 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\
168 $(filter $(dir)%,$(LOCAL_PATH)))),)
169 my_sanitize := hwaddress $(my_sanitize)
170 endif
171endif
172
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800173# If CFI is disabled globally, remove it from my_sanitize.
Vishwath Mohan45665b42017-01-24 13:20:28 -0800174ifeq ($(strip $(ENABLE_CFI)),false)
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800175 my_sanitize := $(filter-out cfi,$(my_sanitize))
176 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
177endif
178
Florian Mayer548a2ad2022-08-31 22:25:55 +0000179# Also disable CFI and MTE if ASAN is enabled.
Vishwath Mohanc026f6d2017-04-20 07:39:13 -0700180ifneq ($(filter address,$(my_sanitize)),)
181 my_sanitize := $(filter-out cfi,$(my_sanitize))
Florian Mayer548a2ad2022-08-31 22:25:55 +0000182 my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
Mitch Phillips8520f4b2023-06-01 14:22:32 +0200183 my_sanitize := $(filter-out memtag_globals,$(my_sanitize))
Florian Mayer548a2ad2022-08-31 22:25:55 +0000184 my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
Vishwath Mohanc026f6d2017-04-20 07:39:13 -0700185 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
186endif
187
Mitch Phillipsb71a8752021-05-19 11:29:02 -0700188# Disable memtag for host targets. Host executables in AndroidMk files are
189# deprecated, but some partners still have them floating around.
190ifdef LOCAL_IS_HOST_MODULE
Mitch Phillips8520f4b2023-06-01 14:22:32 +0200191 my_sanitize := $(filter-out memtag_heap memtag_stack memtag_globals,$(my_sanitize))
192 my_sanitize_diag := $(filter-out memtag_heap memtag_stack memtag_globals,$(my_sanitize_diag))
Mitch Phillipsb71a8752021-05-19 11:29:02 -0700193endif
194
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700195# Disable sanitizers which need the UBSan runtime for host targets.
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800196ifdef LOCAL_IS_HOST_MODULE
197 my_sanitize := $(filter-out cfi,$(my_sanitize))
198 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700199 my_sanitize := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize))
200 my_sanitize_diag := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize_diag))
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800201endif
202
Ivan Lozano4a363732017-06-28 09:11:26 -0700203# Support for local sanitize blacklist paths.
204ifneq ($(my_sanitize)$(my_global_sanitize),)
Pirama Arumuga Nainar56558332020-07-30 15:18:07 -0700205 ifneq ($(LOCAL_SANITIZE_BLOCKLIST),)
206 my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLOCKLIST)
207 endif
Ivan Lozano4a363732017-06-28 09:11:26 -0700208endif
209
Ivan Lozanob4749cb2017-07-21 10:33:32 -0700210# Disable integer_overflow if LOCAL_NOSANITIZE=integer.
Ivan Lozano4a363732017-06-28 09:11:26 -0700211ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),)
212 ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),)
213 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
214 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
215 endif
216endif
217
Evgenii Stepanov42823662016-05-12 13:07:17 -0700218my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
219ifneq ($(my_nosanitize),)
220 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
221endif
222
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700223ifneq ($(filter arm x86 x86_64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
224 my_sanitize := $(filter-out hwaddress,$(my_sanitize))
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800225 my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
Florian Mayer548a2ad2022-08-31 22:25:55 +0000226 my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
Mitch Phillips8520f4b2023-06-01 14:22:32 +0200227 my_sanitize := $(filter-out memtag_globals,$(my_sanitize))
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700228endif
229
230ifneq ($(filter hwaddress,$(my_sanitize)),)
231 my_sanitize := $(filter-out address,$(my_sanitize))
Florian Mayer548a2ad2022-08-31 22:25:55 +0000232 my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
Mitch Phillips8520f4b2023-06-01 14:22:32 +0200233 my_sanitize := $(filter-out memtag_globals,$(my_sanitize))
Florian Mayer548a2ad2022-08-31 22:25:55 +0000234 my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700235 my_sanitize := $(filter-out thread,$(my_sanitize))
Evgenii Stepanov88a95a32018-12-04 17:06:45 -0800236 my_sanitize := $(filter-out cfi,$(my_sanitize))
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700237endif
238
239ifneq ($(filter hwaddress,$(my_sanitize)),)
240 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_RUNTIME_LIBRARY)
Evgenii Stepanoved907462018-11-01 15:43:14 -0700241 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700242 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
Ryan Prichardacf8b0f2019-10-21 20:47:53 -0700243 my_static_libraries := $(my_static_libraries) \
244 $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_STATIC_LIBRARY) \
245 libdl
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700246 endif
247 endif
248endif
249
Mitch Phillips8520f4b2023-06-01 14:22:32 +0200250ifneq ($(filter memtag_heap memtag_stack memtag_globals,$(my_sanitize)),)
Florian Mayer548a2ad2022-08-31 22:25:55 +0000251 ifneq ($(filter memtag_heap,$(my_sanitize_diag)),)
252 my_cflags += -fsanitize-memtag-mode=sync
253 my_sanitize_diag := $(filter-out memtag_heap,$(my_sanitize_diag))
254 else
255 my_cflags += -fsanitize-memtag-mode=async
Florian Mayerfe3e5372022-08-31 18:37:51 +0000256 endif
Florian Mayer548a2ad2022-08-31 22:25:55 +0000257endif
258
Florian Mayer2a676262023-06-07 15:02:22 -0700259# Ignore SANITIZE_TARGET_DIAG=memtag_heap without SANITIZE_TARGET=memtag_heap
260# This can happen if a condition above filters out memtag_heap from
261# my_sanitize. It is easier to handle all of these cases here centrally.
262ifneq ($(filter memtag_heap,$(my_sanitize_diag)),)
263 my_sanitize_diag := $(filter-out memtag_heap,$(my_sanitize_diag))
264endif
265
Florian Mayer548a2ad2022-08-31 22:25:55 +0000266ifneq ($(filter memtag_heap,$(my_sanitize)),)
267 my_cflags += -fsanitize=memtag-heap
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800268 my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
269endif
270
Florian Mayer548a2ad2022-08-31 22:25:55 +0000271ifneq ($(filter memtag_stack,$(my_sanitize)),)
272 my_cflags += -fsanitize=memtag-stack
273 my_cflags += -march=armv8a+memtag
274 my_ldflags += -march=armv8a+memtag
275 my_asflags += -march=armv8a+memtag
276 my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
277endif
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800278
Mitch Phillips8520f4b2023-06-01 14:22:32 +0200279ifneq ($(filter memtag_globals,$(my_sanitize)),)
280 my_cflags += -fsanitize=memtag-globals
281 # TODO(mitchp): For now, enable memtag-heap with memtag-globals because the
282 # linker isn't new enough
283 # (https://reviews.llvm.org/differential/changeset/?ref=4243566).
284 my_sanitize := $(filter-out memtag_globals,$(my_sanitize))
285endif
286
Dan Alberta6311b72015-07-30 10:17:33 -0700287# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
288# its use an error. For multilib cases, don't use it for the 32-bit case.
289ifneq ($(filter thread,$(my_sanitize)),)
290 ifeq ($(my_32_64_bit_suffix),32)
291 ifeq ($(my_module_multilib),both)
292 my_sanitize := $(filter-out thread,$(my_sanitize))
293 else
294 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
295 endif
Yabin Cuie77c32e2017-10-19 14:33:58 -0700296 else
297 my_shared_libraries += $(TSAN_RUNTIME_LIBRARY)
Dan Alberta6311b72015-07-30 10:17:33 -0700298 endif
299endif
300
Evgenii Stepanov7dcb8b82016-05-06 18:15:57 -0700301ifneq ($(filter safe-stack,$(my_sanitize)),)
302 ifeq ($(my_32_64_bit_suffix),32)
303 my_sanitize := $(filter-out safe-stack,$(my_sanitize))
304 endif
305endif
306
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700307# Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700308ifneq ($(filter address thread hwaddress,$(my_sanitize)),)
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700309 my_sanitize := $(filter-out scudo,$(my_sanitize))
310endif
311
Kostya Kortchinsky02732402019-02-01 09:06:42 -0800312# Or if disabled globally.
Anton Hansson8dab0a62019-03-28 15:45:40 +0000313ifeq ($(PRODUCT_DISABLE_SCUDO),true)
Kostya Kortchinsky02732402019-02-01 09:06:42 -0800314 my_sanitize := $(filter-out scudo,$(my_sanitize))
315endif
316
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700317# Undefined symbols can occur if a non-sanitized library links
318# sanitized static libraries. That's OK, because the executable
319# always depends on the ASan runtime library, which defines these
320# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -0700321ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700322 ifndef LOCAL_IS_HOST_MODULE
323 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
324 ifeq ($(my_sanitize),)
325 my_allow_undefined_symbols := true
326 endif
327 endif
328 endif
329endif
330
Dan Albertb5b2ffe2015-04-16 18:07:07 -0700331ifneq ($(filter default-ub,$(my_sanitize)),)
332 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -0800333endif
334
Mitch Phillipsee8f4a02019-05-01 14:37:33 -0700335ifneq ($(filter fuzzer,$(my_sanitize)),)
336 # SANITIZE_TARGET='fuzzer' actually means to create the fuzzer coverage
337 # information, not to link against the fuzzer main().
338 my_sanitize := $(filter-out fuzzer,$(my_sanitize))
339 my_sanitize += fuzzer-no-link
Mitch Phillipsd51048a2022-03-02 01:25:22 +0000340
341 # TODO(b/131771163): Disable LTO for fuzzer builds. Note that Cfi causes
342 # dependency on LTO.
343 my_sanitize := $(filter-out cfi,$(my_sanitize))
344 my_cflags += -fno-lto
345 my_ldflags += -fno-lto
Ivan Krasin74b32b82015-09-18 11:54:43 -0700346endif
347
Ivan Lozano4a363732017-06-28 09:11:26 -0700348ifneq ($(filter integer_overflow,$(my_sanitize)),)
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700349 # Respect LOCAL_NOSANITIZE for integer-overflow flags.
350 ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
351 my_sanitize += signed-integer-overflow
352 endif
353 ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
354 my_sanitize += unsigned-integer-overflow
355 endif
356 my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS)
Ivan Lozano4a363732017-06-28 09:11:26 -0700357
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700358 # Check for diagnostics mode.
359 ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
360 ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
361 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
Ivan Lozano911cb992018-02-21 13:41:05 -0800362 my_sanitize_diag += signed-integer-overflow
363 my_sanitize_diag += unsigned-integer-overflow
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700364 else
365 $(call pretty-error,Make cannot apply integer overflow diagnostics to static binary.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700366 endif
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700367 else
368 $(call pretty-error,Make cannot apply integer overflow diagnostics to static library.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700369 endif
370 endif
371 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
372endif
373
374# Makes sure integer_overflow diagnostics is removed from the diagnostics list
375# even if integer_overflow is not set for some reason.
376ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
377 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
378endif
379
Dan Albert08cca282014-12-11 18:56:26 -0800380ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800381 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800382 my_cflags += -fsanitize=$(fsanitize_arg)
Evgenii Stepanov9b82b3f2018-08-31 12:57:26 -0700383 my_asflags += -fsanitize=$(fsanitize_arg)
Dan Albert08cca282014-12-11 18:56:26 -0800384
Mitch Phillipsee8f4a02019-05-01 14:37:33 -0700385 # When fuzzing, we wish to crash with diagnostics on any bug.
386 ifneq ($(filter fuzzer-no-link,$(my_sanitize)),)
387 my_cflags += -fno-sanitize-trap=all
388 my_cflags += -fno-sanitize-recover=all
389 my_ldflags += -fsanitize=fuzzer-no-link
390 else ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700391 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800392 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albertabf4bc92015-06-16 23:27:34 -0700393 else
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700394 my_cflags += -fsanitize-trap=all
395 my_cflags += -ftrap-function=abort
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700396 ifneq ($(filter address thread,$(my_sanitize)),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700397 my_cflags += -fno-sanitize-trap=address,thread
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700398 my_shared_libraries += libdl
399 endif
Dan Albert08cca282014-12-11 18:56:26 -0800400 endif
401endif
402
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700403ifneq ($(filter cfi,$(my_sanitize)),)
Evgenii Stepanov81bea1b2017-01-20 14:12:08 -0800404 # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp).
405 # LLVM is not set up to do this on a function basis, so force Thumb on the
406 # entire module.
407 LOCAL_ARM_MODE := thumb
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800408 my_cflags += $(CFI_EXTRA_CFLAGS)
Evgenii Stepanov9b82b3f2018-08-31 12:57:26 -0700409 my_asflags += $(CFI_EXTRA_ASFLAGS)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000410 # Only append the default visibility flag if -fvisibility has not already been
411 # set to hidden.
412 ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),)
413 my_cflags += -fvisibility=default
414 endif
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800415 my_ldflags += $(CFI_EXTRA_LDFLAGS)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000416
417 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
418 my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags))
419 my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags))
420 else
421 # Apply the version script to non-static executables
422 my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map
423 LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map
424 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700425endif
426
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800427# If local or global modules need ASAN, add linker flags.
428ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700429 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
430 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800431 # -nodefaultlibs (provided with libc++) prevents the driver from linking
432 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albert1f0d5302015-04-28 14:55:50 -0700433 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700434 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800435 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700436 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800437 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
438 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
439 $(my_shared_libraries)
440 endif
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800441
442 # Do not add unnecessary dependency in shared libraries.
443 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
444 my_ldflags += -Wl,--as-needed
445 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700446
Mikhail Naganovaa73cef2018-12-20 15:55:08 -0800447 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
Colin Crossd08699e2016-07-17 15:28:07 -0700448 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
449 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
450 # Make sure linker_asan get installed.
Logan Chienc6d2cf82019-01-31 17:07:50 +0800451 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER_FILE)
Colin Crossd08699e2016-07-17 15:28:07 -0700452 endif
453 endif
Dan Albert08cca282014-12-11 18:56:26 -0800454 endif
455endif
456
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800457# If local module needs ASAN, add compiler flags.
458ifneq ($(filter address,$(my_sanitize)),)
459 # Frame pointer based unwinder in ASan requires ARM frame setup.
460 LOCAL_ARM_MODE := arm
461 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
462 ifndef LOCAL_IS_HOST_MODULE
463 my_cflags += -mllvm -asan-globals=0
464 endif
465endif
466
Evgenii Stepanovaec1ffc2018-08-28 13:52:08 -0700467# If local module needs HWASAN, add compiler flags.
468ifneq ($(filter hwaddress,$(my_sanitize)),)
469 my_cflags += $(HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
Florian Mayer97222e12023-06-01 14:16:28 -0700470
471 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
472 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
473 my_linker := /system/bin/linker_hwasan64
474 endif
475 endif
476
Evgenii Stepanovaec1ffc2018-08-28 13:52:08 -0700477endif
478
Dan Willemsen58634e12020-03-06 18:09:06 -0800479# Use minimal diagnostics when integer overflow is enabled; never do it for HOST modules
480ifeq ($(LOCAL_IS_HOST_MODULE),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800481 # Pre-emptively add UBSAN minimal runtime incase a static library dependency requires it
482 ifeq ($(filter STATIC_LIBRARIES,$(LOCAL_MODULE_CLASS)),)
483 ifndef LOCAL_SDK_VERSION
484 my_static_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY)
Ivan Lozanoe5081692018-05-11 14:09:36 -0700485 my_ldflags += -Wl,--exclude-libs,$($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY).a
Ivan Lozano911cb992018-02-21 13:41:05 -0800486 endif
487 endif
488 ifneq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize)),)
Kostya Kortchinsky47c10eb2018-10-11 08:56:12 -0700489 ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize_diag)),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800490 ifeq ($(filter cfi,$(my_sanitize_diag)),)
Mitch Phillipsee8f4a02019-05-01 14:37:33 -0700491 ifeq ($(filter address hwaddress fuzzer-no-link,$(my_sanitize)),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800492 my_cflags += -fsanitize-minimal-runtime
493 my_cflags += -fno-sanitize-trap=integer
494 my_cflags += -fno-sanitize-recover=integer
495 endif
496 endif
497 endif
498 endif
499endif
500
Kostya Kortchinsky47c10eb2018-10-11 08:56:12 -0700501# For Scudo, we opt for the minimal runtime, unless some diagnostics are enabled.
502ifneq ($(filter scudo,$(my_sanitize)),)
503 ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer cfi,$(my_sanitize_diag)),)
504 my_cflags += -fsanitize-minimal-runtime
505 endif
506 ifneq ($(filter -fsanitize-minimal-runtime,$(my_cflags)),)
507 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_MINIMAL_RUNTIME_LIBRARY)
508 else
509 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_RUNTIME_LIBRARY)
510 endif
511endif
512
Dan Albert9f176552015-04-28 11:26:45 -0700513ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
514 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700515 my_cflags += -fsanitize-recover=$(recover_arg)
516endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700517
Ivan Lozano5fb2de72018-12-12 10:04:34 -0800518ifneq ($(strip $(LOCAL_SANITIZE_NO_RECOVER)),)
519 no_recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_NO_RECOVER)),
520 my_cflags += -fno-sanitize-recover=$(no_recover_arg)
521endif
522
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800523ifneq ($(my_sanitize_diag),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000524 # TODO(vishwath): Add diagnostic support for static executables once
525 # we switch to clang-4393122 (which adds the static ubsan runtime
526 # that this depends on)
527 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
528 notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
529 my_cflags += -fno-sanitize-trap=$(notrap_arg)
530 # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700531 ifeq ($(filter address thread scudo hwaddress,$(my_sanitize)),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000532 # Does not have to be the first DT_NEEDED unlike ASan.
533 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
534 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700535 endif
536endif
Chih-Hung Hsieh18710622018-11-17 20:18:08 -0800537
538# http://b/119329758, Android core does not boot up with this sanitizer yet.
539# Previously sanitized modules might not pass new implicit-integer-sign-change check.
540# Disable this check unless it has been explicitly specified.
541ifneq ($(findstring fsanitize,$(my_cflags)),)
542 ifneq ($(findstring integer,$(my_cflags)),)
543 ifeq ($(findstring sanitize=implicit-integer-sign-change,$(my_cflags)),)
544 my_cflags += -fno-sanitize=implicit-integer-sign-change
545 endif
546 endif
547endif
Yabin Cui462c12d2021-01-14 14:05:54 -0800548
549# http://b/177566116, libc++ may crash with this sanitizer.
550# Disable this check unless it has been explicitly specified.
551ifneq ($(findstring fsanitize,$(my_cflags)),)
552 ifneq ($(findstring integer,$(my_cflags)),)
553 ifeq ($(findstring sanitize=unsigned-shift-base,$(my_cflags)),)
554 my_cflags += -fno-sanitize=unsigned-shift-base
555 endif
556 endif
557endif