boostorg / interprocess

Boost.org interprocess module
http://boost.org/libs/interprocess
132 stars 117 forks source link

managed_map_file find_or_construct does not return Cache aligned memory #217

Open billzheng opened 4 months ago

billzheng commented 4 months ago
struct alignas( 64 ) type_t {
  int arr[1000];
};

namespace bip = boost::interprocess;
typedef bip::basic_managed_mapped_file<
    char,
    bip::rbtree_best_fit<bip::mutex_family, bip::offset_ptr<void>,
                         64>,
    bip::iset_index>
    managed_mapped_file_aligned;

inline void allocate() {
  managed_mapped_file_aligned mmf( bip::open_only,
                                   "/mnt/hugepage/test_boost_shm" );

  type_t* ptr = mmf.find_or_construct<type_t>( "test name" )();
  // ptr is not CACHE aligned
}
sehe commented 4 months ago

Minified and extrapolating for analysis: https://godbolt.org/z/Eh4K7hnKE

#include <boost/interprocess/managed_heap_memory.hpp>
namespace bip = boost::interprocess;
using Seg = bip::basic_managed_heap_memory<char,                                                                //
       bip::rbtree_best_fit<bip::mutex_family, bip::offset_ptr<void>, 64>,
       bip::iset_index>;

struct alignas(64) T { int arr[1000]; };
static void test(auto key) { Seg(10240).construct<T>(key)(); }

int main() {
    test(bip::anonymous_instance); // OKAY
    test(bip::unique_instance);    // Not OKAY
    test("data");                  // Not OKAY
}

Printing

boost/interprocess/detail/named_proxy.hpp:90:49: runtime error: constructor call on misaligned address 0x526000000298 for type 'T', which requires 64 byte alignment
0x526000000298: note: pointer points here
 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  00 00 00 00
              ^
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /boost/interprocess/detail/named_proxy.hpp:90:49 in

I might have time to look closer later.