linux libasan.so,Address Sanitizer 用法

Address Sanitizer(ASan)是一个快速的内存错误检测工具。这里说明它的用法。

参考资料

1. 简介

Address Sanitizer(ASan)是一个快速的内存错误检测工具。它非常快,只拖慢程序两倍左右(比起Valgrind快多了)。它包括一个编译器instrumentation模块和一个提供malloc()/free()替代项的运行时库。

从gcc 4.8开始,AddressSanitizer成为gcc的一部分。当然,要获得更好的体验,最好使用4.9及以上版本,因为gcc 4.8的AddressSanitizer还不完善,最大的缺点是没有符号信息。

2. 使用步骤

用-fsanitize=address选项编译和链接你的程序。

用-fno-omit-frame-pointer编译,以得到更容易理解stack trace。

可选择-O1或者更高的优化级别编译

gcc -fsanitize=address -fno-omit-frame-pointer -O1 -g use-after-free.c -o use-after-free

运行use-after-fee。如果发现了错误,就会打印出类似下面的信息:

==9901==ERROR: AddressSanitizer: heap-use-after-free on address 0x60700000dfb5

at pc 0x45917b bp 0x7fff4490c700 sp 0x7fff4490c6f8

READ of size 1 at 0x60700000dfb5 thread T0

#0 0x45917a in main use-after-free.c:5

#1 0x7fce9f25e76c in __libc_start_main /build/buildd/eglibc-2.15/csu/libc-start.c:226

#2 0x459074 in _start (a.out+0x459074)

0x60700000dfb5 is located 5 bytes inside of 80-byte region [0x60700000dfb0,0x60700000e000)

freed by thread T0 here:

#0 0x4441ee in __interceptor_free projects/compiler-rt/lib/asan/asan_malloc_linux.cc:64

#1 0x45914a in main use-after-free.c:4

#2 0x7fce9f25e76c in __libc_start_main /build/buildd/eglibc-2.15/csu/libc-start.c:226

previously allocated by thread T0 here:

#0 0x44436e in __interceptor_malloc projects/compiler-rt/lib/asan/asan_malloc_linux.cc:74

#1 0x45913f in main use-after-free.c:3

#2 0x7fce9f25e76c in __libc_start_main /build/buildd/eglibc-2.15/csu/libc-start.c:226

SUMMARY: AddressSanitizer: heap-use-after-free use-after-free.c:5 main

第一部分(ERROR)指出错误类型是heap-use-after-free;

第二部分(READ), 指出线程名thread T0,操作为READ,发生的位置是use-after-free.c:5。

该heapk块之前已经在use-after-free.c:4被释放了;

该heap块是在use-fater-free.c:3分配

第三部分 (SUMMARY) 前面输出的概要说明。

3. 错误类型

3.1 (heap) use after free 释放后使用

下面的代码中,分配array数组并释放,然后返回它的一个元素。

5 int main (int argc, char** argv)

6 {

7 int* array = new int[100];

8 delete []array;

9 return array[1];

10 }

下面的错误信息与前面的“使用步骤”一节中的类似。

==3189==ERROR: AddressSanitizer: heap-use-after-free on address 0x61400000fe44

at pc 0x0000004008f1 bp 0x7ffc9b6e2630 sp 0x7ffc9b6e2620

READ of size 4 at 0x61400000fe44 thread T0

#0 0x4008f0 in main /home/ron/dev/as/use_after_free.cpp:9

#1 0x7f3763aa882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

#2 0x4007b8 in _start (/home/ron/dev/as/build/use_after_free+0x4007b8)

0x61400000fe44 is located 4 bytes inside of 400-byte region [0x61400000fe40,0x61400000ffd0)

freed by thread T0 here:

#0 0x7f3763ef1caa in operator delete[](void*) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x99caa)

#1 0x4008b5 in main /home/ron/dev/as/use_after_free.cpp:8

#2 0x7f3763aa882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

previously allocated by thread T0 here:

#0 0x7f3763ef16b2 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x996b2)

#1 0x40089e in main /home/ron/dev/as/use_after_free.cpp:7

#2 0x7f3763aa882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: heap-use-after-free /home/ron/dev/as/use_after_free.cpp:9 main

