Skip to content

Commit

Permalink
do nothing when resetting an empty mapping, test mmap alloc and reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekleog-NEAR committed Apr 24, 2024
1 parent c1228e5 commit c3ad36f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions runtime/near-vm/vm/src/mmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,12 @@ impl Mmap {
#[cfg(not(target_os = "windows"))]
pub fn reset(&mut self) -> Result<(), String> {
unsafe {
self.as_mut_ptr().write_bytes(0, self.accessible_len);
region::protect(self.as_ptr(), self.accessible_len, region::Protection::NONE)
.map_err(|e| e.to_string())?;
self.accessible_len = 0;
if self.accessible_len > 0 {
self.as_mut_ptr().write_bytes(0, self.accessible_len);
region::protect(self.as_ptr(), self.accessible_len, region::Protection::NONE)
.map_err(|e| e.to_string())?;
self.accessible_len = 0;
}
Ok(())
}
}
Expand Down Expand Up @@ -327,4 +329,14 @@ mod tests {
assert_eq!(round_up_to_page_size(4096, 4096), 4096);
assert_eq!(round_up_to_page_size(4097, 4096), 8192);
}

#[test]
fn alloc_and_reset() {
let mut map = Mmap::accessible_reserved(64 * 1024, 128 * 1024).unwrap();
assert_eq!(map.accessible_len, 64 * 1024);
map.reset().unwrap();
assert_eq!(map.accessible_len, 0);
map.make_accessible(16 * 1024).unwrap();
assert_eq!(map.accessible_len, 16 * 1024);
}
}

0 comments on commit c3ad36f

Please sign in to comment.