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

I’m using Ubuntu 20.04 LTS.

The compiler driver option -gz combines two tasks. For object generation, it acts as -Wa,--compress-debug-sections=zlib and asks the assembler to compress debug sections in the output .o (and .dwo with -gsplit-dwarf ). For linking, -gz acts as -Wl,--compress-debug-sections=zlib and asks the linker to compress debug sections in the linked image. If you need uncompression for linker input but don’t need compression for the linker output, don’t bother with -gz . The linker recognizes compressed input and uncompressed it automatically.

Since clang is not built with ZLIB, the object file is uncompressed but the linked output will be compressed if the linker is built with zlib (likely the case if you use system ld => GNU ld).

At run-time, asan runtime invokes llvm-symbolizer to symbolize the stack trace. llvm-symbolizer is not built with zlib, so it fails to uncompress .debug_* sections, hence the output.

clang and llvm-symbolizer on your system are not built with zlib. sudo apt install zlib1g zlib1g-dev doesn’t add zlib support to your clang and llvm-symbolizer.

On my system:

% ldd /usr/bin/clang | grep libz\.so
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f93e34e6000)
% ldd /usr/bin/llvm-symbolizer | grep libz\.so 
        libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007fa6cdbe7000)
2 Likes

You can’t tell it at runtime – a particular build either links against zlib or it doesn’t, at build time.

It does seem unfortunate that the official release wasn’t build against zlib. I suspect that’s an oversight, and could be addressed for future releases. @tstellar Is there somewhere to add -DLLVM_ENABLE_ZLIB=FORCE_ON to the official build cmake recipe?

1 Like

@MaskRay @jyknight So is there a way to get rid of the errors for now or should I expect the next version of clang to be linked against zlib? The errors still persist if I omit -gz flag. Or should I compile clang-14 from source linking against zlib?

We could add it to the default set of CMake flags in test-release.sh. However, we don’t have any official requirements for how the release are built. It’s up to each release tester what configuration is used, but having it default will make it more likely it is enabled.

Since llvm-symbolizer doesn’t support SHF_COMPRESSED sections, you can only avoid the problem by not feeding it SHF_COMPRESSED sections… i.e. drop -gz=zlib.

You can follow my ldd example to check whether your llvm-symbolizer supports SHF_COMPRESSED.

I have changed the diagnostic from zlib not installed to zlib not enabled. Hope it is clearer now.

1 Like

Thanks. I was able to get rid of the errors.