AddressSanitizer使用入门

AddressSanitizer

centos安装相关库

yum install devtoolset-11-libasan-devel

使用

  • 测试代码
[root@lzw test]# cat test_address_sanitizer.c 
#include <stdlib.h>
#include <stdio.h>
int test() {
    char *x = (char*)malloc(10 * sizeof(char*));
    free(x);
    return x[5];
}

int main() {
    char a = test();
    printf("hello a:%s",a);
}
  • 编译
[root@lzw test]# gcc -g -fsanitize=address test_address_sanitizer.c  -o t
  • 运行效果
[root@lzw test]# ./t
=================================================================
==25973==ERROR: AddressSanitizer: heap-use-after-free on address 0x607000000025 at pc 0x000000401204 bp 0x7ffd14d09d20 sp 0x7ffd14d09d10
READ of size 1 at 0x607000000025 thread T0
    #0 0x401203 in test /data/lzw/game/test/test_address_sanitizer.c:6
    #1 0x40121d in main /data/lzw/game/test/test_address_sanitizer.c:10
    #2 0x7f4e7a73f554 in __libc_start_main (/lib64/libc.so.6+0x22554)
    #3 0x4010f8  (/data/lzw/game/test/t+0x4010f8)

0x607000000025 is located 5 bytes inside of 80-byte region [0x607000000020,0x607000000070)
freed by thread T0 here:
    #0 0x7f4e7ab9f3e7 in free (/lib64/libasan.so.6+0xb43e7)
    #1 0x4011c7 in test /data/lzw/game/test/test_address_sanitizer.c:5
    #2 0x40121d in main /data/lzw/game/test/test_address_sanitizer.c:10
    #3 0x7f4e7a73f554 in __libc_start_main (/lib64/libc.so.6+0x22554)

previously allocated by thread T0 here:
    #0 0x7f4e7ab9f737 in malloc (/lib64/libasan.so.6+0xb4737)
    #1 0x4011b7 in test /data/lzw/game/test/test_address_sanitizer.c:4
    #2 0x40121d in main /data/lzw/game/test/test_address_sanitizer.c:10
    #3 0x7f4e7a73f554 in __libc_start_main (/lib64/libc.so.6+0x22554)

SUMMARY: AddressSanitizer: heap-use-after-free /data/lzw/game/test/test_address_sanitizer.c:6 in test
Shadow bytes around the buggy address:
  0x0c0e7fff7fb0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0e7fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0e7fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0e7fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x0c0e7fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x0c0e7fff8000: fa fa fa fa[fd]fd fd fd fd fd fd fd fd fd fa fa
  0x0c0e7fff8010: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8020: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8030: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8040: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
  0x0c0e7fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
Shadow byte legend (one shadow byte represents 8 application bytes):
  Addressable:           00
  Partially addressable: 01 02 03 04 05 06 07 
  Heap left redzone:       fa
  Freed heap region:       fd
  Stack left redzone:      f1
  Stack mid redzone:       f2
  Stack right redzone:     f3
  Stack after return:      f5
  Stack use after scope:   f8
  Global redzone:          f9
  Global init order:       f6
  Poisoned by user:        f7
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
  Left alloca redzone:     ca
  Right alloca redzone:    cb
  Shadow gap:              cc
==25973==ABORTING

参考文档

https://www.bynav.com/cn/resource/bywork/healthy-work/70.html
https://github.com/google/sanitizers/wiki/AddressSanitizer
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

打杂程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值