blob: c911891630c5e6a8f92ccdcfd6cb3d65ec9e04f3 [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
48 endif
49endif
50
Dan Albert94b57912015-04-17 09:48:33 -070051# Sanitizers can only be used with clang.
52ifneq ($(my_clang),true)
53 ifneq ($(my_sanitize),)
54 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
55 endif
56endif
57
Dan Albertb5b2ffe2015-04-16 18:07:07 -070058ifneq ($(filter default-ub,$(my_sanitize)),)
59 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -080060endif
61
62ifneq ($(my_sanitize),)
Dan Albert08cca282014-12-11 18:56:26 -080063 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)),
64 my_cflags += -fsanitize=$(fsanitize_arg)
65
66 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -070067 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -080068 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albertabf4bc92015-06-16 23:27:34 -070069 my_ldlibs += -ldl
70 else
71 my_cflags += -fsanitize-undefined-trap-on-error
72 my_cflags += -ftrap-function=abort
73 my_shared_libraries += libdl
Dan Albert08cca282014-12-11 18:56:26 -080074 endif
75endif
76
77ifneq ($(filter address,$(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -070078 # Frame pointer based unwinder in ASan requires ARM frame setup.
79 LOCAL_ARM_MODE := arm
80 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
81 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
82 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -080083 # -nodefaultlibs (provided with libc++) prevents the driver from linking
84 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albertabf4bc92015-06-16 23:27:34 -070085 my_ldlibs += -lm -lpthread
Dan Albert1f0d5302015-04-28 14:55:50 -070086 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -070087 else
Evgenii Stepanovff7a7812015-07-13 18:12:17 -070088 my_cflags += -mllvm -asan-globals=0
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -070089 # ASan runtime library must be the first in the link order.
90 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
91 $(my_shared_libraries) \
92 $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES)
Dan Albert08cca282014-12-11 18:56:26 -080093 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -070094 my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH)
Dan Albert08cca282014-12-11 18:56:26 -080095 endif
96endif
97
98ifneq ($(filter undefined,$(my_sanitize)),)
Dan Albertabf4bc92015-06-16 23:27:34 -070099 ifndef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800100 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700101 endif
102endif
Dan Albert4111d482015-04-16 18:08:44 -0700103
Dan Albert9f176552015-04-28 11:26:45 -0700104ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
105 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700106 my_cflags += -fsanitize-recover=$(recover_arg)
107endif