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 | |
| 7 | # Keep compatibility for LOCAL_ADDRESS_SANITIZER until all targets have moved to |
| 8 | # `LOCAL_SANITIZE := address`. |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 9 | ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),true) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 10 | my_sanitize += address |
| 11 | endif |
| 12 | |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 13 | # And `LOCAL_SANITIZE := never`. |
| 14 | ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),false) |
| 15 | my_sanitize := never |
| 16 | endif |
| 17 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 18 | # Don't apply sanitizers to NDK code. |
| 19 | ifdef LOCAL_SDK_VERSION |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 20 | my_sanitize := never |
| 21 | endif |
| 22 | |
Dan Albert | 4c92a68 | 2015-04-17 11:04:06 -0700 | [diff] [blame] | 23 | # Configure SANITIZE_HOST. |
| 24 | ifdef LOCAL_IS_HOST_MODULE |
| 25 | ifeq ($(my_sanitize),) |
| 26 | my_sanitize := $(strip $(SANITIZE_HOST)) |
| 27 | |
| 28 | # SANTIZIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address. |
| 29 | ifeq ($(my_sanitize),true) |
| 30 | my_sanitize := address |
| 31 | endif |
| 32 | |
| 33 | # SANITIZE_HOST is only in effect if the module is already using clang (host |
| 34 | # modules that haven't set `LOCAL_CLANG := false` and device modules that |
| 35 | # have set `LOCAL_CLANG := true`. |
| 36 | ifneq ($(my_clang),true) |
| 37 | my_sanitize := |
| 38 | endif |
| 39 | endif |
Dan Albert | 27ccb75 | 2015-04-16 16:21:02 -0700 | [diff] [blame] | 40 | endif |
| 41 | |
| 42 | ifeq ($(my_sanitize),never) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 43 | my_sanitize := |
| 44 | endif |
| 45 | |
Dan Albert | 94b5791 | 2015-04-17 09:48:33 -0700 | [diff] [blame] | 46 | # Sanitizers can only be used with clang. |
| 47 | ifneq ($(my_clang),true) |
| 48 | ifneq ($(my_sanitize),) |
| 49 | $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true) |
| 50 | endif |
| 51 | endif |
| 52 | |
Dan Albert | b5b2ffe | 2015-04-16 18:07:07 -0700 | [diff] [blame] | 53 | ifneq ($(filter default-ub,$(my_sanitize)),) |
| 54 | my_sanitize := $(CLANG_DEFAULT_UB_CHECKS) |
| 55 | my_ldlibs += -ldl |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 56 | |
Dan Albert | b5b2ffe | 2015-04-16 18:07:07 -0700 | [diff] [blame] | 57 | ifdef LOCAL_IS_HOST_MODULE |
| 58 | my_cflags += -fno-sanitize-recover=all |
| 59 | else |
| 60 | my_cflags += -fsanitize-undefined-trap-on-error |
| 61 | endif |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 62 | endif |
| 63 | |
| 64 | ifneq ($(my_sanitize),) |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 65 | fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)), |
| 66 | my_cflags += -fsanitize=$(fsanitize_arg) |
| 67 | |
| 68 | ifdef LOCAL_IS_HOST_MODULE |
| 69 | my_ldflags += -fsanitize=$(fsanitize_arg) |
| 70 | endif |
| 71 | endif |
| 72 | |
| 73 | ifneq ($(filter address,$(my_sanitize)),) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 74 | # Frame pointer based unwinder in ASan requires ARM frame setup. |
| 75 | LOCAL_ARM_MODE := arm |
| 76 | my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS) |
| 77 | my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS) |
| 78 | ifdef LOCAL_IS_HOST_MODULE |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 79 | # -nodefaultlibs (provided with libc++) prevents the driver from linking |
| 80 | # libraries needed with -fsanitize=address. http://b/18650275 (WAI) |
| 81 | my_ldlibs += -ldl -lpthread |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 82 | else |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 83 | my_shared_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES) |
| 84 | my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES) |
| 85 | endif |
| 86 | endif |
| 87 | |
| 88 | ifneq ($(filter undefined,$(my_sanitize)),) |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame^] | 89 | my_cflags += -fno-sanitize-recover=all |
| 90 | |
Dan Albert | 08cca28 | 2014-12-11 18:56:26 -0800 | [diff] [blame] | 91 | ifdef LOCAL_IS_HOST_MODULE |
| 92 | my_ldlibs += -ldl |
| 93 | else |
| 94 | $(error ubsan is not yet supported on the target) |
Dan Albert | 4ae5d4b | 2014-10-31 16:23:08 -0700 | [diff] [blame] | 95 | endif |
| 96 | endif |
Dan Albert | 4111d48 | 2015-04-16 18:08:44 -0700 | [diff] [blame^] | 97 | |
| 98 | |
| 99 | ifeq ($(strip $(LOCAL_SANITIZE_RECOVER)),true) |
| 100 | recover_arg := $(subst $(space),$(comma),$(my_sanitize)), |
| 101 | my_cflags += -fsanitize-recover=$(recover_arg) |
| 102 | endif |