随着系统提供的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_initcentos7 (epel clang)和ubuntu都以这种方式失败。
我做错什么了吗?
先前的堆栈溢出问题Using memory sanitizer with 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发布于 2015-01-25 21:03:38
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。
https://stackoverflow.com/questions/27774074
复制相似问题