config_sanitizers.mk revision 7508a81d40dcda7367073f3f945b3e8b32ea9d04
1##############################################
2## Perform configuration steps for sanitizers.
3##############################################
4
5my_sanitize := $(strip $(LOCAL_SANITIZE))
6
7# Keep compatibility for LOCAL_ADDRESS_SANITIZER until all targets have moved to
8# `LOCAL_SANITIZE := address`.
9ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),true)
10  my_sanitize += address
11endif
12
13# And `LOCAL_SANITIZE := never`.
14ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),false)
15  my_sanitize := never
16endif
17
18# Don't apply sanitizers to NDK code.
19ifdef LOCAL_SDK_VERSION
20  my_sanitize := never
21endif
22
23# 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
40endif
41
42ifeq ($(my_sanitize),never)
43  my_sanitize :=
44endif
45
46# 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
53ifneq ($(filter default-ub,$(my_sanitize)),)
54  my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
55
56  ifdef LOCAL_IS_HOST_MODULE
57    my_cflags += -fno-sanitize-recover=all
58    my_ldlibs += -ldl
59  else
60    my_cflags += -fsanitize-undefined-trap-on-error
61    my_shared_libraries += libdl
62  endif
63endif
64
65ifneq ($(my_sanitize),)
66  fsanitize_arg := $(subst $(space),$(comma),$(my_sanitize)),
67  my_cflags += -fsanitize=$(fsanitize_arg)
68
69  ifdef LOCAL_IS_HOST_MODULE
70    my_ldflags += -fsanitize=$(fsanitize_arg)
71  endif
72endif
73
74ifneq ($(filter address,$(my_sanitize)),)
75  # Frame pointer based unwinder in ASan requires ARM frame setup.
76  LOCAL_ARM_MODE := arm
77  my_cflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_CFLAGS)
78  my_ldflags += $(ADDRESS_SANITIZER_CONFIG_EXTRA_LDFLAGS)
79  ifdef LOCAL_IS_HOST_MODULE
80    # -nodefaultlibs (provided with libc++) prevents the driver from linking
81    # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
82    my_ldlibs += -lm -ldl -lpthread
83    my_ldflags += -Wl,--no-as-needed
84  else
85    # ASan runtime library must be the first in the link order.
86    my_shared_libraries := $($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RUNTIME_LIBRARY) \
87                           $(my_shared_libraries) \
88                           $(ADDRESS_SANITIZER_CONFIG_EXTRA_SHARED_LIBRARIES)
89    my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
90    my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH)
91  endif
92endif
93
94ifneq ($(filter undefined,$(my_sanitize)),)
95  my_cflags += -fno-sanitize-recover=all
96
97  ifdef LOCAL_IS_HOST_MODULE
98    my_ldlibs += -ldl
99  else
100    $(error ubsan is not yet supported on the target)
101  endif
102endif
103
104
105ifneq ($(strip $(LOCAL_SANITIZE_RECOVER)),)
106  recover_arg := $(subst $(space),$(comma),$(LOCAL_SANITIZE_RECOVER)),
107  my_cflags += -fsanitize-recover=$(recover_arg)
108endif
109