Home | History | Annotate | Download | only in core
      1 ##############################################
      2 ## Perform configuration steps for sanitizers.
      3 ##############################################
      4 
      5 my_sanitize := $(strip $(LOCAL_SANITIZE))
      6 
      7 # Keep compatibility for LOCAL_ADDRESS_SANITIZER until all targets have moved to
      8 # `LOCAL_SANITIZE := address`.
      9 ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),true)
     10   my_sanitize += address
     11 endif
     12 
     13 # And `LOCAL_SANITIZE := never`.
     14 ifeq ($(strip $(LOCAL_ADDRESS_SANITIZER)),false)
     15   my_sanitize := never
     16 endif
     17 
     18 # Don't apply sanitizers to NDK code.
     19 ifdef LOCAL_SDK_VERSION
     20   my_sanitize := never
     21 endif
     22 
     23 # Configure SANITIZE_HOST.
     24 ifdef 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
     40 endif
     41 
     42 ifeq ($(my_sanitize),never)
     43   my_sanitize :=
     44 endif
     45 
     46 # Sanitizers can only be used with clang.
     47 ifneq ($(my_clang),true)
     48   ifneq ($(my_sanitize),)
     49     $(error $(LOCAL_PATH): $(LOCAL_MODULE): Use of sanitizers requires LOCAL_CLANG := true)
     50   endif
     51 endif
     52 
     53 ifneq ($(filter default-ub,$(my_sanitize)),)
     54   my_sanitize := $(CLANG_DEFAULT_UB_CHECKS)
     55   my_ldlibs += -ldl
     56 
     57   ifdef LOCAL_IS_HOST_MODULE
     58     my_cflags += -fno-sanitize-recover=all
     59   else
     60     my_cflags += -fsanitize-undefined-trap-on-error
     61   endif
     62 endif
     63 
     64 ifneq ($(my_sanitize),)
     65   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
     71 endif
     72 
     73 ifneq ($(filter address,$(my_sanitize)),)
     74   # 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
     79     # -nodefaultlibs (provided with libc++) prevents the driver from linking
     80     # libraries needed with -fsanitize=address. http://b/18650275 (WAI)
     81     my_ldlibs += -lm -ldl -lpthread
     82     my_ldflags += -Wl,--no-as-needed
     83   else
     84     # 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)
     88     my_static_libraries += $(ADDRESS_SANITIZER_CONFIG_EXTRA_STATIC_LIBRARIES)
     89     my_ldflags += -Wl,-rpath,$($(LOCAL_2ND_ARCH_VAR_PREFIX)ADDRESS_SANITIZER_RPATH)
     90   endif
     91 endif
     92 
     93 ifneq ($(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)
     98   endif
     99 endif
    100