blob: f9042c290bfb6a86fbe4043ae0a7a84dfa38792d [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
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700118# Enable CFI in included paths (for Arm64 only).
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700119ifeq ($(filter cfi, $(my_sanitize)),)
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700120 ifneq ($(filter arm64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
121 combined_include_paths := $(CFI_INCLUDE_PATHS) \
122 $(PRODUCT_CFI_INCLUDE_PATHS)
Evgenii Stepanov13bc2272021-04-09 14:52:33 -0700123 combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \
124 $(PRODUCT_CFI_EXCLUDE_PATHS)
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700125
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700126 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\
127 $(filter $(dir)%,$(LOCAL_PATH)))),)
Evgenii Stepanov13bc2272021-04-09 14:52:33 -0700128 ifeq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
129 $(filter $(dir)%,$(LOCAL_PATH)))),)
130 my_sanitize := cfi $(my_sanitize)
131 endif
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700132 endif
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700133 endif
134endif
135
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800136# Enable memtag_heap in included paths (for Arm64 only).
137ifeq ($(filter memtag_heap, $(my_sanitize)),)
138 ifneq ($(filter arm64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
139 combined_sync_include_paths := $(MEMTAG_HEAP_SYNC_INCLUDE_PATHS) \
140 $(PRODUCT_MEMTAG_HEAP_SYNC_INCLUDE_PATHS)
141 combined_async_include_paths := $(MEMTAG_HEAP_ASYNC_INCLUDE_PATHS) \
142 $(PRODUCT_MEMTAG_HEAP_ASYNC_INCLUDE_PATHS)
Evgenii Stepanov13bc2272021-04-09 14:52:33 -0700143 combined_exclude_paths := $(MEMTAG_HEAP_EXCLUDE_PATHS) \
144 $(PRODUCT_MEMTAG_HEAP_EXCLUDE_PATHS)
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800145
Evgenii Stepanov13bc2272021-04-09 14:52:33 -0700146 ifeq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
147 $(filter $(dir)%,$(LOCAL_PATH)))),)
148 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_sync_include_paths)),\
149 $(filter $(dir)%,$(LOCAL_PATH)))),)
150 my_sanitize := memtag_heap $(my_sanitize)
151 my_sanitize_diag := memtag_heap $(my_sanitize_diag)
152 else ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_async_include_paths)),\
153 $(filter $(dir)%,$(LOCAL_PATH)))),)
154 my_sanitize := memtag_heap $(my_sanitize)
155 endif
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800156 endif
157 endif
158endif
159
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800160# If CFI is disabled globally, remove it from my_sanitize.
Vishwath Mohan45665b42017-01-24 13:20:28 -0800161ifeq ($(strip $(ENABLE_CFI)),false)
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800162 my_sanitize := $(filter-out cfi,$(my_sanitize))
163 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
164endif
165
Vishwath Mohanc026f6d2017-04-20 07:39:13 -0700166# Also disable CFI if ASAN is enabled.
167ifneq ($(filter address,$(my_sanitize)),)
168 my_sanitize := $(filter-out cfi,$(my_sanitize))
169 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
170endif
171
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700172# Disable sanitizers which need the UBSan runtime for host targets.
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800173ifdef LOCAL_IS_HOST_MODULE
174 my_sanitize := $(filter-out cfi,$(my_sanitize))
175 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700176 my_sanitize := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize))
177 my_sanitize_diag := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize_diag))
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800178endif
179
Ivan Lozano4a363732017-06-28 09:11:26 -0700180# Support for local sanitize blacklist paths.
181ifneq ($(my_sanitize)$(my_global_sanitize),)
Pirama Arumuga Nainar56558332020-07-30 15:18:07 -0700182 ifneq ($(LOCAL_SANITIZE_BLOCKLIST),)
183 my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLOCKLIST)
184 endif
Ivan Lozano4a363732017-06-28 09:11:26 -0700185endif
186
Ivan Lozanob4749cb2017-07-21 10:33:32 -0700187# Disable integer_overflow if LOCAL_NOSANITIZE=integer.
Ivan Lozano4a363732017-06-28 09:11:26 -0700188ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),)
189 ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),)
190 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
191 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
192 endif
193endif
194
Evgenii Stepanov42823662016-05-12 13:07:17 -0700195my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
196ifneq ($(my_nosanitize),)
197 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
198endif
199
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700200ifneq ($(filter arm x86 x86_64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
201 my_sanitize := $(filter-out hwaddress,$(my_sanitize))
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800202 my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700203endif
204
205ifneq ($(filter hwaddress,$(my_sanitize)),)
206 my_sanitize := $(filter-out address,$(my_sanitize))
207 my_sanitize := $(filter-out thread,$(my_sanitize))
Evgenii Stepanov88a95a32018-12-04 17:06:45 -0800208 my_sanitize := $(filter-out cfi,$(my_sanitize))
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700209endif
210
211ifneq ($(filter hwaddress,$(my_sanitize)),)
212 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_RUNTIME_LIBRARY)
Evgenii Stepanoved907462018-11-01 15:43:14 -0700213 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700214 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
Ryan Prichardacf8b0f2019-10-21 20:47:53 -0700215 my_static_libraries := $(my_static_libraries) \
216 $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_STATIC_LIBRARY) \
217 libdl
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700218 endif
219 endif
220endif
221
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800222ifneq ($(filter memtag_heap,$(my_sanitize)),)
223 # Add memtag ELF note.
Peter Collingbourne8b80f3e2021-04-12 11:46:17 -0700224 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
225 ifneq ($(filter memtag_heap,$(my_sanitize_diag)),)
226 my_whole_static_libraries += note_memtag_heap_sync
227 else
228 my_whole_static_libraries += note_memtag_heap_async
229 endif
Evgenii Stepanov3330b2f2021-01-13 12:14:55 -0800230 endif
231 # This is all that memtag_heap does - it is not an actual -fsanitize argument.
232 # Remove it from the list.
233 my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
234endif
235
236my_sanitize_diag := $(filter-out memtag_heap,$(my_sanitize_diag))
237
Dan Alberta6311b72015-07-30 10:17:33 -0700238# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
239# its use an error. For multilib cases, don't use it for the 32-bit case.
240ifneq ($(filter thread,$(my_sanitize)),)
241 ifeq ($(my_32_64_bit_suffix),32)
242 ifeq ($(my_module_multilib),both)
243 my_sanitize := $(filter-out thread,$(my_sanitize))
244 else
245 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
246 endif
Yabin Cuie77c32e2017-10-19 14:33:58 -0700247 else
248 my_shared_libraries += $(TSAN_RUNTIME_LIBRARY)
Dan Alberta6311b72015-07-30 10:17:33 -0700249 endif
250endif
251
Evgenii Stepanov7dcb8b82016-05-06 18:15:57 -0700252ifneq ($(filter safe-stack,$(my_sanitize)),)
253 ifeq ($(my_32_64_bit_suffix),32)
254 my_sanitize := $(filter-out safe-stack,$(my_sanitize))
255 endif
256endif
257
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700258# Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700259ifneq ($(filter address thread hwaddress,$(my_sanitize)),)
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700260 my_sanitize := $(filter-out scudo,$(my_sanitize))
261endif
262
Kostya Kortchinsky02732402019-02-01 09:06:42 -0800263# Or if disabled globally.
Anton Hansson8dab0a62019-03-28 15:45:40 +0000264ifeq ($(PRODUCT_DISABLE_SCUDO),true)
Kostya Kortchinsky02732402019-02-01 09:06:42 -0800265 my_sanitize := $(filter-out scudo,$(my_sanitize))
266endif
267
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700268# Undefined symbols can occur if a non-sanitized library links
269# sanitized static libraries. That's OK, because the executable
270# always depends on the ASan runtime library, which defines these
271# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -0700272ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700273 ifndef LOCAL_IS_HOST_MODULE
274 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
275 ifeq ($(my_sanitize),)
276 my_allow_undefined_symbols := true
277 endif
278 endif
279 endif
280endif
281
Dan Albertb5b2ffe2015-04-16 18:07:07 -0700282ifneq ($(filter default-ub,$(my_sanitize)),)
283 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -0800284endif
285
Mitch Phillipsee8f4a02019-05-01 14:37:33 -0700286ifneq ($(filter fuzzer,$(my_sanitize)),)
287 # SANITIZE_TARGET='fuzzer' actually means to create the fuzzer coverage
288 # information, not to link against the fuzzer main().
289 my_sanitize := $(filter-out fuzzer,$(my_sanitize))
290 my_sanitize += fuzzer-no-link
291
292 # TODO(b/131771163): Disable LTO for fuzzer builds. Note that Cfi causes
293 # dependency on LTO.
294 my_sanitize := $(filter-out cfi,$(my_sanitize))
295 my_cflags += -fno-lto
296 my_ldflags += -fno-lto
Mitch Phillipsee2dcac2019-06-17 10:37:55 -0700297
298 # TODO(b/133876586): Disable experimental pass manager for fuzzer builds.
299 my_cflags += -fno-experimental-new-pass-manager
Ivan Krasin74b32b82015-09-18 11:54:43 -0700300endif
301
Ivan Lozano4a363732017-06-28 09:11:26 -0700302ifneq ($(filter integer_overflow,$(my_sanitize)),)
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700303 # Respect LOCAL_NOSANITIZE for integer-overflow flags.
304 ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
305 my_sanitize += signed-integer-overflow
306 endif
307 ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
308 my_sanitize += unsigned-integer-overflow
309 endif
310 my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS)
Ivan Lozano4a363732017-06-28 09:11:26 -0700311
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700312 # Check for diagnostics mode.
313 ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
314 ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
315 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
Ivan Lozano911cb992018-02-21 13:41:05 -0800316 my_sanitize_diag += signed-integer-overflow
317 my_sanitize_diag += unsigned-integer-overflow
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700318 else
319 $(call pretty-error,Make cannot apply integer overflow diagnostics to static binary.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700320 endif
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700321 else
322 $(call pretty-error,Make cannot apply integer overflow diagnostics to static library.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700323 endif
324 endif
325 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
326endif
327
328# Makes sure integer_overflow diagnostics is removed from the diagnostics list
329# even if integer_overflow is not set for some reason.
330ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
331 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
332endif
333
Dan Albert08cca282014-12-11 18:56:26 -0800334ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800335 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800336 my_cflags += -fsanitize=$(fsanitize_arg)
Evgenii Stepanov9b82b3f2018-08-31 12:57:26 -0700337 my_asflags += -fsanitize=$(fsanitize_arg)
Dan Albert08cca282014-12-11 18:56:26 -0800338
Mitch Phillipsee8f4a02019-05-01 14:37:33 -0700339 # When fuzzing, we wish to crash with diagnostics on any bug.
340 ifneq ($(filter fuzzer-no-link,$(my_sanitize)),)
341 my_cflags += -fno-sanitize-trap=all
342 my_cflags += -fno-sanitize-recover=all
343 my_ldflags += -fsanitize=fuzzer-no-link
344 else ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700345 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800346 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albertabf4bc92015-06-16 23:27:34 -0700347 else
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700348 my_cflags += -fsanitize-trap=all
349 my_cflags += -ftrap-function=abort
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700350 ifneq ($(filter address thread,$(my_sanitize)),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700351 my_cflags += -fno-sanitize-trap=address,thread
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700352 my_shared_libraries += libdl
353 endif
Dan Albert08cca282014-12-11 18:56:26 -0800354 endif
355endif
356
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700357ifneq ($(filter cfi,$(my_sanitize)),)
Evgenii Stepanov81bea1b2017-01-20 14:12:08 -0800358 # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp).
359 # LLVM is not set up to do this on a function basis, so force Thumb on the
360 # entire module.
361 LOCAL_ARM_MODE := thumb
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800362 my_cflags += $(CFI_EXTRA_CFLAGS)
Evgenii Stepanov9b82b3f2018-08-31 12:57:26 -0700363 my_asflags += $(CFI_EXTRA_ASFLAGS)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000364 # Only append the default visibility flag if -fvisibility has not already been
365 # set to hidden.
366 ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),)
367 my_cflags += -fvisibility=default
368 endif
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800369 my_ldflags += $(CFI_EXTRA_LDFLAGS)
Evgenii Stepanove1b96f32017-01-23 16:57:38 -0800370 my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so
Vishwath Mohan85f72442017-11-01 09:21:20 +0000371
372 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
373 my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags))
374 my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags))
375 else
376 # Apply the version script to non-static executables
377 my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map
378 LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map
379 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700380endif
381
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800382# If local or global modules need ASAN, add linker flags.
383ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700384 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
385 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800386 # -nodefaultlibs (provided with libc++) prevents the driver from linking
387 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albert1f0d5302015-04-28 14:55:50 -0700388 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700389 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800390 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700391 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800392 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
393 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
394 $(my_shared_libraries)
395 endif
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800396
397 # Do not add unnecessary dependency in shared libraries.
398 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
399 my_ldflags += -Wl,--as-needed
400 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700401
Mikhail Naganovaa73cef2018-12-20 15:55:08 -0800402 ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
Colin Crossd08699e2016-07-17 15:28:07 -0700403 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
404 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
405 # Make sure linker_asan get installed.
Logan Chienc6d2cf82019-01-31 17:07:50 +0800406 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER_FILE)
Colin Crossd08699e2016-07-17 15:28:07 -0700407 endif
408 endif
Dan Albert08cca282014-12-11 18:56:26 -0800409 endif
410endif
411
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800412# If local module needs ASAN, add compiler flags.
413ifneq ($(filter address,$(my_sanitize)),)
414 # Frame pointer based unwinder in ASan requires ARM frame setup.
415 LOCAL_ARM_MODE := arm
416 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
417 ifndef LOCAL_IS_HOST_MODULE
418 my_cflags += -mllvm -asan-globals=0
419 endif
420endif
421
Evgenii Stepanovaec1ffc2018-08-28 13:52:08 -0700422# If local module needs HWASAN, add compiler flags.
423ifneq ($(filter hwaddress,$(my_sanitize)),)
424 my_cflags += $(HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
425endif
426
Dan Willemsen58634e12020-03-06 18:09:06 -0800427# Use minimal diagnostics when integer overflow is enabled; never do it for HOST modules
428ifeq ($(LOCAL_IS_HOST_MODULE),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800429 # Pre-emptively add UBSAN minimal runtime incase a static library dependency requires it
430 ifeq ($(filter STATIC_LIBRARIES,$(LOCAL_MODULE_CLASS)),)
431 ifndef LOCAL_SDK_VERSION
432 my_static_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY)
Ivan Lozanoe5081692018-05-11 14:09:36 -0700433 my_ldflags += -Wl,--exclude-libs,$($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY).a
Ivan Lozano911cb992018-02-21 13:41:05 -0800434 endif
435 endif
436 ifneq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize)),)
Kostya Kortchinsky47c10eb2018-10-11 08:56:12 -0700437 ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize_diag)),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800438 ifeq ($(filter cfi,$(my_sanitize_diag)),)
Mitch Phillipsee8f4a02019-05-01 14:37:33 -0700439 ifeq ($(filter address hwaddress fuzzer-no-link,$(my_sanitize)),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800440 my_cflags += -fsanitize-minimal-runtime
441 my_cflags += -fno-sanitize-trap=integer
442 my_cflags += -fno-sanitize-recover=integer
443 endif
444 endif
445 endif
446 endif
447endif
448
Kostya Kortchinsky47c10eb2018-10-11 08:56:12 -0700449# For Scudo, we opt for the minimal runtime, unless some diagnostics are enabled.
450ifneq ($(filter scudo,$(my_sanitize)),)
451 ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer cfi,$(my_sanitize_diag)),)
452 my_cflags += -fsanitize-minimal-runtime
453 endif
454 ifneq ($(filter -fsanitize-minimal-runtime,$(my_cflags)),)
455 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_MINIMAL_RUNTIME_LIBRARY)
456 else
457 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_RUNTIME_LIBRARY)
458 endif
459endif
460
Dan Albert9f176552015-04-28 11:26:45 -0700461ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
462 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700463 my_cflags += -fsanitize-recover=$(recover_arg)
464endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700465
Ivan Lozano5fb2de72018-12-12 10:04:34 -0800466ifneq ($(strip $(LOCAL_SANITIZE_NO_RECOVER)),)
467 no_recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_NO_RECOVER)),
468 my_cflags += -fno-sanitize-recover=$(no_recover_arg)
469endif
470
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800471ifneq ($(my_sanitize_diag),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000472 # TODO(vishwath): Add diagnostic support for static executables once
473 # we switch to clang-4393122 (which adds the static ubsan runtime
474 # that this depends on)
475 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
476 notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
477 my_cflags += -fno-sanitize-trap=$(notrap_arg)
478 # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700479 ifeq ($(filter address thread scudo hwaddress,$(my_sanitize)),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000480 # Does not have to be the first DT_NEEDED unlike ASan.
481 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
482 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700483 endif
484endif
Chih-Hung Hsieh18710622018-11-17 20:18:08 -0800485
486# http://b/119329758, Android core does not boot up with this sanitizer yet.
487# Previously sanitized modules might not pass new implicit-integer-sign-change check.
488# Disable this check unless it has been explicitly specified.
489ifneq ($(findstring fsanitize,$(my_cflags)),)
490 ifneq ($(findstring integer,$(my_cflags)),)
491 ifeq ($(findstring sanitize=implicit-integer-sign-change,$(my_cflags)),)
492 my_cflags += -fno-sanitize=implicit-integer-sign-change
493 endif
494 endif
495endif
Yabin Cui462c12d2021-01-14 14:05:54 -0800496
497# http://b/177566116, libc++ may crash with this sanitizer.
498# Disable this check unless it has been explicitly specified.
499ifneq ($(findstring fsanitize,$(my_cflags)),)
500 ifneq ($(findstring integer,$(my_cflags)),)
501 ifeq ($(findstring sanitize=unsigned-shift-base,$(my_cflags)),)
502 my_cflags += -fno-sanitize=unsigned-shift-base
503 endif
504 endif
505endif