mirror of
https://github.com/extism/extism.git
synced 2026-01-08 21:38:13 -05:00
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:
@@ -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();
|
||||
|
||||
@@ -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.
Reference in New Issue
Block a user