0

I am trying to build a simple Android binary to test out UBSAN (Undefined behavior sanitizer) using ndk r15c and clang. However the build fails when trying to link with the following error:

jni/main.cpp:17: error: undefined reference to '__ubsan_handle_type_mismatch_v1'
jni/main.cpp:24: error: undefined reference to '__ubsan_handle_add_overflow'
jni/main.cpp:30: error: undefined reference to '__ubsan_handle_type_mismatch_v1'
jni/main.cpp:32: error: undefined reference to '__ubsan_handle_type_mismatch_v1'
jni/main.cpp:32: error: undefined reference to '__ubsan_handle_type_mismatch_v1'
jni/main.cpp:32: error: undefined reference to '__ubsan_handle_dynamic_type_cache_miss'
jni/main.cpp:33: error: undefined reference to '__ubsan_handle_dynamic_type_cache_miss'
jni/main.cpp:33: error: undefined reference to '__ubsan_handle_dynamic_type_cache_miss'
jni/main.cpp:34: error: undefined reference to '__ubsan_vptr_type_cache'
jni/main.cpp:34: error: undefined reference to '__ubsan_vptr_type_cache'
jni/main.cpp:34: error: undefined reference to '__ubsan_vptr_type_cache'
jni/main.cpp:34: error: undefined reference to '__ubsan_handle_dynamic_type_cache_miss'
jni/main.cpp:36: error: undefined reference to '__ubsan_vptr_type_cache'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_ostream.c:336: error: undefined reference to '__ubsan_handle_sub_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_ostream.c:357: error: undefined reference to '__ubsan_handle_load_invalid_value'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_ostream.c:343: error: undefined reference to '__ubsan_handle_load_invalid_value'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_ostream.c:348: error: undefined reference to '__ubsan_handle_load_invalid_value'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_ostream.h:192: error: undefined reference to '__ubsan_handle_load_invalid_value'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_ostream.h:136: error: undefined reference to '__ubsan_handle_negate_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/char_traits.h:194: error: undefined reference to '__ubsan_handle_negate_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_string.h:379: error: undefined reference to '__ubsan_handle_add_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_string_base.h:105: error: undefined reference to '__ubsan_handle_negate_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_string_base.h:105: error: undefined reference to '__ubsan_handle_divrem_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_alloc.h:330: error: undefined reference to '__ubsan_handle_negate_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_alloc.h:330: error: undefined reference to '__ubsan_handle_divrem_overflow'
/opt/android_ndk/android-ndk-r15c/sources/cxx-stl/stlport/stlport/stl/_alloc.h:352: error: undefined reference to '__ubsan_handle_divrem_overflow'

My Android.mk file is as follows:

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_CFLAGS += -Wall

LOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -llog -g

LOCAL_C_INCLUDES := bionic
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include

LOCAL_SRC_FILES:= main.cpp
LOCAL_CPPFLAGS := -Wall -fPIE -fexceptions -fsanitize=undefined
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog -fPIE -pie -fsanitize=undefined 

LOCAL_MODULE := ubsan_test

include $(BUILD_EXECUTABLE)

My Application.mk is as follows:

NDK_TOOLCHAIN_VERSION:=clang
APP_STL:=stlport_static
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-17

I am guessing that I am missing some linker flag or some other setting but haven't been able to figure out what is going on. Looking at the symbols in the ubsan runtime, the sybols so seem to exist.

Note: I have also tried using LOCAL_SANITIZE := undefined instead of -fsanitize=undefined in my Android.mk with the same result.

user821602
  • 31
  • 1
  • 4

1 Answers1

0

We don't currently ship the ubsan runtime in the NDK: https://github.com/android-ndk/ndk/issues/183

For now, you can use -fsanitize=undefined -fsanitize-trap=undefined instead. This isn't ideal because rather than a helpful diagnostic you just get a trap (a SIGILL on ARM, pretty sure it's the same for other platforms), but it's better than nothing until we get the ubsan runtime into the NDK.

Dan Albert
  • 8,619
  • 1
  • 30
  • 72
  • The UBSAN runtime is included in ndk r15c. I see the library included, and if I add that as a prebuilt library and link against it, things works as expected. However, using the flags doesn't seem to work. – user821602 Sep 06 '17 at 21:35