在Android Studio上使用AddressSanitizer

栏目: IOS · Android · 发布时间: 6年前

内容简介:AddressSanitizer是Google主导的一个开源内存问题检测工具。现在也开始支持Android平台,且受Google推荐来替代之前的Valgrind。目前AddressSanitizer能够发现如下问题:其中,值得一提的是For more information on leak detector in AddressSanitizer, see

AddressSanitizer是Google主导的一个开源内存问题检测工具。现在也开始支持Android平台,且受Google推荐来替代之前的Valgrind。目前AddressSanitizer能够发现如下问题:

  • Out-of-bounds accesses to heap, stack and globals
  • Use-after-free
  • Use-after-return (runtime flag ASAN_OPTIONS=detect_stack_use_after_return=1)
  • Use-after-scope (clang flag -fsanitize-address-use-after-scope)
  • Double-free, invalid free
  • Memory leaks (experimental)

其中,值得一提的是 Memory leaks ,现在还是experiment,准确的说,现在还只支持 Linux 平台,并不支持Android。所以,想写一个内存泄露来检验AddressSanitizer是否生效就要注意了,就像本人一样,其实AddressSanitizer都已经正常运行了,然后一直没有检测出来Memory leaks就以为没有生效,导致浪费了很多时间。

For more information on leak detector in AddressSanitizer, see LeakSanitizer . The leak detection is turned on by default on Linux, and can be enabled using ASAN_OPTIONS=detect_leaks=1 on OS X; however, it is not yet supported on other platforms.

也许有人会说,Android不也是Linux嘛。这个的话,从它的Supported Platforms:

  • Linux i386/x86_64 (tested onUbuntu 12.04)
  • OS X 10.7 - 10.11 (i386/x86_64)
  • iOS Simulator
  • Android ARM
  • NetBSD i386/x86_64
  • FreeBSD i386/x86_64 (tested on FreeBSD 11-current)

就可以看出来,Linux和Android是不同的。而且这里还注明了必须ARM,也就是说如果用非ARM的Android设备可能就不支持。个人认为这和AddressSanitizer的实现机制有关。因为它是通过重写比如malloc之类的函数,构造 shadow Memory 来实现的检测。而这些函数可能都是汇编语言写的,所以不同的CPU硬件就需要不同的实现版本。

AS中的应用

编译指令

其实很简单,就两点,开启AddressSanitizer标志和使用clang编译器。

  1. 开启AddressSanitizer标志:在CMakeLists.txt中添加如下语句:

SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O0 -g -fsanitize=address -fno-omit-frame-pointer")

SET (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -g -fsanitize=address -fno-omit-frame-pointer")

SET (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fsanitize=address")

SET (CMAKE_ANDROID_ARM_MODE ARM)

  1. 使用clang编译:在build.gradle的cmake模块下面添加:

arguments "-DANDROID_TOOLCHAIN=clang"

在设备中安装ASAN

要点:

  • 设备需要root
  • 运行ndk中的asan_device_setup脚本

关于asan_device_setup脚本,在AS自带的ndk-bundle中,其脚本路径为 ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib64/clang/7.0.2/bin/asan_device_setup ,而如果是自己单独安装的NDK,那么其路径为 NDKROOT/toolchains/llvm/prebuilt/linux-x86_64/bin/asan_device_setup 。如果脚本执行成功,那么设备就会自动重启,在本人实践中,最后部分信息如下:

>> Pushing files to the device

Installing /system/lib/libclang_rt.asan-arm-android.so 644

[100%] /system/lib/libclang_rt.asan-arm-android.so

Installing /system/lib64/libclang_rt.asan-aarch64-android.so 644

[100%] /system/lib64/libclang_rt.asan-aarch64-android.so

Installing /system/bin/app_process32 755 u:object_r:zygote_exec:s0

[100%] /system/bin/app_process32

Installing /system/bin/app_process32.real 755 u:object_r:zygote_exec:s0

[100%] /system/bin/app_process32.real

Installing /system/bin/app_process64 755 u:object_r:zygote_exec:s0

[100%] /system/bin/app_process64

Installing /system/bin/app_process64.real 755 u:object_r:zygote_exec:s0

[100%] /system/bin/app_process64.real

Installing /system/bin/asanwrapper 755

[100%] /system/bin/asanwrapper

Installing /system/bin/asanwrapper64 755

[100%] /system/bin/asanwrapper64

>> Restarting shell (asynchronous)

>> Please wait until the device restarts

测试

有了前面两步,就可以实验了,其实还是很简单的。

使用AS新建一个C++支持的默认Android项目,然后修改其native函数如下:

extern "C" JNIEXPORT jstring

JNICALL

Java_com_example_willhua_asantest_MainActivity_stringFromJNI(

JNIEnv *env,

jobject /* this */) {

std::string hello = "Hello from C++";

int *ptr = (int*)malloc(sizeof(int) * 3);

ptr[4] = 6;

return env->NewStringUTF(hello.c_str());

}

然后在注意Build Variant为debug,点击启动就OK啦。然后在log中可以看到检测到了heap-buffer-overflow:

在Android Studio上使用AddressSanitizer

Linux公社的RSS地址: https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2018-09/154273.htm


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Applications (Hacking Exposed)

Web Applications (Hacking Exposed)

Joel Scambray、Mike Shema / McGraw-Hill Osborne Media / 2002-06-19 / USD 49.99

Get in-depth coverage of Web application platforms and their vulnerabilities, presented the same popular format as the international bestseller, Hacking Exposed. Covering hacking scenarios across diff......一起来看看 《Web Applications (Hacking Exposed)》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

URL 编码/解码
URL 编码/解码

URL 编码/解码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具