Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
holykol committed Nov 16, 2023
1 parent fca257c commit b2e6e74
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
9 changes: 8 additions & 1 deletion core-processor/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,6 +721,13 @@ impl Externalities for Ext {
.map_err(Into::into)
}

fn free(&mut self, page: WasmPage) -> Result<(), Self::AllocError> {
self.context
.allocations_context
.free(page)
.map_err(Into::into)
}

fn free_range(&mut self, start: WasmPage, end: WasmPage) -> Result<(), Self::AllocError> {
if start > end {
return Err(AllocExtError::Alloc(AllocError::InvalidRange(
Expand Down Expand Up @@ -1314,7 +1321,7 @@ mod tests {
assert!(ext.free(existing_page).is_ok());
assert_eq!(ext.gas_left(), gas_left);

assert!(ext.free(non_existing_page).is_ok());
assert!(ext.free(non_existing_page).is_err());
assert_eq!(ext.gas_left(), gas_left);
}

Expand Down
6 changes: 2 additions & 4 deletions core/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,8 @@ pub trait Externalities {
mem: &mut impl Memory,
) -> Result<WasmPage, Self::AllocError>;

/// Free specific memory page
fn free(&mut self, page: WasmPage) -> Result<(), Self::AllocError> {
self.free_range(page, page)
}
/// Free specific page
fn free(&mut self, page: WasmPage) -> Result<(), Self::AllocError>;

/// Free specific memory range
fn free_range(&mut self, start: WasmPage, end: WasmPage) -> Result<(), Self::AllocError>;
Expand Down
18 changes: 11 additions & 7 deletions core/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,20 +381,23 @@ impl AllocationsContext {

/// Free specific memory page.
pub fn free(&mut self, page: WasmPage) -> Result<(), AllocError> {
self.free_range(page..=page)
if page < self.static_pages || page >= self.max_pages {
return Err(AllocError::InvalidFree(page.0));
}

if !self.allocations.remove(&page) {
return Err(AllocError::InvalidFree(page.0));
}

Ok(())
}

/// Try to free pages in range. Will only return error if range is invalid.
///
/// Currently running program should own this pages.
pub fn free_range(&mut self, range: RangeInclusive<WasmPage>) -> Result<(), AllocError> {
if *range.start() < self.static_pages || *range.end() >= self.max_pages {
let page = if *range.start() < self.static_pages {
range.start().0
} else {
range.end().0
};
return Err(AllocError::InvalidFree(page));
return Err(AllocError::InvalidRange(range.start().0, range.end().0));
}

self.allocations.retain(|p| !range.contains(p));
Expand Down Expand Up @@ -600,6 +603,7 @@ mod tests {
fn assert_free_error(err: AllocError) {
match err {
AllocError::InvalidFree(_) => {}
AllocError::InvalidRange(_, _) => {}
err => panic!("{err:?}"),
}
}
Expand Down
1 change: 1 addition & 0 deletions pallets/gear/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13810,6 +13810,7 @@ fn free_range_success() {
run_to_next_block(None);

assert_succeed(mid);
assert!(Gear::is_initialized(pid));
assert!(Gear::is_active(pid));
});
}
Expand Down

0 comments on commit b2e6e74

Please sign in to comment.