在 linux 上使用 -fsanitize=memory with clang with libstdc++

Using -fsanitize=memory with clang on linux with libstdc++

本文关键字:with memory clang libstdc++ -fsanitize linux      更新时间:2023-10-16

使用系统提供的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