3.2 heap buffer overflow 堆缓存访问溢出

如下代码中,访问的位置超出堆上数组array的边界。

2 int main (int argc, char** argv)

3 {

4 int* array = new int[100];

5 int res = array[100];

6 delete [] array;

7 return res;

8 }

下面的错误信息指出:

错误类型是heap-buffer-overflow

不合法操作READ发生在线程T0, heap_buf_overflow.cpp:5

heap块分配发生在heap_buf_overflow.cpp

==3322==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x61400000ffd0

at pc 0x0000004008e0 bp 0x7ffeddce53a0 sp 0x7ffeddce5390

READ of size 4 at 0x61400000ffd0 thread T0

#0 0x4008df in main /home/ron/dev/as/heap_buf_overflow.cpp:5

#1 0x7f3b83d0882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

#2 0x4007b8 in _start (/home/ron/dev/as/build/heap_buf_overflow+0x4007b8)

0x61400000ffd0 is located 0 bytes to the right of 400-byte region [0x61400000fe40,0x61400000ffd0)

allocated by thread T0 here:

#0 0x7f3b841516b2 in operator new[](unsigned long) (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x996b2)

#1 0x40089e in main /home/ron/dev/as/heap_buf_overflow.cpp:4

#2 0x7f3b83d0882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: heap-buffer-overflow /home/ron/dev/as/heap_buf_overflow.cpp:5 main

3.2 stack buffer overflow 栈缓存访问溢出

如下代码中,访问的位置超出栈上数组array的边界。

2 int main (int argc, char** argv)

3 {

4 int array[100];

5 return array[100];

6 }

下面的错误信息指出:

错误类型是stack-buffer-overflow

不合法操作READ发生在线程T0, stack_buf_overflow.cpp:5

栈块在线程T0的栈上432偏移位置上。

==3389==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffd061fa4a0

at pc 0x0000004009ff bp 0x7ffd061fa2d0 sp 0x7ffd061fa2c0

READ of size 4 at 0x7ffd061fa4a0 thread T0

#0 0x4009fe in main /home/ron/dev/as/stack_buf_overflow.cpp:5

#1 0x7fbade4e882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

#2 0x400858 in _start (/home/ron/dev/as/build/stack_buf_overflow+0x400858)

Address 0x7ffd061fa4a0 is located in stack of thread T0 at offset 432 in frame

#0 0x400935 in main /home/ron/dev/as/stack_buf_overflow.cpp:3

This frame has 1 object(s):

[32, 432) 'array' <== Memory access at offset 432 overflows this variable

HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext

(longjmp and C++ exceptions *are* supported)

SUMMARY: AddressSanitizer: stack-buffer-overflow /home/ron/dev/as/stack_buf_overflow.cpp:5 main

3.3 global buffer overflow 全局缓冲访问溢出

如下代码中,访问的位置超出全局数组array的边界。

2 int array[100];

3

4 int main (int argc, char** argv)

5 {

6 return array[100];

7 }

下面的错误信息指出:

错误类型是global-buffer-overflow

不合法操作READ发生在线程T0, global_buf_overflow.cpp:6

缓存块在global_buf_overflow.cpp:2 定义。

==3499==ERROR: AddressSanitizer: global-buffer-overflow on address 0x000000601270

at pc 0x000000400915 bp 0x7ffd8e80c020 sp 0x7ffd8e80c010

READ of size 4 at 0x000000601270 thread T0

#0 0x400914 in main /home/ron/dev/as/global_buf_overflow.cpp:6

#1 0x7f613c1c882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

#2 0x400808 in _start (/home/ron/dev/as/build/global_buf_overflow+0x400808)

0x000000601270 is located 0 bytes to the right of global variable 'array' defined in

'/home/ron/dev/as/global_buf_overflow.cpp:2:5' (0x6010e0) of size 400

SUMMARY: AddressSanitizer: global-buffer-overflow /home/ron/dev/as/global_buf_overflow.cpp:6 main

3.4 use after return

3.5 use after scope

3.6 initializations order bugs

3.7 memory leaks 内存泄露

检测内存的LeakSanitizer是集成在AddressSanitizer中的一个相对独立的工具,它工作在检查过程的最后阶段。

下面代码中,p指向的内存没有释放。

4 void* p;

5

6 int main ()

7 {

8 p = malloc (7);

9 p = 0;

10 return 0;

11 }

下面的错误信息指出 detected memory leaks

内存在mem_leak.cpp:8分配

缓存块在global_buf_overflow.cpp:2 定义。

==4088==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7 byte(s) in 1 object(s) allocated from:

#0 0x7ff9ae510602 in malloc (/usr/lib/x86_64-linux-gnu/libasan.so.2+0x98602)

#1 0x4008d3 in main /home/ron/dev/as/mem_leak.cpp:8

#2 0x7ff9ae0c882f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)

SUMMARY: AddressSanitizer: 7 byte(s) leaked in 1 allocation(s).

目前,并不是所有的平台都默认检测内存泄露,可以指定ASAN_OPTIONS开启如下:

ASAN_OPTIONS=detect_leaks=1 yourapp

而且不是所有的平台支持检测内存泄露,比如ARM,就会得到这样的提示:

==1901==AddressSanitizer: detect_leaks is not supported on this platform.

4. 工作原理

4.1 概要说明

AddressSanitizer的运行时库替换malloc()/free()。分配缓存前后的空间标记为poisoned,已经被释放的缓存也被标记为poisoned。内存访问的代码都被编译器替换如下:

替换之前:

*address = ...;

替换之后:

if (IsPoisoned(address))

{

ReportError(address, kAccessSize, kIsWrite);

}

*address = ...;

访问之前检查访问地址是否poisoned,如果是,报告错误。

4.2 memory mapping 和 instrumentation

进程的虚拟地址空间划分为两个不相连的部分:

main application memory (Mem)。这是程序自身代码使用的内存;

Shadow memory (Shadow)。这里放的是shadow value(meta data)。从Mem到Shadow之间有映射关系。将Mem的一个字节标记为poisoned,其实就是在对应的Shadow内存中写入指定值。

伪代码如下。它先从Mem中地址计算对应的Shadow地址。

shadow_address = MemToShadow (address);

if (ShadowIsPoisoned(shadow_address))

{

ReportError (address, kAccessSize, kIsWrite);

}

4.3 mapping

Mem中的8字节映射到Shadow memory中是1字节。

这个字节可能有9种不同的值:

所有8字节都是unpoisoned的,则值为0;

所有8字节都是poisoned的,则值为负;

前k字节为unpoisoned,后面8-k字节为poisoned, 则值为k。

malloc()分配的内存总是8字节的倍数,如果要分配的缓存不是8字节的倍数,则尾部的8个字节poisoned状态不同。比如分配13字节,会得到两个8字节。前1个全是unpoisoned,后一个只有前5个字节是unpoisoned,后3个字节是poisoned。

4.4 栈的处理

为了捕捉栈的访问溢出,AddressSanitizer在缓存前后加上保护区。这里可以看到设置对应Shadow memory的代码。

改编之前为:

void foo()

{

char a[8];

...

return;

}

改编之后为:

void foo()

{

char redzone1[32]; // 32-byte aligned

char a[8]; // 32-byte aligned

char redzone2[24];

char redzone3[32]; // 32-byte aligned

int *shadow_base = MemToShadow(redzone1);

shadow_base[0] = 0xffffffff; // poison redzone1

shadow_base[1] = 0xffffff00; // poison redzone2, unpoison 'a'

shadow_base[2] = 0xffffffff; // poison redzone3

...

shadow_base[0] = shadow_base[1] = shadow_base[2] = 0; // unpoison all

return;

}

4.5 malloc()/free()的处理

运行时库用自己的函数替换malloc() / free()。

malloc()在缓存前后分配保护区。缓存本身标记为unpoisoned,保护区标记为poisoned。

free() 将整个区域,包括缓存和保护区,都标记为poisoned,并将该区域放入一个特别的队列中,以保证malloc()在相当长的时间内不会再次使用它)。

相关链接

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/498569.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

用深度学习模型,解构并重构人类思维

