From 2590aab278f2acbc9ee3db9466ae071e13272937 Mon Sep 17 00:00:00 2001 From: Jarkko Sakkinen Date: Thu, 21 Nov 2024 22:55:28 +0200 Subject: [PATCH] sc-executor-polkavm: Fix allocate_memory() error handling Signed-off-by: Jarkko Sakkinen --- substrate/client/executor/polkavm/src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/substrate/client/executor/polkavm/src/lib.rs b/substrate/client/executor/polkavm/src/lib.rs index b18e6b3bc4ea..a89625a07b07 100644 --- a/substrate/client/executor/polkavm/src/lib.rs +++ b/substrate/client/executor/polkavm/src/lib.rs @@ -132,12 +132,15 @@ impl<'r, 'a> FunctionContext for Context<'r, 'a> { } fn allocate_memory(&mut self, size: WordSize) -> sp_wasm_interface::Result> { - let pointer = self.0.sbrk(0).expect("fetching the current heap pointer never fails"); + let pointer = + self.0.instance.sbrk(0).expect("fetching the current heap pointer never fails"); // TODO: This will leak guest memory; find a better solution. - self.0.sbrk(size).ok_or_else(|| String::from("allocation failed"))?; + if let Err(_) = self.0.instance.sbrk(size) { + return Err(String::from("allocation failed")); + } - Ok(Pointer::new(pointer)) + Ok(Pointer::new(pointer.unwrap())) } fn deallocate_memory(&mut self, _ptr: Pointer) -> sp_wasm_interface::Result<()> {