NDK (C++) 开发中如何使用 ASan 检测内存越界等内存错误

什么是 ASan

ASan 是 Address Sanitizer 简称,它是是一种基于编译器用于快速检测原生代码中内存错误的工具。

简而言之,ASan 就是一个用于快速检测内存错误的工具。这里很多朋友有误解,ASan 其实并不能用于内存泄漏检测,Android 平台内存泄漏检测推荐 MallocDebug 。

另外需要注意的是 Android O(API >= 27)及以上版本才支持 ASan ,NDK 需要选用 r20 及以上版本。

ASan 可以检测到内存错误类型如下:

  • Stack and heap buffer overflow/underflow 栈和堆缓冲区上溢/下溢;
  • Heap use after free 堆内存被释放之后还在使用其指针;
  • Stack use outside scope 在某个局部变量的作用域之外,使用其指针;
  • Double free/wild free 指针重复释放的情况。

ASan 支持 arm 和 x86 平台,使用 ASan 时,APP 性能会变慢且内存占用会飙升。针对 arm64 平台,Android 官方推荐使用 HWAddress Sanitizer (HWASan),后面会介绍。

关于 ASan 的原理本文不做深入讨论,该文章的主要目的是帮助开发者快速上手 ASan 的使用。

这里感性地介绍下 ASan 的工作原理:ASan 相当于接管了内存的分配,当分配一块内存时,会在这块内存的前后添加"标志位",然后再次使用该内存的时候检查"标志位"是否被修改,当发现"标志位"被修改时,判断出现内存错误。

怎么使用 ASan

之所以写这篇文件,就是因为发现一些文章介绍 ASan 使用方法搞得非常复杂,不易上手。

其实 Android 官方的使用说明非常简洁,就是复制黏贴,添加两行代码就搞定。

官方文档:developer.android.com/ndk/guides/…

修改编译脚本

CMake

APP 下面的 build.gradle 添加:

android {
    defaultConfig {
        externalNativeBuild {
            cmake {
                # Can also use system or none as ANDROID_STL.
                arguments "-DANDROID_ARM_MODE=arm", "-DANDROID_STL=c++_shared"
            }
        }
    }
}
复制代码

CMakeLists.txt 脚本添加:

target_compile_options(${libname} PUBLIC -fsanitize=address -fno-omit-frame-pointer)
set_target_properties(${libname} PROPERTIES LINK_FLAGS -fsanitize=address)
复制代码

NDK-BUILD

Application.mk 文件添加:

APP_STL := c++_shared # Or system, or none.
APP_CFLAGS := -fsanitize=address -fno-omit-frame-pointer
APP_LDFLAGS := -fsanitize=address
复制代码

Android.mk 文件添加:

APP_STL := c++_shared # Or system, or none.
APP_CFLAGS := -fsanitize=address -fno-omit-frame-pointer
APP_LDFLAGS := -fsanitize=address
复制代码

拷贝 Asan 库到 jniLibs 目录下

Asan 库位于下面路径下:

android-ndk-r21\toolchains\llvm\prebuilt\windows-x86_64\lib64\clang\9.0.8\lib\linux
复制代码

64 位 libclang_rt.asan-aarch64-android.so , 32 位 libclang_rt.asan-arm-android.so ,分别拷贝两个库到 jniLibs 相应的目录下。

新建 wrap.sh 文件,拷贝下面内容到文件中:

#!/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_LIB $HERE/libc++_shared.so"
else
    export LD_PRELOAD="$ASAN_LIB"
fi
"$@"
复制代码

在 main 文件夹下新建目录 resources\lib 然后将 wrap.sh 文件拷贝到相应的目录下面,最终的目录结构是这样的:


└── app
    └── src
        └── main
            ├── jniLibs
            │   ├── arm64-v8a
            │   │   └── libclang_rt.asan-aarch64-android.so
            │   ├── armeabi-v7a
            │   │   └── libclang_rt.asan-arm-android.so
            │   ├── x86
            │   │   └── libclang_rt.asan-i686-android.so
            │   └── x86_64
            │       └── libclang_rt.asan-x86_64-android.so
            └── resources
                └── lib
                    ├── arm64-v8a
                    │   └── wrap.sh
                    ├── armeabi-v7a
                    │   └── wrap.sh
                    ├── x86
                    │   └── wrap.sh
                    └── x86_64
                        └── wrap.sh
复制代码

自此 ASan 接入完成,是不是很简单?

ASan 检测内存错误

这一节我们在代码中故意设置一些常见的内存错误(内存越界等)用来测试 ASan 检测出来的结果是否正确。需要注意的是,当 ASan 检测出内存错误,程序就会立即 crash ,不再往下执行,log 中会出现关键字 AddressSanitizer 。

堆内存溢出

static void HeapBufferOverflow() {
    int *arr = new int[1024];
    arr[0] = 11;
    arr[1024] = 12;
    LOGCATE("HeapBufferOverflow arr[0]=%d, arr[1024]",arr[0], arr[1024]);
}
复制代码

ASan 检测结果(crash log)中出现关键字 heap-buffer-overflow :

