Skip to content

Leave room for a mutex-policy control block in CONNECTION_BLOCK_BYTES - #263

Merged
kahrendt merged 1 commit into
mainfrom
connection-block-mutex-policy
Nov 12, 2026
Merged

kahrendt merged 1 commit into
mainfrom
connection-block-mutex-policy

Conversation

@chrisuthe

Copy link
Copy Markdown
Member

Summary

Since #254, ConnectionBlockAllocator::allocate() fails its compile-time check on Raspbian's ARMv6 toolchain:

src/connection_block.h:55:33: error: static assertion failed: CONNECTION_BLOCK_BYTES must hold the connection's control block

CONNECTION_BLOCK_BYTES<Conn> reserved sizeof(Conn) + 32 for the object and the control block std::allocate_shared() puts it in. Raspbian's libstdc++ uses the mutex lock policy for shared_ptr, even though the compiler reports lock-free atomics, so the control block embeds a pthread_mutex_t and needs 48 bytes beyond Conn. sendspin-cpp-cli's linux-armv6 job (Raspberry Pi Zero and Pi 1) can't move its pin past 3a3722a until this builds.

Raspbian bookworm, ARMv6 Debian bookworm, armhf
__GCC_ATOMIC_INT_LOCK_FREE 2 2
std::__default_lock_policy _S_mutex _S_atomic
sizeof(std::_Sp_counted_base<>) 36 12
Control block overhead 48 (over 32) 24 (fits)

Change

CONNECTION_BLOCK_BYTES<Conn> is now sizeof(Conn) + 32 + sizeof(std::mutex). libstdc++'s policy mutex wraps the same pthread_mutex_t that std::mutex does, so this covers it without naming libstdc++ internals in the library:

  • Raspbian ARMv6: 56 bytes allowed, 48 needed.
  • 64-bit libstdc++ with the mutex policy: 72 allowed, 64 needed.
  • ESP-IDF: std::mutex is newlib's 4-byte pthread_mutex_t handle, so each connection block grows by about 4 bytes. Nothing else changes: no static RAM, flash, stack or allocation count. The 32 bytes already fit there.

A flat 64 would be simpler, but leaves no headroom for a 64-bit mutex-policy libstdc++. Sizing from the exact control block type would need libstdc++ internals.

Tests

New test_connection_block.cpp places the host SendspinClientConnection in a reserved block, the way connect_to() does:

  • HoldsConnectionUnderDefaultPolicy: the control case. The object lands inside the block, and dropping the last reference frees it (LSan checks the free in the sanitizer build).
  • HoldsConnectionUnderMutexPolicy (libstdc++ only): builds through std::__allocate_shared<Conn, _S_mutex>, so CI's ubuntu jobs build the ARMv6 layout. With the old + 32 it fails to compile (1688 <= 1656); with the fix it passes. A 32-bit armhf job would not have caught this, because armhf uses the atomic policy.

Full suite on Linux (g++ 13, ASan/UBSan): 760/760 pass. The new file also compiles with clang-cl.

Raspbian's ARMv6 libstdc++ uses the mutex lock policy for shared_ptr,
so std::allocate_shared()'s control block embeds a pthread_mutex_t and
needs 48 bytes beyond the connection, over the 32 reserved. The
static_assert in ConnectionBlockAllocator::allocate() then fails and
the library no longer builds there. Reserve sizeof(std::mutex) more,
which has the size of that mutex.

test_connection_block.cpp builds the connection under the default
policy and, on libstdc++, under the mutex one, so a libstdc++ host
reproduces the ARMv6 layout.

@kahrendt kahrendt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kahrendt
kahrendt merged commit 78b13d5 into main Nov 12, 2026
16 checks passed
@kahrendt
kahrendt deleted the connection-block-mutex-policy branch November 12, 2026 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants