python - python 扩展上的地址 sanitizer

标签 python clang address-sanitizer

我正在尝试使用 Address Sanitizer 编译 python 扩展。当我加载扩展时,我得到

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    from extension import package
  File "/tmp/python_test/extension/package.py", line 28, in <module>
    from extension._ext import *
ImportError: /tmp/python_test/extension/_ext.so: undefined symbol: __asan_version_mismatch_check_v8

编译器调用是

clang -g -o _ext.so code.ll -fsanitize=address -lrt -lpthread -ldl -lstdc++ -lm -fPIC -shared

因此,它无法正确加载来自 asan 的符号。我试过使用 -static-libsan ,但结果是一样的。

我看到有些人使用 LD_PRELOAD然而,为了让 Asan 进入共享对象,似乎 libasan.so在我的系统上来自不同版本的 Address Sanitizer(从 Debian 的 libasan3 软件包安装,而我从 deb http://apt.llvm.org/stretch/ llvm-toolchain-stretch-8 main 得到 clang)。

那么,如何让 Address Sanitizer 与共享对象库一起工作? 或者,我需要正确版本的 libasan.so (它似乎不在 deb http://apt.llvm.org/stretch/ llvm-toolchain-stretch-8 main 中,或者我需要一种静态链接的方法)。

我的 clang 版本:

$ clang -v
clang version 8.0.0-svn356034-1~exp1~20190313094216.53 (branches/release_80)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/6.3.0
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/6.3.0
Candidate multilib: .;@m64
Selected multilib: .;@m64

最佳答案

要使用 Clang 清理单个库(而不清理主 python 可执行文件),您应该

  • -shared-libasan 添加到 LDFLAGS(Clang 默认为 -static-libasan,与 GCC 不同)
  • 使用 LD_PRELOAD=$(clang -print-file-name=libclang_rt.asan-x86_64.so) 运行(它应该在标准 Clang 库中)

(参见 AddressSanitizerAsDso wiki)。

另一种选择是使用 GCC,在这种情况下,不需要 -shared-libasan 并且 LD_PRELOAD 值变为 libasan.so.N( N 取决于 GCC 版本,使用 $(gcc -print-file-name=libasan.so) 来定位它。

有关 GCC 和 Clang 在清理 shlib 方面的差异的更多详细信息,请参阅 this answer .

关于python - python 扩展上的地址 sanitizer ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55692357/

相关文章:

python - 尝试使用OpenCV显示图像时发生错误(-215)size.width> 0 && size.height> 0

python - 魔杖对象颜色创建非常慢

python - Matplotlib:如何指定 x-label 边界框的宽度

objective-c - 是否可以为必须重写的方法创建警告?

macos - 使用命令行 clang、OS X 10.9.1、Xcode 5.0.2

python - 在 vba 和 python 中自动进行工作簿计算

c++ - 关于 const 禁止的重言式比较的警告?

c - 可变参数宏 : expansion of pasted tokens

c++ - 地址 sanitizer 在静态转换无效指针时报告错误

ubuntu - 为什么 ASAN_SYMBOLIZER_PATH 不再适用于版本修饰的二进制文件