blob: 9ef2570ea85a28b4b23586b6570e9f07476f977f [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))
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -08006my_sanitize_diag := $(strip $(LOCAL_SANITIZE_DIAG))
Dan Albert08cca282014-12-11 18:56:26 -08007
Dan Albert4c401412015-08-19 20:13:33 -07008# SANITIZE_HOST is only in effect if the module is already using clang (host
9# modules that haven't set `LOCAL_CLANG := false` and device modules that
10# have set `LOCAL_CLANG := true`.
11my_global_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070012my_global_sanitize_diag :=
Dan Albert4c401412015-08-19 20:13:33 -070013ifeq ($(my_clang),true)
14 ifdef LOCAL_IS_HOST_MODULE
Pirama Arumuga Nainar407b6ac2018-06-27 09:40:21 -070015 ifneq ($($(my_prefix)OS),windows)
Pirama Arumuga Nainarc6a3ddf2018-06-26 14:17:55 -070016 my_global_sanitize := $(strip $(SANITIZE_HOST))
Dan Albert4c401412015-08-19 20:13:33 -070017
Pirama Arumuga Nainarc6a3ddf2018-06-26 14:17:55 -070018 # SANITIZE_HOST=true is a deprecated way to say SANITIZE_HOST=address.
19 my_global_sanitize := $(subst true,address,$(my_global_sanitize))
20 endif
Dan Albert4c401412015-08-19 20:13:33 -070021 else
22 my_global_sanitize := $(strip $(SANITIZE_TARGET))
Ivan Lozano4a363732017-06-28 09:11:26 -070023 my_global_sanitize_diag := $(strip $(SANITIZE_TARGET_DIAG))
Dan Albert4c401412015-08-19 20:13:33 -070024 endif
25endif
26
Ivan Lozanob4749cb2017-07-21 10:33:32 -070027# Disable global integer_overflow in excluded paths.
28ifneq ($(filter integer_overflow, $(my_global_sanitize)),)
29 combined_exclude_paths := $(INTEGER_OVERFLOW_EXCLUDE_PATHS) \
30 $(PRODUCT_INTEGER_OVERFLOW_EXCLUDE_PATHS)
31
32 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
33 $(filter $(dir)%,$(LOCAL_PATH)))),)
34 my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize))
35 my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag))
36 endif
37endif
38
Ivan Lozano702e8bd2018-03-15 14:49:20 -070039# Global integer sanitization doesn't support static modules.
40ifeq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
41 my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize))
42 my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag))
43endif
44ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
45 my_global_sanitize := $(filter-out integer_overflow,$(my_global_sanitize))
46 my_global_sanitize_diag := $(filter-out integer_overflow,$(my_global_sanitize_diag))
47endif
48
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -070049# Disable global CFI in excluded paths
50ifneq ($(filter cfi, $(my_global_sanitize)),)
51 combined_exclude_paths := $(CFI_EXCLUDE_PATHS) \
52 $(PRODUCT_CFI_EXCLUDE_PATHS)
53
54 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_exclude_paths)),\
55 $(filter $(dir)%,$(LOCAL_PATH)))),)
56 my_global_sanitize := $(filter-out cfi,$(my_global_sanitize))
57 my_global_sanitize_diag := $(filter-out cfi,$(my_global_sanitize_diag))
58 endif
59endif
60
Dan Albert4c401412015-08-19 20:13:33 -070061ifneq ($(my_global_sanitize),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -070062 my_sanitize := $(my_global_sanitize) $(my_sanitize)
Dan Albert4c401412015-08-19 20:13:33 -070063endif
Ivan Lozano4a363732017-06-28 09:11:26 -070064ifneq ($(my_global_sanitize_diag),)
65 my_sanitize_diag := $(my_global_sanitize_diag) $(my_sanitize_diag)
66endif
Dan Albert4c401412015-08-19 20:13:33 -070067
Andreas Gampe6b30d772016-06-27 15:15:31 -070068# The sanitizer specified in the product configuration wins over the previous.
69ifneq ($(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG),)
70 my_sanitize := $(SANITIZER.$(TARGET_PRODUCT).$(LOCAL_MODULE).CONFIG)
71 ifeq ($(my_sanitize),never)
72 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070073 my_sanitize_diag :=
Andreas Gampe6b30d772016-06-27 15:15:31 -070074 endif
75endif
76
Colin Cross23618422016-11-02 15:05:21 -070077ifndef LOCAL_IS_HOST_MODULE
78 # Add a filter point for 32-bit vs 64-bit sanitization (to lighten the burden)
79 SANITIZE_TARGET_ARCH ?= $(TARGET_ARCH) $(TARGET_2ND_ARCH)
80 ifeq ($(filter $(SANITIZE_TARGET_ARCH),$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
81 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070082 my_sanitize_diag :=
Colin Cross23618422016-11-02 15:05:21 -070083 endif
Andreas Gampecd257402016-06-20 17:36:49 -070084endif
85
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070086# Add a filter point based on module owner (to lighten the burden). The format is a space- or
87# colon-separated list of owner names.
88ifneq (,$(SANITIZE_NEVER_BY_OWNER))
89 ifneq (,$(LOCAL_MODULE_OWNER))
90 ifneq (,$(filter $(LOCAL_MODULE_OWNER),$(subst :, ,$(SANITIZE_NEVER_BY_OWNER))))
91 $(warning Not sanitizing $(LOCAL_MODULE) based on module owner.)
92 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -070093 my_sanitize_diag :=
Andreas Gampe3d3b0c92016-06-20 17:46:29 -070094 endif
95 endif
96endif
97
Dan Albert08cca282014-12-11 18:56:26 -080098# Don't apply sanitizers to NDK code.
99ifdef LOCAL_SDK_VERSION
Dan Albert4c401412015-08-19 20:13:33 -0700100 my_sanitize :=
Dan Willemsenf761c0f2016-06-28 16:47:43 -0700101 my_global_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -0700102 my_sanitize_diag :=
Dan Albert27ccb752015-04-16 16:21:02 -0700103endif
104
Dan Albert4c401412015-08-19 20:13:33 -0700105# Never always wins.
106ifeq ($(LOCAL_SANITIZE),never)
Dan Albert08cca282014-12-11 18:56:26 -0800107 my_sanitize :=
Ivan Lozano4a363732017-06-28 09:11:26 -0700108 my_sanitize_diag :=
Dan Albert08cca282014-12-11 18:56:26 -0800109endif
110
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700111# Enable CFI in included paths (for Arm64 only).
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700112ifeq ($(filter cfi, $(my_sanitize)),)
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700113 ifneq ($(filter arm64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
114 combined_include_paths := $(CFI_INCLUDE_PATHS) \
115 $(PRODUCT_CFI_INCLUDE_PATHS)
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700116
Vishwath Mohan6106a4e2018-05-24 18:04:25 -0700117 ifneq ($(strip $(foreach dir,$(subst $(comma),$(space),$(combined_include_paths)),\
118 $(filter $(dir)%,$(LOCAL_PATH)))),)
119 my_sanitize := cfi $(my_sanitize)
120 my_sanitize_diag := cfi $(my_sanitize_diag)
121 endif
Vishwath Mohan23b2d2e2017-10-31 02:25:16 -0700122 endif
123endif
124
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800125# If CFI is disabled globally, remove it from my_sanitize.
Vishwath Mohan45665b42017-01-24 13:20:28 -0800126ifeq ($(strip $(ENABLE_CFI)),false)
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800127 my_sanitize := $(filter-out cfi,$(my_sanitize))
128 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
129endif
130
Vishwath Mohana2046062017-02-07 20:28:07 -0800131# Disable CFI for arm32 (b/35157333).
132ifneq ($(filter arm,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
133 my_sanitize := $(filter-out cfi,$(my_sanitize))
134 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
135endif
136
Vishwath Mohanc026f6d2017-04-20 07:39:13 -0700137# Also disable CFI if ASAN is enabled.
138ifneq ($(filter address,$(my_sanitize)),)
139 my_sanitize := $(filter-out cfi,$(my_sanitize))
140 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
141endif
142
Evgenii Stepanov8c50e3c2017-01-31 17:08:33 -0800143# CFI needs gold linker, and mips toolchain does not have one.
144ifneq ($(filter mips mips64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
145 my_sanitize := $(filter-out cfi,$(my_sanitize))
146 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
147endif
148
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700149# Disable sanitizers which need the UBSan runtime for host targets.
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800150ifdef LOCAL_IS_HOST_MODULE
151 my_sanitize := $(filter-out cfi,$(my_sanitize))
152 my_sanitize_diag := $(filter-out cfi,$(my_sanitize_diag))
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700153 my_sanitize := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize))
154 my_sanitize_diag := $(filter-out signed-integer-overflow unsigned-integer-overflow integer_overflow,$(my_sanitize_diag))
Vishwath Mohan96a130b2017-11-17 11:19:36 -0800155endif
156
Ivan Lozano4a363732017-06-28 09:11:26 -0700157# Support for local sanitize blacklist paths.
158ifneq ($(my_sanitize)$(my_global_sanitize),)
159 ifneq ($(LOCAL_SANITIZE_BLACKLIST),)
160 my_cflags += -fsanitize-blacklist=$(LOCAL_PATH)/$(LOCAL_SANITIZE_BLACKLIST)
161 endif
162endif
163
Ivan Lozanob4749cb2017-07-21 10:33:32 -0700164# Disable integer_overflow if LOCAL_NOSANITIZE=integer.
Ivan Lozano4a363732017-06-28 09:11:26 -0700165ifneq ($(filter integer_overflow, $(my_global_sanitize) $(my_sanitize)),)
166 ifneq ($(filter integer, $(strip $(LOCAL_NOSANITIZE))),)
167 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
168 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
169 endif
170endif
171
Evgenii Stepanov42823662016-05-12 13:07:17 -0700172my_nosanitize = $(strip $(LOCAL_NOSANITIZE))
173ifneq ($(my_nosanitize),)
174 my_sanitize := $(filter-out $(my_nosanitize),$(my_sanitize))
175endif
176
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700177ifneq ($(filter arm x86 x86_64,$(TARGET_$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)),)
178 my_sanitize := $(filter-out hwaddress,$(my_sanitize))
179endif
180
181ifneq ($(filter hwaddress,$(my_sanitize)),)
182 my_sanitize := $(filter-out address,$(my_sanitize))
183 my_sanitize := $(filter-out thread,$(my_sanitize))
184endif
185
186ifneq ($(filter hwaddress,$(my_sanitize)),)
187 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_RUNTIME_LIBRARY)
188 ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES)
189 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
190 my_static_libraries := $(my_static_libraries) $($(LOCAL_2ND_ARCH_VAR_PREFIX)HWADDRESS_SANITIZER_STATIC_LIBRARY)
191 endif
192 endif
193endif
194
Dan Alberta6311b72015-07-30 10:17:33 -0700195# TSAN is not supported on 32-bit architectures. For non-multilib cases, make
196# its use an error. For multilib cases, don't use it for the 32-bit case.
197ifneq ($(filter thread,$(my_sanitize)),)
198 ifeq ($(my_32_64_bit_suffix),32)
199 ifeq ($(my_module_multilib),both)
200 my_sanitize := $(filter-out thread,$(my_sanitize))
201 else
202 $(error $(LOCAL_PATH): $(LOCAL_MODULE): TSAN cannot be used for 32-bit modules.)
203 endif
Yabin Cuie77c32e2017-10-19 14:33:58 -0700204 else
205 my_shared_libraries += $(TSAN_RUNTIME_LIBRARY)
Dan Alberta6311b72015-07-30 10:17:33 -0700206 endif
207endif
208
Evgenii Stepanov7dcb8b82016-05-06 18:15:57 -0700209ifneq ($(filter safe-stack,$(my_sanitize)),)
210 ifeq ($(my_32_64_bit_suffix),32)
211 my_sanitize := $(filter-out safe-stack,$(my_sanitize))
212 endif
213endif
214
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700215# Disable Scudo if ASan or TSan is enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700216ifneq ($(filter address thread hwaddress,$(my_sanitize)),)
Kostya Kortchinsky2cfa9972018-06-14 11:02:15 -0700217 my_sanitize := $(filter-out scudo,$(my_sanitize))
218endif
219
220ifneq ($(filter scudo,$(my_sanitize)),)
221 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)SCUDO_RUNTIME_LIBRARY)
222endif
223
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700224# Undefined symbols can occur if a non-sanitized library links
225# sanitized static libraries. That's OK, because the executable
226# always depends on the ASan runtime library, which defines these
227# symbols.
Evgenii Stepanov912b51f2016-05-19 17:49:51 -0700228ifneq ($(filter address thread,$(strip $(SANITIZE_TARGET))),)
Evgenii Stepanov5adfcb12015-06-25 16:38:25 -0700229 ifndef LOCAL_IS_HOST_MODULE
230 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
231 ifeq ($(my_sanitize),)
232 my_allow_undefined_symbols := true
233 endif
234 endif
235 endif
236endif
237
Dan Albert94b57912015-04-17 09:48:33 -0700238# Sanitizers can only be used with clang.
239ifneq ($(my_clang),true)
240 ifneq ($(my_sanitize),)
241 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
242 endif
243endif
244
Dan Albertb5b2ffe2015-04-16 18:07:07 -0700245ifneq ($(filter default-ub,$(my_sanitize)),)
246 my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
Dan Albert08cca282014-12-11 18:56:26 -0800247endif
248
Ivan Krasin74b32b82015-09-18 11:54:43 -0700249ifneq ($(filter coverage,$(my_sanitize)),)
250 ifeq ($(filter address,$(my_sanitize)),)
251 $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of 'coverage' also requires 'address')
252 endif
Zach Rigglebe0811f2017-08-21 17:07:42 -0400253 my_cflags += -fsanitize-coverage=trace-pc-guard,indirect-calls,trace-cmp
Ivan Krasin74b32b82015-09-18 11:54:43 -0700254 my_sanitize := $(filter-out coverage,$(my_sanitize))
255endif
256
Ivan Lozano4a363732017-06-28 09:11:26 -0700257ifneq ($(filter integer_overflow,$(my_sanitize)),)
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700258 # Respect LOCAL_NOSANITIZE for integer-overflow flags.
259 ifeq ($(filter signed-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
260 my_sanitize += signed-integer-overflow
261 endif
262 ifeq ($(filter unsigned-integer-overflow, $(strip $(LOCAL_NOSANITIZE))),)
263 my_sanitize += unsigned-integer-overflow
264 endif
265 my_cflags += $(INTEGER_OVERFLOW_EXTRA_CFLAGS)
Ivan Lozano4a363732017-06-28 09:11:26 -0700266
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700267 # Check for diagnostics mode.
268 ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
269 ifneq ($(filter SHARED_LIBRARIES EXECUTABLES,$(LOCAL_MODULE_CLASS)),)
270 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
Ivan Lozano911cb992018-02-21 13:41:05 -0800271 my_sanitize_diag += signed-integer-overflow
272 my_sanitize_diag += unsigned-integer-overflow
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700273 else
274 $(call pretty-error,Make cannot apply integer overflow diagnostics to static binary.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700275 endif
Ivan Lozano702e8bd2018-03-15 14:49:20 -0700276 else
277 $(call pretty-error,Make cannot apply integer overflow diagnostics to static library.)
Ivan Lozano4a363732017-06-28 09:11:26 -0700278 endif
279 endif
280 my_sanitize := $(filter-out integer_overflow,$(my_sanitize))
281endif
282
283# Makes sure integer_overflow diagnostics is removed from the diagnostics list
284# even if integer_overflow is not set for some reason.
285ifneq ($(filter integer_overflow,$(my_sanitize_diag)),)
286 my_sanitize_diag := $(filter-out integer_overflow,$(my_sanitize_diag))
287endif
288
Dan Albert08cca282014-12-11 18:56:26 -0800289ifneq ($(my_sanitize),)
Stephen Hinese8119e92015-11-09 16:32:11 -0800290 fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize))
Dan Albert08cca282014-12-11 18:56:26 -0800291 my_cflags += -fsanitize=$(fsanitize_arg)
292
293 ifdef LOCAL_IS_HOST_MODULE
Dan Albertabf4bc92015-06-16 23:27:34 -0700294 my_cflags += -fno-sanitize-recover=all
Dan Albert08cca282014-12-11 18:56:26 -0800295 my_ldflags += -fsanitize=$(fsanitize_arg)
Dan Albertabf4bc92015-06-16 23:27:34 -0700296 else
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700297 my_cflags += -fsanitize-trap=all
298 my_cflags += -ftrap-function=abort
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700299 ifneq ($(filter address thread,$(my_sanitize)),)
Evgenii Stepanov71faa192016-05-19 17:45:21 -0700300 my_cflags += -fno-sanitize-trap=address,thread
Evgenii Stepanov55f73e62016-05-12 13:07:36 -0700301 my_shared_libraries += libdl
302 endif
Dan Albert08cca282014-12-11 18:56:26 -0800303 endif
304endif
305
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700306ifneq ($(filter cfi,$(my_sanitize)),)
Evgenii Stepanov81bea1b2017-01-20 14:12:08 -0800307 # __cfi_check needs to be built as Thumb (see the code in linker_cfi.cpp).
308 # LLVM is not set up to do this on a function basis, so force Thumb on the
309 # entire module.
310 LOCAL_ARM_MODE := thumb
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800311 my_cflags += $(CFI_EXTRA_CFLAGS)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000312 # Only append the default visibility flag if -fvisibility has not already been
313 # set to hidden.
314 ifeq ($(filter -fvisibility=hidden,$(LOCAL_CFLAGS)),)
315 my_cflags += -fvisibility=default
316 endif
Vishwath Mohan5b69c062017-02-14 07:55:37 -0800317 my_ldflags += $(CFI_EXTRA_LDFLAGS)
Evgenii Stepanove1b96f32017-01-23 16:57:38 -0800318 my_arflags += --plugin $(LLVM_PREBUILTS_PATH)/../lib64/LLVMgold.so
Vishwath Mohan85f72442017-11-01 09:21:20 +0000319
320 ifeq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
321 my_ldflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_ldflags))
322 my_cflags := $(filter-out -fsanitize-cfi-cross-dso,$(my_cflags))
323 else
324 # Apply the version script to non-static executables
325 my_ldflags += -Wl,--version-script,build/soong/cc/config/cfi_exports.map
326 LOCAL_ADDITIONAL_DEPENDENCIES += build/soong/cc/config/cfi_exports.map
327 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700328endif
329
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800330# If local or global modules need ASAN, add linker flags.
331ifneq ($(filter address,$(my_global_sanitize) $(my_sanitize)),)
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700332 my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
333 ifdef LOCAL_IS_HOST_MODULE
Dan Albert08cca282014-12-11 18:56:26 -0800334 # -nodefaultlibs (provided with libc++) prevents the driver from linking
335 # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
Dan Albert1f0d5302015-04-28 14:55:50 -0700336 my_ldflags += -Wl,--no-as-needed
Dan Albert4ae5d4b2014-10-31 16:23:08 -0700337 else
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800338 # Add asan libraries unless LOCAL_MODULE is the asan library.
Evgenii Stepanovf0b15e12015-04-24 16:34:47 -0700339 # ASan runtime library must be the first in the link order.
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800340 ifeq (,$(filter $(LOCAL_MODULE),$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY)))
341 my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
342 $(my_shared_libraries)
343 endif
344 ifeq (,$(filter $(LOCAL_MODULE),$(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)))
345 my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
346 endif
347
348 # Do not add unnecessary dependency in shared libraries.
349 ifeq ($(LOCAL_MODULE_CLASS),SHARED_LIBRARIES)
350 my_ldflags += -Wl,--as-needed
351 endif
Ying Wanga05e2222015-08-17 16:13:24 -0700352
Colin Crossd08699e2016-07-17 15:28:07 -0700353 ifeq ($(LOCAL_MODULE_CLASS),EXECUTABLES)
354 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
355 my_linker := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
356 # Make sure linker_asan get installed.
357 $(LOCAL_INSTALLED_MODULE) : | $(PRODUCT_OUT)$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_LINKER)
358 endif
359 endif
Dan Albert08cca282014-12-11 18:56:26 -0800360 endif
361endif
362
Chih-Hung Hsiehad741e62016-03-09 14:54:55 -0800363# If local module needs ASAN, add compiler flags.
364ifneq ($(filter address,$(my_sanitize)),)
365 # Frame pointer based unwinder in ASan requires ARM frame setup.
366 LOCAL_ARM_MODE := arm
367 my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
368 ifndef LOCAL_IS_HOST_MODULE
369 my_cflags += -mllvm -asan-globals=0
370 endif
371endif
372
Evgenii Stepanovaec1ffc2018-08-28 13:52:08 -0700373# If local module needs HWASAN, add compiler flags.
374ifneq ($(filter hwaddress,$(my_sanitize)),)
375 my_cflags += $(HWADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
376endif
377
Alexey Polyudove98e5622018-03-27 15:28:12 -0700378# Use minimal diagnostics when integer overflow is enabled; never do it for HOST or AUX modules
379ifeq ($(LOCAL_IS_HOST_MODULE)$(LOCAL_IS_AUX_MODULE),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800380 # Pre-emptively add UBSAN minimal runtime incase a static library dependency requires it
381 ifeq ($(filter STATIC_LIBRARIES,$(LOCAL_MODULE_CLASS)),)
382 ifndef LOCAL_SDK_VERSION
383 my_static_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY)
Ivan Lozanoe5081692018-05-11 14:09:36 -0700384 my_ldflags += -Wl,--exclude-libs,$($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_MINIMAL_RUNTIME_LIBRARY).a
Ivan Lozano911cb992018-02-21 13:41:05 -0800385 endif
386 endif
387 ifneq ($(filter unsigned-integer-overflow signed-integer-overflow integer,$(my_sanitize)),)
388 ifeq ($(filter unsigned-integer-overflow signed-integer overflow integer,$(my_sanitize_diag)),)
389 ifeq ($(filter cfi,$(my_sanitize_diag)),)
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700390 ifeq ($(filter address hwaddress,$(my_sanitize)),)
Ivan Lozano911cb992018-02-21 13:41:05 -0800391 my_cflags += -fsanitize-minimal-runtime
392 my_cflags += -fno-sanitize-trap=integer
393 my_cflags += -fno-sanitize-recover=integer
394 endif
395 endif
396 endif
397 endif
398endif
399
Dan Albert9f176552015-04-28 11:26:45 -0700400ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
401 recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
Dan Albert4111d482015-04-16 18:08:44 -0700402 my_cflags += -fsanitize-recover=$(recover_arg)
403endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700404
Vishwath Mohan8dcfdce2017-01-18 17:50:29 -0800405ifneq ($(my_sanitize_diag),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000406 # TODO(vishwath): Add diagnostic support for static executables once
407 # we switch to clang-4393122 (which adds the static ubsan runtime
408 # that this depends on)
409 ifneq ($(LOCAL_FORCE_STATIC_EXECUTABLE),true)
410 notrap_arg := $(subst $(space),$(comma),$(my_sanitize_diag)),
411 my_cflags += -fno-sanitize-trap=$(notrap_arg)
412 # Diagnostic requires a runtime library, unless ASan or TSan are also enabled.
Evgenii Stepanov8841a7f2018-07-27 11:54:32 -0700413 ifeq ($(filter address thread scudo hwaddress,$(my_sanitize)),)
Vishwath Mohan85f72442017-11-01 09:21:20 +0000414 # Does not have to be the first DT_NEEDED unlike ASan.
415 my_shared_libraries += $($(LOCAL_2ND_ARCH_VAR_PREFIX)UBSAN_RUNTIME_LIBRARY)
416 endif
Evgenii Stepanov202c7a72016-07-07 10:56:39 -0700417 endif
418endif