2014-11-01 07:23:08 +08:00
|
|
|
##############################################
|
|
|
|
## Perform configuration steps for sanitizers.
|
|
|
|
##############################################
|
|
|
|
|
2015-04-17 07:21:02 +08:00
|
|
|
my_sanitize := $(strip $(LOCAL_SANITIZE))
|
2017-01-19 09:50:29 +08:00
|
|
|
my_sanitize_diag := $(strip $(LOCAL_SANITIZE_DIAG))
|
2014-12-12 10:56:26 +08:00
|
|
|
|
2015-08-20 11:13:33 +08:00
|
|
|
# SANITIZE_HOST is only in effect if the module is already using clang (host
|
|
|
|
# modules that haven't set `LOCAL_CLANG := false` and device modules that
|
|
|
|
# have set `LOCAL_CLANG := true`.
|
|
|
|
my_global_sanitize :=
|
|
|
|
ifeq ($(my_clang),true)
|
2015-06-12 04:57:10 +08:00
|
|
|
ifdef LOCAL_IS_HOST_MODULE
|
2015-08-20 11:13:33 +08:00
|
|
|
my_global_sanitize := $(strip $(SANITIZE_HOST))
|
|
|
|
|
|
|
|
# SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
|
|
|
|
my_global_sanitize := $(subst true,address,$(my_global_sanitize))
|
2015-06-12 04:57:10 +08:00
|
|
|
else
|
2015-08-20 11:13:33 +08:00
|
|
|
my_global_sanitize := $(strip $(SANITIZE_TARGET))
|
2015-06-12 04:57:10 +08:00
|
|
|
endif
|
2015-08-20 11:13:33 +08:00
|
|
|
endif
|
2015-04-18 02:04:06 +08:00
|
|
|
|
2015-08-20 11:13:33 +08:00
|
|
|
ifneq ($(my_global_sanitize),)
|
2016-05-20 08:45:21 +08:00
|
|
|
my_sanitize := $(my_global_sanitize) $(my_sanitize)
|
2015-08-20 11:13:33 +08:00
|
|
|
endif
|
2015-04-18 02:04:06 +08:00
|
|
|
|
2016-06-28 06:15:31 +08:00
|
|
|
# The sanitizer specified in the product configuration wins over the previous.
|
|
|
|
ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),)
|
|
|
|
my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
|
|
|
|
ifeq ($(my_sanitize),never)
|
|
|
|
my_sanitize :=
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2016-11-03 06:05:21 +08:00
|
|
|
ifndef LOCAL_IS_HOST_MODULE
|
|
|
|
# Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden)
|
|
|
|
SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH)
|
|
|
|
ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
|
|
|
|
my_sanitize :=
|
|
|
|
endif
|
2016-06-21 08:36:49 +08:00
|
|
|
endif
|
|
|
|
|
2016-06-21 08:46:29 +08:00
|
|
|
# Add a filter point based on module owner (to lighten the burden). The format is a space- or
|
|
|
|
# colon-separated list of owner names.
|
|
|
|
ifneq (,$(SANITIZE_NEVER_BY_OWNER))
|
|
|
|
ifneq (,$(LOCAL_MODULE_OWNER))
|
|
|
|
ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
|
|
|
|
$(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
|
|
|
|
my_sanitize :=
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2015-08-20 11:13:33 +08:00
|
|
|
# Don't apply sanitizers to NDK code.
|
|
|
|
ifdef LOCAL_SDK_VERSION
|
|
|
|
my_sanitize :=
|
2016-06-29 07:47:43 +08:00
|
|
|
my_global_sanitize :=
|
2015-04-17 07:21:02 +08:00
|
|
|
endif
|
|
|
|
|
2015-08-20 11:13:33 +08:00
|
|
|
# Never always wins.
|
|
|
|
ifeq ($(LOCAL_SANITIZE),never)
|
2014-12-12 10:56:26 +08:00
|
|
|
my_sanitize :=
|
|
|
|
endif
|
|
|
|
|
2017-01-19 09:50:29 +08:00
|
|
|
# If CFI is disabled globally, remove it from my_sanitize.
|
|
|
|
ifeq ($(strip $(ENABLE_CFI)),)
|
|
|
|
my_sanitize := $(filter-out cfi,$(my_sanitize))
|
|
|
|
my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
|
|
|
|
endif
|
|
|
|
|
2016-05-13 04:07:17 +08:00
|
|
|
my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
|
|
|
|
ifneq ($(my_nosanitize),)
|
|
|
|
my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
|
|
|
|
endif
|
|
|
|
|
2015-07-31 01:17:33 +08:00
|
|
|
# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
|
|
|
|
# its use an error. For multilib cases, don't use it for the 32-bit case.
|
|
|
|
ifneq ($(filter thread,$(my_sanitize)),)
|
|
|
|
ifeq ($(my_32_64_bit_suffix),32)
|
|
|
|
ifeq ($(my_module_multilib),both)
|
|
|
|
my_sanitize := $(filter-out thread,$(my_sanitize))
|
|
|
|
else
|
|
|
|
$(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2016-05-07 09:15:57 +08:00
|
|
|
ifneq ($(filter safe-stack,$(my_sanitize)),)
|
|
|
|
ifeq ($(my_32_64_bit_suffix),32)
|
|
|
|
my_sanitize := $(filter-out safe-stack,$(my_sanitize))
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2015-06-26 07:38:25 +08:00
|
|
|
# Undefined symbols can occur if a non-sanitized library links
|
|
|
|
# sanitized static libraries. That's OK, because the executable
|
|
|
|
# always depends on the ASan runtime library, which defines these
|
|
|
|
# symbols.
|
2016-05-20 08:49:51 +08:00
|
|
|
ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
|
2015-06-26 07:38:25 +08:00
|
|
|
ifndef LOCAL_IS_HOST_MODULE
|
|
|
|
ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
|
|
|
|
ifeq ($(my_sanitize),)
|
|
|
|
my_allow_undefined_symbols := true
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2015-04-18 00:48:33 +08:00
|
|
|
# Sanitizers can only be used with clang.
|
|
|
|
ifneq ($(my_clang),true)
|
|
|
|
ifneq ($(my_sanitize),)
|
|
|
|
$(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2015-04-17 09:07:07 +08:00
|
|
|
ifneq ($(filter default-ub,$(my_sanitize)),)
|
|
|
|
my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
|
2014-12-12 10:56:26 +08:00
|
|
|
endif
|
|
|
|
|
2015-09-19 02:54:43 +08:00
|
|
|
ifneq ($(filter coverage,$(my_sanitize)),)
|
|
|
|
ifeq ($(filter address,$(my_sanitize)),)
|
|
|
|
$(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
|
|
|
|
endif
|
|
|
|
my_cflags += -fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp
|
|
|
|
my_sanitize := $(filter-out coverage,$(my_sanitize))
|
|
|
|
endif
|
|
|
|
|
2014-12-12 10:56:26 +08:00
|
|
|
ifneq ($(my_sanitize),)
|
2015-11-10 08:32:11 +08:00
|
|
|
fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
|
2014-12-12 10:56:26 +08:00
|
|
|
my_cflags += -fsanitize=$(fsanitize_arg)
|
|
|
|
|
|
|
|
ifdef LOCAL_IS_HOST_MODULE
|
2015-06-17 14:27:34 +08:00
|
|
|
my_cflags += -fno-sanitize-recover=all
|
2014-12-12 10:56:26 +08:00
|
|
|
my_ldflags += -fsanitize=$(fsanitize_arg)
|
2015-08-14 08:25:10 +08:00
|
|
|
my_ldlibs += -lrt -ldl
|
2015-06-17 14:27:34 +08:00
|
|
|
else
|
2016-05-20 08:45:21 +08:00
|
|
|
my_cflags += -fsanitize-trap=all
|
|
|
|
my_cflags += -ftrap-function=abort
|
2016-05-13 04:07:36 +08:00
|
|
|
ifneq ($(filter address thread,$(my_sanitize)),)
|
2016-05-20 08:45:21 +08:00
|
|
|
my_cflags += -fno-sanitize-trap=address,thread
|
2016-05-13 04:07:36 +08:00
|
|
|
my_shared_libraries += libdl
|
|
|
|
endif
|
2014-12-12 10:56:26 +08:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2016-07-08 01:56:39 +08:00
|
|
|
ifneq ($(filter cfi,$(my_sanitize)),)
|
|
|
|
my_cflags += -flto -fsanitize-cfi-cross-dso -fvisibility=default
|
|
|
|
my_ldflags += -flto -fsanitize-cfi-cross-dso -fsanitize=cfi -Wl,-plugin-opt,O1 -Wl,-export-dynamic-symbol=__cfi_check
|
|
|
|
endif
|
|
|
|
|
2016-03-10 06:54:55 +08:00
|
|
|
# If local or global modules need ASAN, add linker flags.
|
|
|
|
ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
|
2014-11-01 07:23:08 +08:00
|
|
|
my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
|
|
|
|
ifdef LOCAL_IS_HOST_MODULE
|
2014-12-12 10:56:26 +08:00
|
|
|
# -nodefaultlibs (provided with libc++) prevents the driver from linking
|
|
|
|
# libraries needed with -fsanitize=address. http://b/18650275 (WAI)
|
2015-06-17 14:27:34 +08:00
|
|
|
my_ldlibs += -lm -lpthread
|
2015-04-29 05:55:50 +08:00
|
|
|
my_ldflags += -Wl,--no-as-needed
|
2014-12-12 10:56:26 +08:00
|
|
|
else
|
2016-03-10 06:54:55 +08:00
|
|
|
# Add asan libraries unless LOCAL_MODULE is the asan library.
|
2015-04-25 07:34:47 +08:00
|
|
|
# ASan runtime library must be the first in the link order.
|
2016-03-10 06:54:55 +08:00
|
|
|
ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
|
|
|
|
my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
|
|
|
|
$(my_shared_libraries)
|
|
|
|
endif
|
|
|
|
ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
|
|
|
|
my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
|
|
|
|
endif
|
|
|
|
|
|
|
|
# Do not add unnecessary dependency in shared libraries.
|
|
|
|
ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
|
|
|
|
my_ldflags += -Wl,--as-needed
|
|
|
|
endif
|
2015-08-18 07:13:24 +08:00
|
|
|
|
2016-07-18 06:28:07 +08:00
|
|
|
ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES)
|
|
|
|
ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
|
|
|
|
my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
|
|
|
|
# Make sure linker_asan get installed.
|
|
|
|
$(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
|
|
|
|
endif
|
|
|
|
endif
|
2014-12-12 10:56:26 +08:00
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2016-03-10 06:54:55 +08:00
|
|
|
# If local module needs ASAN, add compiler flags.
|
|
|
|
ifneq ($(filter address,$(my_sanitize)),)
|
|
|
|
# Frame pointer based unwinder in ASan requires ARM frame setup.
|
|
|
|
LOCAL_ARM_MODE := arm
|
|
|
|
my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
|
|
|
|
ifndef LOCAL_IS_HOST_MODULE
|
|
|
|
my_cflags += -mllvm -asan-globals=0
|
|
|
|
endif
|
|
|
|
endif
|
|
|
|
|
2014-12-12 10:56:26 +08:00
|
|
|
ifneq ($(filter undefined,$(my_sanitize)),)
|
2015-06-17 14:27:34 +08:00
|
|
|
ifndef LOCAL_IS_HOST_MODULE
|
2014-12-12 10:56:26 +08:00
|
|
|
$(error ubsan is not yet supported on the target)
|
2014-11-01 07:23:08 +08:00
|
|
|
endif
|
|
|
|
endif
|
2015-04-17 09:08:44 +08:00
|
|
|
|
2015-04-29 02:26:45 +08:00
|
|
|
ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
|
|
|
|
recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
|
2015-04-17 09:08:44 +08:00
|
|
|
my_cflags += -fsanitize-recover=$(recover_arg)
|
|
|
|
endif
|
2016-07-08 01:56:39 +08:00
|
|
|
|
2017-01-19 09:50:29 +08:00
|
|
|
ifneq ($(my_sanitize_diag),)
|
|
|
|
notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
|
2016-07-08 01:56:39 +08:00
|
|
|
my_cflags += -fno-sanitize-trap=$(notrap_arg)
|
|
|
|
# Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
|
|
|
|
ifeq ($(filter address thread,$(my_sanitize)),)
|
|
|
|
# Does not have to be the first DT_NEEDED unlike ASan.
|
|
|
|
my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
|
|
|
|
endif
|
|
|
|
endif
|