gcc AddressSanitizer

3081 단어
프로그램에서 메모리와 관련된 문제가 발생하는 것은 줄곧 사람을 크게 만든다.여기서 Asan 관련 내용을 소개하고, debug 메모리 관련 문제에 도움이 되었으면 합니다.
Asan 관련 프로필은wiki를 참조하십시오.https://en.wikipedia.org/wiki/AddressSanitizer
Asan은 clang(3.1 이후)과 gcc(4.8 이후)에서 모두 지원되며, 다음은 gcc의 예입니다.
int main(int argc ,char **argv)
{
    int stack_array[100];
    stack_array[1] = 100;
    return stack_array[argc + 100];
}

g++ -g -fsanitize=address main.cpp

컴파일한 후 실행
./a.out
=================================================================
==7163==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7fff9666ccd4 at pc 0x000000400906 bp 0x7fff9666cb00 sp 0x7fff9666caf0
READ of size 4 at 0x7fff9666ccd4 thread T0
    #0 0x400905 in main /home/tsing/asan/main.cpp:5
    #1 0x7f199d0e982f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
    #2 0x400748 in _start (/home/tsing/asan/a.out+0x400748)

Address 0x7fff9666ccd4 is located in stack of thread T0 at offset 436 in frame
    #0 0x400825 in main /home/tsing/asan/main.cpp:2

  This frame has 1 object(s):
    [32, 432) 'stack_array' <== Memory access at offset 436 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/tsing/asan/main.cpp:5 main
Shadow bytes around the buggy address:
  0x100072cc5940: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100072cc5950: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100072cc5960: 00 00 00 00 f1 f1 f1 f1 00 00 00 00 00 00 00 00
  0x100072cc5970: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100072cc5980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
=>0x100072cc5990: 00 00 00 00 00 00 00 00 00 00[f4]f4 f3 f3 f3 f3
  0x100072cc59a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100072cc59b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100072cc59c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100072cc59d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
  0x100072cc59e0: 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 right 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
  Container overflow:      fc
  Array cookie:            ac
  Intra object redzone:    bb
  ASan internal:           fe
==7163==ABORTING

프로그램을 실행하면 경계를 넘어서 방문하는 콜스테이크를 출력합니다.
gcc-fsanitize 옵션은 정말 강력합니다.heap구역에서 동적 신청한 메모리, 전역 데이터 구역의 메모리를 모두 탐지할 수 있습니다.또한 -fsanitize 옵션이 많기 때문에 gcc의 매뉴얼을 참고할 수 있습니다.https://gcc.gnu.org/onlinedocs/한 부 준비하라고 강력히 건의하다.이러한 옵션은 메모리 유출 검측, 다중 스레드 임계 자원 검측 등을 포함한다.

좋은 웹페이지 즐겨찾기