From 915a071c90f46b7be94d21e63556e0d2f8bd2c9b Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Fri, 9 Jan 2026 09:25:28 +0800 Subject: [PATCH] test/osd: fix buffer alignment issue in unittest_ecbackend 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 --- src/test/osd/TestECBackend.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/osd/TestECBackend.cc b/src/test/osd/TestECBackend.cc index 697f4a1a9951..76f9cc0aff69 100644 --- a/src/test/osd/TestECBackend.cc +++ b/src/test/osd/TestECBackend.cc @@ -1286,9 +1286,12 @@ bufferlist create_buf(uint64_t len) { bufferlist bl; while (bl.length() < len) { - uint64_t pages = std::rand() % 5; + 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 + bufferptr ptr = buffer::create_page_aligned(len_to_add); + memset(ptr.c_str(), 0, len_to_add); + bl.append(ptr); } ceph_assert(bl.is_aligned(EC_ALIGN_SIZE)); ceph_assert(len == bl.length()); -- 2.47.3