来源&#xff1a;人机与认知实验室概要&#xff1a;人类的知识&#xff0c;往往由事实 fact 和规则 rule 组成&#xff0c;而且人类习惯于用简练的词汇&#xff0c;来表达事实和规则。所以&#xff0c;维特根斯坦认为&#xff0c;人类思维受制于语言结构&#xff0c;语言的界限…

Ios 12 linux,苹果发布iOS 12.4.1,以修补越狱漏洞

Apple今天发布了针对iPhone和iPad设备的最新iOS 12.4移动操作系统&#xff0c;Apple TV设备的tvOS 12.4以及Apple Watch设备的watchOS 5.3的第一个小Bug和安全更新。iOS 12.4.1是iOS 12.4发布多月后的一项重大更新&#xff0c;引入了新的iPhone迁移功能&#xff0c;允许用户将数…

谷歌AI智商达苹果Siri两倍 但不及6岁儿童

来源&#xff1a;IT之家概要&#xff1a;3名中国研究员近期发表的论文指出&#xff0c;谷歌人工智能技术相对于苹果Siri“智商”明显更高。3名中国研究员近期发表的论文指出&#xff0c;谷歌人工智能技术相对于苹果Siri“智商”明显更高。这篇论文比较了多个人工智能系统。论文…

linux传输文件到linux速度慢,linux中往nand(jffs2)中拷贝文件时速度慢的问题

最近在调试nand和jffs2时&#xff0c;发现将一个文件从SD卡拷贝到jffs2的NAND分区时&#xff0c;速度非常慢&#xff0c;和从NAND中拷出(例如拷贝到SD卡)相比&#xff0c;慢了近5倍。一开始以为是驱动写NAND和读NAND的差异&#xff0c;后来用mtd-utile的nand write测试了一下&a…

Deepmind 预测地图论文背后:神经科学或将助力AI迎来新突破

来源&#xff1a;AI科技评论概要&#xff1a;这篇论文中&#xff0c;Deepmind通过对主管人类长期记忆行为的“海马体”&#xff08;hippocampus&#xff09;神经元活动的研究&#xff0c;进一步提出了可以转化为神经网络架构的“预测图”理论。对人类神经网络的理解越来越在左右…

c语言更改编译时字体,c习题编译时出现空的字符常量,怎么修改?

题目要求&#xff1a;统计各个数字&#xff0c;空白符以及所有其他字符出现的次数。[CODE]#include main(){int c, i, nwhite, nother;int ndigit[10];nwhite nother 0;for (i 0; i < 10; i)ndigit[i] 0;while ((c getchar()) ! EOF)if (c > ’0′ && c <…

你所未知的人工智能应用领域

来源&#xff1a;199IT互联网数据中心 概要&#xff1a;一组专家为我们详细描述了随着我们所使用的机器变得越来越智能&#xff0c;我们周围的世界正在如何发生变化的图景。 对有些人来说&#xff0c;人工智能和机器人技术的普及对我们的隐私、工作甚至人身安全构成了威胁&…

c语言高亮字符,C语言必背18个程序+190例--语法高亮

《C语言必背18个程序190例--语法高亮》由会员分享&#xff0c;可在线阅读&#xff0c;更多相关《C语言必背18个程序190例--语法高亮(111页珍藏版)》请在人人文库网上搜索。1、C语言必背18个经典程序输入什么显示什么main()int c&#xff1b;cgetchar()&#xff1b;同时(c&#…

原来你是这样子的OpenAI!来看看它背后那些有趣的人和事

来源&#xff1a;AI科技大本营 概要&#xff1a;它的目标非常明确&#xff0c;就是要确保人类安全&#xff0c;确保人类的权益不受未来机器人的影响&#xff0c;确保人类可以长久地与智能机器人共存。 提到OpenAI&#xff0c;业内可谓无人不知&#xff0c;无人不晓。 这可是人气…

C语言for循环的嵌套例题,c语言 for循环的嵌套(含答案)

c语言 for循环的嵌套(含答案)第 33、34 课时 for 循环的嵌套实验题一&#xff1a;1、下面程序的功能是计算:至 50 中是 7 的倍数的数值之和,请选择填空。D#include main() int i,sum 0; for(i1;i Bmain() int a,b,c,i; ac0; for(i0;imain() int i; #imain() int x,i; for(i1;im…

