【发布时间】:2015-01-05 05:49:13
【问题描述】:
使用系统提供的 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 都以这种方式失败。
是不是我做错了什么?
之前的堆栈溢出问题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
【问题讨论】:
标签: c++ memory-leaks clang msan