使用内存消毒器与libstdc ++ [英] Using memory sanitizer with libstdc++
问题描述
我希望使用clang中的 -fsanitize = memory
标志来分析以下程序:
I wish to use the -fsanitize=memory
flag in clang to analyse a program like the following:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
void writeToFile(){
ofstream o;
o.open("dum");
o<<"test"<<endl; //The error is here.
//It does not matter if the file is opened this way,
//or with o("dum");
o.close();
}
int main(){
writeToFile();
}
据我所知,这个程序是正确的, code> clang ++ san.cpp -fsanitize = memory 它在运行时失败:
As far as I know, this program is correct, but when I use clang++ san.cpp -fsanitize=memory
It fails (at runtime) with:
UMR in __interceptor_write at offset 0 inside [0x64800000e000, +5)
==9685== WARNING: MemorySanitizer: use-of-uninitialized-value
#0 0x7f48d0899ae5 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x7bae5)
#1 0x7f48d08d1787 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb3787)
#2 0x7f48d08d21e2 (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb41e2)
#3 0x7f48d08cfd1e (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0xb1d1e)
#4 0x7f48d08b1f2d (/usr/lib/x86_64-linux-gnu/libstdc++.so.6+0x93f2d)
#5 0x7f48d16d60f5 in writeToFile() /home/daniel/programming/test/santest.cpp:10
#6 0x7f48d16d61f4 in main /home/daniel/programming/test/santest.cpp:15
#7 0x7f48d0261de4 (/lib/x86_64-linux-gnu/libc.so.6+0x21de4)
#8 0x7f48d16d5e42 in _start (/home/daniel/programming/test/a.out+0x61e42)
SUMMARY: MemorySanitizer: use-of-uninitialized-value ??:0 ??
如何使这项功能正常运作?
How can I make this work properly?
Clang版本3.5,stdlibc ++版本6
Clang version 3.5, stdlibc++ version 6
推荐答案
代码很好,但是很多类似的错误是由以下原因引起的:需要clang的内存消毒器工具:
The code is fine, of course but many similar errors are cause by the following requirement of clang's memory sanitizer tool:
MemorySanitizer(没有动态组件)要求整个程序
代码包括库,除了libc / libm / libpthread,在某种程度上)。
MemorySanitizer (without a dynamic component) requires that the entire program code including libraries, (except libc/libm/libpthread, to some extent), is instrumented.
使用libstdc ++的cplusplus运行时是unistrumented并导致错误。你将不得不遵循一个有点费力的过程,如链接所述重建一个仪器化的libstdc ++或切换到libc ++(易于ish)
The cplusplus runtime you are using libstdc++ is unistrumented and causes errors. You will unfortunately have to follow a somewhat fiddly process as described at that link to rebuild an instrumented libstdc++ or switch to libc++ (easier-ish)
这篇关于使用内存消毒器与libstdc ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!