Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 1 | ############################################## |
| 2 | ## Perform configuration steps for sanitizers. |
| 3 | ############################################## |
| 4 | |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 5 | my_sanitize := $(strip $(LOCAL_SANITIZE)) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 6 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 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`. |
| 10 | my_global_sanitize := |
| 11 | ifeq ($(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 |
| 20 | endif |
| 21 | |
| 22 | # The sanitizer specified by the environment wins over the module. |
| 23 | ifneq ($(my_global_sanitize),) |
| 24 | my_sanitize := $(my_global_sanitize) |
| 25 | endif |
| 26 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 27 | # Don't apply sanitizers to NDK code. |
| 28 | ifdef LOCAL_SDK_VERSION |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 29 | my_sanitize := |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 30 | endif |
| 31 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 32 | # Never always wins. |
| 33 | ifeq ($(LOCAL_SANITIZE),never) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 34 | my_sanitize := |
| 35 | endif |
| 36 | |
Dan Albert | a6311b7 | 2015-07-30 10:17:33 -0700 | [diff] [blame] | 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. |
| 39 | ifneq ($(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 |
| 47 | endif |
| 48 | |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 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. |
| 53 | ifneq ($(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 |
Evgenii Stepanov | 4b396e4 | 2015-07-20 16:32:53 -0700 | [diff] [blame] | 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 |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 64 | endif |
| 65 | endif |
| 66 | |
Dan Albert | 94b5791 | 2015-04-17 09:48:33 -0700 | [diff] [blame] | 67 | # Sanitizers can only be used with clang. |
| 68 | ifneq ($(my_clang),true) |
| 69 | ifneq ($(my_sanitize),) |
| 70 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true) |
| 71 | endif |
| 72 | endif |
| 73 | |
Dan Albert | b5b2ffe | 2015-04-16 18:07:07 -0700 | [diff] [blame] | 74 | ifneq ($(filter default-ub,$(my_sanitize)),) |
| 75 | my_sanitize := $(CLANG_DEFAULT_UB_CHECKS) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 76 | endif |
| 77 | |
Ivan Krasin | 74b32b8 | 2015-09-18 11:54:43 -0700 | [diff] [blame] | 78 | ifneq ($(filter coverage,$(my_sanitize)),) |
| 79 | ifeq ($(filter address,$(my_sanitize)),) |
| 80 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address') |
| 81 | endif |
| 82 | my_cflags += -fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp |
| 83 | my_sanitize := $(filter-out coverage,$(my_sanitize)) |
| 84 | endif |
| 85 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 86 | ifneq ($(my_sanitize),) |
Stephen Hines | f7dbab1 | 2015-10-21 08:32:45 -0700 | [diff] [blame] | 87 | fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)), |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 88 | my_cflags += -fsanitize=$(fsanitize_arg) |
| 89 | |
| 90 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 91 | my_cflags += -fno-sanitize-recover=all |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 92 | my_ldflags += -fsanitize=$(fsanitize_arg) |
Dan Albert | 2922411 | 2015-08-13 17:25:10 -0700 | [diff] [blame] | 93 | my_ldlibs += -lrt -ldl |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 94 | else |
Stephen Hines | f7dbab1 | 2015-10-21 08:32:45 -0700 | [diff] [blame] | 95 | my_cflags += -fsanitize-undefined-trap-on-error |
| 96 | my_cflags += -ftrap-function=abort |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 97 | my_shared_libraries += libdl |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 98 | endif |
| 99 | endif |
| 100 | |
| 101 | ifneq ($(filter address,$(my_sanitize)),) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 102 | # Frame pointer based unwinder in ASan requires ARM frame setup. |
| 103 | LOCAL_ARM_MODE := arm |
| 104 | my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS) |
| 105 | my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS) |
| 106 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 107 | # -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 108 | # libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 109 | my_ldlibs += -lm -lpthread |
Dan Albert | 1f0d530 | 2015-04-28 14:55:50 -0700 | [diff] [blame] | 110 | my_ldflags += -Wl,--no-as-needed |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 111 | else |
Evgenii Stepanov | ff7a781 | 2015-07-13 18:12:17 -0700 | [diff] [blame] | 112 | my_cflags += -mllvm -asan-globals=0 |
Evgenii Stepanov | f0b15e1 | 2015-04-24 16:34:47 -0700 | [diff] [blame] | 113 | # ASan runtime library must be the first in the link order. |
| 114 | my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \ |
| 115 | $(my_shared_libraries) \ |
| 116 | $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 117 | my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES) |
Evgenii Stepanov | f0b15e1 | 2015-04-24 16:34:47 -0700 | [diff] [blame] | 118 | my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH) |
Ying Wang | a05e222 | 2015-08-17 16:13:24 -0700 | [diff] [blame] | 119 | |
Evgenii Stepanov | 8f5e67a | 2015-07-10 18:06:51 -0700 | [diff] [blame] | 120 | my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) |
Ying Wang | a05e222 | 2015-08-17 16:13:24 -0700 | [diff] [blame] | 121 | # Make sure linker_asan get installed. |
| 122 | $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 123 | endif |
| 124 | endif |
| 125 | |
| 126 | ifneq ($(filter undefined,$(my_sanitize)),) |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 127 | ifndef LOCAL_IS_HOST_MODULE |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 128 | $(error ubsan is not yet supported on the target) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 129 | endif |
| 130 | endif |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame] | 131 | |
Dan Albert | 9f17655 | 2015-04-28 11:26:45 -0700 | [diff] [blame] | 132 | ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),) |
| 133 | recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)), |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame] | 134 | my_cflags += -fsanitize-recover=$(recover_arg) |
| 135 | endif |