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)) |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 6 | my_sanitize_diag := $(strip $(LOCAL_SANITIZE_DIAG)) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 7 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 8 | # SANITIZE_HOST is only in effect if the module is already using clang (host |
| 9 | # modules that haven't set `LOCAL_CLANG := false` and device modules that |
| 10 | # have set `LOCAL_CLANG := true`. |
| 11 | my_global_sanitize := |
| 12 | ifeq ($(my_clang),true) |
| 13 | ifdef LOCAL_IS_HOST_MODULE |
| 14 | my_global_sanitize := $(strip $(SANITIZE_HOST)) |
| 15 | |
| 16 | # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address. |
| 17 | my_global_sanitize := $(subst true,address,$(my_global_sanitize)) |
| 18 | else |
| 19 | my_global_sanitize := $(strip $(SANITIZE_TARGET)) |
| 20 | endif |
| 21 | endif |
| 22 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 23 | ifneq ($(my_global_sanitize),) |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 24 | my_sanitize := $(my_global_sanitize) $(my_sanitize) |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 25 | endif |
| 26 | |
Andreas Gampe | 6b30d77 | 2016-06-27 15:15:31 -0700 | [diff] [blame] | 27 | # The sanitizer specified in the product configuration wins over the previous. |
| 28 | ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),) |
| 29 | my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG) |
| 30 | ifeq ($(my_sanitize),never) |
| 31 | my_sanitize := |
| 32 | endif |
| 33 | endif |
| 34 | |
Colin Cross | 2361842 | 2016-11-02 15:05:21 -0700 | [diff] [blame] | 35 | ifndef LOCAL_IS_HOST_MODULE |
| 36 | # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden) |
| 37 | SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH) |
| 38 | ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 39 | my_sanitize := |
| 40 | endif |
Andreas Gampe | cd25740 | 2016-06-20 17:36:49 -0700 | [diff] [blame] | 41 | endif |
| 42 | |
Andreas Gampe | 3d3b0c9 | 2016-06-20 17:46:29 -0700 | [diff] [blame] | 43 | # Add a filter point based on module owner (to lighten the burden). The format is a space- or |
| 44 | # colon-separated list of owner names. |
| 45 | ifneq (,$(SANITIZE_NEVER_BY_OWNER)) |
| 46 | ifneq (,$(LOCAL_MODULE_OWNER)) |
| 47 | ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER)))) |
| 48 | $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.) |
| 49 | my_sanitize := |
| 50 | endif |
| 51 | endif |
| 52 | endif |
| 53 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 54 | # Don't apply sanitizers to NDK code. |
| 55 | ifdef LOCAL_SDK_VERSION |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 56 | my_sanitize := |
Dan Willemsen | f761c0f | 2016-06-28 16:47:43 -0700 | [diff] [blame] | 57 | my_global_sanitize := |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 58 | endif |
| 59 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 60 | # Never always wins. |
| 61 | ifeq ($(LOCAL_SANITIZE),never) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 62 | my_sanitize := |
| 63 | endif |
| 64 | |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 65 | # If CFI is disabled globally, remove it from my_sanitize. |
Vishwath Mohan | 45665b4 | 2017-01-24 13:20:28 -0800 | [diff] [blame^] | 66 | ifeq ($(strip $(ENABLE_CFI)),false) |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 67 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 68 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 69 | endif |
| 70 | |
Evgenii Stepanov | 4282366 | 2016-05-12 13:07:17 -0700 | [diff] [blame] | 71 | my_nosanitize = $(strip $(LOCAL_NOSANITIZE)) |
| 72 | ifneq ($(my_nosanitize),) |
| 73 | my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize)) |
| 74 | endif |
| 75 | |
Dan Albert | a6311b7 | 2015-07-30 10:17:33 -0700 | [diff] [blame] | 76 | # TSAN is not supported on 32-bit architectures. For non-multilib cases, make |
| 77 | # its use an error. For multilib cases, don't use it for the 32-bit case. |
| 78 | ifneq ($(filter thread,$(my_sanitize)),) |
| 79 | ifeq ($(my_32_64_bit_suffix),32) |
| 80 | ifeq ($(my_module_multilib),both) |
| 81 | my_sanitize := $(filter-out thread,$(my_sanitize)) |
| 82 | else |
| 83 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.) |
| 84 | endif |
| 85 | endif |
| 86 | endif |
| 87 | |
Evgenii Stepanov | 7dcb8b8 | 2016-05-06 18:15:57 -0700 | [diff] [blame] | 88 | ifneq ($(filter safe-stack,$(my_sanitize)),) |
| 89 | ifeq ($(my_32_64_bit_suffix),32) |
| 90 | my_sanitize := $(filter-out safe-stack,$(my_sanitize)) |
| 91 | endif |
| 92 | endif |
| 93 | |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 94 | # Undefined symbols can occur if a non-sanitized library links |
| 95 | # sanitized static libraries. That's OK, because the executable |
| 96 | # always depends on the ASan runtime library, which defines these |
| 97 | # symbols. |
Evgenii Stepanov | 912b51f | 2016-05-19 17:49:51 -0700 | [diff] [blame] | 98 | ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),) |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 99 | ifndef LOCAL_IS_HOST_MODULE |
| 100 | ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES) |
| 101 | ifeq ($(my_sanitize),) |
| 102 | my_allow_undefined_symbols := true |
| 103 | endif |
| 104 | endif |
| 105 | endif |
| 106 | endif |
| 107 | |
Dan Albert | 94b5791 | 2015-04-17 09:48:33 -0700 | [diff] [blame] | 108 | # Sanitizers can only be used with clang. |
| 109 | ifneq ($(my_clang),true) |
| 110 | ifneq ($(my_sanitize),) |
| 111 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true) |
| 112 | endif |
| 113 | endif |
| 114 | |
Dan Albert | b5b2ffe | 2015-04-16 18:07:07 -0700 | [diff] [blame] | 115 | ifneq ($(filter default-ub,$(my_sanitize)),) |
| 116 | my_sanitize := $(CLANG_DEFAULT_UB_CHECKS) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 117 | endif |
| 118 | |
Ivan Krasin | 74b32b8 | 2015-09-18 11:54:43 -0700 | [diff] [blame] | 119 | ifneq ($(filter coverage,$(my_sanitize)),) |
| 120 | ifeq ($(filter address,$(my_sanitize)),) |
| 121 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address') |
| 122 | endif |
| 123 | my_cflags += -fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp |
| 124 | my_sanitize := $(filter-out coverage,$(my_sanitize)) |
| 125 | endif |
| 126 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 127 | ifneq ($(my_sanitize),) |
Stephen Hines | e8119e9 | 2015-11-09 16:32:11 -0800 | [diff] [blame] | 128 | fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 129 | my_cflags += -fsanitize=$(fsanitize_arg) |
| 130 | |
| 131 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 132 | my_cflags += -fno-sanitize-recover=all |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 133 | my_ldflags += -fsanitize=$(fsanitize_arg) |
Dan Albert | 2922411 | 2015-08-13 17:25:10 -0700 | [diff] [blame] | 134 | my_ldlibs += -lrt -ldl |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 135 | else |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 136 | my_cflags += -fsanitize-trap=all |
| 137 | my_cflags += -ftrap-function=abort |
Evgenii Stepanov | 55f73e6 | 2016-05-12 13:07:36 -0700 | [diff] [blame] | 138 | ifneq ($(filter address thread,$(my_sanitize)),) |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 139 | my_cflags += -fno-sanitize-trap=address,thread |
Evgenii Stepanov | 55f73e6 | 2016-05-12 13:07:36 -0700 | [diff] [blame] | 140 | my_shared_libraries += libdl |
| 141 | endif |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 142 | endif |
| 143 | endif |
| 144 | |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 145 | ifneq ($(filter cfi,$(my_sanitize)),) |
Evgenii Stepanov | 81bea1b | 2017-01-20 14:12:08 -0800 | [diff] [blame] | 146 | # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). |
| 147 | # LLVM is not set up to do this on a function basis, so force Thumb on the |
| 148 | # entire module. |
| 149 | LOCAL_ARM_MODE := thumb |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 150 | my_cflags += -flto -fsanitize-cfi-cross-dso -fvisibility=default |
| 151 | my_ldflags += -flto -fsanitize-cfi-cross-dso -fsanitize=cfi -Wl,-plugin-opt,O1 -Wl,-export-dynamic-symbol=__cfi_check |
Evgenii Stepanov | e1b96f3 | 2017-01-23 16:57:38 -0800 | [diff] [blame] | 152 | my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 153 | endif |
| 154 | |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 155 | # If local or global modules need ASAN, add linker flags. |
| 156 | ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 157 | my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS) |
| 158 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 159 | # -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 160 | # libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 161 | my_ldlibs += -lm -lpthread |
Dan Albert | 1f0d530 | 2015-04-28 14:55:50 -0700 | [diff] [blame] | 162 | my_ldflags += -Wl,--no-as-needed |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 163 | else |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 164 | # Add asan libraries unless LOCAL_MODULE is the asan library. |
Evgenii Stepanov | f0b15e1 | 2015-04-24 16:34:47 -0700 | [diff] [blame] | 165 | # ASan runtime library must be the first in the link order. |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 166 | ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY))) |
| 167 | my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \ |
| 168 | $(my_shared_libraries) |
| 169 | endif |
| 170 | ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES))) |
| 171 | my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES) |
| 172 | endif |
| 173 | |
| 174 | # Do not add unnecessary dependency in shared libraries. |
| 175 | ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES) |
| 176 | my_ldflags += -Wl,--as-needed |
| 177 | endif |
Ying Wang | a05e222 | 2015-08-17 16:13:24 -0700 | [diff] [blame] | 178 | |
Colin Cross | d08699e | 2016-07-17 15:28:07 -0700 | [diff] [blame] | 179 | ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES) |
| 180 | ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 181 | my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) |
| 182 | # Make sure linker_asan get installed. |
| 183 | $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) |
| 184 | endif |
| 185 | endif |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 186 | endif |
| 187 | endif |
| 188 | |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 189 | # If local module needs ASAN, add compiler flags. |
| 190 | ifneq ($(filter address,$(my_sanitize)),) |
| 191 | # Frame pointer based unwinder in ASan requires ARM frame setup. |
| 192 | LOCAL_ARM_MODE := arm |
| 193 | my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS) |
| 194 | ifndef LOCAL_IS_HOST_MODULE |
| 195 | my_cflags += -mllvm -asan-globals=0 |
| 196 | endif |
| 197 | endif |
| 198 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 199 | ifneq ($(filter undefined,$(my_sanitize)),) |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 200 | ifndef LOCAL_IS_HOST_MODULE |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 201 | $(error ubsan is not yet supported on the target) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 202 | endif |
| 203 | endif |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame] | 204 | |
Dan Albert | 9f17655 | 2015-04-28 11:26:45 -0700 | [diff] [blame] | 205 | ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),) |
| 206 | recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)), |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame] | 207 | my_cflags += -fsanitize-recover=$(recover_arg) |
| 208 | endif |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 209 | |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 210 | ifneq ($(my_sanitize_diag),) |
| 211 | notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)), |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 212 | my_cflags += -fno-sanitize-trap=$(notrap_arg) |
| 213 | # Diagnostic requires a runtime library, unless ASan or TSan are also enabled. |
| 214 | ifeq ($(filter address thread,$(my_sanitize)),) |
| 215 | # Does not have to be the first DT_NEEDED unlike ASan. |
| 216 | my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY) |
| 217 | endif |
| 218 | endif |