2

With the system supplied libstdc++ the clang memory sanitizer is basically unusable due to false positives - eg the code below fails.

#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);
}

Building libstdc++ as described here:
https://code.google.com/p/memory-sanitizer/wiki/InstrumentingLibstdcxx

and running it like so:

LD_LIBRARY_PATH=~/goog-gcc-4_8/build/src/.libs ./msan_example

gives the foolowing output

/usr/bin/llvm-symbolizer: symbol lookup error: /home/hal/goog-gcc-4_8/build/src/.libs/libstdc++.so.6: undefined symbol: __msan_init

Both centos7 (epel clang) and ubuntu fail in this manner.

Is there something I'm doing wrong?

Previous stack overflow question Using memory sanitizer with libstdc++

edit Using @eugins suggestion, compile command line for this code is:

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
4

1 回答 1

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

于 2015-01-25T21:03:38.523 回答