Repository navigation
Description
Problem
When an object is loaded through a pointer (raw pointer, std::shared_ptr, std::unique_ptr, ...) and its type
has no class-specific operator new, boost::archive::detail::heap_allocation<T> gets its storage with
::operator new(sizeof(T)) and placement-constructs T in it. That only guarantees
__STDCPP_DEFAULT_NEW_ALIGNMENT__ (16 bytes on x86-64), so a type aligned beyond that, e.g. with alignas(32) or
with a fixed-size vectorizable Eigen member built with AVX, is constructed in misaligned storage:
- undefined behaviour: in practice vectorized code faults on it (aligned AVX loads/stores), and Eigen aborts on its
unaligned array assertion - such an object is usually destroyed later with a delete-expression, which since C++17 calls the aligned
operator deletefor an over-aligned type, so allocation and deallocation don't match either. It happens to work
with glibc, but not where aligned and unaligned allocation differ.
A C++17 new-expression of the same type allocates it correctly, with ::operator new(std::size_t, std::align_val_t).
How to reproduce
// Boost.Serialization ignores alignof(T) when it deserializes through a pointer. No Eigen involved.
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <cstdint>
#include <iostream>
#include <sstream>
struct alignas(32) Aligned
{
double v[4];
template <class Archive>
void serialize(Archive& archive, const unsigned int /* version */)
{
archive & v;
}
};
int main()
{
static_assert(alignof(Aligned) == 32, "over-aligned type");
int misaligned_boost = 0, misaligned_new = 0;
constexpr int kRuns = 1000;
for (int i = 0; i < kRuns; ++i)
{
std::stringstream stream;
{
Aligned* original = new Aligned;
original->v[0] = 1;
boost::archive::binary_oarchive out(stream);
out << original;
delete original;
}
Aligned* loaded = nullptr;
{
boost::archive::binary_iarchive in(stream);
in >> loaded; // Boost: heap_allocation<Aligned> + placement new
}
Aligned* created = new Aligned; // C++17 new-expression: aligned operator new
misaligned_boost += reinterpret_cast<std::uintptr_t>(loaded) % alignof(Aligned) != 0;
misaligned_new += reinterpret_cast<std::uintptr_t>(created) % alignof(Aligned) != 0;
delete loaded;
delete created;
}
std::cout << "misaligned objects out of " << kRuns << ": Boost deserialization " << misaligned_boost
<< ", new-expression " << misaligned_new << std::endl;
return 0;
}$ g++ -std=c++20 -O0 repro.cpp -o repro -lboost_serialization
$ ./repro
misaligned objects out of 1000: Boost deserialization 1000, new-expression 0
With -O2 -mavx the same program segfaults instead, when constructing the misaligned object.
Seen with Boost 1.86 and with develop (ba49f7d), GCC 14, -std=c++17 and -std=c++20. The code in
heap_allocation<T> is the same in both.
Proposed fix
Since C++17 (__cpp_aligned_new), allocate types aligned beyond __STDCPP_DEFAULT_NEW_ALIGNMENT__ that have no
class-specific operator new with ::operator new(sizeof(T), std::align_val_t(alignof(T))), and free them with
the matching aligned operator delete, as new- and delete-expressions do. Types with a class-specific
operator new and pre-C++17 builds stay as they are. PR: #396
Activity
Honour alignof(T) when allocating objects loaded through a pointer