Fix alignment issue with new arena context system

UBSAN fails the modified test here without the changes in arena

PiperOrigin-RevId: 638901311
pull/36786/head
Craig Tiller 6 months ago committed by Copybara-Service
parent d8862cc2a4
commit 35a896aaae
  1. 8
      src/core/lib/resource_quota/arena.cc
  2. 4
      test/core/resource_quota/arena_test.cc

@ -36,8 +36,9 @@ void* ArenaStorage(size_t& initial_size) {
static constexpr size_t base_size =
GPR_ROUND_UP_TO_ALIGNMENT_SIZE(sizeof(Arena));
initial_size = GPR_ROUND_UP_TO_ALIGNMENT_SIZE(initial_size);
initial_size = std::max(initial_size,
arena_detail::BaseArenaContextTraits::ContextSize());
initial_size = std::max(
initial_size, GPR_ROUND_UP_TO_ALIGNMENT_SIZE(
arena_detail::BaseArenaContextTraits::ContextSize()));
size_t alloc_size = base_size + initial_size;
static constexpr size_t alignment =
(GPR_CACHELINE_SIZE > GPR_MAX_ALIGNMENT &&
@ -76,7 +77,8 @@ RefCountedPtr<Arena> Arena::Create(size_t initial_size,
Arena::Arena(size_t initial_size, RefCountedPtr<ArenaFactory> arena_factory)
: initial_zone_size_(initial_size),
total_used_(arena_detail::BaseArenaContextTraits::ContextSize()),
total_used_(GPR_ROUND_UP_TO_ALIGNMENT_SIZE(
arena_detail::BaseArenaContextTraits::ContextSize())),
arena_factory_(std::move(arena_factory)) {
for (size_t i = 0; i < arena_detail::BaseArenaContextTraits::NumContexts();
++i) {

@ -284,12 +284,16 @@ struct ArenaContextType<Foo> {
static void Destroy(Foo* p) { p->~Foo(); }
};
struct alignas(16) VeryAligned {};
TEST(ArenaTest, FooContext) {
auto arena = SimpleArenaAllocator()->MakeArena();
EXPECT_EQ(arena->GetContext<Foo>(), nullptr);
arena->SetContext(arena->New<Foo>(42));
ASSERT_NE(arena->GetContext<Foo>(), nullptr);
EXPECT_EQ(*arena->GetContext<Foo>()->p, 42);
arena->New<VeryAligned>();
arena->New<VeryAligned>();
}
class MockArenaFactory : public ArenaFactory {

Loading…
Cancel
Save