1

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. Because the vector is empty accessing container[0] is invalid.

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

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++ -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?

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.

ks1322
  • 33,961
  • 14
  • 109
  • 164
hetepeperfan
  • 4,292
  • 1
  • 29
  • 47

1 Answers1

4

Install the llvm-symbolizer. Also set the ASAN_SYMBOLIZER_PATH environment variable to something like

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. If all your symbolizers have version numbers in their file name, make a symbolic link that has no version number in it.

Michael Veksler
  • 8,217
  • 1
  • 20
  • 33
  • Hi michael, thanks for your answer, however I think I've got the llvm-symbolizer on my path: `$which llvm-symbolizer-3.8 /usr/bin/llvm-symbolizer-3.8` If I put that on the path I get: ==25910==ERROR: External symbolizer path is set to '/usr/bin/llvm-symbolizer-3.8' which isn't a known symbolizer. Please set the path to the llvm-symbolizer binary or other known tool. – hetepeperfan Aug 09 '18 at 19:46
  • Apparently, this is not enough since llvm looks for `llvm-symbolizer` not `llvm-symbolizer-3.8`. – Michael Veksler Aug 09 '18 at 19:57
  • Thanks for the tip. It works now I've made a soft link in my working directory without the "-3.8" suffix to the one on in my /usr/bin directory and if I put that one on the the path `ASAN_SYMBOLIZER_PATH=./llvm-symbolizer` then it works. Seems a small packaging problem on Ubuntu-16.04 – hetepeperfan Aug 09 '18 at 21:12
  • 1
    Glad to help. I have seen this tip in Jason Turner's "C++ weekly" on YouTube. – Michael Veksler Aug 09 '18 at 21:14