c语言bellman算法,求 最短路径中BELLMAN FORD算法实现的C程序

匿名用户1级2010-06-01 回答//这个是邻接表typedef struct oo{int len,num;struct oo *next;} link;typedef struct{int num;link *next;} graph;/*node[]图的邻接表n节点总数s源点dis[]到源点的最短路径长度pre[]最短路径上的前驱结点算法返回true&#xff0c;当且仅当途中不包…

Gartner十大IT预测:七大数字巨头,有五家将心甘情愿“自我颠覆”

来源&#xff1a;凤凰科技 概要&#xff1a;Gartner的预测&#xff0c;以及支持这些预测的假设&#xff0c;表明企业首席信息官必须首先是业务战略师&#xff0c;然后才是技术专家。 Gartner的预测&#xff0c;以及支持这些预测的假设&#xff0c;表明企业首席信息官必须首先是…

谷歌花4亿英镑下注AI开始有回报 DeepMind创收了

来源&#xff1a;腾讯科技 概要&#xff1a;DeepMind虽然还保持独立运营&#xff0c;但其对于谷歌产品的贡献恰逢其时。该公司2016年对于Alphabet其他公司的产品和服务所作出的贡献价值4000万英镑&#xff0c;这也是该公司首次创收。 外媒指出&#xff0c;Alphabet子公司谷歌周…

2016全球教育机器人发展白皮书

来源&#xff1a;199IT互联网数据中心 概要&#xff1a;白皮书梳理了全球教育机器人发展现状与趋势。从教育机器人的全球重要研究机构、市场产品评测、需求调研、产业链分析及未来市场发展预测&#xff0c;进行阐述。 白皮书梳理了全球教育机器人发展现状与趋势。从教育机器人的…

android上的java编译器,Android Studio:需要一个java编译器

我在尝试运行android应用程序时遇到错误。我在Windows 8 64位上运行android studio 0.3.2。Android Studio&#xff1a;需要一个java编译器这里的错误java: System Java Compiler was not found in classpath:java.lang.ClassNotFoundException: com.sun.tools.javac.api.Javac…

谷歌硬件战略缺陷:无法围绕谷歌助手打造硬件体系

来源&#xff1a;元器件交易网 概要&#xff1a;谷歌希望大规模销售基于谷歌助手的硬件&#xff0c;战略调整或许是其中重要的一步。 谷歌是否犯了重大错误&#xff1f; 谷歌正在以“人工智能优先”的战略取代此前“移动优先”的战略&#xff0c;并公开地推进这一战略。这是个大…

android 代码 升级rom,ROM之家简析:Android手机系统怎么升级

Android系统现在基本上每年都会有一次大的升级&#xff0c;以及很多次小的升级&#xff0c;对于手机用户来说&#xff0c;当然希望自己能用上最新版本的系统。除了手机厂商也会在适当时间发布官方版本系统升级&#xff0c;也可以自己在网上下载安装第三方系统升级。目前安卓手机…

在这场人工智能“战争”中,这些国家都在做些什么?

来源&#xff1a;Future智能 概要&#xff1a; 人工智能是引领未来的战略性技术&#xff0c;世界主要发达国家都已经把发展人工智能都作为提升其国家竞争力、维护国家安全的重要战略&#xff0c;进行针对性布局&#xff0c;力图在新一轮国际科技竞争中掌握主导权。 人工智能是引…

android使用 注解框架,Android实践 | 注解框架ButterKnife基本使用

使用ButterKnife&#xff0c;我们可以不用写很多的findViewById()语句&#xff0c;以及通过getResources获取String、Color等资源&#xff0c;这可以让我们的代码更加简洁&#xff0c;使用起来也很方便。下面来看怎么用吧&#xff01;首先当然是添加依赖(建议去github查看最新版…

DeepMind成立DMES新部门,旨在研究AI带来的社会伦理问题

来源&#xff1a;36kr 概要&#xff1a;随着人工智能的发展越来越成熟&#xff0c;随之带来的伦理和道德问题也成为了社会讨论的焦点问题之一。DeepMind成立了一个新部门&#xff0c;旨在专门研究人工智能带来的社会伦理问题。 DMES将于2018年启动关于算法偏见、问责制和自主杀…