• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1##############################################
2## Perform configuration steps for sanitizers.
3##############################################
4
5my_sanitize := $(strip $(LOCAL_SANITIZE))
6my_sanitize_diag := $(strip $(LOCAL_SANITIZE_DIAG))
7
8my_global_sanitize :=
9my_global_sanitize_diag :=
10ifdef LOCAL_IS_HOST_MODULE
11  ifneq ($($(my_prefix)OS),windows)
12    my_global_sanitize := $(strip $(SANITIZE_HOST))
13
14    # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
15    my_global_sanitize := $(subst true,address,$(my_global_sanitize))
16  endif
17else
18  my_global_sanitize := $(strip $(SANITIZE_TARGET))
19  my_global_sanitize_diag := $(strip $(SANITIZE_TARGET_DIAG))
20endif
21
22# 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
34# 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
44# 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
56# 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
68ifneq ($(my_global_sanitize),)
69  my_sanitize := $(my_global_sanitize) $(my_sanitize)
70endif
71ifneq ($(my_global_sanitize_diag),)
72  my_sanitize_diag := $(my_global_sanitize_diag) $(my_sanitize_diag)
73endif
74
75# 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 :=
80    my_sanitize_diag :=
81  endif
82endif
83
84ifndef 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 :=
89    my_sanitize_diag :=
90  endif
91endif
92
93# 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 :=
100      my_sanitize_diag :=
101    endif
102  endif
103endif
104
105# Don't apply sanitizers to NDK code.
106ifdef LOCAL_SDK_VERSION
107  my_sanitize :=
108  my_global_sanitize :=
109  my_sanitize_diag :=
110endif
111
112# Never always wins.
113ifeq ($(LOCAL_SANITIZE),never)
114  my_sanitize :=
115  my_sanitize_diag :=
116endif
117
118# Enable CFI in included paths.
119ifeq ($(filter cfi, $(my_sanitize)),)
120  combined_include_paths := $(CFI_INCLUDE_PATHS) \
121                            $(PRODUCT_CFI_INCLUDE_PATHS)
122  combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \
123                            $(PRODUCT_CFI_EXCLUDE_PATHS)
124
125  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)
130    endif
131  endif
132endif
133
134# 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)
141    combined_exclude_paths := $(MEMTAG_HEAP_EXCLUDE_PATHS) \
142                              $(PRODUCT_MEMTAG_HEAP_EXCLUDE_PATHS)
143    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
147
148    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
158    endif
159  endif
160endif
161
162# 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
173# If CFI is disabled globally, remove it from my_sanitize.
174ifeq ($(strip $(ENABLE_CFI)),false)
175  my_sanitize := $(filter-out cfi,$(my_sanitize))
176  my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
177endif
178
179# Also disable CFI and MTE if ASAN is enabled.
180ifneq ($(filter address,$(my_sanitize)),)
181  my_sanitize := $(filter-out cfi,$(my_sanitize))
182  my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
183  my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
184  my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
185endif
186
187# Disable memtag for host targets. Host executables in AndroidMk files are
188# deprecated, but some partners still have them floating around.
189ifdef LOCAL_IS_HOST_MODULE
190  my_sanitize := $(filter-out memtag_heap memtag_stack,$(my_sanitize))
191  my_sanitize_diag := $(filter-out memtag_heap memtag_stack,$(my_sanitize_diag))
192endif
193
194# Disable sanitizers which need the UBSan runtime for host targets.
195ifdef LOCAL_IS_HOST_MODULE
196  my_sanitize := $(filter-out cfi,$(my_sanitize))
197  my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
198  my_sanitize := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize))
199  my_sanitize_diag := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize_diag))
200endif
201
202# Support for local sanitize blacklist paths.
203ifneq ($(my_sanitize)$(my_global_sanitize),)
204  ifneq ($(LOCAL_SANITIZE_BLOCKLIST),)
205    my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLOCKLIST)
206  endif
207endif
208
209# Disable integer_overflow if LOCAL_NOSANITIZE=integer.
210ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),)
211  ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),)
212    my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
213    my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
214  endif
215endif
216
217my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
218ifneq ($(my_nosanitize),)
219  my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
220endif
221
222ifneq ($(filter arm x86 x86_64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
223  my_sanitize := $(filter-out hwaddress,$(my_sanitize))
224  my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
225  my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
226endif
227
228ifneq ($(filter hwaddress,$(my_sanitize)),)
229  my_sanitize := $(filter-out address,$(my_sanitize))
230  my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
231  my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
232  my_sanitize := $(filter-out thread,$(my_sanitize))
233  my_sanitize := $(filter-out cfi,$(my_sanitize))
234endif
235
236ifneq ($(filter hwaddress,$(my_sanitize)),)
237  my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_RUNTIME_LIBRARY)
238  ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
239    ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
240      my_static_libraries := $(my_static_libraries) \
241                             $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_STATIC_LIBRARY) \
242                             libdl
243    endif
244  endif
245endif
246
247ifneq ($(filter memtag_heap memtag_stack,$(my_sanitize)),)
248  ifneq ($(filter memtag_heap,$(my_sanitize_diag)),)
249    my_cflags += -fsanitize-memtag-mode=sync
250    my_sanitize_diag := $(filter-out memtag_heap,$(my_sanitize_diag))
251  else
252    my_cflags += -fsanitize-memtag-mode=async
253  endif
254endif
255
256# Ignore SANITIZE_TARGET_DIAG=memtag_heap without SANITIZE_TARGET=memtag_heap
257# This can happen if a condition above filters out memtag_heap from
258# my_sanitize. It is easier to handle all of these cases here centrally.
259ifneq ($(filter memtag_heap,$(my_sanitize_diag)),)
260  my_sanitize_diag := $(filter-out memtag_heap,$(my_sanitize_diag))
261endif
262
263ifneq ($(filter memtag_heap,$(my_sanitize)),)
264  my_cflags += -fsanitize=memtag-heap
265  my_sanitize := $(filter-out memtag_heap,$(my_sanitize))
266endif
267
268ifneq ($(filter memtag_stack,$(my_sanitize)),)
269  my_cflags += -fsanitize=memtag-stack
270  my_cflags += -march=armv8a+memtag
271  my_ldflags += -march=armv8a+memtag
272  my_asflags += -march=armv8a+memtag
273  my_sanitize := $(filter-out memtag_stack,$(my_sanitize))
274endif
275
276# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
277# its use an error. For multilib cases, don't use it for the 32-bit case.
278ifneq ($(filter thread,$(my_sanitize)),)
279  ifeq ($(my_32_64_bit_suffix),32)
280    ifeq ($(my_module_multilib),both)
281        my_sanitize := $(filter-out thread,$(my_sanitize))
282    else
283        $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
284    endif
285  else
286    my_shared_libraries += $(TSAN_RUNTIME_LIBRARY)
287  endif
288endif
289
290ifneq ($(filter safe-stack,$(my_sanitize)),)
291  ifeq ($(my_32_64_bit_suffix),32)
292    my_sanitize := $(filter-out safe-stack,$(my_sanitize))
293  endif
294endif
295
296# Disable Scudo if ASan or TSan is enabled.
297ifneq ($(filter address thread hwaddress,$(my_sanitize)),)
298  my_sanitize := $(filter-out scudo,$(my_sanitize))
299endif
300
301# Or if disabled globally.
302ifeq ($(PRODUCT_DISABLE_SCUDO),true)
303  my_sanitize := $(filter-out scudo,$(my_sanitize))
304endif
305
306# Undefined symbols can occur if a non-sanitized library links
307# sanitized static libraries. That's OK, because the executable
308# always depends on the ASan runtime library, which defines these
309# symbols.
310ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
311  ifndef LOCAL_IS_HOST_MODULE
312    ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
313      ifeq ($(my_sanitize),)
314        my_allow_undefined_symbols := true
315      endif
316    endif
317  endif
318endif
319
320ifneq ($(filter default-ub,$(my_sanitize)),)
321  my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
322endif
323
324ifneq ($(filter fuzzer,$(my_sanitize)),)
325  # SANITIZE_TARGET='fuzzer' actually means to create the fuzzer coverage
326  # information, not to link against the fuzzer main().
327  my_sanitize := $(filter-out fuzzer,$(my_sanitize))
328  my_sanitize += fuzzer-no-link
329
330  # TODO(b/131771163): Disable LTO for fuzzer builds. Note that Cfi causes
331  # dependency on LTO.
332  my_sanitize := $(filter-out cfi,$(my_sanitize))
333  my_cflags += -fno-lto
334  my_ldflags += -fno-lto
335endif
336
337ifneq ($(filter integer_overflow,$(my_sanitize)),)
338  # Respect LOCAL_NOSANITIZE for integer-overflow flags.
339  ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
340    my_sanitize += signed-integer-overflow
341  endif
342  ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
343    my_sanitize += unsigned-integer-overflow
344  endif
345  my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS)
346
347  # Check for diagnostics mode.
348  ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
349    ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
350      ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
351        my_sanitize_diag += signed-integer-overflow
352        my_sanitize_diag += unsigned-integer-overflow
353      else
354        $(call pretty-error,Make cannot apply integer overflow diagnostics to static binary.)
355      endif
356    else
357      $(call pretty-error,Make cannot apply integer overflow diagnostics to static library.)
358    endif
359  endif
360  my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
361endif
362
363# Makes sure integer_overflow diagnostics is removed from the diagnostics list
364# even if integer_overflow is not set for some reason.
365ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
366  my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
367endif
368
369ifneq ($(my_sanitize),)
370  fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
371  my_cflags += -fsanitize=$(fsanitize_arg)
372  my_asflags += -fsanitize=$(fsanitize_arg)
373
374  # When fuzzing, we wish to crash with diagnostics on any bug.
375  ifneq ($(filter fuzzer-no-link,$(my_sanitize)),)
376    my_cflags += -fno-sanitize-trap=all
377    my_cflags += -fno-sanitize-recover=all
378    my_ldflags += -fsanitize=fuzzer-no-link
379  else ifdef LOCAL_IS_HOST_MODULE
380    my_cflags += -fno-sanitize-recover=all
381    my_ldflags += -fsanitize=$(fsanitize_arg)
382  else
383    my_cflags += -fsanitize-trap=all
384    my_cflags += -ftrap-function=abort
385    ifneq ($(filter address thread,$(my_sanitize)),)
386      my_cflags += -fno-sanitize-trap=address,thread
387      my_shared_libraries += libdl
388    endif
389  endif
390endif
391
392ifneq ($(filter cfi,$(my_sanitize)),)
393  # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp).
394  # LLVM is not set up to do this on a function basis, so force Thumb on the
395  # entire module.
396  LOCAL_ARM_MODE := thumb
397  my_cflags += $(CFI_EXTRA_CFLAGS)
398  my_asflags += $(CFI_EXTRA_ASFLAGS)
399  # Only append the default visibility flag if -fvisibility has not already been
400  # set to hidden.
401  ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),)
402    my_cflags += -fvisibility=default
403  endif
404  my_ldflags += $(CFI_EXTRA_LDFLAGS)
405  my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so
406
407  ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
408        my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags))
409        my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags))
410  else
411        # Apply the version script to non-static executables
412        my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map
413        LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map
414  endif
415endif
416
417# If local or global modules need ASAN, add linker flags.
418ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
419  my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
420  ifdef LOCAL_IS_HOST_MODULE
421    # -nodefaultlibs (provided with libc++) prevents the driver from linking
422    # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
423    my_ldflags += -Wl,--no-as-needed
424  else
425    # Add asan libraries unless LOCAL_MODULE is the asan library.
426    # ASan runtime library must be the first in the link order.
427    ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
428      my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
429                             $(my_shared_libraries)
430    endif
431
432    # Do not add unnecessary dependency in shared libraries.
433    ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
434      my_ldflags += -Wl,--as-needed
435    endif
436
437    ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),)
438      ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
439        my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
440        # Make sure linker_asan get installed.
441        $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER_FILE)
442      endif
443    endif
444  endif
445endif
446
447# If local module needs ASAN, add compiler flags.
448ifneq ($(filter address,$(my_sanitize)),)
449  # Frame pointer based unwinder in ASan requires ARM frame setup.
450  LOCAL_ARM_MODE := arm
451  my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
452  ifndef LOCAL_IS_HOST_MODULE
453    my_cflags += -mllvm -asan-globals=0
454  endif
455endif
456
457# If local module needs HWASAN, add compiler flags.
458ifneq ($(filter hwaddress,$(my_sanitize)),)
459  my_cflags += $(HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
460endif
461
462# Use minimal diagnostics when integer overflow is enabled; never do it for HOST modules
463ifeq ($(LOCAL_IS_HOST_MODULE),)
464  # Pre-emptively add UBSAN minimal runtime incase a static library dependency requires it
465  ifeq ($(filter STATIC_LIBRARIES,$(LOCAL_MODULE_CLASS)),)
466    ifndef LOCAL_SDK_VERSION
467      my_static_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY)
468      my_ldflags += -Wl,--exclude-libs,$($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY).a
469    endif
470  endif
471  ifneq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize)),)
472    ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize_diag)),)
473      ifeq ($(filter cfi,$(my_sanitize_diag)),)
474        ifeq ($(filter address hwaddress fuzzer-no-link,$(my_sanitize)),)
475          my_cflags += -fsanitize-minimal-runtime
476          my_cflags += -fno-sanitize-trap=integer
477          my_cflags += -fno-sanitize-recover=integer
478        endif
479      endif
480    endif
481  endif
482endif
483
484# For Scudo, we opt for the minimal runtime, unless some diagnostics are enabled.
485ifneq ($(filter scudo,$(my_sanitize)),)
486  ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer cfi,$(my_sanitize_diag)),)
487    my_cflags += -fsanitize-minimal-runtime
488  endif
489  ifneq ($(filter -fsanitize-minimal-runtime,$(my_cflags)),)
490    my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_MINIMAL_RUNTIME_LIBRARY)
491  else
492    my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_RUNTIME_LIBRARY)
493  endif
494endif
495
496ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
497  recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
498  my_cflags += -fsanitize-recover=$(recover_arg)
499endif
500
501ifneq ($(strip $(LOCAL_SANITIZE_NO_RECOVER)),)
502  no_recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_NO_RECOVER)),
503  my_cflags += -fno-sanitize-recover=$(no_recover_arg)
504endif
505
506ifneq ($(my_sanitize_diag),)
507  # TODO(vishwath): Add diagnostic support for static executables once
508  # we switch to clang-4393122 (which adds the static ubsan runtime
509  # that this depends on)
510  ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
511    notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
512    my_cflags += -fno-sanitize-trap=$(notrap_arg)
513    # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
514    ifeq ($(filter address thread scudo hwaddress,$(my_sanitize)),)
515      # Does not have to be the first DT_NEEDED unlike ASan.
516      my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
517    endif
518  endif
519endif
520
521# http://b/119329758, Android core does not boot up with this sanitizer yet.
522# Previously sanitized modules might not pass new implicit-integer-sign-change check.
523# Disable this check unless it has been explicitly specified.
524ifneq ($(findstring fsanitize,$(my_cflags)),)
525  ifneq ($(findstring integer,$(my_cflags)),)
526    ifeq ($(findstring sanitize=implicit-integer-sign-change,$(my_cflags)),)
527      my_cflags += -fno-sanitize=implicit-integer-sign-change
528    endif
529  endif
530endif
531
532# http://b/177566116, libc++ may crash with this sanitizer.
533# Disable this check unless it has been explicitly specified.
534ifneq ($(findstring fsanitize,$(my_cflags)),)
535  ifneq ($(findstring integer,$(my_cflags)),)
536    ifeq ($(findstring sanitize=unsigned-shift-base,$(my_cflags)),)
537      my_cflags += -fno-sanitize=unsigned-shift-base
538    endif
539  endif
540endif
541