简体   繁体   English

为什么内存消毒器报告使用了 std::map 的未初始化值?

[英]Why does the memory-sanitizer report use of an uninitialized value for std::map?

I'm using manjaro linux on x86-64.我在 x86-64 上使用 manjaro linux。 Memory-sanitizer in clang version 10.0.1 reported a use of uninitialized value error in std::map , which quite surprised me. Memory-sanitizer in clang version 10.0.1 报告了 use of uninitialized value in std::map错误,这让我很惊讶。 Did I do something wrong?我做错什么了吗?

$ cat test.cpp 
#include <map>
int main() {
    std::map<int, int> test;
    test.insert({1,2});
}
$ clang++ -fsanitize=memory test.cpp && ./a.out
==51936==WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x562889eaad9a  (/tmp/build/a.out+0x9fd9a)
    #1 0x562889eaae28  (/tmp/build/a.out+0x9fe28)
    #2 0x562889eaaba1  (/tmp/build/a.out+0x9fba1)
    #3 0x562889eaa51e  (/tmp/build/a.out+0x9f51e)
    #4 0x562889eaa087  (/tmp/build/a.out+0x9f087)
    #5 0x7f418e02b151  (/usr/lib/libc.so.6+0x28151)
    #6 0x562889e2b1dd  (/tmp/build/a.out+0x201dd)

SUMMARY: MemorySanitizer: use-of-uninitialized-value (/tmp/build/a.out+0x9fd9a) 
Exiting

FWIW it looks like libc++ is more MSAN-friendly than stdlibc++ because compiling a similar FWIW 看起来 libc++ 比 stdlibc++ 对 MSAN 更友好,因为编译了类似的

#include <map>
#include <string>

int main(int argc, char** argv) {
    std::map<int, std::string> m;
    m[argc] = argv[argc - 1];
    return 0;
}

code with the latter and running后者的代码并运行

% clang++ -fsanitize=memory -fno-omit-frame-pointer -g -O2 umr.cpp

results in a similar error, but doing导致类似的错误,但做

% clang++ -fsanitize=memory -fno-omit-frame-pointer -stdlib=libc++ -g -O2 umr.cpp && ./a.out

works fine (clang 13, Debian Sid).工作正常(clang 13,Debian Sid)。

When using MemorySanitizer, all libraries you use must be compiled with MemorySanitizer.使用 MemorySanitizer 时,您使用的所有库都必须使用 MemorySanitizer 进行编译。 Otherwise, there is a risk of false positives.否则,存在误报的风险。 This includes the C++ standard library itself.这包括 C++ 标准库本身。

You will find instructions for compiling libc++ with MemorySanitizer in the official sanitizers wiki:您可以在官方 sanitizers wiki 中找到使用 MemorySanitizer 编译 libc++ 的说明:

https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM