1

I'm using CLion IDE, Cmake with GCC compiler and I'm trying to run binary with address sanitizer. I followed this: What's the proper way to enable AddressSanitizer in CMake that works in Xcode and added

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")

to CMakeLists.txt file and then got an error when running the binary in CLion using the run button:

==22084==ASan runtime does not come first in initial library list; you should either link runtime to your application or manually preload it with LD_PRELOAD.

When I run the binary via terminal, it works as it should be. It doesn't work in CLion's run window. https://i.stack.imgur.com/Lnqmc.jpg

Edit: The solution was adding LD_PRELOAD=libasan.so.5 to environment variables of CLion.

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.17)
project(Playground)

set(CMAKE_CXX_STANDARD 14)

add_executable(Playground main.cpp)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -g")

What's happening?

Adding these lines from the stackoverflow thread to CMakeLists.txt file didnt work too.

set (CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")

set (CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fno-omit-frame-pointer -fsanitize=address")

set(CMAKE_EXE_LINKER_FLAGS_INIT "-fsanitize=address -fno-omit-frame-pointer")

and this line from this guide https://www.jetbrains.com/help/clion/google-sanitizers.html#LSanChapter also didnt work

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address -fno-omit-frame-pointer -O1")

Then I tried adding LD_TRACE_LOADED_OBJECTS=1 to CLion environment variables and nothing changed.

My ldd ./file command output of the binary:

    linux-vdso.so.1 (0x00007ffd82d96000)
    libgtk3-nocsd.so.0 => /lib/x86_64-linux-gnu/libgtk3-nocsd.so.0 (0x00007f7d532e1000)
    libasan.so.5 => /lib/x86_64-linux-gnu/libasan.so.5 (0x00007f7d528af000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f7d526bd000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f7d526b7000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f7d52694000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f7d52689000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f7d52538000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f7d5251d000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f7d5350c000)
burny
  • 11
  • 4
  • Please show us your `CMakeLists.txt`. And have you tried to do what the error message tells you ("you should either link runtime to your application or manually preload it with LD_PRELOAD")? – Some programmer dude Nov 13 '20 at 13:08
  • 1
    Since apparently CLion sees a different load order, can you add `LD_TRACE_LOADED_OBJECTS=1` to your run configuration's environment variables and compare to a regular `ldd` run in the terminal? – Botje Nov 13 '20 at 13:09
  • What compiler are you using? – Thomas Nov 13 '20 at 13:21
  • @Someprogrammerdude edited to my post – burny Nov 13 '20 at 13:28
  • a run with LD_TRACE_LOADED_OBJECTS=1 should have produced a similar output list. Note how libgtk3-nocsd.so.0 comes before libasan.so.5, so ASan is (correctly) complaining that it cannot track all memory managed by the library that came before it. – Botje Nov 13 '20 at 13:29
  • @Thomas Im using GCC – burny Nov 13 '20 at 13:30
  • Which version of GCC? And have you tried the `LD_PRELOAD` "trick"? – Some programmer dude Nov 13 '20 at 13:32
  • Also, is it possible that libgtk3-nocsd.so.0 library is already in LD_PRELOAD or LD_AUDIT? That seems to have been a thing in older Ubuntu installations. – Botje Nov 13 '20 at 13:32
  • I can't explain why it would work from the command line, but you are missing `-fsanitize=address` on the _linker_ command line; it must be set both for compilation and for linking. This might explain why ASAN complains that its runtime is not linked to the application. Try `target_link_libraries(Playground "-fsanitize=address")`. – Thomas Nov 13 '20 at 13:35
  • Yes, LD_PRELOAD variable has libgtk3-nocsd.so.0 – burny Nov 13 '20 at 13:35
  • There's your problem, then. Unset or overwrite the LD_PRELOAD environment variable in your CLion run configuration. – Botje Nov 13 '20 at 13:37
  • @Thomas added ```target_link_libraries(Playground "-fsanitize=address")``` to CMakeLists.txt but nothing changed – burny Nov 13 '20 at 13:37
  • @Botje apparently doing that won't work, the library cannot be preloaded? https://imgur.com/a/j6FT2Sr this shows up for a moment before running it with the error – burny Nov 13 '20 at 13:44
  • You need to remove the LD_TRACE_LOADED_OBJECTS=1 environment variable again. It also appears you have left quotes around the value of LD_PRELOAD; those should not be there. – Botje Nov 13 '20 at 13:45
  • @Botje Thank you, it works! *virtual hug* – burny Nov 13 '20 at 13:49
  • so apparently this isnt problem only in older ubuntu installations, because I have OS Ubuntu 20.04.1 LTS x86_64 – burny Nov 13 '20 at 13:52
  • @Botje "Note how libgtk3-nocsd.so.0 comes before libasan.so.5, so ASan is (correctly) complaining that it cannot track all memory managed by the library that came before it. " why is that anyway? why it cannot track all memory managed by the library that came before it? – burny Nov 13 '20 at 14:01
  • Asan traps all memory reads and writes in order to detect accesses to memory that is not allocated or not initialized. If it cannot witness the allocation or the initialization it cannot give an accurate answer. – Botje Nov 13 '20 at 14:04

2 Answers2

2

First remove all of the set commands you added, and then add these commands before the compile:

add_compile_options(-fsanitize=address)
add_link_options(-fsanitize=address)
Kladskull
  • 10,332
  • 20
  • 69
  • 111
0

The problem is caused by the LD_PRELOAD=libgtk3-nocsd.so.0 setting somewhere in your environment. Overwrite it or unset the variable in your CLion run configuration.

Botje
  • 26,269
  • 3
  • 31
  • 41