From ad90720829db5ba0c3d0e44994856dcce33d7940 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Sat, 12 Oct 2024 20:09:48 -0700 Subject: [PATCH] enforce alignment on channels::detail::AtomicQueue consumer types Summary: Class template `folly::channels::detail::AtomicQueue` is parameterized by a consumer type template. The class holds pointers to consumers but, in the interest of minimizing memory usage, may steal the two lowest bits of the pointers. A unit-test for `AtomicQueue` uses a consumer type holding only a `bool`, giving that consumer type alignment 1 and therefore pointers to it no bits allowable to steal. Enforce a minimum alignment on the consumer type that allows stealing the two lowest bits of pointers, and update the unit-test. Reviewed By: dmm-fb Differential Revision: D64287317 fbshipit-source-id: fb54e15559f12d82717cbea20075c01adfa6e8d1 --- folly/channels/detail/AtomicQueue.h | 2 +- folly/channels/detail/test/AtomicQueueTest.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/folly/channels/detail/AtomicQueue.h b/folly/channels/detail/AtomicQueue.h index eb4dc4dc383..1bf2a903070 100644 --- a/folly/channels/detail/AtomicQueue.h +++ b/folly/channels/detail/AtomicQueue.h @@ -82,7 +82,7 @@ class AtomicQueue { public: using MessageQueue = Queue; - AtomicQueue() {} + AtomicQueue() { static_assert(alignof(Consumer) > kTypeMask); } ~AtomicQueue() { auto storage = storage_.load(std::memory_order_acquire); auto type = static_cast(storage & kTypeMask); diff --git a/folly/channels/detail/test/AtomicQueueTest.cpp b/folly/channels/detail/test/AtomicQueueTest.cpp index 6feb6392cec..bc71d3a3e07 100644 --- a/folly/channels/detail/test/AtomicQueueTest.cpp +++ b/folly/channels/detail/test/AtomicQueueTest.cpp @@ -98,7 +98,7 @@ TEST(AtomicQueueTest, Basic) { } TEST(AtomicQueueTest, Canceled) { - struct Consumer { + struct alignas(int) Consumer { void consume(int*) { ADD_FAILURE() << "consume() shouldn't be called"; } void canceled(int* consumerParam) { EXPECT_EQ(consumerParam, getConsumerParam());