05-13 19:52:16.247  4194  4194 I com.byteflow.learnffmpeg: =================================================================
05-13 19:52:16.247  4194  4194 I com.byteflow.learnffmpeg: ==4194==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x004f0d5a8100 at pc 0x0074f822b648 bp 0x007ff0227a00 sp 0x007ff02279f8
05-13 19:52:16.247  4194  4194 I com.byteflow.learnffmpeg: WRITE of size 4 at 0x004f0d5a8100 thread T0
05-13 19:52:16.257 23334 23334 D Launcher_UnlockAnimationStateMachine: mResetIdleStateRunnable
05-13 19:52:16.265  4194  4194 I com.byteflow.learnffmpeg:     #0 0x74f822b644  (/data/app/com.byteflow.learnffmpeg-mVg7CcQSTXVnJhfo7u0XLA==/lib/arm64/liblearn-ffmpeg.so+0x146644)
05-13 19:52:16.265  4194  4194 I com.byteflow.learnffmpeg:     #1 0x74f8229b20  (/data/app/com.byteflow.learnffmpeg-mVg7CcQSTXVnJhfo7u0XLA==/lib/arm64/liblearn-ffmpeg.so+0x144b20)
05-13 19:52:16.265  4194  4194 I com.byteflow.learnffmpeg:     #2 0x74f8229a7c  (/data/app/com.byteflow.learnffmpeg-mVg7CcQSTXVnJhfo7u0XLA==/lib/arm64/liblearn-ffmpeg.so+0x144a7c)
05-13 19:52:16.265  4194  4194 I com.byteflow.learnffmpeg:     #3 0x74fdaac0a0  (/data/app/com.byteflow.learnffmpeg-mVg7CcQSTXVnJhfo7u0XLA==/oat/arm64/base.odex+0xb0a0)
05-13 19:52:16.265  4194  4194 I com.byteflow.learnffmpeg: ........
复制代码

栈内存溢出

 //stack-buffer-overflow
 static void StackBufferOverflow() {
     int arr[1024];
     arr[0] = 11;
     arr[1024] = 12;
     LOGCATE("StackBufferOverflow arr[0]=%d, arr[1024]",arr[0], arr[1024]);
 }
复制代码

ASan 检测结果(crash log)中出现关键字 stack-buffer-overflow :

05-13 19:54:30.371  5002  5002 I com.byteflow.learnffmpeg: =================================================================
05-13 19:54:30.371  5002  5002 I com.byteflow.learnffmpeg: ==5002==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x007ff02278c0 at pc 0x0074f8bd6640 bp 0x007ff0226890 sp 0x007ff0226888
05-13 19:54:30.372  5002  5002 I com.byteflow.learnffmpeg: WRITE of size 4 at 0x007ff02278c0 thread T0
05-13 19:54:30.389  5002  5002 I com.byteflow.learnffmpeg:     #0 0x74f8bd663c  (/data/app/com.byteflow.learnffmpeg-aaLGh4G_-2c2WC7sX0ibag==/lib/arm64/liblearn-ffmpeg.so+0x14663c)
05-13 19:54:30.389  5002  5002 I com.byteflow.learnffmpeg:     #1 0x74f8bd4a20  (/data/app/com.byteflow.learnffmpeg-aaLGh4G_-2c2WC7sX0ibag==/lib/arm64/liblearn-ffmpeg.so+0x144a20)
05-13 19:54:30.389  5002  5002 I com.byteflow.learnffmpeg:     #2 0x74f8bd497c  (/data/app/com.byteflow.learnffmpeg-aaLGh4G_-2c2WC7sX0ibag==/lib/arm64/liblearn-ffmpeg.so+0x14497c)
......
复制代码

使用已释放的指针

//heap-use-after-free
static void UseAfterFree() {
    int *arr = new int[1024];
    arr[0] = 11;
    delete [] arr;
    LOGCATE("UseAfterFree arr[0]=%d, arr[1024]",arr[0], arr[1024]);
}
复制代码

ASan 检测结果(crash log)中出现关键字 heap-use-after-free :

05-13 19:56:44.430  5235  5235 I com.byteflow.learnffmpeg: =================================================================
05-13 19:56:44.430  5235  5235 I com.byteflow.learnffmpeg: ==5235==ERROR: AddressSanitizer: heap-use-after-free on address 0x004f0d5a7100 at pc 0x0074f7ac945c bp 0x007ff02279d0 sp 0x007ff02279c8
05-13 19:56:44.430  5235  5235 I com.byteflow.learnffmpeg: READ of size 4 at 0x004f0d5a7100 thread T0
05-13 19:56:44.447  5235  5235 I com.byteflow.learnffmpeg:     #0 0x74f7ac9458  (/data/app/com.byteflow.learnffmpeg-w2WNuKKPLEj7i4_8_Oj3Sw==/lib/arm64/liblearn-ffmpeg.so+0x146458)
05-13 19:56:44.448  5235  5235 I com.byteflow.learnffmpeg:     #1 0x74f7ac7920  (/data/app/com.byteflow.learnffmpeg-w2WNuKKPLEj7i4_8_Oj3Sw==/lib/arm64/liblearn-ffmpeg.so+0x144920)
05-13 19:56:44.448  5235  5235 I com.byteflow.learnffmpeg:     #2 0x74f7ac787c  (/data/app/com.byteflow.learnffmpeg-w2WNuKKPLEj7i4_8_Oj3Sw==/lib/arm64/liblearn-ffmpeg.so+0x14487c)
......
复制代码

