From cf917cf0460e6f66b182cd43010ec3a4d8173005 Mon Sep 17 00:00:00 2001 From: Andrew Krieger <akrieger@fb.com> Date: Wed, 22 Jan 2020 16:43:14 -0800 Subject: [PATCH] Use aligned allocation for hazptr_rec<Atom> Summary: The default allocator for `new` does not apparently respect the extended alignment requirement for `hasptr_rec<Atom>`. We use folly's AlignedSysAllocator for these cases, which will then pass UBSan checks for alignment. ``` xplat/folly/synchronization/HazptrDomain.h:572:20: runtime error: constructor call on misaligned address 0x60c0000004c0 for type 'hazptr_rec<atomic>', which requires 128 byte alignment 0x60c0000004c0: note: pointer points here 02 00 00 7f be be be be be be be be be be be be be be be be be be be be be be be be be be be be ^ #0 0x77949e in folly::hazptr_domain<std::atomic>::acquire_new_hprec() xplat/folly/synchronization/HazptrDomain.h:572:16 #1 0x7791cc in folly::hazptr_domain<std::atomic>::hprec_acquire() xplat/folly/synchronization/HazptrDomain.h:227:35 #2 0x766a7f in folly::hazptr_holder<std::atomic>::hazptr_holder(folly::hazptr_domain<std::atomic>&) xplat/folly/synchronization/HazptrHolder.h:71:21 #3 0x766a7f in void folly::UnboundedQueue<folly::CPUThreadPoolExecutor::CPUTask, false, false, false, 6ul, 7ul, std::atomic>::enqueueImpl<folly::CPUThreadPoolExecutor::CPUTask>(folly::CPUThreadPoolExecutor::CPUTask&&) xplat/folly/concurrency/UnboundedQueue.h:374 ``` Reviewed By: magedm Differential Revision: D19237973 fbshipit-source-id: 0edea12bb3f028d21830689d52f7e0290d187ff9 --- folly/synchronization/HazptrDomain.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/folly/synchronization/HazptrDomain.h b/folly/synchronization/HazptrDomain.h index 17991d9a9..873972c42 100644 --- a/folly/synchronization/HazptrDomain.h +++ b/folly/synchronization/HazptrDomain.h @@ -21,6 +21,7 @@ #include <folly/synchronization/HazptrRec.h> #include <folly/synchronization/HazptrThrLocal.h> +#include <folly/Memory.h> #include <folly/Portability.h> #include <folly/executors/QueuedImmediateExecutor.h> #include <folly/synchronization/AsymmetricMemoryBarrier.h> @@ -222,6 +223,10 @@ class hazptr_domain { } private: + using hazptr_rec_alloc = AlignedSysAllocator< + hazptr_rec<Atom>, + FixedAlign<alignof(hazptr_rec<Atom>)>>; + friend void hazptr_domain_push_list<Atom>( hazptr_obj_list<Atom>&, hazptr_domain<Atom>&) noexcept; @@ -463,7 +468,8 @@ class hazptr_domain { while (rec) { auto next = rec->next(); DCHECK(!rec->active()); - delete rec; + rec->~hazptr_rec<Atom>(); + hazptr_rec_alloc{}.deallocate(rec, 1); rec = next; } } @@ -597,7 +603,8 @@ class hazptr_domain { } hazptr_rec<Atom>* acquire_new_hprec() { - auto rec = new hazptr_rec<Atom>; + auto rec = hazptr_rec_alloc{}.allocate(1); + new (rec) hazptr_rec<Atom>(); rec->set_active(); rec->set_domain(this); while (true) { -- 2.26.2