Skip to content

Commit

Permalink
Use aligned allocation for hazptr_rec<Atom>
Browse files Browse the repository at this point in the history
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
  • Loading branch information
akrieger authored and facebook-github-bot committed Jan 23, 2020
1 parent d2bc79c commit cf917cf
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions folly/synchronization/HazptrDomain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit cf917cf

Please sign in to comment.