VS2019中使用AddressSanitizer檢測(cè)內(nèi)存問(wèn)題

VS2019配置

image.png

PS:親測(cè)只支持Release | x64,雖然官方說(shuō)支持x86,但親測(cè)出現(xiàn)以下錯(cuò)誤:

==11836==Shadow memory range interleaves with an existing memory mapping. ASan cannot proceed correctly. ABORTING.
This can happen for multiple possible reasons:
   1. There is a module in the range of the shadow memory
   2. Some binaries are compiled with the Static CRT /MT(d) and some are built
      with the Dynamic CRT /MD(d)
       - this needs to be consistent across all binaries in a process, or ASan may
         try to initialize the runtime twice
   3. The function stack may be in the range of the shadow memory. This can
      sometimes be worked around by restarting the computer and trying again
==11836==ASan shadow was supposed to be located in the [0x2fff0000-0x3fffffff] range.

問(wèn)題代碼1:越界

#include <iostream>
int x1[100];
int main() {
    printf("Hello!\n");

    x1[101] = 5; // Boom!
    return 0;
}

運(yùn)行結(jié)果1

=================================================================
==14924==ERROR: AddressSanitizer: global-buffer-overflow on address 0x7ff6163d7a14 at pc 0x7ff6163d10ce bp 0x0065990ff870 sp 0x0065990ff878
WRITE of size 4 at 0x7ff6163d7a14 thread T0
==14924==WARNING: Failed to use and restart external symbolizer!
    #0 0x7ff6163d10cd in main C:\Users\Nova001845\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:6
    #1 0x7ff6163d1633 in __scrt_common_main_seh D:\a01\_work\26\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #2 0x7ffde2287973 in BaseThreadInitThunk+0x13 (C:\Windows\System32\KERNEL32.DLL+0x180017973)
    #3 0x7ffde25ba2f0 in RtlUserThreadStart+0x20 (C:\Windows\SYSTEM32\ntdll.dll+0x18005a2f0)

0x7ff6163d7a14 is located 4 bytes to the right of global variable 'x1' defined in 'ConsoleApplication1.cpp:2:4' (0x7ff6163d7880) of size 400
SUMMARY: AddressSanitizer: global-buffer-overflow C:\Users\Nova001845\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:6 in main

問(wèn)題代碼2:野指針

#include <iostream>
int main() {
    printf("Hello!\n");
    char* x2 = (char*)malloc(10 * sizeof(char*));
    free(x2);
    printf("%c", x2[5]); // Boom!
    return 0;
}

運(yùn)行結(jié)果2

=================================================================
==10072==ERROR: AddressSanitizer: heap-use-after-free on address 0x126f3b080025 at pc 0x7ff7a65b10d9 bp 0x008c8f6ff6e0 sp 0x008c8f6ff6e8
READ of size 1 at 0x126f3b080025 thread T0
==10072==WARNING: Failed to use and restart external symbolizer!
    #0 0x7ff7a65b10d8 in main C:\Users\Nova001845\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:6
    #1 0x7ff7a65b1643 in __scrt_common_main_seh D:\a01\_work\26\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #2 0x7ffde2287973 in BaseThreadInitThunk+0x13 (C:\Windows\System32\KERNEL32.DLL+0x180017973)
    #3 0x7ffde25ba2f0 in RtlUserThreadStart+0x20 (C:\Windows\SYSTEM32\ntdll.dll+0x18005a2f0)

0x126f3b080025 is located 5 bytes inside of 80-byte region [0x126f3b080020,0x126f3b080070)
freed by thread T0 here:
    #0 0x7ffda093bcc2 in _asan_wrap_GlobalSize+0x49129 (C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.28.29910\bin\HostX86\x64\clang_rt.asan_dynamic-x86_64.dll+0x18004bcc2)
    #1 0x7ff7a65b10b3 in main C:\Users\Nova001845\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:5
    #2 0x7ff7a65b1643 in __scrt_common_main_seh D:\a01\_work\26\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #3 0x7ffde2287973 in BaseThreadInitThunk+0x13 (C:\Windows\System32\KERNEL32.DLL+0x180017973)
    #4 0x7ffde25ba2f0 in RtlUserThreadStart+0x20 (C:\Windows\SYSTEM32\ntdll.dll+0x18005a2f0)

previously allocated by thread T0 here:
    #0 0x7ffda093be32 in _asan_wrap_GlobalSize+0x49299 (C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Tools\MSVC\14.28.29910\bin\HostX86\x64\clang_rt.asan_dynamic-x86_64.dll+0x18004be32)
    #1 0x7ff7a65b10a7 in main C:\Users\Nova001845\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:4
    #2 0x7ff7a65b1643 in __scrt_common_main_seh D:\a01\_work\26\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
    #3 0x7ffde2287973 in BaseThreadInitThunk+0x13 (C:\Windows\System32\KERNEL32.DLL+0x180017973)
    #4 0x7ffde25ba2f0 in RtlUserThreadStart+0x20 (C:\Windows\SYSTEM32\ntdll.dll+0x18005a2f0)

SUMMARY: AddressSanitizer: heap-use-after-free C:\Users\Nova001845\source\repos\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.cpp:6 in main
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容