config_sanitizers.mk revision 4c40141a91c987551451d8d773c6a7b18f172fd7
1############################################## 2## Perform configuration steps for sanitizers. 3############################################## 4 5my_sanitize := $(strip $(LOCAL_SANITIZE)) 6 7# SANITIZE_HOST is only in effect if the module is already using clang (host 8# modules that haven't set `LOCAL_CLANG := false` and device modules that 9# have set `LOCAL_CLANG := true`. 10my_global_sanitize := 11ifeq ($(my_clang),true) 12 ifdef LOCAL_IS_HOST_MODULE 13 my_global_sanitize := $(strip $(SANITIZE_HOST)) 14 15 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address. 16 my_global_sanitize := $(subst true,address,$(my_global_sanitize)) 17 else 18 my_global_sanitize := $(strip $(SANITIZE_TARGET)) 19 endif 20endif 21 22# The sanitizer specified by the environment wins over the module. 23ifneq ($(my_global_sanitize),) 24 my_sanitize := $(my_global_sanitize) 25endif 26 27# Don't apply sanitizers to NDK code. 28ifdef LOCAL_SDK_VERSION 29 my_sanitize := 30endif 31 32# Never always wins. 33ifeq ($(LOCAL_SANITIZE),never) 34 my_sanitize := 35endif 36 37# TSAN is not supported on 32-bit architectures. For non-multilib cases, make 38# its use an error. For multilib cases, don't use it for the 32-bit case. 39ifneq ($(filter thread,$(my_sanitize)),) 40 ifeq ($(my_32_64_bit_suffix),32) 41 ifeq ($(my_module_multilib),both) 42 my_sanitize := $(filter-out thread,$(my_sanitize)) 43 else 44 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.) 45 endif 46 endif 47endif 48 49# Undefined symbols can occur if a non-sanitized library links 50# sanitized static libraries. That's OK, because the executable 51# always depends on the ASan runtime library, which defines these 52# symbols. 53ifneq ($(strip $(SANITIZE_TARGET)),) 54 ifndef LOCAL_IS_HOST_MODULE 55 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES) 56 ifeq ($(my_sanitize),) 57 my_allow_undefined_symbols := true 58 endif 59 endif 60 # Workaround for a bug in AddressSanitizer that breaks stack unwinding. 61 # https://code.google.com/p/address-sanitizer/issues/detail?id=387 62 # Revert when external/compiler-rt is updated past r236014. 63 LOCAL_PACK_MODULE_RELOCATIONS := false 64 endif 65endif 66 67# Sanitizers can only be used with clang. 68ifneq ($(my_clang),true) 69 ifneq ($(my_sanitize),) 70 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true) 71 endif 72endif 73 74ifneq ($(filter default-ub,$(my_sanitize)),) 75 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS) 76endif 77 78ifneq ($(my_sanitize),) 79 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)), 80 my_cflags += -fsanitize=$(fsanitize_arg) 81 82 ifdef LOCAL_IS_HOST_MODULE 83 my_cflags += -fno-sanitize-recover=all 84 my_ldflags += -fsanitize=$(fsanitize_arg) 85 my_ldlibs += -lrt -ldl 86 else 87 my_cflags += -fsanitize-undefined-trap-on-error 88 my_cflags += -ftrap-function=abort 89 my_shared_libraries += libdl 90 endif 91endif 92 93ifneq ($(filter address,$(my_sanitize)),) 94 # Frame pointer based unwinder in ASan requires ARM frame setup. 95 LOCAL_ARM_MODE := arm 96 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS) 97 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS) 98 ifdef LOCAL_IS_HOST_MODULE 99 # -nodefaultlibs (provided with libc++) prevents the driver from linking 100 # libraries needed with -fsanitize=address. http://b/18650275 (WAI) 101 my_ldlibs += -lm -lpthread 102 my_ldflags += -Wl,--no-as-needed 103 else 104 my_cflags += -mllvm -asan-globals=0 105 # ASan runtime library must be the first in the link order. 106 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \ 107 $(my_shared_libraries) \ 108 $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES) 109 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES) 110 my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH) 111 112 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) 113 # Make sure linker_asan get installed. 114 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) 115 endif 116endif 117 118ifneq ($(filter undefined,$(my_sanitize)),) 119 ifndef LOCAL_IS_HOST_MODULE 120 $(error ubsan is not yet supported on the target) 121 endif 122endif 123 124ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),) 125 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)), 126 my_cflags += -fsanitize-recover=$(recover_arg) 127endif 128