这是由于Apple LLVM编译器在arm64上的行为不同于其他平台,造成内存对齐问题。解决方法是通过添加编译器选项"-fno-omit-frame-pointer"强制保留帧指针信息,从而使AddressSanitizer可以正确地捕获堆栈跟踪信息。
示例:
C++代码:
#include
int main() { char* buffer = new char[10]; buffer[11] = 'a'; // 内存溢出
std::cout << buffer << std::endl; // 输出溢出的数据
delete[] buffer;
return 0;
}
编译和运行:
clang++ -fsanitize=address -fno-omit-frame-pointer main.cpp ./a.out
输出:
==105029==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x600000000..0x60000000a at pc 0x00010ca170a9 bp 0x7ffeefbfffd0 sp 0x7ffeefbfffc0 WRITE of size 1 at 0x60000000000a thread T0 #0 0x10ca170a8 in main /path/to/main.cpp:6:16 #1 0x7fff2041ac88 (/usr/lib/system/libdyld.dylib+0x1ac88)