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 := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 12 | my_global_sanitize_diag := |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 13 | ifeq ($(my_clang),true) |
| 14 | ifdef LOCAL_IS_HOST_MODULE |
| 15 | my_global_sanitize := $(strip $(SANITIZE_HOST)) |
| 16 | |
| 17 | # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address. |
| 18 | my_global_sanitize := $(subst true,address,$(my_global_sanitize)) |
| 19 | else |
| 20 | my_global_sanitize := $(strip $(SANITIZE_TARGET)) |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 21 | my_global_sanitize_diag := $(strip $(SANITIZE_TARGET_DIAG)) |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 22 | endif |
| 23 | endif |
| 24 | |
Ivan Lozano | b4749cb | 2017-07-21 10:33:32 -0700 | [diff] [blame] | 25 | # Disable global integer_overflow in excluded paths. |
| 26 | ifneq ($(filter integer_overflow, $(my_global_sanitize)),) |
| 27 | combined_exclude_paths := $(INTEGER_OVERFLOW_EXCLUDE_PATHS) \ |
| 28 | $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS) |
| 29 | |
| 30 | ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\ |
| 31 | $(filter $(dir)%,$(LOCAL_PATH)))),) |
| 32 | my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize)) |
| 33 | my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag)) |
| 34 | endif |
| 35 | endif |
| 36 | |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 37 | # Global integer sanitization doesn't support static modules. |
| 38 | ifeq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),) |
| 39 | my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize)) |
| 40 | my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag)) |
| 41 | endif |
| 42 | ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 43 | my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize)) |
| 44 | my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag)) |
| 45 | endif |
| 46 | |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 47 | # Disable global CFI in excluded paths |
| 48 | ifneq ($(filter cfi, $(my_global_sanitize)),) |
| 49 | combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \ |
| 50 | $(PRODUCT_CFI_EXCLUDE_PATHS) |
| 51 | |
| 52 | ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\ |
| 53 | $(filter $(dir)%,$(LOCAL_PATH)))),) |
| 54 | my_global_sanitize := $(filter-out cfi,$(my_global_sanitize)) |
| 55 | my_global_sanitize_diag := $(filter-out cfi,$(my_global_sanitize_diag)) |
| 56 | endif |
| 57 | endif |
| 58 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 59 | ifneq ($(my_global_sanitize),) |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 60 | my_sanitize := $(my_global_sanitize) $(my_sanitize) |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 61 | endif |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 62 | ifneq ($(my_global_sanitize_diag),) |
| 63 | my_sanitize_diag := $(my_global_sanitize_diag) $(my_sanitize_diag) |
| 64 | endif |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 65 | |
Andreas Gampe | 6b30d77 | 2016-06-27 15:15:31 -0700 | [diff] [blame] | 66 | # The sanitizer specified in the product configuration wins over the previous. |
| 67 | ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),) |
| 68 | my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG) |
| 69 | ifeq ($(my_sanitize),never) |
| 70 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 71 | my_sanitize_diag := |
Andreas Gampe | 6b30d77 | 2016-06-27 15:15:31 -0700 | [diff] [blame] | 72 | endif |
| 73 | endif |
| 74 | |
Colin Cross | 2361842 | 2016-11-02 15:05:21 -0700 | [diff] [blame] | 75 | ifndef LOCAL_IS_HOST_MODULE |
| 76 | # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden) |
| 77 | SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH) |
| 78 | ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 79 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 80 | my_sanitize_diag := |
Colin Cross | 2361842 | 2016-11-02 15:05:21 -0700 | [diff] [blame] | 81 | endif |
Andreas Gampe | cd25740 | 2016-06-20 17:36:49 -0700 | [diff] [blame] | 82 | endif |
| 83 | |
Andreas Gampe | 3d3b0c9 | 2016-06-20 17:46:29 -0700 | [diff] [blame] | 84 | # Add a filter point based on module owner (to lighten the burden). The format is a space- or |
| 85 | # colon-separated list of owner names. |
| 86 | ifneq (,$(SANITIZE_NEVER_BY_OWNER)) |
| 87 | ifneq (,$(LOCAL_MODULE_OWNER)) |
| 88 | ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER)))) |
| 89 | $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.) |
| 90 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 91 | my_sanitize_diag := |
Andreas Gampe | 3d3b0c9 | 2016-06-20 17:46:29 -0700 | [diff] [blame] | 92 | endif |
| 93 | endif |
| 94 | endif |
| 95 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 96 | # Don't apply sanitizers to NDK code. |
| 97 | ifdef LOCAL_SDK_VERSION |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 98 | my_sanitize := |
Dan Willemsen | f761c0f | 2016-06-28 16:47:43 -0700 | [diff] [blame] | 99 | my_global_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 100 | my_sanitize_diag := |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 101 | endif |
| 102 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 103 | # Never always wins. |
| 104 | ifeq ($(LOCAL_SANITIZE),never) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 105 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 106 | my_sanitize_diag := |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 107 | endif |
| 108 | |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 109 | # Enable CFI in included paths. |
| 110 | ifeq ($(filter cfi, $(my_sanitize)),) |
Vishwath Mohan | 21204e4 | 2018-03-30 02:55:05 +0000 | [diff] [blame] | 111 | combined_include_paths := $(CFI_INCLUDE_PATHS) \ |
| 112 | $(PRODUCT_CFI_INCLUDE_PATHS) |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 113 | |
Vishwath Mohan | 21204e4 | 2018-03-30 02:55:05 +0000 | [diff] [blame] | 114 | ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\ |
| 115 | $(filter $(dir)%,$(LOCAL_PATH)))),) |
| 116 | my_sanitize := cfi $(my_sanitize) |
| 117 | my_sanitize_diag := cfi $(my_sanitize_diag) |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 118 | endif |
| 119 | endif |
| 120 | |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 121 | # If CFI is disabled globally, remove it from my_sanitize. |
Vishwath Mohan | 45665b4 | 2017-01-24 13:20:28 -0800 | [diff] [blame] | 122 | ifeq ($(strip $(ENABLE_CFI)),false) |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 123 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 124 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 125 | endif |
| 126 | |
Vishwath Mohan | a204606 | 2017-02-07 20:28:07 -0800 | [diff] [blame] | 127 | # Disable CFI for arm32 (b/35157333). |
| 128 | ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 129 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 130 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 131 | endif |
| 132 | |
Vishwath Mohan | c026f6d | 2017-04-20 07:39:13 -0700 | [diff] [blame] | 133 | # Also disable CFI if ASAN is enabled. |
| 134 | ifneq ($(filter address,$(my_sanitize)),) |
| 135 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 136 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 137 | endif |
| 138 | |
Evgenii Stepanov | 8c50e3c | 2017-01-31 17:08:33 -0800 | [diff] [blame] | 139 | # CFI needs gold linker, and mips toolchain does not have one. |
| 140 | ifneq ($(filter mips mips64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 141 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 142 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 143 | endif |
| 144 | |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 145 | # Disable sanitizers which need the UBSan runtime for host targets. |
Vishwath Mohan | 96a130b | 2017-11-17 11:19:36 -0800 | [diff] [blame] | 146 | ifdef LOCAL_IS_HOST_MODULE |
| 147 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 148 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 149 | my_sanitize := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize)) |
| 150 | my_sanitize_diag := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize_diag)) |
Vishwath Mohan | 96a130b | 2017-11-17 11:19:36 -0800 | [diff] [blame] | 151 | endif |
| 152 | |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 153 | # Support for local sanitize blacklist paths. |
| 154 | ifneq ($(my_sanitize)$(my_global_sanitize),) |
| 155 | ifneq ($(LOCAL_SANITIZE_BLACKLIST),) |
| 156 | my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLACKLIST) |
| 157 | endif |
| 158 | endif |
| 159 | |
Ivan Lozano | b4749cb | 2017-07-21 10:33:32 -0700 | [diff] [blame] | 160 | # Disable integer_overflow if LOCAL_NOSANITIZE=integer. |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 161 | ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),) |
| 162 | ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),) |
| 163 | my_sanitize := $(filter-out integer_overflow,$(my_sanitize)) |
| 164 | my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag)) |
| 165 | endif |
| 166 | endif |
| 167 | |
Evgenii Stepanov | 4282366 | 2016-05-12 13:07:17 -0700 | [diff] [blame] | 168 | my_nosanitize = $(strip $(LOCAL_NOSANITIZE)) |
| 169 | ifneq ($(my_nosanitize),) |
| 170 | my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize)) |
| 171 | endif |
| 172 | |
Dan Albert | a6311b7 | 2015-07-30 10:17:33 -0700 | [diff] [blame] | 173 | # TSAN is not supported on 32-bit architectures. For non-multilib cases, make |
| 174 | # its use an error. For multilib cases, don't use it for the 32-bit case. |
| 175 | ifneq ($(filter thread,$(my_sanitize)),) |
| 176 | ifeq ($(my_32_64_bit_suffix),32) |
| 177 | ifeq ($(my_module_multilib),both) |
| 178 | my_sanitize := $(filter-out thread,$(my_sanitize)) |
| 179 | else |
| 180 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.) |
| 181 | endif |
Yabin Cui | e77c32e | 2017-10-19 14:33:58 -0700 | [diff] [blame] | 182 | else |
| 183 | my_shared_libraries += $(TSAN_RUNTIME_LIBRARY) |
Dan Albert | a6311b7 | 2015-07-30 10:17:33 -0700 | [diff] [blame] | 184 | endif |
| 185 | endif |
| 186 | |
Evgenii Stepanov | 7dcb8b8 | 2016-05-06 18:15:57 -0700 | [diff] [blame] | 187 | ifneq ($(filter safe-stack,$(my_sanitize)),) |
| 188 | ifeq ($(my_32_64_bit_suffix),32) |
| 189 | my_sanitize := $(filter-out safe-stack,$(my_sanitize)) |
| 190 | endif |
| 191 | endif |
| 192 | |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 193 | # Undefined symbols can occur if a non-sanitized library links |
| 194 | # sanitized static libraries. That's OK, because the executable |
| 195 | # always depends on the ASan runtime library, which defines these |
| 196 | # symbols. |
Evgenii Stepanov | 912b51f | 2016-05-19 17:49:51 -0700 | [diff] [blame] | 197 | ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),) |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 198 | ifndef LOCAL_IS_HOST_MODULE |
| 199 | ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES) |
| 200 | ifeq ($(my_sanitize),) |
| 201 | my_allow_undefined_symbols := true |
| 202 | endif |
| 203 | endif |
| 204 | endif |
| 205 | endif |
| 206 | |
Dan Albert | 94b5791 | 2015-04-17 09:48:33 -0700 | [diff] [blame] | 207 | # Sanitizers can only be used with clang. |
| 208 | ifneq ($(my_clang),true) |
| 209 | ifneq ($(my_sanitize),) |
| 210 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true) |
| 211 | endif |
| 212 | endif |
| 213 | |
Dan Albert | b5b2ffe | 2015-04-16 18:07:07 -0700 | [diff] [blame] | 214 | ifneq ($(filter default-ub,$(my_sanitize)),) |
| 215 | my_sanitize := $(CLANG_DEFAULT_UB_CHECKS) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 216 | endif |
| 217 | |
Ivan Krasin | 74b32b8 | 2015-09-18 11:54:43 -0700 | [diff] [blame] | 218 | ifneq ($(filter coverage,$(my_sanitize)),) |
| 219 | ifeq ($(filter address,$(my_sanitize)),) |
| 220 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address') |
| 221 | endif |
Zach Riggle | be0811f | 2017-08-21 17:07:42 -0400 | [diff] [blame] | 222 | my_cflags += -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp |
Ivan Krasin | 74b32b8 | 2015-09-18 11:54:43 -0700 | [diff] [blame] | 223 | my_sanitize := $(filter-out coverage,$(my_sanitize)) |
| 224 | endif |
| 225 | |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 226 | ifneq ($(filter integer_overflow,$(my_sanitize)),) |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 227 | # Respect LOCAL_NOSANITIZE for integer-overflow flags. |
| 228 | ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),) |
| 229 | my_sanitize += signed-integer-overflow |
| 230 | endif |
| 231 | ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),) |
| 232 | my_sanitize += unsigned-integer-overflow |
| 233 | endif |
| 234 | my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS) |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 235 | |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 236 | # Check for diagnostics mode. |
| 237 | ifneq ($(filter integer_overflow,$(my_sanitize_diag)),) |
| 238 | ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),) |
| 239 | ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
Ivan Lozano | 911cb99 | 2018-02-21 13:41:05 -0800 | [diff] [blame] | 240 | my_sanitize_diag += signed-integer-overflow |
| 241 | my_sanitize_diag += unsigned-integer-overflow |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 242 | else |
| 243 | $(call pretty-error,Make cannot apply integer overflow diagnostics to static binary.) |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 244 | endif |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 245 | else |
| 246 | $(call pretty-error,Make cannot apply integer overflow diagnostics to static library.) |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 247 | endif |
| 248 | endif |
| 249 | my_sanitize := $(filter-out integer_overflow,$(my_sanitize)) |
| 250 | endif |
| 251 | |
| 252 | # Makes sure integer_overflow diagnostics is removed from the diagnostics list |
| 253 | # even if integer_overflow is not set for some reason. |
| 254 | ifneq ($(filter integer_overflow,$(my_sanitize_diag)),) |
| 255 | my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag)) |
| 256 | endif |
| 257 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 258 | ifneq ($(my_sanitize),) |
Stephen Hines | e8119e9 | 2015-11-09 16:32:11 -0800 | [diff] [blame] | 259 | fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 260 | my_cflags += -fsanitize=$(fsanitize_arg) |
| 261 | |
| 262 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 263 | my_cflags += -fno-sanitize-recover=all |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 264 | my_ldflags += -fsanitize=$(fsanitize_arg) |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 265 | else |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 266 | my_cflags += -fsanitize-trap=all |
| 267 | my_cflags += -ftrap-function=abort |
Evgenii Stepanov | 55f73e6 | 2016-05-12 13:07:36 -0700 | [diff] [blame] | 268 | ifneq ($(filter address thread,$(my_sanitize)),) |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 269 | my_cflags += -fno-sanitize-trap=address,thread |
Evgenii Stepanov | 55f73e6 | 2016-05-12 13:07:36 -0700 | [diff] [blame] | 270 | my_shared_libraries += libdl |
| 271 | endif |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 272 | endif |
| 273 | endif |
| 274 | |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 275 | ifneq ($(filter cfi,$(my_sanitize)),) |
Evgenii Stepanov | 81bea1b | 2017-01-20 14:12:08 -0800 | [diff] [blame] | 276 | # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). |
| 277 | # LLVM is not set up to do this on a function basis, so force Thumb on the |
| 278 | # entire module. |
| 279 | LOCAL_ARM_MODE := thumb |
Vishwath Mohan | 5b69c06 | 2017-02-14 07:55:37 -0800 | [diff] [blame] | 280 | my_cflags += $(CFI_EXTRA_CFLAGS) |
Vishwath Mohan | 85f7244 | 2017-11-01 09:21:20 +0000 | [diff] [blame] | 281 | # Only append the default visibility flag if -fvisibility has not already been |
| 282 | # set to hidden. |
| 283 | ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),) |
| 284 | my_cflags += -fvisibility=default |
| 285 | endif |
Vishwath Mohan | 5b69c06 | 2017-02-14 07:55:37 -0800 | [diff] [blame] | 286 | my_ldflags += $(CFI_EXTRA_LDFLAGS) |
Evgenii Stepanov | e1b96f3 | 2017-01-23 16:57:38 -0800 | [diff] [blame] | 287 | my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so |
Vishwath Mohan | 85f7244 | 2017-11-01 09:21:20 +0000 | [diff] [blame] | 288 | |
| 289 | ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 290 | my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags)) |
| 291 | my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags)) |
| 292 | else |
| 293 | # Apply the version script to non-static executables |
| 294 | my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map |
| 295 | LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map |
| 296 | endif |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 297 | endif |
| 298 | |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 299 | # If local or global modules need ASAN, add linker flags. |
| 300 | ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 301 | my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS) |
| 302 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 303 | # -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 304 | # libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Dan Albert | 1f0d530 | 2015-04-28 14:55:50 -0700 | [diff] [blame] | 305 | my_ldflags += -Wl,--no-as-needed |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 306 | else |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 307 | # Add asan libraries unless LOCAL_MODULE is the asan library. |
Evgenii Stepanov | f0b15e1 | 2015-04-24 16:34:47 -0700 | [diff] [blame] | 308 | # ASan runtime library must be the first in the link order. |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 309 | ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY))) |
| 310 | my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \ |
| 311 | $(my_shared_libraries) |
| 312 | endif |
| 313 | ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES))) |
| 314 | my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES) |
| 315 | endif |
| 316 | |
| 317 | # Do not add unnecessary dependency in shared libraries. |
| 318 | ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES) |
| 319 | my_ldflags += -Wl,--as-needed |
| 320 | endif |
Ying Wang | a05e222 | 2015-08-17 16:13:24 -0700 | [diff] [blame] | 321 | |
Colin Cross | d08699e | 2016-07-17 15:28:07 -0700 | [diff] [blame] | 322 | ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES) |
| 323 | ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 324 | my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) |
| 325 | # Make sure linker_asan get installed. |
| 326 | $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) |
| 327 | endif |
| 328 | endif |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 329 | endif |
| 330 | endif |
| 331 | |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 332 | # If local module needs ASAN, add compiler flags. |
| 333 | ifneq ($(filter address,$(my_sanitize)),) |
| 334 | # Frame pointer based unwinder in ASan requires ARM frame setup. |
| 335 | LOCAL_ARM_MODE := arm |
| 336 | my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS) |
| 337 | ifndef LOCAL_IS_HOST_MODULE |
| 338 | my_cflags += -mllvm -asan-globals=0 |
| 339 | endif |
| 340 | endif |
| 341 | |
Alexey Polyudov | e98e562 | 2018-03-27 15:28:12 -0700 | [diff] [blame] | 342 | # Use minimal diagnostics when integer overflow is enabled; never do it for HOST or AUX modules |
| 343 | ifeq ($(LOCAL_IS_HOST_MODULE)$(LOCAL_IS_AUX_MODULE),) |
Ivan Lozano | 911cb99 | 2018-02-21 13:41:05 -0800 | [diff] [blame] | 344 | # Pre-emptively add UBSAN minimal runtime incase a static library dependency requires it |
| 345 | ifeq ($(filter STATIC_LIBRARIES,$(LOCAL_MODULE_CLASS)),) |
| 346 | ifndef LOCAL_SDK_VERSION |
| 347 | my_static_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY) |
Ivan Lozano | e508169 | 2018-05-11 14:09:36 -0700 | [diff] [blame^] | 348 | my_ldflags += -Wl,--exclude-libs,$($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY).a |
Ivan Lozano | 911cb99 | 2018-02-21 13:41:05 -0800 | [diff] [blame] | 349 | endif |
| 350 | endif |
| 351 | ifneq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize)),) |
| 352 | ifeq ($(filter unsigned-integer-overflow signed-integer overflow integer,$(my_sanitize_diag)),) |
| 353 | ifeq ($(filter cfi,$(my_sanitize_diag)),) |
| 354 | ifeq ($(filter address,$(my_sanitize)),) |
| 355 | my_cflags += -fsanitize-minimal-runtime |
| 356 | my_cflags += -fno-sanitize-trap=integer |
| 357 | my_cflags += -fno-sanitize-recover=integer |
| 358 | endif |
| 359 | endif |
| 360 | endif |
| 361 | endif |
| 362 | endif |
| 363 | |
Dan Albert | 9f17655 | 2015-04-28 11:26:45 -0700 | [diff] [blame] | 364 | ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),) |
| 365 | recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)), |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame] | 366 | my_cflags += -fsanitize-recover=$(recover_arg) |
| 367 | endif |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 368 | |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 369 | ifneq ($(my_sanitize_diag),) |
Vishwath Mohan | 85f7244 | 2017-11-01 09:21:20 +0000 | [diff] [blame] | 370 | # TODO(vishwath): Add diagnostic support for static executables once |
| 371 | # we switch to clang-4393122 (which adds the static ubsan runtime |
| 372 | # that this depends on) |
| 373 | ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 374 | notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)), |
| 375 | my_cflags += -fno-sanitize-trap=$(notrap_arg) |
| 376 | # Diagnostic requires a runtime library, unless ASan or TSan are also enabled. |
| 377 | ifeq ($(filter address thread,$(my_sanitize)),) |
| 378 | # Does not have to be the first DT_NEEDED unlike ASan. |
| 379 | my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY) |
| 380 | endif |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 381 | endif |
| 382 | endif |