局部变量的作用域之外使用其指针

//stack-use-after-scope
static int *p;
static void UseAfterScope()
{
    {
        int a = 0;
        p = &a;
    }
    *p = 1111;
    LOGCATE("UseAfterScope *p=%d",*p);
}
复制代码

ASan 检测结果(crash log)中出现关键字 stack-use-after-scope :

https://developer.android.com/ndk/guides/asan#cmake05-13 20:01:19.447  5907  5907 I com.byteflow.learnffmpeg: =================================================================
05-13 20:01:19.447  5907  5907 I com.byteflow.learnffmpeg: ==5907==ERROR: AddressSanitizer: stack-use-after-scope on address 0x007ff02279a0 at pc 0x0074f8236438 bp 0x007ff0227970 sp 0x007ff0227968
05-13 20:01:19.448  5907  5907 I com.byteflow.learnffmpeg: WRITE of size 4 at 0x007ff02279a0 thread T0
05-13 20:01:19.462 23334 23334 D Launcher_UnlockAnimationStateMachine: mResetIdleStateRunnable
05-13 20:01:19.464  5907  5907 I com.byteflow.learnffmpeg:     #0 0x74f8236434  (/data/app/com.byteflow.learnffmpeg-jx_Xi14rwGHag_VZ9KWXYA==/lib/arm64/liblearn-ffmpeg.so+0x146434)
05-13 20:01:19.465  5907  5907 I com.byteflow.learnffmpeg:     #1 0x74f8234860  (/data/app/com.byteflow.learnffmpeg-jx_Xi14rwGHag_VZ9KWXYA==/lib/arm64/liblearn-ffmpeg.so+0x144860)
05-13 20:01:19.465  5907  5907 I com.byteflow.learnffmpeg:     #2 0x74f82347bc  (/data/app/com.byteflow.learnffmpeg-jx_Xi14rwGHag_VZ9KWXYA==/lib/arm64/liblearn-ffmpeg.so+0x1447bc)
05-13 20:01:19.465  5907  5907 I com.byteflow.learnffmpeg:     #3 0x74fdabe0a0  (/data/app/com.byteflow.learnffmpeg-jx_Xi14rwGHag_VZ9KWXYA==/oat/arm64/base.odex+0xb0a0)
.....
复制代码

重复释放指针

//double-free
static void DoubleFree() {
    int *arr = new int[1024];
    arr[0] = 11;
    delete [] arr;
    delete [] arr;
    LOGCATE("UseAfterFree arr[0]=%d",arr[0]);
}
复制代码

ASan 检测结果(crash log)中出现关键字 double-free :

05-13 20:02:16.474  6102  6102 I com.byteflow.learnffmpeg: =================================================================
05-13 20:02:16.475  6102  6102 I com.byteflow.learnffmpeg: ==6102==ERROR: AddressSanitizer: attempting double-free on 0x004f0d5a7100 in thread T0:
05-13 20:02:16.492  6102  6102 I com.byteflow.learnffmpeg:     #0 0x74f9f2b7b0  (/data/app/com.byteflow.learnffmpeg-kjj44NZxl-eyA06gf3E2MA==/lib/arm64/libclang_rt.asan-aarch64-android.so+0xd57b0)
05-13 20:02:16.492  6102  6102 I com.byteflow.learnffmpeg:     #1 0x74f88cd210  (/data/app/com.byteflow.learnffmpeg-kjj44NZxl-eyA06gf3E2MA==/lib/arm64/liblearn-ffmpeg.so+0x146210)
05-13 20:02:16.492  6102  6102 I com.byteflow.learnffmpeg:     #2 0x74f88cb720  (/data/app/com.byteflow.learnffmpeg-kjj44NZxl-eyA06gf3E2MA==/lib/arm64/liblearn-ffmpeg.so+0x144720)
05-13 20:02:16.492  6102  6102 I com.byteflow.learnffmpeg:     #3 0x74f88cb67c  (/data/app/com.byteflow.learnffmpeg-kjj44NZxl-eyA06gf3E2MA==/lib/arm64/liblearn-ffmpeg.so+0x14467c)
......
复制代码

ASan 基本上可以覆盖到常见的内存错误问题,还有其他 Case 就不一一展示了,

音视频入门到精通在开源项目:https://github.com/Android-Alvin/Android-LearningNotes中已收录,里面还包含不同方向的自学编程路线、面试题集合/面经、及系列技术文章等,资源持续更新中...

你可能感兴趣的:(NDK (C++) 开发中如何使用 ASan 检测内存越界等内存错误)