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 | my_global_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 9 | my_global_sanitize_diag := |
Dan Willemsen | f063839 | 2018-09-04 22:25:22 -0700 | [diff] [blame] | 10 | ifdef LOCAL_IS_HOST_MODULE |
| 11 | ifneq ($($(my_prefix)OS),windows) |
| 12 | my_global_sanitize := $(strip $(SANITIZE_HOST)) |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 13 | |
Dan Willemsen | f063839 | 2018-09-04 22:25:22 -0700 | [diff] [blame] | 14 | # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address. |
| 15 | my_global_sanitize := $(subst true,address,$(my_global_sanitize)) |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 16 | endif |
Dan Willemsen | f063839 | 2018-09-04 22:25:22 -0700 | [diff] [blame] | 17 | else |
| 18 | my_global_sanitize := $(strip $(SANITIZE_TARGET)) |
| 19 | my_global_sanitize_diag := $(strip $(SANITIZE_TARGET_DIAG)) |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 20 | endif |
| 21 | |
Ivan Lozano | b4749cb | 2017-07-21 10:33:32 -0700 | [diff] [blame] | 22 | # Disable global integer_overflow in excluded paths. |
| 23 | ifneq ($(filter integer_overflow, $(my_global_sanitize)),) |
| 24 | combined_exclude_paths := $(INTEGER_OVERFLOW_EXCLUDE_PATHS) \ |
| 25 | $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS) |
| 26 | |
| 27 | ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\ |
| 28 | $(filter $(dir)%,$(LOCAL_PATH)))),) |
| 29 | my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize)) |
| 30 | my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag)) |
| 31 | endif |
| 32 | endif |
| 33 | |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 34 | # Global integer sanitization doesn't support static modules. |
| 35 | ifeq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),) |
| 36 | my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize)) |
| 37 | my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag)) |
| 38 | endif |
| 39 | ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 40 | my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize)) |
| 41 | my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag)) |
| 42 | endif |
| 43 | |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 44 | # Disable global CFI in excluded paths |
| 45 | ifneq ($(filter cfi, $(my_global_sanitize)),) |
| 46 | combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \ |
| 47 | $(PRODUCT_CFI_EXCLUDE_PATHS) |
| 48 | |
| 49 | ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\ |
| 50 | $(filter $(dir)%,$(LOCAL_PATH)))),) |
| 51 | my_global_sanitize := $(filter-out cfi,$(my_global_sanitize)) |
| 52 | my_global_sanitize_diag := $(filter-out cfi,$(my_global_sanitize_diag)) |
| 53 | endif |
| 54 | endif |
| 55 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 56 | ifneq ($(my_global_sanitize),) |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 57 | my_sanitize := $(my_global_sanitize) $(my_sanitize) |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 58 | endif |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 59 | ifneq ($(my_global_sanitize_diag),) |
| 60 | my_sanitize_diag := $(my_global_sanitize_diag) $(my_sanitize_diag) |
| 61 | endif |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 62 | |
Andreas Gampe | 6b30d77 | 2016-06-27 15:15:31 -0700 | [diff] [blame] | 63 | # The sanitizer specified in the product configuration wins over the previous. |
| 64 | ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),) |
| 65 | my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG) |
| 66 | ifeq ($(my_sanitize),never) |
| 67 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 68 | my_sanitize_diag := |
Andreas Gampe | 6b30d77 | 2016-06-27 15:15:31 -0700 | [diff] [blame] | 69 | endif |
| 70 | endif |
| 71 | |
Colin Cross | 2361842 | 2016-11-02 15:05:21 -0700 | [diff] [blame] | 72 | ifndef LOCAL_IS_HOST_MODULE |
| 73 | # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden) |
| 74 | SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH) |
| 75 | ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 76 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 77 | my_sanitize_diag := |
Colin Cross | 2361842 | 2016-11-02 15:05:21 -0700 | [diff] [blame] | 78 | endif |
Andreas Gampe | cd25740 | 2016-06-20 17:36:49 -0700 | [diff] [blame] | 79 | endif |
| 80 | |
Andreas Gampe | 3d3b0c9 | 2016-06-20 17:46:29 -0700 | [diff] [blame] | 81 | # Add a filter point based on module owner (to lighten the burden). The format is a space- or |
| 82 | # colon-separated list of owner names. |
| 83 | ifneq (,$(SANITIZE_NEVER_BY_OWNER)) |
| 84 | ifneq (,$(LOCAL_MODULE_OWNER)) |
| 85 | ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER)))) |
| 86 | $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.) |
| 87 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 88 | my_sanitize_diag := |
Andreas Gampe | 3d3b0c9 | 2016-06-20 17:46:29 -0700 | [diff] [blame] | 89 | endif |
| 90 | endif |
| 91 | endif |
| 92 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 93 | # Don't apply sanitizers to NDK code. |
| 94 | ifdef LOCAL_SDK_VERSION |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 95 | my_sanitize := |
Dan Willemsen | f761c0f | 2016-06-28 16:47:43 -0700 | [diff] [blame] | 96 | my_global_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 97 | my_sanitize_diag := |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 98 | endif |
| 99 | |
Dan Albert | 4c40141 | 2015-08-19 20:13:33 -0700 | [diff] [blame] | 100 | # Never always wins. |
| 101 | ifeq ($(LOCAL_SANITIZE),never) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 102 | my_sanitize := |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 103 | my_sanitize_diag := |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 104 | endif |
| 105 | |
Vishwath Mohan | 6106a4e | 2018-05-24 18:04:25 -0700 | [diff] [blame] | 106 | # Enable CFI in included paths (for Arm64 only). |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 107 | ifeq ($(filter cfi, $(my_sanitize)),) |
Vishwath Mohan | 6106a4e | 2018-05-24 18:04:25 -0700 | [diff] [blame] | 108 | ifneq ($(filter arm64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 109 | combined_include_paths := $(CFI_INCLUDE_PATHS) \ |
| 110 | $(PRODUCT_CFI_INCLUDE_PATHS) |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 111 | |
Vishwath Mohan | 6106a4e | 2018-05-24 18:04:25 -0700 | [diff] [blame] | 112 | ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\ |
| 113 | $(filter $(dir)%,$(LOCAL_PATH)))),) |
| 114 | my_sanitize := cfi $(my_sanitize) |
Vishwath Mohan | 6106a4e | 2018-05-24 18:04:25 -0700 | [diff] [blame] | 115 | endif |
Vishwath Mohan | 23b2d2e | 2017-10-31 02:25:16 -0700 | [diff] [blame] | 116 | endif |
| 117 | endif |
| 118 | |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 119 | # If CFI is disabled globally, remove it from my_sanitize. |
Vishwath Mohan | 45665b4 | 2017-01-24 13:20:28 -0800 | [diff] [blame] | 120 | ifeq ($(strip $(ENABLE_CFI)),false) |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 121 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 122 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 123 | endif |
| 124 | |
Vishwath Mohan | a204606 | 2017-02-07 20:28:07 -0800 | [diff] [blame] | 125 | # Disable CFI for arm32 (b/35157333). |
| 126 | ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 127 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 128 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 129 | endif |
| 130 | |
Vishwath Mohan | c026f6d | 2017-04-20 07:39:13 -0700 | [diff] [blame] | 131 | # Also disable CFI if ASAN is enabled. |
| 132 | ifneq ($(filter address,$(my_sanitize)),) |
| 133 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 134 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
| 135 | endif |
| 136 | |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 137 | # Disable sanitizers which need the UBSan runtime for host targets. |
Vishwath Mohan | 96a130b | 2017-11-17 11:19:36 -0800 | [diff] [blame] | 138 | ifdef LOCAL_IS_HOST_MODULE |
| 139 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 140 | my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag)) |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 141 | my_sanitize := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize)) |
| 142 | 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] | 143 | endif |
| 144 | |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 145 | # Support for local sanitize blacklist paths. |
| 146 | ifneq ($(my_sanitize)$(my_global_sanitize),) |
Pirama Arumuga Nainar | 5655833 | 2020-07-30 15:18:07 -0700 | [diff] [blame] | 147 | ifneq ($(LOCAL_SANITIZE_BLOCKLIST),) |
| 148 | my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLOCKLIST) |
| 149 | endif |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 150 | endif |
| 151 | |
Ivan Lozano | b4749cb | 2017-07-21 10:33:32 -0700 | [diff] [blame] | 152 | # Disable integer_overflow if LOCAL_NOSANITIZE=integer. |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 153 | ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),) |
| 154 | ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),) |
| 155 | my_sanitize := $(filter-out integer_overflow,$(my_sanitize)) |
| 156 | my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag)) |
| 157 | endif |
| 158 | endif |
| 159 | |
Evgenii Stepanov | 4282366 | 2016-05-12 13:07:17 -0700 | [diff] [blame] | 160 | my_nosanitize = $(strip $(LOCAL_NOSANITIZE)) |
| 161 | ifneq ($(my_nosanitize),) |
| 162 | my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize)) |
| 163 | endif |
| 164 | |
Evgenii Stepanov | 8841a7f | 2018-07-27 11:54:32 -0700 | [diff] [blame] | 165 | ifneq ($(filter arm x86 x86_64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),) |
| 166 | my_sanitize := $(filter-out hwaddress,$(my_sanitize)) |
| 167 | endif |
| 168 | |
| 169 | ifneq ($(filter hwaddress,$(my_sanitize)),) |
| 170 | my_sanitize := $(filter-out address,$(my_sanitize)) |
| 171 | my_sanitize := $(filter-out thread,$(my_sanitize)) |
Evgenii Stepanov | 88a95a3 | 2018-12-04 17:06:45 -0800 | [diff] [blame] | 172 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
Evgenii Stepanov | 8841a7f | 2018-07-27 11:54:32 -0700 | [diff] [blame] | 173 | endif |
| 174 | |
| 175 | ifneq ($(filter hwaddress,$(my_sanitize)),) |
| 176 | my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_RUNTIME_LIBRARY) |
Evgenii Stepanov | ed90746 | 2018-11-01 15:43:14 -0700 | [diff] [blame] | 177 | ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),) |
Evgenii Stepanov | 8841a7f | 2018-07-27 11:54:32 -0700 | [diff] [blame] | 178 | ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
Ryan Prichard | acf8b0f | 2019-10-21 20:47:53 -0700 | [diff] [blame] | 179 | my_static_libraries := $(my_static_libraries) \ |
| 180 | $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_STATIC_LIBRARY) \ |
| 181 | libdl |
Evgenii Stepanov | 8841a7f | 2018-07-27 11:54:32 -0700 | [diff] [blame] | 182 | endif |
| 183 | endif |
| 184 | endif |
| 185 | |
Dan Albert | a6311b7 | 2015-07-30 10:17:33 -0700 | [diff] [blame] | 186 | # TSAN is not supported on 32-bit architectures. For non-multilib cases, make |
| 187 | # its use an error. For multilib cases, don't use it for the 32-bit case. |
| 188 | ifneq ($(filter thread,$(my_sanitize)),) |
| 189 | ifeq ($(my_32_64_bit_suffix),32) |
| 190 | ifeq ($(my_module_multilib),both) |
| 191 | my_sanitize := $(filter-out thread,$(my_sanitize)) |
| 192 | else |
| 193 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.) |
| 194 | endif |
Yabin Cui | e77c32e | 2017-10-19 14:33:58 -0700 | [diff] [blame] | 195 | else |
| 196 | my_shared_libraries += $(TSAN_RUNTIME_LIBRARY) |
Dan Albert | a6311b7 | 2015-07-30 10:17:33 -0700 | [diff] [blame] | 197 | endif |
| 198 | endif |
| 199 | |
Evgenii Stepanov | 7dcb8b8 | 2016-05-06 18:15:57 -0700 | [diff] [blame] | 200 | ifneq ($(filter safe-stack,$(my_sanitize)),) |
| 201 | ifeq ($(my_32_64_bit_suffix),32) |
| 202 | my_sanitize := $(filter-out safe-stack,$(my_sanitize)) |
| 203 | endif |
| 204 | endif |
| 205 | |
Kostya Kortchinsky | 2cfa997 | 2018-06-14 11:02:15 -0700 | [diff] [blame] | 206 | # Disable Scudo if ASan or TSan is enabled. |
Evgenii Stepanov | 8841a7f | 2018-07-27 11:54:32 -0700 | [diff] [blame] | 207 | ifneq ($(filter address thread hwaddress,$(my_sanitize)),) |
Kostya Kortchinsky | 2cfa997 | 2018-06-14 11:02:15 -0700 | [diff] [blame] | 208 | my_sanitize := $(filter-out scudo,$(my_sanitize)) |
| 209 | endif |
| 210 | |
Kostya Kortchinsky | 0273240 | 2019-02-01 09:06:42 -0800 | [diff] [blame] | 211 | # Or if disabled globally. |
Anton Hansson | 8dab0a6 | 2019-03-28 15:45:40 +0000 | [diff] [blame] | 212 | ifeq ($(PRODUCT_DISABLE_SCUDO),true) |
Kostya Kortchinsky | 0273240 | 2019-02-01 09:06:42 -0800 | [diff] [blame] | 213 | my_sanitize := $(filter-out scudo,$(my_sanitize)) |
| 214 | endif |
| 215 | |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 216 | # Undefined symbols can occur if a non-sanitized library links |
| 217 | # sanitized static libraries. That's OK, because the executable |
| 218 | # always depends on the ASan runtime library, which defines these |
| 219 | # symbols. |
Evgenii Stepanov | 912b51f | 2016-05-19 17:49:51 -0700 | [diff] [blame] | 220 | ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),) |
Evgenii Stepanov | 5adfcb1 | 2015-06-25 16:38:25 -0700 | [diff] [blame] | 221 | ifndef LOCAL_IS_HOST_MODULE |
| 222 | ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES) |
| 223 | ifeq ($(my_sanitize),) |
| 224 | my_allow_undefined_symbols := true |
| 225 | endif |
| 226 | endif |
| 227 | endif |
| 228 | endif |
| 229 | |
Dan Albert | b5b2ffe | 2015-04-16 18:07:07 -0700 | [diff] [blame] | 230 | ifneq ($(filter default-ub,$(my_sanitize)),) |
| 231 | my_sanitize := $(CLANG_DEFAULT_UB_CHECKS) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 232 | endif |
| 233 | |
Mitch Phillips | ee8f4a0 | 2019-05-01 14:37:33 -0700 | [diff] [blame] | 234 | ifneq ($(filter fuzzer,$(my_sanitize)),) |
| 235 | # SANITIZE_TARGET='fuzzer' actually means to create the fuzzer coverage |
| 236 | # information, not to link against the fuzzer main(). |
| 237 | my_sanitize := $(filter-out fuzzer,$(my_sanitize)) |
| 238 | my_sanitize += fuzzer-no-link |
| 239 | |
| 240 | # TODO(b/131771163): Disable LTO for fuzzer builds. Note that Cfi causes |
| 241 | # dependency on LTO. |
| 242 | my_sanitize := $(filter-out cfi,$(my_sanitize)) |
| 243 | my_cflags += -fno-lto |
| 244 | my_ldflags += -fno-lto |
Mitch Phillips | ee2dcac | 2019-06-17 10:37:55 -0700 | [diff] [blame] | 245 | |
| 246 | # TODO(b/133876586): Disable experimental pass manager for fuzzer builds. |
| 247 | my_cflags += -fno-experimental-new-pass-manager |
Ivan Krasin | 74b32b8 | 2015-09-18 11:54:43 -0700 | [diff] [blame] | 248 | endif |
| 249 | |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 250 | ifneq ($(filter integer_overflow,$(my_sanitize)),) |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 251 | # Respect LOCAL_NOSANITIZE for integer-overflow flags. |
| 252 | ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),) |
| 253 | my_sanitize += signed-integer-overflow |
| 254 | endif |
| 255 | ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),) |
| 256 | my_sanitize += unsigned-integer-overflow |
| 257 | endif |
| 258 | my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS) |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 259 | |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 260 | # Check for diagnostics mode. |
| 261 | ifneq ($(filter integer_overflow,$(my_sanitize_diag)),) |
| 262 | ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),) |
| 263 | ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
Ivan Lozano | 911cb99 | 2018-02-21 13:41:05 -0800 | [diff] [blame] | 264 | my_sanitize_diag += signed-integer-overflow |
| 265 | my_sanitize_diag += unsigned-integer-overflow |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 266 | else |
| 267 | $(call pretty-error,Make cannot apply integer overflow diagnostics to static binary.) |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 268 | endif |
Ivan Lozano | 702e8bd | 2018-03-15 14:49:20 -0700 | [diff] [blame] | 269 | else |
| 270 | $(call pretty-error,Make cannot apply integer overflow diagnostics to static library.) |
Ivan Lozano | 4a36373 | 2017-06-28 09:11:26 -0700 | [diff] [blame] | 271 | endif |
| 272 | endif |
| 273 | my_sanitize := $(filter-out integer_overflow,$(my_sanitize)) |
| 274 | endif |
| 275 | |
| 276 | # Makes sure integer_overflow diagnostics is removed from the diagnostics list |
| 277 | # even if integer_overflow is not set for some reason. |
| 278 | ifneq ($(filter integer_overflow,$(my_sanitize_diag)),) |
| 279 | my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag)) |
| 280 | endif |
| 281 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 282 | ifneq ($(my_sanitize),) |
Stephen Hines | e8119e9 | 2015-11-09 16:32:11 -0800 | [diff] [blame] | 283 | fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 284 | my_cflags += -fsanitize=$(fsanitize_arg) |
Evgenii Stepanov | 9b82b3f | 2018-08-31 12:57:26 -0700 | [diff] [blame] | 285 | my_asflags += -fsanitize=$(fsanitize_arg) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 286 | |
Mitch Phillips | ee8f4a0 | 2019-05-01 14:37:33 -0700 | [diff] [blame] | 287 | # When fuzzing, we wish to crash with diagnostics on any bug. |
| 288 | ifneq ($(filter fuzzer-no-link,$(my_sanitize)),) |
| 289 | my_cflags += -fno-sanitize-trap=all |
| 290 | my_cflags += -fno-sanitize-recover=all |
| 291 | my_ldflags += -fsanitize=fuzzer-no-link |
| 292 | else ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 293 | my_cflags += -fno-sanitize-recover=all |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 294 | my_ldflags += -fsanitize=$(fsanitize_arg) |
Dan Albert | abf4bc9 | 2015-06-16 23:27:34 -0700 | [diff] [blame] | 295 | else |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 296 | my_cflags += -fsanitize-trap=all |
| 297 | my_cflags += -ftrap-function=abort |
Evgenii Stepanov | 55f73e6 | 2016-05-12 13:07:36 -0700 | [diff] [blame] | 298 | ifneq ($(filter address thread,$(my_sanitize)),) |
Evgenii Stepanov | 71faa19 | 2016-05-19 17:45:21 -0700 | [diff] [blame] | 299 | my_cflags += -fno-sanitize-trap=address,thread |
Evgenii Stepanov | 55f73e6 | 2016-05-12 13:07:36 -0700 | [diff] [blame] | 300 | my_shared_libraries += libdl |
| 301 | endif |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 302 | endif |
| 303 | endif |
| 304 | |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 305 | ifneq ($(filter cfi,$(my_sanitize)),) |
Evgenii Stepanov | 81bea1b | 2017-01-20 14:12:08 -0800 | [diff] [blame] | 306 | # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp). |
| 307 | # LLVM is not set up to do this on a function basis, so force Thumb on the |
| 308 | # entire module. |
| 309 | LOCAL_ARM_MODE := thumb |
Vishwath Mohan | 5b69c06 | 2017-02-14 07:55:37 -0800 | [diff] [blame] | 310 | my_cflags += $(CFI_EXTRA_CFLAGS) |
Evgenii Stepanov | 9b82b3f | 2018-08-31 12:57:26 -0700 | [diff] [blame] | 311 | my_asflags += $(CFI_EXTRA_ASFLAGS) |
Vishwath Mohan | 85f7244 | 2017-11-01 09:21:20 +0000 | [diff] [blame] | 312 | # Only append the default visibility flag if -fvisibility has not already been |
| 313 | # set to hidden. |
| 314 | ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),) |
| 315 | my_cflags += -fvisibility=default |
| 316 | endif |
Vishwath Mohan | 5b69c06 | 2017-02-14 07:55:37 -0800 | [diff] [blame] | 317 | my_ldflags += $(CFI_EXTRA_LDFLAGS) |
Evgenii Stepanov | e1b96f3 | 2017-01-23 16:57:38 -0800 | [diff] [blame] | 318 | my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so |
Vishwath Mohan | 85f7244 | 2017-11-01 09:21:20 +0000 | [diff] [blame] | 319 | |
| 320 | ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 321 | my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags)) |
| 322 | my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags)) |
| 323 | else |
| 324 | # Apply the version script to non-static executables |
| 325 | my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map |
| 326 | LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map |
| 327 | endif |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 328 | endif |
| 329 | |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 330 | # If local or global modules need ASAN, add linker flags. |
| 331 | ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 332 | my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS) |
| 333 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 334 | # -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 335 | # libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
Dan Albert | 1f0d530 | 2015-04-28 14:55:50 -0700 | [diff] [blame] | 336 | my_ldflags += -Wl,--no-as-needed |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 337 | else |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 338 | # Add asan libraries unless LOCAL_MODULE is the asan library. |
Evgenii Stepanov | f0b15e1 | 2015-04-24 16:34:47 -0700 | [diff] [blame] | 339 | # ASan runtime library must be the first in the link order. |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 340 | ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY))) |
| 341 | my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \ |
| 342 | $(my_shared_libraries) |
| 343 | endif |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 344 | |
| 345 | # Do not add unnecessary dependency in shared libraries. |
| 346 | ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES) |
| 347 | my_ldflags += -Wl,--as-needed |
| 348 | endif |
Ying Wang | a05e222 | 2015-08-17 16:13:24 -0700 | [diff] [blame] | 349 | |
Mikhail Naganov | aa73cef | 2018-12-20 15:55:08 -0800 | [diff] [blame] | 350 | ifneq ($(filter EXECUTABLES NATIVE_TESTS,$(LOCAL_MODULE_CLASS)),) |
Colin Cross | d08699e | 2016-07-17 15:28:07 -0700 | [diff] [blame] | 351 | ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 352 | my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER) |
| 353 | # Make sure linker_asan get installed. |
Logan Chien | c6d2cf8 | 2019-01-31 17:07:50 +0800 | [diff] [blame] | 354 | $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER_FILE) |
Colin Cross | d08699e | 2016-07-17 15:28:07 -0700 | [diff] [blame] | 355 | endif |
| 356 | endif |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 357 | endif |
| 358 | endif |
| 359 | |
Chih-Hung Hsieh | ad741e6 | 2016-03-09 14:54:55 -0800 | [diff] [blame] | 360 | # If local module needs ASAN, add compiler flags. |
| 361 | ifneq ($(filter address,$(my_sanitize)),) |
| 362 | # Frame pointer based unwinder in ASan requires ARM frame setup. |
| 363 | LOCAL_ARM_MODE := arm |
| 364 | my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS) |
| 365 | ifndef LOCAL_IS_HOST_MODULE |
| 366 | my_cflags += -mllvm -asan-globals=0 |
| 367 | endif |
| 368 | endif |
| 369 | |
Evgenii Stepanov | aec1ffc | 2018-08-28 13:52:08 -0700 | [diff] [blame] | 370 | # If local module needs HWASAN, add compiler flags. |
| 371 | ifneq ($(filter hwaddress,$(my_sanitize)),) |
| 372 | my_cflags += $(HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS) |
| 373 | endif |
| 374 | |
Dan Willemsen | 58634e1 | 2020-03-06 18:09:06 -0800 | [diff] [blame] | 375 | # Use minimal diagnostics when integer overflow is enabled; never do it for HOST modules |
| 376 | ifeq ($(LOCAL_IS_HOST_MODULE),) |
Ivan Lozano | 911cb99 | 2018-02-21 13:41:05 -0800 | [diff] [blame] | 377 | # Pre-emptively add UBSAN minimal runtime incase a static library dependency requires it |
| 378 | ifeq ($(filter STATIC_LIBRARIES,$(LOCAL_MODULE_CLASS)),) |
| 379 | ifndef LOCAL_SDK_VERSION |
| 380 | my_static_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY) |
Ivan Lozano | e508169 | 2018-05-11 14:09:36 -0700 | [diff] [blame] | 381 | 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] | 382 | endif |
| 383 | endif |
| 384 | ifneq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize)),) |
Kostya Kortchinsky | 47c10eb | 2018-10-11 08:56:12 -0700 | [diff] [blame] | 385 | ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize_diag)),) |
Ivan Lozano | 911cb99 | 2018-02-21 13:41:05 -0800 | [diff] [blame] | 386 | ifeq ($(filter cfi,$(my_sanitize_diag)),) |
Mitch Phillips | ee8f4a0 | 2019-05-01 14:37:33 -0700 | [diff] [blame] | 387 | ifeq ($(filter address hwaddress fuzzer-no-link,$(my_sanitize)),) |
Ivan Lozano | 911cb99 | 2018-02-21 13:41:05 -0800 | [diff] [blame] | 388 | my_cflags += -fsanitize-minimal-runtime |
| 389 | my_cflags += -fno-sanitize-trap=integer |
| 390 | my_cflags += -fno-sanitize-recover=integer |
| 391 | endif |
| 392 | endif |
| 393 | endif |
| 394 | endif |
| 395 | endif |
| 396 | |
Kostya Kortchinsky | 47c10eb | 2018-10-11 08:56:12 -0700 | [diff] [blame] | 397 | # For Scudo, we opt for the minimal runtime, unless some diagnostics are enabled. |
| 398 | ifneq ($(filter scudo,$(my_sanitize)),) |
| 399 | ifeq ($(filter unsigned-integer-overflow signed-integer-overflow integer cfi,$(my_sanitize_diag)),) |
| 400 | my_cflags += -fsanitize-minimal-runtime |
| 401 | endif |
| 402 | ifneq ($(filter -fsanitize-minimal-runtime,$(my_cflags)),) |
| 403 | my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_MINIMAL_RUNTIME_LIBRARY) |
| 404 | else |
| 405 | my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_RUNTIME_LIBRARY) |
| 406 | endif |
| 407 | endif |
| 408 | |
Dan Albert | 9f17655 | 2015-04-28 11:26:45 -0700 | [diff] [blame] | 409 | ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),) |
| 410 | recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)), |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame] | 411 | my_cflags += -fsanitize-recover=$(recover_arg) |
| 412 | endif |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 413 | |
Ivan Lozano | 5fb2de7 | 2018-12-12 10:04:34 -0800 | [diff] [blame] | 414 | ifneq ($(strip $(LOCAL_SANITIZE_NO_RECOVER)),) |
| 415 | no_recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_NO_RECOVER)), |
| 416 | my_cflags += -fno-sanitize-recover=$(no_recover_arg) |
| 417 | endif |
| 418 | |
Vishwath Mohan | 8dcfdce | 2017-01-18 17:50:29 -0800 | [diff] [blame] | 419 | ifneq ($(my_sanitize_diag),) |
Vishwath Mohan | 85f7244 | 2017-11-01 09:21:20 +0000 | [diff] [blame] | 420 | # TODO(vishwath): Add diagnostic support for static executables once |
| 421 | # we switch to clang-4393122 (which adds the static ubsan runtime |
| 422 | # that this depends on) |
| 423 | ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true) |
| 424 | notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)), |
| 425 | my_cflags += -fno-sanitize-trap=$(notrap_arg) |
| 426 | # Diagnostic requires a runtime library, unless ASan or TSan are also enabled. |
Evgenii Stepanov | 8841a7f | 2018-07-27 11:54:32 -0700 | [diff] [blame] | 427 | ifeq ($(filter address thread scudo hwaddress,$(my_sanitize)),) |
Vishwath Mohan | 85f7244 | 2017-11-01 09:21:20 +0000 | [diff] [blame] | 428 | # Does not have to be the first DT_NEEDED unlike ASan. |
| 429 | my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY) |
| 430 | endif |
Evgenii Stepanov | 202c7a7 | 2016-07-07 10:56:39 -0700 | [diff] [blame] | 431 | endif |
| 432 | endif |
Chih-Hung Hsieh | 1871062 | 2018-11-17 20:18:08 -0800 | [diff] [blame] | 433 | |
| 434 | # http://b/119329758, Android core does not boot up with this sanitizer yet. |
| 435 | # Previously sanitized modules might not pass new implicit-integer-sign-change check. |
| 436 | # Disable this check unless it has been explicitly specified. |
| 437 | ifneq ($(findstring fsanitize,$(my_cflags)),) |
| 438 | ifneq ($(findstring integer,$(my_cflags)),) |
| 439 | ifeq ($(findstring sanitize=implicit-integer-sign-change,$(my_cflags)),) |
| 440 | my_cflags += -fno-sanitize=implicit-integer-sign-change |
| 441 | endif |
| 442 | endif |
| 443 | endif |
Yabin Cui | 462c12d | 2021-01-14 14:05:54 -0800 | [diff] [blame^] | 444 | |
| 445 | # http://b/177566116, libc++ may crash with this sanitizer. |
| 446 | # Disable this check unless it has been explicitly specified. |
| 447 | ifneq ($(findstring fsanitize,$(my_cflags)),) |
| 448 | ifneq ($(findstring integer,$(my_cflags)),) |
| 449 | ifeq ($(findstring sanitize=unsigned-shift-base,$(my_cflags)),) |
| 450 | my_cflags += -fno-sanitize=unsigned-shift-base |
| 451 | endif |
| 452 | endif |
| 453 | endif |