我最近尝试使用 boost 序列化来序列化一个包含 astd::vector<std::unique_ptr<Base>>>
作为成员的类。根据 boost 文档(https://www.boost.org/doc/libs/1_71_0/libs/serialization/doc/serialization.html#derivedpointersregister_type
),我们必须使用归档的方法注册派生类以进行序列化好好工作。一切确实构建并运行良好,但地址清理程序构建(在我们的 CI 中运行)失败并出现以下错误:
ASAN:DEADLYSIGNAL
=================================================================
==3==ERROR: AddressSanitizer: SEGV on unknown address 0x000000100000 (pc 0x559bc5f18288 bp 0x7ffe74fd8d30 sp 0x7ffe74fd8d10 T0)
==3==The signal is caused by a READ memory access.
==3==Hint: address points to the zero page.
#0 0x559bc5f18287 in boost::serialization::void_cast_detail::void_caster_primitive<Derived, Base>::void_caster_primitive() /usr/include/boost/serialization/void_cast.hpp:188
#1 0x559bc5f1714a in boost::serialization::singleton<boost::serialization::void_cast_detail::void_caster_primitive<Derived, Base> >::get_instance()::singleton_wrapper::singleton_wrapper() /usr/include/boost/serialization/singleton.hpp:117
#2 0x559bc5f173be in boost::serialization::singleton<boost::serialization::void_cast_detail::void_caster_primitive<Derived, Base> >::get_instance() /usr/include/boost/serialization/singleton.hpp:118
#3 0x559bc5ef3294 in __static_initialization_and_destruction_0 /usr/include/boost/serialization/singleton.hpp:155
...
在检查其中的内容后void_cast.hpp
,我发现了以下构造函数中有问题的代码void_caster_primitive
:
/* note about displacement:
* displace 0: at least one compiler treated 0 by not shifting it at all
* displace by small value (8): caused ICE on certain mingw gcc versions */
reinterpret_cast<std::ptrdiff_t>(
static_cast<Derived *>(
reinterpret_cast<Base *>(1 << 20)
)
) - (1 << 20)
从代码和注释来看,这个表达式是在计算Base
类的位移Derived
。但是,它仍然看起来很神奇,尤其是在将(看似)随机数投射到Base
指针时。如果有人能解释为什么这实际上计算了位移,那就太好了。
编辑:这是一个使用上面显示的位移计算方法的简单示例:https : //godbolt.org/z/Bmp7zH 如果关闭消毒剂,它会编译并运行,但打开后,程序会异常终止。
这也是在编译器资源管理器上重现 SEGV 的原始问题的尝试:https ://godbolt.org/z/w8ZNx8但是,链接 boost 序列化似乎不起作用,而且如果我打开 sanitizer 选项,构建有时会超时。