blob: b69a6ce20baae99d6ddb15d8febf571b44f67f43 [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 Albert4c401412015-08-19 20:13:33 -07007# SANITIZE_HOST is only in effect if the module is already using clang (host
8# modules that haven't set `LOCAL_CLANG := false` and device modules that
9# have set `LOCAL_CLANG := true`.
10my_global_sanitize :=
11ifeq ($(my_clang),true)
12 ifdef LOCAL_IS_HOST_MODULE
13 my_global_sanitize := $(strip $(SANITIZE_HOST))
14
15 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
16 my_global_sanitize := $(subst true,address,$(my_global_sanitize))
17 else
18 my_global_sanitize := $(strip $(SANITIZE_TARGET))
19 endif
20endif
21
22# The sanitizer specified by the environment wins over the module.
23ifneq ($(my_global_sanitize),)
24 my_sanitize := $(my_global_sanitize)
25endif
26
Dan Albert08cca282014-12-11 18:56:26 -080027# Don't apply sanitizers to NDK code.
28ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -070029 my_sanitize :=
Dan Albert27ccb752015-04-16 16:21:02 -070030endif
31
Dan Albert4c401412015-08-19 20:13:33 -070032# Never always wins.
33ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -080034 my_sanitize :=
35endif
36
Evgenii Stepanov42823662016-05-12 13:07:17 -070037my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
38ifneq ($(my_nosanitize),)
39 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
40endif
41
Dan Alberta6311b72015-07-30 10:17:33 -070042# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
43# its use an error. For multilib cases, don't use it for the 32-bit case.
44ifneq ($(filter thread,$(my_sanitize)),)
45 ifeq ($(my_32_64_bit_suffix),32)
46 ifeq ($(my_module_multilib),both)
47 my_sanitize := $(filter-out thread,$(my_sanitize))
48 else
49 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
50 endif
51 endif
52endif
53
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070054# Undefined symbols can occur if a non-sanitized library links
55# sanitized static libraries. That's OK, because the executable
56# always depends on the ASan runtime library, which defines these
57# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -070058ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -070059 ifndef LOCAL_IS_HOST_MODULE
60 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
61 ifeq ($(my_sanitize),)
62 my_allow_undefined_symbols := true
63 endif
64 endif
65 endif
66endif
67
Dan Albert94b57912015-04-17 09:48:33 -070068# Sanitizers can only be used with clang.
69ifneq ($(my_clang),true)
70 ifneq ($(my_sanitize),)
71 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
72 endif
73endif
74
Dan Albertb5b2ffe2015-04-16 18:07:07 -070075ifneq ($(filter default-ub,$(my_sanitize)),)
76 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -080077endif
78
Ivan Krasin74b32b82015-09-18 11:54:43 -070079ifneq ($(filter coverage,$(my_sanitize)),)
80 ifeq ($(filter address,$(my_sanitize)),)
81 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
82 endif
83 my_cflags += -fsanitize-coverage=edge,indirect-calls,8bit-counters,trace-cmp
84 my_sanitize := $(filter-out coverage,$(my_sanitize))
85endif
86
Dan Albert08cca282014-12-11 18:56:26 -080087ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -080088 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -080089 my_cflags += -fsanitize=$(fsanitize_arg)
90
91 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -070092 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -080093 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albert29224112015-08-13 17:25:10 -070094 my_ldlibs += -lrt -ldl
Dan Albertabf4bc92015-06-16 23:27:34 -070095 else
Stephen Hinese8119e92015-11-09 16:32:11 -080096 ifeq ($(filter address,$(my_sanitize)),)
97 my_cflags += -fsanitize-trap=all
98 my_cflags += -ftrap-function=abort
99 endif
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700100 ifneq ($(filter address thread,$(my_sanitize)),)
101 my_shared_libraries += libdl
102 endif
Dan Albert08cca282014-12-11 18:56:26 -0800103 endif
104endif
105
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800106# If local or global modules need ASAN, add linker flags.
107ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700108 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
109 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800110 # -nodefaultlibs (provided with libc++) prevents the driver from linking
111 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albertabf4bc92015-06-16 23:27:34 -0700112 my_ldlibs += -lm -lpthread
Dan Albert1f0d5302015-04-28 14:55:50 -0700113 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700114 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800115 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700116 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800117 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
118 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
119 $(my_shared_libraries)
120 endif
121 ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
122 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
123 endif
124
125 # Do not add unnecessary dependency in shared libraries.
126 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
127 my_ldflags += -Wl,--as-needed
128 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700129
Evgenii Stepanov8f5e67a2015-07-10 18:06:51 -0700130 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
Ying Wanga05e2222015-08-17 16:13:24 -0700131 # Make sure linker_asan get installed.
132 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
Dan Albert08cca282014-12-11 18:56:26 -0800133 endif
134endif
135
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800136# If local module needs ASAN, add compiler flags.
137ifneq ($(filter address,$(my_sanitize)),)
138 # Frame pointer based unwinder in ASan requires ARM frame setup.
139 LOCAL_ARM_MODE := arm
140 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
141 ifndef LOCAL_IS_HOST_MODULE
142 my_cflags += -mllvm -asan-globals=0
143 endif
144endif
145
Dan Albert08cca282014-12-11 18:56:26 -0800146ifneq ($(filter undefined,$(my_sanitize)),)
Dan Albertabf4bc92015-06-16 23:27:34 -0700147 ifndef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800148 $(error ubsan is not yet supported on the target)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700149 endif
150endif
Dan Albert4111d482015-04-16 18:08:44 -0700151
Dan Albert9f176552015-04-28 11:26:45 -0700152ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
153 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700154 my_cflags += -fsanitize-recover=$(recover_arg)
155endif