【发布时间】:2022-06-15 12:27:47
【问题描述】:
问题 - 在在线编辑器中执行 C++ 程序的问题 - 可能的运行时错误。 目标 - 想知道什么样的错误会导致这种错误? 场景 - 我正在解决 LeetCode 平台的编辑器上的一个问题(他们在问题旁边提供的用于编写解决方案的编辑器)并且由于一些运行时错误,我收到了这条我无法解释的消息:-
the message that was displayed after the program was run
如果在图片中看不清楚,这里是日志消息的文本:-
=================================================================
==33==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x6020000000a0 at pc 0x0000003479c9 bp 0x7ffd817004f0 sp 0x7ffd817004e8
WRITE of size 4 at 0x6020000000a0 thread T0
#2 0x7f1c7bd650b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
0x6020000000a0 is located 0 bytes to the right of 16-byte region [0x602000000090,0x6020000000a0)
allocated by thread T0 here:
#6 0x7f1c7bd650b2 (/lib/x86_64-linux-gnu/libc.so.6+0x270b2)
Shadow bytes around the buggy address:
0x0c047fff7fc0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fd0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7fe0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff7ff0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
0x0c047fff8000: fa fa fd fa fa fa fd fa fa fa fd fa fa fa fd fa
=>0x0c047fff8010: fa fa 00 00[fa]fa fd fa fa fa fd fa fa fa fd fa
0x0c047fff8020: fa fa 00 fa fa fa fd fa fa fa 00 fa fa fa fd fa
0x0c047fff8030: fa fa 00 fa fa fa 00 00 fa fa 00 00 fa fa 00 00
0x0c047fff8040: fa fa 00 00 fa fa fd fa fa fa fd fa fa fa 00 fa
0x0c047fff8050: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
0x0c047fff8060: 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
==33==ABORTING
第一行“heap-buffer-overflow”让我知道程序使用了太多的堆内存,因此出现了运行时错误。 但我很想知道——这里到底发生了什么?尤其是“Heap left redzone”、“Poisoned by User”、“Array cookie”、“ASan internal” >"、"阴影间隙" - 它们是什么意思?
【问题讨论】:
-
很可能,您的代码中有越界数组访问。
-
你想阅读 Asan 手册,试试这个github.com/google/sanitizers/wiki/AddressSanitizer#faq
-
您应该在 gdb 中本地运行代码并将 ASAN 配置为 break on failures,这将告诉您在数组边界之外访问代码中的哪个位置
-
这真的很有帮助。非常感谢
标签: c++ debugging logging runtime-error heap-memory