Skip to content

Honour alignof(T) when allocating objects loaded through a pointer - #396

Open
efernandez wants to merge 1 commit into
boostorg:developfrom
efernandez:aligned-heap-allocation
Open

efernandez wants to merge 1 commit into
boostorg:developfrom
efernandez:aligned-heap-allocation

Conversation

@efernandez

Copy link
Copy Markdown

Fixes #395.

heap_allocation<T> allocated the storage for a T without a class-specific operator new with
::operator new(sizeof(T)), which only guarantees __STDCPP_DEFAULT_NEW_ALIGNMENT__, so objects of over-aligned
types loaded through a pointer were constructed in misaligned storage, and were later freed by a delete-expression
with the aligned operator delete, which didn't match the allocation.

Change

  • include/boost/archive/detail/iserializer.hpp: since C++17 (__cpp_aligned_new), heap_allocation<T> allocates
    types with alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__ with ::operator new(sizeof(T), std::align_val_t(alignof(T)))
    and frees them with the matching aligned operator delete, as new- and delete-expressions do. Both the
    BOOST_SERIALIZATION_DONT_USE_HAS_NEW_OPERATOR path and doesnt_have_new_operator use it. Types with a
    class-specific operator new (has_new_operator) and builds without aligned new are unchanged.
  • test/test_aligned_pointer.cpp (+ test/Jamfile.v2): loads 100 objects of an alignas(64) type through raw
    pointers and through std::shared_ptr, checks their alignment and values, and destroys them with
    delete-expressions. Passes trivially without __cpp_aligned_new.

Testing

GCC 14, -std=c++17, the library built from this branch:

develop this PR
test_aligned_pointer, text / binary / xml archives fails (misaligned objects) passes
test_new_operator, test_interrupted_pointer_reclaim, test_shared_ptr, test_unique_ptr, test_non_default_ctor, test_non_default_ctor2, test_null_ptr, test_cyclic_ptrs, test_delete_pointer, test_list_ptrs, test_multiple_ptrs, test_mi, test_diamond; text / binary / xml all pass

The same change applied to Boost 1.86 also fixes the reproducer in the issue (0 of 1000 objects misaligned, from
1000 of 1000).

heap_allocation<T> allocated the storage for a T without a class specific
operator new with ::operator new(sizeof(T)), which only guarantees
__STDCPP_DEFAULT_NEW_ALIGNMENT__. For a type aligned beyond that, e.g. with
alignas(32) or a fixed-size vectorizable Eigen member built with AVX, the
object was constructed in misaligned storage, which is undefined behaviour:
vectorized code can fault on it, and Eigen asserts. Since such an object is
normally destroyed later with a delete-expression, which for an over-aligned
type calls the aligned operator delete, allocation and deallocation didn't
match either.

Since C++17, allocate those types with the aligned allocation functions,
::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 builds without aligned new
(__cpp_aligned_new), are unchanged.

test_aligned_pointer loads 100 objects of an alignas(64) type through raw
pointers and through std::shared_ptr and checks their alignment.

Fixes boostorg#395.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Objects of over-aligned types loaded through a pointer are allocated misaligned

1 participant