Search code examples
c++ubuntugccaddress-sanitizerld-preload

Error while trying to run valgrind on Ubuntu (ASan runtime does not come first in initial library list)


I am trying to run valgrind to find memory leaks in my c++ program. I have set the flags -fsanitize=adress in my CMake. However, whenever I am trying to run the program with or without valgrind after this flag is set, I am getting the error: 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..

Then I tried setting LD_PRELOAD=/path/libasan.so.5 but it didn't make any difference. I am still getting the same error. Could anyone please give me some insights into what could cause this issue and how can I solve it? I'm using Ubuntu 20.04 with gcc 9.6.

First, when I realized there could be memory leak, I install valgrind and set the flags fsanitize=adress and -fsanitize=leak. First time, then compiled the program and ran it with valgrind. First run was successful, and the valgrind produced the output. Then, I made some changes to the program, build it successfully, and tried to run it with valgrind again, which caused the error. I tried using the LD_PRELOAD=/path/to/libasan.so.5 but it didn't seem to fix the issue


Solution

  • I am trying to run valgrind to find memory leaks in my c++ program. I have set the flags -fsanitize=adress in my CMake

    You are trying to use two separate tools: Valgrind and AddressSanitizer at the same time. Don't do that -- use one or the other -- these tools can each individually find memory leaks (and a lot more), and are not designed to work together.

    Even if this did work, it would be unnecessarily slow, as Valgrind would spend time emulating/executing instructions which AddressSanitizer inserted into your program. So instead of 4x slowdown typical for ASan or 15x slowdown typical for VG, you would be hit with 60x slowdown.