From 5ed9f2e3d8a480f11e06594182e924a13702be53 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Mon, 13 Apr 2026 22:37:40 +0200 Subject: [PATCH] block: qcow: Test aligned_pwrite bounce unaligned buffer Write 4096 bytes via plain Vec whose address is not guaranteed to be aligned. The bounce buffer path copies data into an aligned allocation before the syscall. Read back with pread_exact to verify data integrity. Signed-off-by: Anatol Belski --- block/src/qcow_sync.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/block/src/qcow_sync.rs b/block/src/qcow_sync.rs index af6bfc1ba..8989354c3 100644 --- a/block/src/qcow_sync.rs +++ b/block/src/qcow_sync.rs @@ -1807,4 +1807,22 @@ mod unit_tests { pread_exact(fd, &mut readback, 0).unwrap(); assert_eq!(readback, data); } + + #[test] + fn test_aligned_pwrite_bounce_unaligned_buffer() { + // Force a misaligned buffer so aligned_pwrite must take the + // bounce path. A plain vec![0u8; 4096] is often page-aligned + // by the allocator, which would skip the bounce entirely. + let size = 4096usize; + let (_tf, fd) = create_pattern_file(size); + let alignment = 512; + + let backing: Vec = (0..size + 1).map(|i| ((i + 1) % 251) as u8).collect(); + let data = &backing[1..size + 1]; + aligned_pwrite(fd, data, 0, alignment).unwrap(); + + let mut readback = vec![0u8; size]; + pread_exact(fd, &mut readback, 0).unwrap(); + assert_eq!(readback, data); + } }