blob: e0ca8f8aceae0b34b0ae53edfe2d54df6ea31c4c [file]
// Copyright 2026 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
use super::{BASE_ADDR, BinderProcessFixture, VMO_LENGTH};
use crate::binder::BinderDevice;
use crate::shared_memory::SharedMemory;
use assert_matches::assert_matches;
use starnix_core::mm::PAGE_SIZE;
use starnix_core::mm::memory::MemoryObject;
use starnix_core::testing::*;
use starnix_types::user_buffer::UserBuffer;
use starnix_uapi::user_address::UserAddress;
use starnix_uapi::{binder_uintptr_t, errno};
use zerocopy::IntoBytes;
/// Fills the provided shared memory with n buffers, each spanning 1/n-th of the memory.
fn fill_with_buffers(shared_memory: &mut SharedMemory, n: usize) -> Vec<UserAddress> {
let mut addresses = vec![];
for _ in 0..n {
let address = {
let buffer = shared_memory
.allocate_buffers(VMO_LENGTH / n, 0, 0, 0)
.unwrap_or_else(|_| panic!("allocate {n:?}-th buffer"))
.data_buffer;
buffer.memory.user_address + buffer.offset
};
addresses.push(address.expect("buffer address range is out of bounds!"));
}
addresses
}
#[fuchsia::test]
fn shared_memory_allocation_fails_with_invalid_offsets_length() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
shared_memory.allocate_buffers(3, 1, 0, 0).expect_err("offsets_length should be multiple of 8");
shared_memory.allocate_buffers(3, 8, 1, 0).expect_err("buffers_length should be multiple of 8");
shared_memory
.allocate_buffers(3, 8, 0, 1)
.expect_err("security_context_buffer_length should be multiple of 8");
}
#[fuchsia::test]
fn shared_memory_allocation_aligns_offsets_buffer() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
const DATA_LEN: usize = 3;
const OFFSETS_COUNT: usize = 1;
const OFFSETS_LEN: usize = std::mem::size_of::<binder_uintptr_t>() * OFFSETS_COUNT;
const BUFFERS_LEN: usize = 8;
const SECURITY_CONTEXT_BUFFER_LEN: usize = 24;
let allocations = shared_memory
.allocate_buffers(DATA_LEN, OFFSETS_LEN, BUFFERS_LEN, SECURITY_CONTEXT_BUFFER_LEN)
.expect("allocate buffer");
assert_eq!(
allocations.data_buffer.user_buffer(),
UserBuffer { address: BASE_ADDR, length: DATA_LEN }
);
assert_eq!(
allocations.offsets_buffer.user_buffer(),
UserBuffer { address: (BASE_ADDR + 8usize).unwrap(), length: OFFSETS_LEN }
);
assert_eq!(
allocations.scatter_gather_buffer.user_buffer(),
UserBuffer { address: (BASE_ADDR + (8usize + OFFSETS_LEN)).unwrap(), length: BUFFERS_LEN }
);
assert_eq!(
allocations.security_context_buffer.as_ref().expect("security_context").user_buffer(),
UserBuffer {
address: (BASE_ADDR + (8usize + OFFSETS_LEN + BUFFERS_LEN)).unwrap(),
length: SECURITY_CONTEXT_BUFFER_LEN
}
);
assert_eq!(allocations.data_buffer.as_bytes().len(), DATA_LEN);
assert_eq!(allocations.offsets_buffer.as_bytes().len(), OFFSETS_COUNT);
assert_eq!(allocations.scatter_gather_buffer.as_bytes().len(), BUFFERS_LEN);
assert_eq!(
allocations.security_context_buffer.as_ref().expect("security_context").as_bytes().len(),
SECURITY_CONTEXT_BUFFER_LEN
);
}
#[fuchsia::test]
fn shared_memory_allocation_buffers_correctly_write_through() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
const DATA_LEN: usize = 256;
const OFFSETS_COUNT: usize = 4;
const OFFSETS_LEN: usize = std::mem::size_of::<binder_uintptr_t>() * OFFSETS_COUNT;
let mut allocations =
shared_memory.allocate_buffers(DATA_LEN, OFFSETS_LEN, 0, 0).expect("allocate buffer");
// Write data to the allocated buffers.
const DATA_FILL: u8 = 0xff;
allocations.data_buffer.as_mut_bytes().fill(0xff);
const OFFSETS_FILL: binder_uintptr_t = 0xDEADBEEFDEADBEEF;
allocations.offsets_buffer.as_mut_bytes().fill(OFFSETS_FILL);
// Check that the correct bit patterns were written through to the underlying VMO.
let mut data = [0u8; DATA_LEN];
memory.read(&mut data, 0).expect("read VMO failed");
assert!(data.iter().all(|b| *b == DATA_FILL));
let mut data = [0u64; OFFSETS_COUNT];
memory.read(data.as_mut_bytes(), DATA_LEN as u64).expect("read VMO failed");
assert!(data.iter().all(|b| *b == OFFSETS_FILL));
}
#[fuchsia::test]
fn shared_memory_allocates_multiple_buffers() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
// Check that two buffers allocated from the same shared memory region don't overlap.
const BUF1_DATA_LEN: usize = 64;
const BUF1_OFFSETS_LEN: usize = 8;
const BUF1_BUFFERS_LEN: usize = 8;
const BUF1_SECURITY_CONTEXT_BUFFER_LEN: usize = 8;
let allocations = shared_memory
.allocate_buffers(
BUF1_DATA_LEN,
BUF1_OFFSETS_LEN,
BUF1_BUFFERS_LEN,
BUF1_SECURITY_CONTEXT_BUFFER_LEN,
)
.expect("allocate buffer 1");
assert_eq!(
allocations.data_buffer.user_buffer(),
UserBuffer { address: BASE_ADDR, length: BUF1_DATA_LEN }
);
assert_eq!(
allocations.offsets_buffer.user_buffer(),
UserBuffer {
address: BASE_ADDR.checked_add(BUF1_DATA_LEN).unwrap(),
length: BUF1_OFFSETS_LEN
}
);
assert_eq!(
allocations.scatter_gather_buffer.user_buffer(),
UserBuffer {
address: BASE_ADDR
.checked_add(BUF1_DATA_LEN)
.unwrap()
.checked_add(BUF1_OFFSETS_LEN)
.unwrap(),
length: BUF1_BUFFERS_LEN
}
);
assert_eq!(
allocations.security_context_buffer.expect("security_context").user_buffer(),
UserBuffer {
address: BASE_ADDR
.checked_add(BUF1_DATA_LEN)
.unwrap()
.checked_add(BUF1_OFFSETS_LEN)
.unwrap()
.checked_add(BUF1_BUFFERS_LEN)
.unwrap(),
length: BUF1_SECURITY_CONTEXT_BUFFER_LEN
}
);
const BUF2_DATA_LEN: usize = 32;
const BUF2_OFFSETS_LEN: usize = 0;
const BUF2_BUFFERS_LEN: usize = 0;
const BUF2_SECURITY_CONTEXT_BUFFER_LEN: usize = 0;
let allocations = shared_memory
.allocate_buffers(
BUF2_DATA_LEN,
BUF2_OFFSETS_LEN,
BUF2_BUFFERS_LEN,
BUF2_SECURITY_CONTEXT_BUFFER_LEN,
)
.expect("allocate buffer 2");
assert_eq!(
allocations.data_buffer.user_buffer(),
UserBuffer {
address: BASE_ADDR
.checked_add(BUF1_DATA_LEN)
.unwrap()
.checked_add(BUF1_OFFSETS_LEN)
.unwrap()
.checked_add(BUF1_BUFFERS_LEN)
.unwrap()
.checked_add(BUF1_SECURITY_CONTEXT_BUFFER_LEN)
.unwrap(),
length: BUF2_DATA_LEN
}
);
assert_eq!(
allocations.offsets_buffer.user_buffer(),
UserBuffer {
address: BASE_ADDR
.checked_add(BUF1_DATA_LEN)
.unwrap()
.checked_add(BUF1_OFFSETS_LEN)
.unwrap()
.checked_add(BUF1_BUFFERS_LEN)
.unwrap()
.checked_add(BUF1_SECURITY_CONTEXT_BUFFER_LEN)
.unwrap()
.checked_add(BUF2_DATA_LEN)
.unwrap(),
length: BUF2_OFFSETS_LEN
}
);
}
#[fuchsia::test]
fn shared_memory_too_large_allocation_fails() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
shared_memory.allocate_buffers(VMO_LENGTH + 1, 0, 0, 0).expect_err("out-of-bounds allocation");
shared_memory.allocate_buffers(VMO_LENGTH, 8, 0, 0).expect_err("out-of-bounds allocation");
shared_memory.allocate_buffers(VMO_LENGTH - 8, 8, 8, 0).expect_err("out-of-bounds allocation");
shared_memory.allocate_buffers(VMO_LENGTH, 0, 0, 0).expect("allocate buffer");
// Now that the previous buffer allocation succeeded, there should be no more room.
shared_memory.allocate_buffers(1, 0, 0, 0).expect_err("out-of-bounds allocation");
}
#[fuchsia::test]
fn shared_memory_allocation_wraps_in_order() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
let n = 4;
for buffer in fill_with_buffers(&mut shared_memory, n) {
shared_memory
.allocate_buffers(VMO_LENGTH / n, 0, 0, 0)
.expect_err(&format!("allocated buffer when shared memory was full {n:?}"));
shared_memory.free_buffer(buffer).expect("didn't free buffer");
shared_memory.allocate_buffers(VMO_LENGTH / n, 0, 0, 0).unwrap_or_else(|_| {
panic!("couldn't allocate new buffer even after {n:?}-th was released")
});
}
}
#[fuchsia::test]
fn shared_memory_allocation_single() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
let n = 1;
let buffers = fill_with_buffers(&mut shared_memory, n);
shared_memory
.allocate_buffers(VMO_LENGTH / n, 0, 0, 0)
.expect_err("could allocate when buffer was full");
shared_memory.free_buffer(buffers[0]).expect("didn't free buffer");
shared_memory
.allocate_buffers(VMO_LENGTH / n, 0, 0, 0)
.expect("couldn't allocate even after first slot opened up");
}
#[fuchsia::test]
fn shared_memory_allocation_can_allocate_in_hole() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
let n = 4;
let buffers = fill_with_buffers(&mut shared_memory, n);
// Free all the buffers in reverse order, and verify that the new buffer isn't allocated
// until the first buffer is freed.
shared_memory.allocate_buffers(VMO_LENGTH / n, 0, 0, 0).expect_err("cannot allocate when full");
shared_memory.free_buffer(buffers[1]).expect("didn't free buffer");
shared_memory.allocate_buffers(VMO_LENGTH / n, 0, 0, 0).expect("can allocate in hole");
}
#[fuchsia::test]
fn shared_memory_allocation_doesnt_wrap_when_cant_fit() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
let n = 4;
let buffers = fill_with_buffers(&mut shared_memory, n);
shared_memory.free_buffer(buffers[0]).expect("didn't free buffer");
// Allocate slightly more than what can fit at the start (after freeing the first 1/4th).
shared_memory
.allocate_buffers((VMO_LENGTH / n) + 1, 0, 0, 0)
.expect_err("allocated over existing buffer");
shared_memory.free_buffer(buffers[1]).expect("didn't free buffer");
shared_memory
.allocate_buffers((VMO_LENGTH / n) + 1, 0, 0, 0)
.expect("couldn't allocate when there was enough space");
}
#[fuchsia::test]
fn shared_memory_allocation_doesnt_wrap_when_cant_fit_at_end() {
let memory =
MemoryObject::from(zx::Vmo::create(VMO_LENGTH as u64).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, VMO_LENGTH).expect("failed to map shared memory");
// Test that a buffer can still be allocated even if it can't fit at the end, but can fit
// at the start by first allocating 3/4 of the memory.
let buffer_1 = {
let allocations =
shared_memory.allocate_buffers(VMO_LENGTH / 4, 0, 0, 0).expect("couldn't allocate");
(allocations.data_buffer.memory.user_address + allocations.data_buffer.offset).unwrap()
};
let buffer_2 = {
let allocations =
shared_memory.allocate_buffers(VMO_LENGTH / 4, 0, 0, 0).expect("couldn't allocate");
(allocations.data_buffer.memory.user_address + allocations.data_buffer.offset).unwrap()
};
let buffer_3 = {
let allocations =
shared_memory.allocate_buffers(VMO_LENGTH / 4, 0, 0, 0).expect("couldn't allocate");
(allocations.data_buffer.memory.user_address + allocations.data_buffer.offset).unwrap()
};
// Attempt to allocate a buffer at the end that is larger than 1/4th.
shared_memory
.allocate_buffers(VMO_LENGTH / 3, 0, 0, 0)
.expect_err("allocated over existing buffer");
// Now free all the buffers at the start.
shared_memory.free_buffer(buffer_1).expect("didn't free buffer");
shared_memory.free_buffer(buffer_2).expect("didn't free buffer");
shared_memory.free_buffer(buffer_3).expect("didn't free buffer");
// Try the allocation again.
shared_memory
.allocate_buffers(VMO_LENGTH / 3, 0, 0, 0)
.expect("failed even though there was room at start");
}
#[fuchsia::test]
fn shared_memory_best_fit_and_coalescing() {
let memory = MemoryObject::from(zx::Vmo::create(1024).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, 1024).expect("failed to map shared memory");
// Allocate 4 chunks of 256 bytes each (A: 0..256, B: 256..512, C: 512..768, D: 768..1024)
let addr_a = (BASE_ADDR
+ shared_memory.allocate_buffers(256, 0, 0, 0).unwrap().data_buffer.offset)
.unwrap();
let addr_b = (BASE_ADDR
+ shared_memory.allocate_buffers(256, 0, 0, 0).unwrap().data_buffer.offset)
.unwrap();
let addr_c = (BASE_ADDR
+ shared_memory.allocate_buffers(256, 0, 0, 0).unwrap().data_buffer.offset)
.unwrap();
let addr_d = (BASE_ADDR
+ shared_memory.allocate_buffers(256, 0, 0, 0).unwrap().data_buffer.offset)
.unwrap();
// Memory is full.
shared_memory.allocate_buffers(8, 0, 0, 0).expect_err("memory should be full");
// Free B (hole 256..512) and D (hole 768..1024)
shared_memory.free_buffer(addr_b).expect("free B");
shared_memory.free_buffer(addr_d).expect("free D");
// Best-fit test: allocating 128 bytes should pick the smallest fitting hole (e.g. B at 256)
let (small_offset, small_addr) = {
let alloc_small = shared_memory.allocate_buffers(128, 0, 0, 0).expect("allocate small");
(alloc_small.data_buffer.offset, (BASE_ADDR + alloc_small.data_buffer.offset).unwrap())
};
assert_eq!(small_offset, 256);
// Free A (0..256), coalescing with remaining free space in slot B (384..512) when small is freed.
shared_memory.free_buffer(addr_a).expect("free A");
shared_memory.free_buffer(small_addr).expect("free small");
// Now 0..512 is completely coalesced into a single 512-byte free chunk!
let (large_offset, large_addr) = {
let alloc_large = shared_memory.allocate_buffers(512, 0, 0, 0).expect("allocate 512 bytes");
(alloc_large.data_buffer.offset, (BASE_ADDR + alloc_large.data_buffer.offset).unwrap())
};
assert_eq!(large_offset, 0);
// Free C (512..768) and large (0..512). Since D was already freed, all 1024 bytes are coalesced!
shared_memory.free_buffer(addr_c).expect("free C");
shared_memory.free_buffer(large_addr).expect("free large");
// Can allocate the entire 1024-byte buffer in one chunk!
let alloc_full = shared_memory.allocate_buffers(1024, 0, 0, 0).expect("allocate full");
assert_eq!(alloc_full.data_buffer.offset, 0);
}
#[fuchsia::test]
fn shared_memory_invalid_and_double_free() {
let memory = MemoryObject::from(zx::Vmo::create(1024).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, 1024).expect("failed to map shared memory");
let addr = {
let alloc = shared_memory.allocate_buffers(256, 0, 0, 0).expect("allocate 256");
(BASE_ADDR + alloc.data_buffer.offset).unwrap()
};
// Valid free
shared_memory.free_buffer(addr).expect("valid free");
// Double free must fail with EINVAL
assert_eq!(shared_memory.free_buffer(addr), Err(errno!(EINVAL)));
// Freeing unmapped address must fail with EINVAL
assert_eq!(shared_memory.free_buffer(UserAddress::from(0xdeadbeef)), Err(errno!(EINVAL)));
assert_eq!(shared_memory.free_buffer((BASE_ADDR + 5000usize).unwrap()), Err(errno!(EINVAL)));
}
#[fuchsia::test]
fn shared_memory_exact_fit_allocation() {
let memory = MemoryObject::from(zx::Vmo::create(1024).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, 1024).expect("failed to map shared memory");
// Allocate three 256-byte chunks: A (0..256), B (256..512), C (512..768)
let addr_a = (BASE_ADDR
+ shared_memory.allocate_buffers(256, 0, 0, 0).unwrap().data_buffer.offset)
.unwrap();
let addr_b = (BASE_ADDR
+ shared_memory.allocate_buffers(256, 0, 0, 0).unwrap().data_buffer.offset)
.unwrap();
let addr_c = (BASE_ADDR
+ shared_memory.allocate_buffers(256, 0, 0, 0).unwrap().data_buffer.offset)
.unwrap();
// Free middle chunk B (hole of exactly 256 bytes)
shared_memory.free_buffer(addr_b).expect("free B");
// Allocate exact-fit chunk of 256 bytes (remainder_size == 0)
let exact_addr = {
let exact_alloc = shared_memory.allocate_buffers(256, 0, 0, 0).expect("exact-fit allocate");
assert_eq!(exact_alloc.data_buffer.offset, 256);
(BASE_ADDR + exact_alloc.data_buffer.offset).unwrap()
};
// Free all chunks and ensure full coalescing back to 1024 bytes
shared_memory.free_buffer(addr_a).expect("free A");
shared_memory.free_buffer(addr_c).expect("free C");
shared_memory.free_buffer(exact_addr).expect("free exact alloc");
let full_alloc = shared_memory.allocate_buffers(1024, 0, 0, 0).expect("full buffer allocate");
assert_eq!(full_alloc.data_buffer.offset, 0);
}
#[fuchsia::test]
async fn external_vmo_buffers_stay_within_mapped_size() {
spawn_kernel_and_run(async |current_task| {
let device = BinderDevice::default();
let remote = BinderProcessFixture::new_current_without_shared_memory(current_task, &device);
let page_size = *PAGE_SIZE as usize;
let vmo_size = 8 * page_size;
let mapped_size = 2 * page_size;
let mapped_address = 0x10000;
let vmo = zx::Vmo::create(vmo_size as u64).expect("Vmo::create");
remote
.proc
.map_external_vmo(vmo, mapped_address, Some(mapped_size as u64))
.expect("map_external_vmo");
let mut shared_memory = remote.lock_shared_memory();
assert_eq!(shared_memory.length, mapped_size);
// A buffer larger than the mapping does not fit, although the VMO is larger.
assert_matches!(shared_memory.allocate_buffers(mapped_size + 8, 0, 0, 0), Err(_));
// Buffers fill the mapping, and no more fit.
let buffer_size = page_size / 2;
let mut addresses = vec![];
for _ in 0..mapped_size / buffer_size {
let buffer = shared_memory
.allocate_buffers(buffer_size, 0, 0, 0)
.expect("allocate_buffers")
.data_buffer;
assert!(buffer.offset + buffer_size <= mapped_size);
addresses.push((buffer.memory.user_address + buffer.offset).expect("address"));
}
assert_matches!(shared_memory.allocate_buffers(8, 0, 0, 0), Err(_));
let mapped_end = mapped_address as usize + mapped_size;
for address in addresses {
assert!(address.ptr() >= mapped_address as usize && address.ptr() < mapped_end);
}
})
.await;
}
#[fuchsia::test]
async fn external_vmo_rejects_invalid_mapped_size() {
spawn_kernel_and_run(async |current_task| {
let device = BinderDevice::default();
let remote = BinderProcessFixture::new_current_without_shared_memory(current_task, &device);
let page_size = *PAGE_SIZE;
let vmo_size = 4 * page_size;
let vmo = zx::Vmo::create(vmo_size).expect("Vmo::create");
for mapped_size in [0, page_size + 1, vmo_size + page_size] {
let dup = vmo.duplicate_handle(zx::Rights::SAME_RIGHTS).expect("duplicate_handle");
assert_eq!(
remote.proc.map_external_vmo(dup, 0x10000, Some(mapped_size)),
Err(errno!(EINVAL)),
"mapped_size: {mapped_size}"
);
}
assert!(remote.proc.shared_memory.lock().is_none());
// Without a size, as with `MapVmo`, the whole VMO is used.
remote.proc.map_external_vmo(vmo, 0x10000, None).expect("map_external_vmo");
assert_eq!(remote.lock_shared_memory().length, vmo_size as usize);
})
.await;
}
#[fuchsia::test]
fn shared_memory_zero_length_mapping() {
let memory = MemoryObject::from(zx::Vmo::create(1024).expect("failed to create VMO"));
assert_eq!(SharedMemory::map(&memory, BASE_ADDR, 0).unwrap_err(), errno!(EINVAL));
}
#[fuchsia::test]
fn shared_memory_churn_coalescing() {
let memory = MemoryObject::from(zx::Vmo::create(2048).expect("failed to create VMO"));
let mut shared_memory =
SharedMemory::map(&memory, BASE_ADDR, 2048).expect("failed to map shared memory");
// Allocate chunks of varying sizes: [64, 128, 256, 512, 128, 64, 256] (sum = 1408)
let sizes = [64, 128, 256, 512, 128, 64, 256];
let mut addrs = Vec::new();
for &sz in &sizes {
let addr = {
let alloc = shared_memory.allocate_buffers(sz, 0, 0, 0).unwrap();
(BASE_ADDR + alloc.data_buffer.offset).unwrap()
};
addrs.push(addr);
}
// Free odd indices first: [1 (128), 3 (512), 5 (64)]
shared_memory.free_buffer(addrs[1]).expect("free 1");
shared_memory.free_buffer(addrs[3]).expect("free 3");
shared_memory.free_buffer(addrs[5]).expect("free 5");
// Allocate into freed holes (e.g. 512-byte hole at index 3)
let addr_hole = {
let alloc_hole = shared_memory.allocate_buffers(512, 0, 0, 0).unwrap();
(BASE_ADDR + alloc_hole.data_buffer.offset).unwrap()
};
// Free remaining original allocations and the hole allocation
shared_memory.free_buffer(addrs[0]).expect("free 0");
shared_memory.free_buffer(addrs[2]).expect("free 2");
shared_memory.free_buffer(addrs[4]).expect("free 4");
shared_memory.free_buffer(addrs[6]).expect("free 6");
shared_memory.free_buffer(addr_hole).expect("free hole");
// Everything is coalesced back into a single 2048-byte region
let full = shared_memory.allocate_buffers(2048, 0, 0, 0).unwrap();
assert_eq!(full.data_buffer.offset, 0);
}