blob: f71996d2b29718b5dca60cff0e92bd17b2788113 [file] [log] [blame]
Dan Albert4ae5d4b2014-10-31 16:23:08 -07001##############################################
2## Perform configuration steps for sanitizers.
3##############################################
4
Dan Albert27ccb752015-04-16 16:21:02 -07005my_sanitize := $(strip $(LOCAL_SANITIZE))
Dan Albert08cca282014-12-11 18:56:26 -08006
Dan Albert08cca282014-12-11 18:56:26 -08007# Don't apply sanitizers to NDK code.
8ifdef LOCAL_SDK_VERSION
Dan Albert27ccb752015-04-16 16:21:02 -07009 my_sanitize := never
10endif
11
Evgenii Stepanov3632cc32015-06-11 13:57:10 -070012# Configure SANITIZE_HOST / SANITIZE_TARGET.
13ifeq ($(my_sanitize),)
14 ifdef LOCAL_IS_HOST_MODULE
Dan Albert4c92a682015-04-17 11:04:06 -070015 my_sanitize := $(strip $(SANITIZE_HOST))
Evgenii Stepanov3632cc32015-06-11 13:57:10 -070016 else
17 my_sanitize := $(strip $(SANITIZE_TARGET))
18 endif
Dan Albert4c92a682015-04-17 11:04:06 -070019
Evgenii Stepanov3632cc32015-06-11 13:57:10 -070020 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
21 ifeq ($(my_sanitize),true)
22 my_sanitize := address
23 endif
Dan Albert4c92a682015-04-17 11:04:06 -070024
Evgenii Stepanov3632cc32015-06-11 13:57:10 -070025 # SANITIZE_HOST is only in effect if the module is already using clang (host
26 # modules that haven't set `LOCAL_CLANG := false` and device modules that
27 # have set `LOCAL_CLANG := true`.
28 ifneq ($(my_clang),true)
29 my_sanitize :=
Dan Albert4c92a682015-04-17 11:04:06 -070030 endif
Dan Albert27ccb752015-04-16 16:21:02 -070031endif
32
33ifeq ($(my_sanitize),never)
Dan Albert08cca282014-12-11 18:56:26 -080034 my_sanitize :=
35endif
36
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070037# Undefined symbols can occur if a non-sanitized library links
38# sanitized static libraries. That's OK, because the executable
39# always depends on the ASan runtime library, which defines these
40# symbols.
41ifneq ($(strip $(SANITIZE_TARGET)),)
42 ifndef LOCAL_IS_HOST_MODULE
43 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
44 ifeq ($(my_sanitize),)
45 my_allow_undefined_symbols := true
46 endif
47 endif
Evgenii Stepanov4b396e42015-07-20 16:32:53 -070048 # Workaround for a bug in AddressSanitizer that breaks stack unwinding.
49 # https://code.google.com/p/address-sanitizer/issues/detail?id=387
50 # Revert when external/compiler-rt is updated past r236014.
51 LOCAL_PACK_MODULE_RELOCATIONS := false
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070052 endif
53endif
54
Dan Albert94b57912015-04-17 09:48:33 -070055# Sanitizers can only be used with clang.
56ifneq ($(my_clang),true)
57 ifneq ($(my_sanitize),)
58 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
59 endif
60endif
61
Dan Albertb5b2ffe2015-04-16 18:07:07 -070062ifneq ($(filter default-ub,$(my_sanitize)),)
63 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -080064endif
65
66ifneq ($(my_sanitize),)
Dan Albert08cca282014-12-11 18:56:26 -080067 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)),
68 my_cflags += -fsanitize=$(fsanitize_arg)
69
70 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -070071 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -080072 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albertabf4bc92015-06-16 23:27:34 -070073 my_ldlibs += -ldl
74 else
75 my_cflags += -fsanitize-undefined-trap-on-error
76 my_cflags += -ftrap-function=abort
77 my_shared_libraries += libdl
Dan Albert08cca282014-12-11 18:56:26 -080078 endif
79endif
80
81ifneq ($(filter address,$(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -070082 # Frame pointer based unwinder in ASan requires ARM frame setup.
83 LOCAL_ARM_MODE := arm
84 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
85 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
86 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -080087 # -nodefaultlibs (provided with libc++) prevents the driver from linking
88 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albertabf4bc92015-06-16 23:27:34 -070089 my_ldlibs += -lm -lpthread
Dan Albert1f0d5302015-04-28 14:55:50 -070090 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -070091 else
Evgenii Stepanovff7a7812015-07-13 18:12:17 -070092 my_cflags += -mllvm -asan-globals=0
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -070093 # ASan runtime library must be the first in the link order.
94 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
95 $(my_shared_libraries) \
96 $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES)
Dan Albert08cca282014-12-11 18:56:26 -080097 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -070098 my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH)
Evgenii Stepanov8f5e67a2015-07-10 18:06:51 -070099 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
Dan Albert08cca282014-12-11 18:56:26 -0800100 endif
101endif
102
103ifneq ($(filter undefined,$(my_sanitize)),)
Dan Albertabf4bc92015-06-16 23:27:34 -0700104 ifndef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800105 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700106 endif
107endif
Dan Albert4111d482015-04-16 18:08:44 -0700108
Dan Albert9f176552015-04-28 11:26:45 -0700109ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
110 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700111 my_cflags += -fsanitize-recover=$(recover_arg)
112endif