blob: 7eb1c89817095829ff0648449460d263fb9747e1 [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
7# Keep compatibility for LOCAL_ADDRESS_SANITIZER until all targets have moved to
8# `LOCAL_SANITIZE := address`.
Dan Albert4ae5d4b2014-10-31 16:23:08 -07009ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),true)
Dan Albert08cca282014-12-11 18:56:26 -080010 my_sanitize += address
11endif
12
Dan Albert27ccb752015-04-16 16:21:02 -070013# And `LOCAL_SANITIZE := never`.
14ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),false)
15 my_sanitize := never
16endif
17
Dan Albert08cca282014-12-11 18:56:26 -080018# Don't apply sanitizers to NDK code.
19ifdef LOCAL_SDK_VERSION
Dan Albert27ccb752015-04-16 16:21:02 -070020 my_sanitize := never
21endif
22
Dan Albert4c92a682015-04-17 11:04:06 -070023# Configure SANITIZE_HOST.
24ifdef 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 Albert27ccb752015-04-16 16:21:02 -070040endif
41
42ifeq ($(my_sanitize),never)
Dan Albert08cca282014-12-11 18:56:26 -080043 my_sanitize :=
44endif
45
Dan Albert94b57912015-04-17 09:48:33 -070046# Sanitizers can only be used with clang.
47ifneq ($(my_clang),true)
48 ifneq ($(my_sanitize),)
49 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
50 endif
51endif
52
Dan Albertb5b2ffe2015-04-16 18:07:07 -070053ifneq ($(filter default-ub,$(my_sanitize)),)
54 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
55 my_ldlibs += -ldl
Dan Albert08cca282014-12-11 18:56:26 -080056
Dan Albertb5b2ffe2015-04-16 18:07:07 -070057 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 Albert08cca282014-12-11 18:56:26 -080062endif
63
64ifneq ($(my_sanitize),)
Dan Albert08cca282014-12-11 18:56:26 -080065 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
71endif
72
73ifneq ($(filter address,$(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -070074 # 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 Albert08cca282014-12-11 18:56:26 -080079 # -nodefaultlibs (provided with libc++) prevents the driver from linking
80 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Alberteebb8002015-04-28 13:23:36 -070081 my_ldlibs += -lm -ldl -lpthread
Dan Albertbdd8ca02015-04-28 14:55:50 -070082 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -070083 else
Evgenii Stepanov6708b6c2015-04-24 16:34:47 -070084 # ASan runtime library must be the first in the link order.
85 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
86 $(my_shared_libraries) \
87 $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES)
Dan Albert08cca282014-12-11 18:56:26 -080088 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
Evgenii Stepanov6708b6c2015-04-24 16:34:47 -070089 my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH)
Dan Albert08cca282014-12-11 18:56:26 -080090 endif
91endif
92
93ifneq ($(filter undefined,$(my_sanitize)),)
94 ifdef LOCAL_IS_HOST_MODULE
95 my_ldlibs += -ldl
96 else
97 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -070098 endif
99endif