在libstdc ++上使用-fsanitize = linux上的clang内存

时间:2015-01-05 05:49:13

标签: c++ memory-leaks llvm-clang msan

使用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

1 个答案:

答案 0 :(得分:2)

MSan生成llvm-symbolizer进程,将堆栈跟踪PC转换为函数名称和文件/行号。由于LD_LIBRARY_PATH设置,已检测的libstdc ++被加载到主MSan进程(这是好的)和llvm-symbolizer进程(它不会工作)。

处理它的首选方法是通过RPATH设置(在链接时):

-Wl,-rpath,/path/to/libstdcxx_msan

您还可以查看更详细和最新的msan / libc ++指南: https://code.google.com/p/memory-sanitizer/wiki/LibcxxHowTo