【发布时间】:2018-05-17 01:50:03
【问题描述】:
我的项目包括一个大型 C++ 库和 Python 绑定(通过 Boost.Python)。测试套件主要是在 Python 绑定之上编写的,我想使用 sanitizer 来运行它,从 ASAN 开始。
我正在运行 macOS(10.13.1 FWIW,但我以前的版本也有问题),我似乎找不到在 Python 模块上运行 ASAN 的方法(我非常怀疑这与Boost.Python,我想其他技术也一样)。
这是一个简单的 Python 模块:
// hello_ext.cc
#include <boost/python.hpp>
char const* greet()
{
auto* res = new char[100];
std::strcpy(res, "Hello, world!");
delete [] res;
return res;
}
BOOST_PYTHON_MODULE(hello_ext)
{
using namespace boost::python;
def("greet", greet);
}
这是我为 MacPorts 制作的 Makefile:
// Makefile
CXX = clang++-mp-4.0
CXXFLAGS = -g -std=c++14 -fsanitize=address -fno-omit-frame-pointer
CPPFLAGS = -isystem/opt/local/include $$($(PYTHON_CONFIG) --includes)
LDFLAGS = -L/opt/local/lib
PYTHON = python3.5
PYTHON_CONFIG = python3.5-config
LIBS = -lboost_python3-mt $$($(PYTHON_CONFIG) --ldflags)
all: hello_ext.so
hello_ext.so: hello_ext.cc
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(LDFLAGS) -shared -o $@ $< $(LIBS)
check: all
$(ENV) $(PYTHON) -c 'import hello_ext; print(hello_ext.greet())'
clean:
-rm -f hello_ext.so
没有 asan,一切都很好(嗯,实际上太好了......)。但是对于 ASAN,我遇到了 LD_PRELOAD 类似的问题:
$ make check
python -c 'import hello_ext; print(hello_ext.greet())'
==19013==ERROR: Interceptors are not working. This may be because AddressSanitizer is loaded too late (e.g. via dlopen). Please launch the executable with:
DYLD_INSERT_LIBRARIES=/opt/local/libexec/llvm-4.0/lib/clang/4.0.1/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
"interceptors not installed" && 0make: *** [check] Abort trap: 6
好的,让我们这样做:定义 DYLD_INSERT_LIBRARIES
$ DYLD_INSERT_LIBRARIES=/opt/local/libexec/llvm-4.0/lib/clang/4.0.1/lib/darwin/libclang_rt.asan_osx_dynamic.dylib \
python -c 'import hello_ext; print(hello_ext.greet())'
==19023==ERROR: Interceptors are not working. This may be because AddressSanitizer is loaded too late (e.g. via dlopen). Please launch the executable with:
DYLD_INSERT_LIBRARIES=/opt/local/libexec/llvm-4.0/lib/clang/4.0.1/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
"interceptors not installed" && 0zsh: abort DYLD_INSERT_LIBRARIES= python -c 'import hello_ext; print(hello_ext.greet())'
让我们怀疑 SIP,所以我在这里禁用了 SIP,让我们解决符号链接:
$ DYLD_INSERT_LIBRARIES=/opt/local/libexec/llvm-4.0/lib/clang/4.0.1/lib/darwin/libclang_rt.asan_osx_dynamic.dylib \
/opt/local/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 -c 'import hello_ext; print(hello_ext.greet())'
==19026==ERROR: Interceptors are not working. This may be because AddressSanitizer is loaded too late (e.g. via dlopen). Please launch the executable with:
DYLD_INSERT_LIBRARIES=/opt/local/libexec/llvm-4.0/lib/clang/4.0.1/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
"interceptors not installed" && 0zsh: abort DYLD_INSERT_LIBRARIES= -c 'import hello_ext; print(hello_ext.greet())'
这样做的正确方法是什么?我还尝试使用ctypes.PyDLL 加载libasan,即使使用sys.setdlopenflags(os.RTLD_NOW | os.RTLD_GLOBAL) 我也无法正常工作。
【问题讨论】:
标签: python c++ boost-python address-sanitizer