From 69800ae1b3814626e5218f4b3be8ba64ce21a452 Mon Sep 17 00:00:00 2001 From: xermicus Date: Tue, 26 Mar 2024 12:03:04 +0100 Subject: [PATCH] always allocate the total required heap size Signed-off-by: xermicus --- crates/llvm-context/src/eravm/context/mod.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/llvm-context/src/eravm/context/mod.rs b/crates/llvm-context/src/eravm/context/mod.rs index b89dd6c..88a2506 100644 --- a/crates/llvm-context/src/eravm/context/mod.rs +++ b/crates/llvm-context/src/eravm/context/mod.rs @@ -1262,6 +1262,8 @@ where /// Call PolkaVM `sbrk` for extending the heap by `size`, /// trapping the contract if the call failed. + /// + /// Returns the end of memory pointer. pub fn build_heap_alloc( &self, size: inkwell::values::IntValue<'ctx>, @@ -1287,8 +1289,8 @@ where Ok(end_of_memory) } - /// Returns a pointer to `offset` into the heap, allocating `length` - /// bytes more memory if `offset + length` would be out of bounds. + /// Returns a pointer to `offset` into the heap, allocating + /// enough memory if `offset + length` would be out of bounds. /// /// # Panics /// Assumes `offset` and `length` to be an i32 value. @@ -1323,7 +1325,14 @@ where self.build_conditional_branch(is_out_of_bounds, out_of_bounds_block, heap_offset_block)?; self.set_basic_block(out_of_bounds_block); - self.build_heap_alloc(length)?; + let size = self.builder().build_int_nuw_sub( + self.builder() + .build_ptr_to_int(value_end.value, self.xlen_type(), "value_end")?, + self.builder() + .build_ptr_to_int(heap_end, self.xlen_type(), "heap_end")?, + "heap_alloc_size", + )?; + self.build_heap_alloc(size)?; self.build_unconditional_branch(heap_offset_block); self.set_basic_block(heap_offset_block);