示例代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
chunli@blog:~/source$ cat gcc_asan_test.c
#include <stdio.h>
#include <string.h>

int fun1(char *dst, const char *str)
{
dst[10] = str[10];
return 0;
}

int main(int argc, const char**argv)
{
int i = 0;
for(int i = 0; i < argc; i++)
{
printf("参数%u [%s]\n", i, argv[i]);
}


char buff[10];
const char *str = "HelloWorld HelloWorld HelloWorld";
fun1(buff, str);

int n = 2;
switch(n)
{
case 0x1 ... 0xf:
printf("hello\n");
break;
default:
break;
}

return 0;
}
chunli@blog:~/source$


编译:
gcc -O0 -g gcc_asan_test.c -lasan

远程机

阿里云主机 (Ubuntu 20.04.3 LTS)
程序版本:(原版 去除符号后的版本)
运行:

1
2
3
4
5
6
7
8
9
10
11
12
root@Shanghai:/tmp# gdbserver localhost:10000 ./a.out 123 ABC
Process ./a.out created; pid = 3388876
Listening on port 10000

Remote debugging from host ::ffff:183.193.42.172, port 9346
参数0 [./a.out]
参数1 [123]
参数2 [ABC]
hello
==3388876==LeakSanitizer has encountered a fatal error.
==3388876==HINT: For debugging, try setting environment variable LSAN_OPTIONS=verbosity=1:log_threads=1
==3388876==HINT: LeakSanitizer does not work under ptrace (strace, gdb, etc)

本地端

vmware虚拟机 (Debian 11.2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# gdb --eval-command="file a.out" --eval-command="target remote fly.li-chunli.top:10000"
Reading symbols from a.out...
Remote debugging using fly.li-chunli.top:10000
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
warning: File transfers from remote targets can be slow. Use "set sysroot" to access files locally instead.
Reading /lib64/ld-linux-x86-64.so.2 from remote target...
Reading symbols from target:/lib64/ld-linux-x86-64.so.2...
Reading /lib64/814b3de3206080ff0f23fc29a7b85fd990787d.debug from remote target...
Reading /lib64/.debug/814b3de3206080ff0f23fc29a7b85fd990787d.debug from remote target...
Reading /usr/lib/debug//lib64/814b3de3206080ff0f23fc29a7b85fd990787d.debug from remote target...
Reading /usr/lib/debug/lib64//814b3de3206080ff0f23fc29a7b85fd990787d.debug from remote target...
Reading target:/usr/lib/debug/lib64//814b3de3206080ff0f23fc29a7b85fd990787d.debug from remote target...
(No debugging symbols found in target:/lib64/ld-linux-x86-64.so.2)
0x00007ffff7fd0100 in ?? () from target:/lib64/ld-linux-x86-64.so.2


(gdb) b main
Breakpoint 1 at 0x555555555179: file gcc_asan_test.c, line 12.
(gdb) c
Breakpoint 1, main (argc=3, argv=0x7fffffffe5b8) at gcc_asan_test.c:12
12 int i = 0;

Continuing.
Reading /lib/x86_64-linux-gnu/libasan.so.6 from remote target...
Reading /lib/x86_64-linux-gnu/libc.so.6 from remote target...
Reading /lib/x86_64-linux-gnu/libdl.so.2 from remote target...
Reading /lib/x86_64-linux-gnu/libpthread.so.0 from remote target...
Reading /lib/x86_64-linux-gnu/libm.so.6 from remote target...
Reading /lib/x86_64-linux-gnu/libgcc_s.so.1 from remote target...
Reading /lib/x86_64-linux-gnu/eb2bf2ca2e61f4c09d33c3feba0ec3e5bea8d4.debug from remote target...
Reading /lib/x86_64-linux-gnu/.debug/eb2bf2ca2e61f4c09d33c3feba0ec3e5bea8d4.debug from remote target...
Reading /usr/lib/debug//lib/x86_64-linux-gnu/eb2bf2ca2e61f4c09d33c3feba0ec3e5bea8d4.debug from remote target...
Reading /usr/lib/debug/lib/x86_64-linux-gnu//eb2bf2ca2e61f4c09d33c3feba0ec3e5bea8d4.debug from remote target...
Reading target:/usr/lib/debug/lib/x86_64-linux-gnu//eb2bf2ca2e61f4c09d33c3feba0ec3e5bea8d4.debug from remote target...
Reading /lib/x86_64-linux-gnu/20051dc51cefc607e9561cab6e1afce2f406f9.debug from remote target...
Reading /lib/x86_64-linux-gnu/.debug/20051dc51cefc607e9561cab6e1afce2f406f9.debug from remote target...
Reading /usr/lib/debug//lib/x86_64-linux-gnu/20051dc51cefc607e9561cab6e1afce2f406f9.debug from remote target...
Reading /usr/lib/debug/lib/x86_64-linux-gnu//20051dc51cefc607e9561cab6e1afce2f406f9.debug from remote target...
Reading target:/usr/lib/debug/lib/x86_64-linux-gnu//20051dc51cefc607e9561cab6e1afce2f406f9.debug from remote target...
Reading /lib/x86_64-linux-gnu/410a28019221ff90dc8a71cc7ab4113ee0e2db.debug from remote target...
Reading /lib/x86_64-linux-gnu/.debug/410a28019221ff90dc8a71cc7ab4113ee0e2db.debug from remote target...
Reading /usr/lib/debug//lib/x86_64-linux-gnu/410a28019221ff90dc8a71cc7ab4113ee0e2db.debug from remote target...
Reading /usr/lib/debug/lib/x86_64-linux-gnu//410a28019221ff90dc8a71cc7ab4113ee0e2db.debug from remote target...
Reading target:/usr/lib/debug/lib/x86_64-linux-gnu//410a28019221ff90dc8a71cc7ab4113ee0e2db.debug from remote target...
Reading /lib/x86_64-linux-gnu/3fa5753fbaa3f50acd506fe262fae16aed327f.debug from remote target...
Reading /lib/x86_64-linux-gnu/.debug/3fa5753fbaa3f50acd506fe262fae16aed327f.debug from remote target...
Reading /usr/lib/debug//lib/x86_64-linux-gnu/3fa5753fbaa3f50acd506fe262fae16aed327f.debug from remote target...
Reading /usr/lib/debug/lib/x86_64-linux-gnu//3fa5753fbaa3f50acd506fe262fae16aed327f.debug from remote target...
Reading target:/usr/lib/debug/lib/x86_64-linux-gnu//3fa5753fbaa3f50acd506fe262fae16aed327f.debug from remote target...
Reading /lib/x86_64-linux-gnu/96a8ef14673927602c39ff755abfe97732dc6f.debug from remote target...
Reading /lib/x86_64-linux-gnu/.debug/96a8ef14673927602c39ff755abfe97732dc6f.debug from remote target...
Reading /usr/lib/debug//lib/x86_64-linux-gnu/96a8ef14673927602c39ff755abfe97732dc6f.debug from remote target...
Reading /usr/lib/debug/lib/x86_64-linux-gnu//96a8ef14673927602c39ff755abfe97732dc6f.debug from remote target...
Reading target:/usr/lib/debug/lib/x86_64-linux-gnu//96a8ef14673927602c39ff755abfe97732dc6f.debug from remote target...
[Inferior 1 (process 3388876) exited with code 01]
(gdb)