Android nkd r15c和UBSAN clang

时间:2017-08-24 19:17:09

标签: android c++ android-ndk clang ubsan

我正在尝试使用ndk r15c和clang构建一个简单的Android二进制文件来测试UBSAN(Undefined behavior sanitizer)。但是,在尝试链接以下错误时,构建失败:

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'

我的Android.mk文件如下:

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)

我的Application.mk如下:

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

我猜我错过了一些链接器标志或其他一些设置但却无法弄清楚发生了什么。看看ubsan运行时中的符号,sybols似乎就存在了。

注意:我也尝试在Android.mk中使用LOCAL_SANITIZE:= undefined而不是-fsanitize = undefined,结果相同。

1 个答案:

答案 0 :(得分:0)

我们目前不在NDK中发布ubsan运行时:https://github.com/android-ndk/ndk/issues/183

目前,您可以使用-fsanitize=undefined -fsanitize-trap=undefined代替。这不是理想的,因为您只需获得一个陷阱(ARM上的SIGILL,非常确定它对其他平台是相同的),而不是一个有用的诊断,但是在我们将ubsan运行时放入NDK之前,它总比没有好。