clang-14:警告:使用地址消毒程序时不能压缩调试段(未安装zlib) [-Wdebug-compression-u



我有一个c++示例程序,它会导致一个明显的分段错误。

test.cxx:

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

所以我使用地址消毒器来调试它:

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

所以它说zlib没有安装。所以我试着安装zlib。它产生如下结果:

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.

这意味着zlib实际上已经安装了,但是clang找不到它。这是我的clang版本:

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

那么我如何告诉clang如何以及在哪里找到zlib呢?我是从llvm-releases下载clang-14的二进制版本安装的。

请注意,如果我使用g++而不是clang++,则不会发生与zlib相关的错误。

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

我发现,在LLVM下载页面中找到的clang-14的预构建版本(以及后来的版本)没有正确构建。-g编译器标志在二进制文件中不包含任何调试符号。消毒器需要调试符号才能工作。这就是为什么我得到这些错误。所以,如果你想使用最新的clang版本,你有三个选择。

  1. 下载clang+llvm-13.0.0如果你是ubuntu 20.04。这个很好。

  2. 像我一样在你的机器上自己构建LLVM和clang(和lldb等,所有你发现必要的)。

  3. 从官方包存储库安装clang。虽然clang-12是目前可用的最新版本。

最新更新