Repository navigation
Honour alignof(T) when allocating objects loaded through a pointer - #396
Open
efernandez wants to merge 1 commit into
Open
efernandez wants to merge 1 commit into
efernandez wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #395.
heap_allocation<T>allocated the storage for aTwithout a class-specificoperator newwith::operator new(sizeof(T)), which only guarantees__STDCPP_DEFAULT_NEW_ALIGNMENT__, so objects of over-alignedtypes 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>allocatestypes 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 theBOOST_SERIALIZATION_DONT_USE_HAS_NEW_OPERATORpath anddoesnt_have_new_operatoruse it. Types with aclass-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 analignas(64)type through rawpointers and through
std::shared_ptr, checks their alignment and values, and destroys them withdelete-expressions. Passes trivially without
__cpp_aligned_new.Testing
GCC 14,
-std=c++17, the library built from this branch:test_aligned_pointer, text / binary / xml archivestest_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 / xmlThe same change applied to Boost 1.86 also fixes the reproducer in the issue (0 of 1000 objects misaligned, from
1000 of 1000).