linux下使用libasan检测进程内存异常

3 篇文章 0 订阅
1 篇文章 0 订阅

软件环境

CentOS Linux release 8.3.2011

按照libasan包

yum install libasan

测试例子

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
int my_print()
{
    int *a = malloc(10);
    printf("this is my_print, a:%p\n", a);
    return 0;
}
 
int main()
{
    my_print();
    return 0;
}

编译连接libasan

gcc -g tmp.c -lasan -o hello -fsanitize=address -fno-omit-frame-pointer -fsanitize-recover=address

运行例子

[ych]# ./hello 
this is my_print, a:0x602000000010

=================================================================
==3487654==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10 byte(s) in 1 object(s) allocated from:
    #0 0x7f209308fba8 in __interceptor_malloc (/lib64/libasan.so.5+0xefba8)
    #1 0x4007d7 in my_print /var/ych/tmp.c:7
    #2 0x400806 in main /var/ych/tmp.c:14
    #3 0x7f2092c007b2 in __libc_start_main (/lib64/libc.so.6+0x237b2)

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

指定asan选项启动进程

ASAN_OPTIONS=halt_on_error=0:user_sigaltstack=0:detect_leaks=1:malloc_context_size=15:log_path=/var/ych/asan.log ./hello

[ych]# ASAN_OPTIONS=halt_on_error=0:user_sigaltstack=0:detect_leaks=1:malloc_context_size=15:log_path=/var/ych/asan.log ./hello
this is my_print, a:0x602000000010

[ych]# cat asan.log.3488778 

=================================================================
==3488778==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 10 byte(s) in 1 object(s) allocated from:
    #0 0x7fd46ca72ba8 in __interceptor_malloc (/lib64/libasan.so.5+0xefba8)
    #1 0x4007d7 in my_print /var/ych/tmp.c:7
    #2 0x400806 in main /var/ych/tmp.c:14
    #3 0x7fd46c5e37b2 in __libc_start_main (/lib64/libc.so.6+0x237b2)

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

相关链接

asan官方资料

AddressSanitizerFlags

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值