clang memory sanitizer; how to make it print source line numbers
Asked Answered
N

1

16

I'm compiling my program with clang++ -fsanitize=memory -fsanitize-memory-track-origins -fno-omit-frame-pointer -g -O0 and when I run it, the output is:

matiu@matiu-laptop:~/projects/json++11/build$ ./tests 
.......==10534== WARNING: MemorySanitizer: use-of-uninitialized-value
    #0 0x7fe7602d4a51 (/home/matiu/projects/json++11/build/tests+0x106a51)
    #1 0x7fe7602dfca6 (/home/matiu/projects/json++11/build/tests+0x111ca6)
    ...
    #31 0x7fe75edbaec4 (/lib/x86_64-linux-gnu/libc.so.6+0x21ec4)
    #32 0x7fe7602808dc (/home/matiu/projects/json++11/build/tests+0xb28dc)

  Uninitialized value was created by a heap allocation
    #0 0x7fe76026e7b3 (/home/matiu/projects/json++11/build/tests+0xa07b3)
    #1 0x7fe7602ee7da (/home/matiu/projects/json++11/build/tests+0x1207da)
    ...
    #18 0x7fe7602c1c4c (/home/matiu/projects/json++11/build/tests+0xf3c4c)
    #19 0x7fe7602873fa (/home/matiu/projects/json++11/build/tests+0xb93fa)

SUMMARY: MemorySanitizer: use-of-uninitialized-value ??:0 ??
Exiting

How can I make it show line numbers like in the beautiful examples: http://clang.llvm.org/docs/MemorySanitizer.html

I'm suspecting it might not be possible, due to my pragram being one giant nested bunch of lambdas: https://github.com/matiu2/json--11/blob/master/tests.cpp

Nagaland answered 25/6, 2014 at 2:59 Comment(2)
So it does print line numbers in examples, but not in your code? addr2line could be helpful, but not truly what you've asked for.Cockloft
Thanks @Cockloft that definitely helps: $ addr2line -e tests 0x15c59a /.../bandit/src/bandit/bandit/grammar.h:126Nagaland
T
14

With the address sanitizer I noticed that I needed to have these environment variables defined:

  • ASAN_OPTIONS=symbolize=1 (only needed when compiled with GCC > 4.8) and
  • ASAN_SYMBOLIZER_PATH=$(which llvm-symbolizer) I think the symbolizer is what you're looking for. It transforms symbols to file names with line numbers and columns.

On the memory sanitizer project website it reads:

Symbolization

Set MSAN_SYMBOLIZER_PATH environment variable to the path to llvm-symbolizer binary (normally built with LLVM). MemorySanitizer will use it to symbolize reports on-the-fly.

So you need MSAN_SYMBOLIZER_PATH to be set analogous to ASAN_SYMBOLIZER_PATH.

Terrify answered 18/7, 2014 at 9:29 Comment(4)
Thanks Konrad, that was exactly what I needed :) It turns out, on my Ubuntu installation the llvm-symbolizer was named llvm-symbolizer-3.5 .. adding a symlink to it at /usr/bin/llvm-symbolizer and just having it in the path did the trick.Nagaland
On Ubuntu, creating the symbolic link is still needed for the line numbers to be printed.Diagnosis
on ubuntu, you need apt install llvm (at least as of 18.04)Ockeghem
@matiu, in Ubuntu 20.10 the location is the expected one: /usr/bin/llvm-symbolizer.Shout

© 2022 - 2024 — McMap. All rights reserved.