컴퓨터/노트북/인터넷
IT 컴퓨터 기기를 좋아하는 사람들의 모임방
🕰️
2022.03.02 21:27
Linux gcc에는 메모리 누수를 감지하는 도구인 San이 함께 제공됩니다.
조회 수 44 추천 수 0 댓글 0
메모리 누수 를 감지하고 문제를 해결할 때 몇 가지 유용한 도구를 선택해야 합니다.dmalloc은 컴파일이 복잡하고 valgrind에 너무 많이 의존하기 때문에 gcc의 자체 메모리 누수 감지 도구와 함께 제공되는 asan을 사용하기로 선택합니다.
Asan은 버전 4.8 이후에 지원됩니다. 아래에서 사용해보자 효과를 보자.
의존 하는 asan 라이브러리 설치 : libasan.so yum 설치 libasan ce:normal">
매개변수 : -fsanitize=주소 -fno-생략-프레임 포인터 -g -O2 tion-style:initial">
이 기능은 런타임 감지이며 실행되지 않는 코드는 감지할 수 없습니다. 범위를 벗어난 메모리
int fun0(){ char str[4] = {0,}; strcpy(str,"测试"); return 0; }
================================================================= ==12724== ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffe1ac0d566 at pc 0x4008d3 bp 0x7ffe1ac0d530 sp 0x7ffe1ac0d520 WRITE of size 1 at 0x7ffe1ac0d566 thread T0 #0 0x4008d2 (/home/yubo.wang/4g-box/func-call/a.out+0x4008d2) #1 0x7f82632a9444 (/usr/lib64/libc-2.17.so+0x22444) #2 0x400931 (/home/yubo.wang/4g-box/func-call/a.out+0x400931) Address 0x7ffe1ac0d566 is located at offset 38 in frame <main> of T0's stack: This frame has 1 object(s): [32, 36) 'str' HINT: this may be a false positive if your program uses some custom stack unwind mechanism or swapcontext (longjmp and C++ exceptions *are* supported) Shadow bytes around the buggy address: 0x100043579a50: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579a60: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579a70: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579a80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579a90: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 =>0x100043579aa0: 00 00 00 00 00 00 00 00 f1 f1 f1 f1[04]f4 f4 f4 0x100043579ab0: f3 f3 f3 f3 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579ac0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579ad0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579ae0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0x100043579af0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 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 Heap righ redzone: fb Freed Heap region: fd Stack left redzone: f1 Stack mid redzone: f2 Stack right redzone: f3 Stack partial redzone: f4 Stack after return: f5 Stack use after scope: f8 Global redzone: f9 Global init order: f6 Poisoned by user: f7 ASan internal: fe ==12724== ABORTING
메모리 누수
char *fun1(char *str) { static char *p; p = malloc(64); strcpy(p,str); return p; } int fun2(){ char *str=fun1("abcd"); printf("str=%sn",str); return 0;
int fun3(){ char *p = NULL; strcpy(p,"a"); return 0; } ASAN:SIGSEGV ================================================================= ==12787== ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x00000040081c sp 0x7ffc02097320 bp 0x7ffc02097320 T0) AddressSanitizer can not provide additional info. #0 0x40081b (/home/yubo.wang/4g-box/func-call/a.out+0x40081b) #1 0x7f7be5ab3444 (/usr/lib64/libc-2.17.so+0x22444) #2 0x4008b1 (/home/yubo.wang/4g-box/func-call/a.out+0x4008b1) ==12787== ABORTING
요약하다