Skip to content

test/osd: fix buffer alignment issue in unittest_ecbackend#66849

Merged
tchaikov merged 1 commit intoceph:mainfrom
tchaikov:wip-unittest_ecbackend-alignment
Jan 10, 2026
Merged

test/osd: fix buffer alignment issue in unittest_ecbackend#66849
tchaikov merged 1 commit intoceph:mainfrom
tchaikov:wip-unittest_ecbackend-alignment

Conversation

@tchaikov
Copy link
Contributor

@tchaikov tchaikov commented Jan 9, 2026

The create_buf() function in TestECBackend.cc had two issues that caused test failures when running with ASan enabled:

  1. Infinite loop potential: When std::rand() % 5 returned 0, len_to_add would be 0, causing an infinite loop if the buffer hadn't reached the target length yet.
  2. Memory alignment issue: Using append_zero() doesn't guarantee that the resulting buffer's memory address is aligned to EC_ALIGN_SIZE (4096 bytes). The is_aligned() check verifies that all buffers in the bufferlist have their data pointers properly aligned, not just that the length is a multiple of the alignment size.

Fix by:

  • Changing std::rand() % 5 to std::rand() % 5 + 1 to ensure we always allocate 1-5 pages worth of data, avoiding the infinite loop
  • Replacing append_zero() with buffer::create_page_aligned() followed by memset() and append(), which ensures each buffer has its data pointer aligned to page boundaries (4096 bytes)

This ensures the test passes consistently with ASan enabled.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@tchaikov tchaikov requested a review from a team as a code owner January 9, 2026 02:15
@tchaikov tchaikov requested review from aainscow and removed request for a team January 9, 2026 03:55
@tchaikov
Copy link
Contributor Author

tchaikov commented Jan 9, 2026

@aainscow hi Alex, could you please help review this change?

The create_buf() function in TestECBackend.cc had two issues that
caused test failures when running with ASan enabled:

1. Infinite loop potential: When std::rand() % 5 returned 0, len_to_add
   would be 0, causing an infinite loop if the buffer hadn't reached
   the target length yet.

2. Memory alignment issue: Using append_zero() doesn't guarantee that
   the resulting buffer's memory address is aligned to EC_ALIGN_SIZE
   (4096 bytes). The is_aligned() check verifies that all buffers in
   the bufferlist have their data pointers properly aligned, not just
   that the length is a multiple of the alignment size.

Fix by:
- Changing std::rand() % 5 to std::rand() % 5 + 1 to ensure we always
  allocate 1-5 pages worth of data, avoiding the infinite loop
- Replacing append_zero() with buffer::create_page_aligned() followed
  by memset() and append(), which ensures each buffer has its data
  pointer aligned to page boundaries (4096 bytes)

This ensures the test passes consistently with ASan enabled.

Signed-off-by: Kefu Chai <k.chai@proxmox.com>
@tchaikov tchaikov force-pushed the wip-unittest_ecbackend-alignment branch from 4c79113 to 915a071 Compare January 9, 2026 04:03
@tchaikov
Copy link
Contributor Author

tchaikov commented Jan 9, 2026

the failure manifests when testing with ASan enabled. it looks like:

[ RUN      ] ECCommon.decode
/ceph/src/test/osd/TestECBackend.cc: In function 'bufferlist create_buf(uint64_t)' thread 7b14465b95c0 time 2026-01-08T05:46:54.959848+0000
/ceph/src/test/osd/TestECBackend.cc: 1293: FAILED ceph_assert(bl.is_aligned(EC_ALIGN_SIZE))
 ceph version Development (no_version) tentacle (dev - Debug)
 1: (ceph::ClibBackTrace::ClibBackTrace(int)+0xf5) [0x7b14497c2c85]
 2: (ceph::__ceph_assert_fail(char const*, char const*, int, char const*)+0x6f2) [0x7b14497bda52]
 3: (ceph::__ceph_assert_fail(ceph::assert_data const&)+0xdd) [0x7b14497be84d]
 4: /ceph/build/bin/unittest_ecbackend(+0x51c930) [0x5cfa4b82e930]
 5: /ceph/build/bin/unittest_ecbackend(+0x517b01) [0x5cfa4b829b01]
 6: (create_buf(unsigned long)+0x2bb) [0x5cfa4b82994b]
 7: (test_decode(unsigned int, unsigned int, unsigned long, unsigned long, ECUtil::shard_extent_set_t const&, bitset_set<128ul, shard_id_t> const&)+0x18fd) [0x5cfa4b82b43d]
 8: (ECCommon_decode_Test::TestBody()+0x280) [0x5cfa4b82bf30]
 9: (void testing::internal::HandleSehExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)+0x8e) [0x5cfa4b976a9e]
 10: (void testing::internal::HandleExceptionsInMethodIfSupported<testing::Test, void>(testing::Test*, void (testing::Test::*)(), char const*)+0x1a6) [0x5cfa4b92c966]
 11: (testing::Test::Run()+0x13e) [0x5cfa4b8e598e]
 12: (testing::TestInfo::Run()+0x37d) [0x5cfa4b8e710d]
 13: (testing::TestSuite::Run()+0x3a6) [0x5cfa4b8e8586]
 14: (testing::internal::UnitTestImpl::RunAllTests()+0xaf5) [0x5cfa4b908de5]
 15: (bool testing::internal::HandleSehExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*)+0x8e) [0x5cfa4b97a54e]
 16: (bool testing::internal::HandleExceptionsInMethodIfSupported<testing::internal::UnitTestImpl, bool>(testing::internal::UnitTestImpl*, bool (testing::internal::UnitTestImpl::*)(), char const*)+0x1bb) [0x5cfa4b93170b]
 17: (testing::UnitTest::Run()+0x1ca) [0x5cfa4b9081da]
 18: (RUN_ALL_TESTS()+0x11) [0x5cfa4b809bc1]
 19: main()
 20: /lib/x86_64-linux-gnu/libc.so.6(+0x29d90) [0x7b1446e29d90]
 21: __libc_start_main()
 22: _start()
*** Caught signal (Aborted) **

@tchaikov tchaikov mentioned this pull request Jan 9, 2026
14 tasks
uint64_t pages = std::rand() % 5 + 1; // 1-5 pages to avoid infinite loop
uint64_t len_to_add = std::min(len - bl.length(), pages * EC_ALIGN_SIZE);
bl.append_zero(len_to_add);
// Create page-aligned buffer to ensure memory alignment
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if we should really fix bl.append_zero() so that it create page aligned buffers by default.

Clearly that would be a more intrusive change, so I am going to approve this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I don't dare to "fix" append_zero(), hence changed the test instead.

@aainscow
Copy link
Contributor

aainscow commented Jan 9, 2026

Thanks for the fix. LGTM.

@tchaikov tchaikov merged commit 6c05765 into ceph:main Jan 10, 2026
13 checks passed
@tchaikov tchaikov deleted the wip-unittest_ecbackend-alignment branch January 10, 2026 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants