Skip to content

Conversation

@sjanel
Copy link

@sjanel sjanel commented Nov 15, 2025

The allocated blocks from bytell_hash_map are sometimes misaligned, this PR fixes the alignment issue.

Clang with UBSan reports errors at various lines:

bytell_hash_map.hpp:422:38: runtime error: member access within misaligned address 0x559e88edae1c for type 'BlockType' (aka 'sherwood_v8_block<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char>>, aeronet::Router::RouteNode *>, '\b'>'), which requires 8 byte alignment
0x559e88edae1c: note: pointer points here
  ff ff ff ff ff ff ff ff  ff ff ff ff 00 00 00 00  a0 a5 ab c8 66 7f 00 00  b4 8a e4 88 9e 55 00 00
              ^ 
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

bytell_hash_map.hpp:509:38: runtime error: member access within misaligned address 0x55ce75dc31fc for type 'BlockType' (aka 'sherwood_v8_block<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char>>, std::basic_string<char, std::char_traits<char>, std::allocator<char>>>, '\b'>'), which requires 8 byte alignment
0x55ce75dc31fc: note: pointer points here
  02 00 00 00 ff ff ff ff  ff ff ff ff 00 00 00 00  a0 a5 6b de 8b 7f 00 00  10 b2 d2 75 ce 55 00 00
              ^ 
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior

Here is a small test showing the issue:

  struct alignas(64) CacheLineAlignedValue {
    std::array<std::uint8_t, 64> data{};
  };

  bytell_hash_map<int, CacheLineAlignedValue> map;
  constexpr int expectedAlignment = alignof(CacheLineAlignedValue);

  for (int i = 0; i < 128; ++i) {
    CacheLineAlignedValue value{};
    value.data[0] = static_cast<std::uint8_t>(i);
    map.emplace(i, value);
  }

  ASSERT_EQ(map.size(), 128U);
  for (auto &entry : map) {
    auto addr = reinterpret_cast<std::uintptr_t>(&entry.second);
    EXPECT_EQ(addr % expectedAlignment, 0U) << "value storage is not properly aligned";
  }

  map.reserve(512);
  map.emplace(512, CacheLineAlignedValue{});
  auto &value = map[1024];
  auto addr = reinterpret_cast<std::uintptr_t>(&value);
  EXPECT_EQ(addr % expectedAlignment, 0U);

Without the fix:

bytell_hash_map.hpp:509:38: runtime error: member access within misaligned address 0x5b7cce77e260 for type 'BlockType' (aka 'sherwood_v8_block<std::pair<int, (anonymous namespace)::CacheLineAlignedValue>, '@'>'), which requires 64 byte alignment
0x5b7cce77e260: note: pointer points here
 ff ff ff ff  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  ff ff ff ff ff ff ff ff  ff ff ff ff
              ^ 
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
``

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