mem/memory_map: validate desc_size in MemoryMapOwned constructor

`from_initialized_mem` only checked that `desc_size` was large enough,
unlike the `MemoryMapRef`/`MemoryMapRefMut` constructors, which also
require it to be a multiple of the descriptor alignment. With an
unaligned `desc_size`, every entry but the first was accessed through a
misaligned `&MemoryDescriptor`.

Reuse `validate_meta` so all constructors enforce the same invariant.
This commit is contained in:
Philipp Schuster 2026-08-24 15:17:21 +02:00
parent b17551c70e
commit 4f8ca7069c
No known key found for this signature in database

View file

@ -402,8 +402,10 @@ impl MemoryMapOwned {
/// (stored inside the provided buffer) and the corresponding
/// [`MemoryMapMeta`].
pub(crate) fn from_initialized_mem(buf: MemoryMapBackingMemory, meta: MemoryMapMeta) -> Self {
assert!(meta.desc_size >= size_of::<MemoryDescriptor>());
let len = meta.entry_count();
// Validate `desc_size` fully: besides being large enough, it must be a
// multiple of the descriptor alignment. Otherwise descriptors past the
// first would be accessed through misaligned references.
let len = validate_meta(meta).expect("The memory map metadata should be valid");
Self { buf, meta, len }
}
}