简体   繁体   English

clang ++与g ++的-fsanitize = address的不同输出

[英]different output for -fsanitize=address with clang++ vs g++

In the following code I have a problem. 在以下代码中,我遇到了问题。 When I give it a vector that is still completely empty the code crashes, because vector.size() - 1 can't be negative, hence it wraps around. 当我给它一个仍然完全为空的向量时,代码会崩溃,因为vector.size()-1不能为负,因此它会回绕。 Because the vector is empty accessing container[0] is invalid. 由于向量为空,因此访问container [0]无效。

using namespace std;

template<typename T, typename A>
std::string vec_to_python_list(
        const std::string& name,
        const vector<T, A>& container
        ) {
    std::stringstream stream(ios::out);
    stream << name << " = [" << '\n';
    for (typename vector<T, A>::size_type i = 0; i < container.size() - 1; i++)
        stream << "\t" << container[i] << ",\n";
    if (name.size() > 0)
        stream << "\t" << container[container.size() - 1] << "\n";
    stream << "]";
    return stream.str();
}

My question is about the output that it produces. 我的问题是关于它产生的输出。

If I compile with g++ on Ubuntu-16.04 如果我在Ubuntu-16.04上使用g ++进行编译

g++ -Wall -Wextra -std=c++14 -g -fsanitize=address -O0  -o test_kmeans test_kmeans.cpp

I obtain the next useful info: 我获得下一个有用的信息:

#0 0x402a56 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > vec_to_python_list<double, std::allocator<double> >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<double, std::allocator<double> > const&) /home/hetepeperfan/programming/cpp/kmeans/test_kmeans.cpp:46
#1 0x401f07 in main /home/hetepeperfan/programming/cpp/kmeans/test_kmeans.cpp:66
#2 0x7f393881f82f in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#3 0x401ba8 in _start (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x401ba8)

However if i compile with clang++ (still on Ubuntu-16.04) 但是如果我用clang ++编译(仍然在Ubuntu-16.04上)

clang++ -Wall -Wextra -std=c++14 -g -fsanitize=address -O0  -o test_kmeans test_kmeans.cpp

I get less useful results: 我得到的有用结果较少:

#0 0x4eecae  (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x4eecae)
#1 0x4ee056  (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x4ee056)
#2 0x7f6ed883a82f  (/lib/x86_64-linux-gnu/libc.so.6+0x2082f)
#3 0x419838  (/home/hetepeperfan/programming/cpp/kmeans/test_kmeans+0x419838)

What am I do'ing wrong so that g++ with -fsanitize=address works and clang++ doesn't produce as useful results, It seems like the debugging symbols aren't added? 我在做什么错,以便带有-fsanitize = address的g ++可以正常工作,而clang ++不会产生有用的结果,似乎未添加调试符号?

EDIT The debug symbols seem to be present, because with gdb I can easily step through the code and with the --tui gdb option I can see my code, so that isn't the issue. 编辑调试符号似乎存在,因为使用gdb可以轻松地逐步执行代码,而使用--tui gdb选项可以看到我的代码,所以这不是问题。

Install the llvm-symbolizer. 安装llvm-symbolizer。 Also set the ASAN_SYMBOLIZER_PATH environment variable to something like 还要将ASAN_SYMBOLIZER_PATH环境变量设置为类似

ASAN_SYMBOLIZER_PATH=/usr/lib/llvm-5.0/bin/llvm-symbolizer

llvm is looking for an executable named llvm-symbolizer not llvm-symbolizer-3.8 , that's why the environment variable must point to llvm-symbolizer a filename that has no version number. llvm正在寻找名为llvm-symbolizer而不是llvm-symbolizer-3.8的可执行文件,这就是为什么环境变量必须指向llvm-symbolizer的文件名没有版本号的原因。 If all your symbolizers have version numbers in their file name, make a symbolic link that has no version number in it. 如果您所有的符号化程序的文件名中都有版本号,请建立一个没有版本号的符号链接。

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

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