Search code examples
c++zlibclang++ubuntu-20.04address-sanitizer

clang-14: warning: cannot compress debug sections (zlib not installed) [-Wdebug-compression-unavailable] while using address sanitizer


I have a sample C++ program that would cause an obvious segmentation fault.

test.cxx:

int main()
{
  int* ptr{nullptr};
  *ptr = 3;
}

So I am using address sanitizer to debug it:

metal888@ThinkPad:~$ clang++ -g -fsanitize=address -fno-omit-frame-pointer -gz=zlib test.cxx -o vimbin && ./vimbin
clang-14: warning: cannot compress debug sections (zlib not installed) [-Wdebug-compression-unavailable]
AddressSanitizer:DEADLYSIGNAL
=================================================================
==42036==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x0000004dbeb1 bp 0x7ffd802e9310 sp 0x7ffd802e92f0 T0)
==42036==The signal is caused by a WRITE memory access.
==42036==Hint: address points to the zero page.
error: failed to decompress '.debug_aranges', zlib is not available
error: failed to decompress '.debug_info', zlib is not available
error: failed to decompress '.debug_abbrev', zlib is not available
error: failed to decompress '.debug_line', zlib is not available
error: failed to decompress '.debug_str', zlib is not available
error: failed to decompress '.debug_addr', zlib is not available
error: failed to decompress '.debug_line_str', zlib is not available
error: failed to decompress '.debug_rnglists', zlib is not available
error: failed to decompress '.debug_str_offsets', zlib is not available
error: failed to decompress '.debug_aranges', zlib is not available
error: failed to decompress '.debug_info', zlib is not available
error: failed to decompress '.debug_abbrev', zlib is not available
error: failed to decompress '.debug_line', zlib is not available
error: failed to decompress '.debug_str', zlib is not available
error: failed to decompress '.debug_loc', zlib is not available
error: failed to decompress '.debug_ranges', zlib is not available
    #0 0x4dbeb1 in main (/home/metal888/vimbin+0x4dbeb1)
    #1 0x7f5165493082 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 1878e6b475720c7c51969e69ab2d276fae6d1dee)
    #2 0x41c30d in _start (/home/metal888/vimbin+0x41c30d)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/home/metal888/vimbin+0x4dbeb1) in main
==42036==ABORTING

So it says zlib is not installed. So I tried to install zlib. It produces this result:

metal888@ThinkPad:~$ sudo apt install zlib1g zlib1g-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
zlib1g is already the newest version (1:1.2.11.dfsg-2ubuntu1.3).
zlib1g-dev is already the newest version (1:1.2.11.dfsg-2ubuntu1.3).
0 upgraded, 0 newly installed, 0 to remove and 1 not upgraded.

So that means zlib is actually installed but clang cannot find it. This is my clang version:

metal888@ThinkPad:~$ clang --version
clang version 14.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
InstalledDir: /opt/clang14/bin

So how can I tell clang how and where to find zlib? I installed clang by downloading binary release of clang-14 from llvm-releases.

Note that the zlib related errors don't occur if I use g++ instead of clang++.

metal888@ThinkPad:~$ g++ -g -fsanitize=address -fno-omit-frame-pointer -gz=zlib test.cxx -o vimbin && ./vimbin
AddressSanitizer:DEADLYSIGNAL
=================================================================
==44183==ERROR: AddressSanitizer: SEGV on unknown address 0x000000000000 (pc 0x5601ea2f41d8 bp 0x7ffc8f97d9d0 sp 0x7ffc8f97d9c0 T0)
==44183==The signal is caused by a WRITE memory access.
==44183==Hint: address points to the zero page.
    #0 0x5601ea2f41d7 in main /home/metal888/test.cxx:4
    #1 0x7fb073a17082 in __libc_start_main ../csu/libc-start.c:308
    #2 0x5601ea2f40cd in _start (/home/metal888/vimbin+0x10cd)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/metal888/test.cxx:4 in main
==44183==ABORTING

Solution

  • What I found out is that the prebuilt versions of clang-14 (and versions that came later) that's found in the LLVM download page are not built properly. The -g compiler flag doesn't include any debug symbols in the binary. And sanitizer needs debug symbols to work. That's why I was getting those errors. So you have 3 options left if you want to use the latest clang release that works perfectly.

    1. Download clang+llvm-13.0.0 if you are on ubuntu 20.04. This one works just fine.

    2. Build LLVM and clang (and lldb etc etc, all that you find necessary) in your machine yourself like I did.

    3. Install clang from the official package repository. Although currently clang-12 is the most latest one that's available.