From 08cca28f9215a840274a1b093b93bff8aaa6f33a Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Thu, 11 Dec 2014 18:56:26 -0800 Subject: [PATCH] Add support for ubsan. Rather than adding LOCAL_UB_SANITIZER, LOCAL_THREAD_SANITIZER, etc for each new sanitizer, deprecate LOCAL_ADDRESS_SANITIZER in favor of LOCAL_SANITZE that mirrors the behavior of -fsanitize=. For example, the following will use both asan and ubsan: LOCAL_SANITIZE := address undefined We'll leave LOCAL_ADDRESS_SANITIZER around for compatibility until we can clean up the tree. Change-Id: I8a62315129d4753f8e992584ca6db1e5dfdd4d2a --- core/clang/config.mk | 2 +- core/clear_vars.mk | 1 + core/config_sanitizers.mk | 52 +++++++++++++++++++++++++++++++++------ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/core/clang/config.mk b/core/clang/config.mk index 13a5ba13f..bdcfe60c3 100644 --- a/core/clang/config.mk +++ b/core/clang/config.mk @@ -115,7 +115,7 @@ endif # Address sanitizer clang config ADDRESS_SANITIZER_RUNTIME_LIBRARY := libclang_rt.asan_$(TARGET_ARCH)_android -ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS := -fsanitize=address -fno-omit-frame-pointer +ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS := -fno-omit-frame-pointer ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS := -Wl,-u,__asan_preinit ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES := libdl $(ADDRESS_SANITIZER_RUNTIME_LIBRARY) diff --git a/core/clear_vars.mk b/core/clear_vars.mk index 7837ae365..51cdc5b16 100644 --- a/core/clear_vars.mk +++ b/core/clear_vars.mk @@ -173,6 +173,7 @@ LOCAL_CXX_STL := default LOCAL_NATIVE_COVERAGE := LOCAL_DPI_VARIANTS:= LOCAL_DPI_FILE_STEM:= +LOCAL_SANITIZE:= # arch specific variables LOCAL_SRC_FILES_$(TARGET_ARCH):= diff --git a/core/config_sanitizers.mk b/core/config_sanitizers.mk index a1964a9a4..3c07f6916 100644 --- a/core/config_sanitizers.mk +++ b/core/config_sanitizers.mk @@ -7,26 +7,62 @@ ifdef LOCAL_IS_HOST_MODULE ifeq ($(SANITIZE_HOST),true) ifneq ($(strip $(LOCAL_CLANG)),false) ifneq ($(strip $(LOCAL_ADDRESS_SANITIZER)),false) - LOCAL_ADDRESS_SANITIZER := true + LOCAL_SANITIZE := address endif endif endif endif -# Configure address sanitizer. +my_sanitize := $(LOCAL_SANITIZE) + +# Keep compatibility for LOCAL_ADDRESS_SANITIZER until all targets have moved to +# `LOCAL_SANITIZE := address`. ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),true) + my_sanitize += address +endif + +# Don't apply sanitizers to NDK code. +ifdef LOCAL_SDK_VERSION + my_sanitize := +endif + +unknown_sanitizers := $(filter-out address, \ + $(filter-out undefined,$(my_sanitize))) + +ifneq ($(unknown_sanitizers),) + $(error Unknown sanitizers: $(unknown_sanitizers)) +endif + +ifneq ($(my_sanitize),) my_clang := true + + fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)), + my_cflags += -fsanitize=$(fsanitize_arg) + + ifdef LOCAL_IS_HOST_MODULE + my_ldflags += -fsanitize=$(fsanitize_arg) + endif +endif + +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) my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS) ifdef LOCAL_IS_HOST_MODULE - my_ldflags += -fsanitize=address - # -nodefaultlibs (provided with libc++) prevents the driver from linking - # libraries needed with -fsanitize=address. http://b/18650275 - my_ldlibs += -ldl -lpthread + # -nodefaultlibs (provided with libc++) prevents the driver from linking + # libraries needed with -fsanitize=address. http://b/18650275 (WAI) + my_ldlibs += -ldl -lpthread else - my_shared_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES) - my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES) + my_shared_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES) + my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES) + endif +endif + +ifneq ($(filter undefined,$(my_sanitize)),) + ifdef LOCAL_IS_HOST_MODULE + my_ldlibs += -ldl + else + $(error ubsan is not yet supported on the target) endif endif