在 linux 上使用 -fsanitize=memory with clang with libstdc++
Using -fsanitize=memory with clang on linux with libstdc++
使用系统提供的libstdc++,由于误报,clang内存清理器基本上无法使用 - 例如下面的代码失败。
#include <iostream>
#include <fstream>
int main(int argc, char **argv)
{
double foo = 1.2;
std::ofstream out("/tmp/junk");
auto prev = out.flags(); //false positive here
out.setf(std::ios::scientific);
out << foo << std::endl;
out.setf(prev);
}
构建 libstdc++,如下所述:
https://code.google.com/p/memory-sanitizer/wiki/InstrumentingLibstdcxx
并像这样运行它:
LD_LIBRARY_PATH=~/goog-gcc-4_8/build/src/.libs ./msan_example
给出嘟的输出
/usr/bin/llvm-symbolizer: symbol lookup error: /home/hal/goog-gcc-4_8/build/src/.libs/libstdc++.so.6: undefined symbol: __msan_init
Centos7 (epel clang) 和 ubuntu 都以这种方式失败。
我做错了什么吗?
以前的堆栈溢出问题 将内存清理器与 libstdc++
结合使用
编辑使用@eugins建议,此代码的编译命令行为:
clang++ -std=c++11 -fPIC -O3 -g -fsanitize=memory -fsanitize-memory-track-origins -fPIE -fno-omit-frame-pointer -Wall -Werror -Xlinker --build-id -fsanitize=memory -fPIE -pie -Wl,-rpath,/home/hal/goog-gcc-4_8/build/src/.libs/libstdc++.so test_msan.cpp -o test_msan
$ ./test_msan
==19027== WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x7f66df377d4e in main /home/hal/tradingsystems/test_msan.cpp:9
#1 0x7f66ddefaaf4 in __libc_start_main (/lib64/libc.so.6+0x21af4)
#2 0x7f66df37780c in _start (/home/hal/tradingsystems/test_msan+0x7380c)
Uninitialized value was created by an allocation of 'out' in the stack frame of function 'main'
#0 0x7f66df377900 in main /home/hal/tradingsystems/test_msan.cpp:6
SUMMARY: MemorySanitizer: use-of-uninitialized-value /home/hal/tradingsystems/test_msan.cpp:9 main
Exiting
> MSan 生成 llvm 符号化程序进程,将堆栈跟踪 PC 转换为函数名称和文件/行号。由于LD_LIBRARY_PATH设置,检测的libstdc++被加载到主MSan进程(这很好)和llvm符号化器进程(这将不起作用)中。
处理它的首选方法是通过 RPATH 设置(在链接时):
-Wl,-rpath,/path/to/libstdcxx_msan
你也可以看看这个 msan/libc++ 指南,它更详细和最新:https://code.google.com/p/memory-sanitizer/wiki/LibcxxHowTo
相关文章:
- Problems with std::cin.fail()
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- Python ctype 'c_char_p' Memory Leak
- 这对"With a stackless coroutine, only the top-level routine may be suspended."意味着什么
- Boost.TEST with CLion: "Test framework quit unexpectedly"
- 避免碎片化的ClientHellos with OpenSSL (DTLS)
- Issues with Win32 ReadProcessMemory API
- Qt with WinAPI MouseProc
- [[maybe_unused]] with structured_binding?
- Issue with WriteProcessMemory
- 模式"allocate memory or use existing data"
- OpenCV RTP-Stream with FFMPEG
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- std::adjacent_difference with std::chrono time_point
- DLL Made with CMake 使程序崩溃
- QtCreator with C 库中的链接器问题
- SHBrowseForFolder with BIF_BROWSEFORCOMPUTER and SHGetPathFr
- 在 linux 上使用 -fsanitize=memory with clang with libstdc++
- Bad Linux Memory Map File Performance with Random Access C++ & Python
- 使用 C 字符串会发出警告:"Address of stack memory associated with local variable returned"