fix(kernel): avoid allocating 0-length blocks (#465)

Fixes an issue where the kernel was able to allocate empty blocks,
causing some weird memory issues.
This commit is contained in:
zach
2023-09-18 11:23:36 -07:00
committed by GitHub
parent ab458ebd44
commit d040c8b8a8
3 changed files with 12 additions and 0 deletions

View File

@@ -310,6 +310,9 @@ impl MemoryBlock {
/// Allocate a block of memory and return the offset
#[no_mangle]
pub unsafe fn extism_alloc(n: Length) -> Pointer {
if n == 0 {
return 0;
}
let region = MemoryRoot::new();
let block = region.alloc(n);
match block {
@@ -321,6 +324,9 @@ pub unsafe fn extism_alloc(n: Length) -> Pointer {
/// Free allocated memory
#[no_mangle]
pub unsafe fn extism_free(p: Pointer) {
if p == 0 {
return;
}
let block = MemoryRoot::new().find_block(p);
if let Some(block) = block {
block.free();

View File

@@ -125,6 +125,12 @@ impl CurrentPlugin {
}
pub fn memory_alloc(&mut self, n: u64) -> Result<MemoryHandle, Error> {
if n == 0 {
return Ok(MemoryHandle {
offset: 0,
length: 0,
});
}
let (linker, mut store) = self.linker_and_store();
let output = &mut [Val::I64(0)];
if let Some(f) = linker.get(&mut store, "env", "extism_alloc") {

Binary file not shown.