github-samplecode:https://github.com/MitnickG/Android.git
Google原文:https://developer.android.com/ndk/guides/asan#cmake
当使用VisualStudio编译cpp代码时,有一键选择编出ASan版本从而检测内存错误的功能。
但AndroidStudio用到NDK时,从API27开始才支持ASan检测。
在AS中使用ASan需要注意的是,ASan的CPU开销约为2倍,代码大小开销在50%到2倍之间,并且内存开销很大,约为2倍。
官方建议对于64位ARM(一般现在的设备都是这个了),建议使用HWAddress Sanitizer
但本文还是记录下,使用ASan的几个坑。
先写使用流程:
- 需要准备的文件有:
1、wrap.sh(自己写一个,内容如下):(log_to_syslog=false原文没有介绍,之后可以改成true试试看会有什么不同)
#!/system/bin/sh
HERE="$(cd "$(dirname "$0")" && pwd)"
export ASAN_OPTIONS=log_to_syslog=false,allow_user_segv_handler=1
ASAN_LIB=$(ls $HERE/libclang_rt.asan-*-android.so)
if [ -f "$HERE/libc++_shared.so" ]; then
# Workaround for https://github.com/android-ndk/ndk/issues/988.
export LD_PRELOAD="$ASAN_L