Skip to content

Honor cacheline alignment of pooled objects in C++14 builds - #3698

Open
AnDiXL wants to merge 1 commit into
apache:masterfrom
AnDiXL:fix/aligned-new-20261101
Open

AnDiXL wants to merge 1 commit into
apache:masterfrom
AnDiXL:fix/aligned-new-20261101

Conversation

@AnDiXL

@AnDiXL AnDiXL commented Nov 1, 2026

Copy link
Copy Markdown
Contributor

What problem does this PR solve?

ResourcePool, ObjectPool, their LocalPool and SocketPool are declared with BAIDU_CACHELINE_ALIGNMENT and created with plain new. C++17 uses the aligned operator new for them. C++14 builds don't, and glibc then returns only 16-byte aligned storage. CMake and config_brpc.sh both pick C++14 when protobuf < 22.

On Ubuntu 22.04 with Clang 14, this crashes client connects in Socket::ResetFileDescriptor (_transport is null). It reproduces 3/3 with the FlatBuffers protocol tests in #3559. Rebuilding only socket.cpp with -faligned-new or -std=gnu++17 fixes it, and -std=gnu++17 -fno-aligned-new brings the crash back. GCC 11 in C++14 mode misaligns these objects the same way, but it did not crash in those tests.

What is changed and the side effects?

  • CMake and config_brpc.sh pass -faligned-new when the compiler accepts it. Bazel already builds with C++17 and is unchanged.
  • New test ResourcePoolTest.plain_new_honors_cacheline_alignment fails when aligned new is not in effect.
  • C++17 builds are unaffected. C++14 builds now need a C++ runtime that provides aligned operator new: libstdc++ from GCC 7, or libc++.

Tests

Ubuntu 22.04 Clang 14: the new test passes, and fails with -DBRPC_HAS_ALIGNED_NEW=OFF. config_brpc.sh with Clang 14 and GCC 11 produces -std=c++14 -faligned-new. macOS Apple clang 15 detects the flag.

ResourcePool, ObjectPool, their LocalPool and SocketPool are declared
with BAIDU_CACHELINE_ALIGNMENT and created with plain new. C++17 uses
aligned operator new for them, but C++14 builds (the CMake and Make
default with protobuf < 22) do not, and glibc returns only 16-byte
aligned storage. With Clang 14 this corrupts sockets created during a
client connect and crashes in Socket::ResetFileDescriptor.

Pass -faligned-new when the compiler accepts it, and add a test that
fails when aligned new is not in effect.
@AnDiXL AnDiXL mentioned this pull request Nov 1, 2026
3 of 4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant