Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add crumbles::Mmap::set_len and adapt piecrust memory behavior #262

Merged
merged 2 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crumbles/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `Mmap::set_len` function

## [0.1.1] - 2023-09-07

### Fixed
Expand Down
24 changes: 23 additions & 1 deletion crumbles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#![deny(clippy::pedantic)]

use std::{
cmp,
collections::BTreeMap,
fs::File,
mem::{self, MaybeUninit},
Expand Down Expand Up @@ -304,6 +305,11 @@ impl Mmap {
)
})
}

/// Sets the length of the memory mapping to the given number of bytes.
pub fn set_len(&mut self, len: u32) {
self.0.set_len(len);
}
}

impl AsRef<[u8]> for Mmap {
Expand Down Expand Up @@ -465,8 +471,15 @@ impl MmapInner {

unsafe fn set_dirty(&mut self, page_num: usize) -> io::Result<()> {
let start_addr = self.bytes.as_ptr() as usize;

let page_offset = page_num * PAGE_SIZE;

if page_offset >= self.bytes.len() {
return Err(io::Error::new(
io::ErrorKind::PermissionDenied,
"Out of bounds write",
));
}

let page_addr = start_addr + page_offset;

if libc::mprotect(page_addr as _, PAGE_SIZE, PROT_READ | PROT_WRITE)
Expand Down Expand Up @@ -544,6 +557,15 @@ impl MmapInner {
Ok(())
}

fn set_len(&mut self, len: u32) {
let ptr = self.bytes.as_mut_ptr();

let len = len as usize + PAGE_SIZE;
let len = cmp::min(MEM_SIZE, len);

unsafe { self.bytes = slice::from_raw_parts_mut(ptr, len) };
}

fn last_snapshot(&self) -> &Snapshot {
self.snapshots
.last()
Expand Down
4 changes: 4 additions & 0 deletions piecrust/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## Changed

- Change to use `crumbles::Mmap::set_len` on growing memory

## [0.9.1] - 2023-09-07

### Changed
Expand Down
27 changes: 17 additions & 10 deletions piecrust/src/store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Copyright (c) DUSK NETWORK. All rights reserved.

use std::fs::OpenOptions;
use std::io;
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::ptr::NonNull;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{cmp, io};

use crumbles::{Mmap, PAGE_SIZE};
use wasmer::WASM_MAX_PAGES;
Expand All @@ -27,7 +27,6 @@ pub const MAX_MEM_SIZE: usize = MAX_PAGES * PAGE_SIZE;
#[derive(Debug)]
pub(crate) struct MemoryInner {
pub(crate) mmap: Mmap,
pub(crate) current_len: usize,
def: VMMemoryDefinition,
init: bool,
}
Expand All @@ -42,15 +41,16 @@ impl Memory {
pub(crate) fn new() -> io::Result<Self> {
let mut mmap = Mmap::new()?;

mmap.set_len(MIN_MEM_SIZE as u32);

let def = VMMemoryDefinition {
base: mmap.as_mut_ptr(),
current_length: MAX_MEM_SIZE,
current_length: MIN_MEM_SIZE + PAGE_SIZE,
};

Ok(Self {
inner: Arc::new(RwLock::new(MemoryInner {
mmap,
current_len: MIN_MEM_SIZE,
def,
init: false,
})),
Expand All @@ -72,15 +72,16 @@ impl Memory {
}))?
};

mmap.set_len(len as u32);

let def = VMMemoryDefinition {
base: mmap.as_mut_ptr(),
current_length: MAX_MEM_SIZE,
current_length: cmp::min(len + PAGE_SIZE, MAX_MEM_SIZE),
};

Ok(Self {
inner: Arc::new(RwLock::new(MemoryInner {
mmap,
current_len: len,
def,
init: true,
})),
Expand Down Expand Up @@ -156,21 +157,21 @@ impl LinearMemory for Memory {
}

fn size(&self) -> Pages {
let pages = self.read().inner.current_len / PAGE_SIZE;
let pages = self.read().len() / PAGE_SIZE;
Pages(pages as u32)
}

fn style(&self) -> MemoryStyle {
MemoryStyle::Static {
bound: Pages(MAX_PAGES as u32),
offset_guard_size: PAGE_SIZE as u64,
offset_guard_size: 0,
}
}

fn grow(&mut self, delta: Pages) -> Result<Pages, MemoryError> {
let mut memory = self.write();

let current_len = memory.inner.current_len;
let current_len = memory.len();
let new_len = current_len + delta.0 as usize * PAGE_SIZE;

if new_len > MAX_PAGES * PAGE_SIZE {
Expand All @@ -180,7 +181,13 @@ impl LinearMemory for Memory {
});
}

memory.inner.current_len = new_len;
memory.set_len(new_len as u32);

memory.inner.def = VMMemoryDefinition {
base: memory.as_mut_ptr(),
current_length: cmp::min(new_len + PAGE_SIZE, MAX_MEM_SIZE),
};

Ok(Pages((new_len / PAGE_SIZE) as u32))
}

Expand Down
2 changes: 1 addition & 1 deletion piecrust/src/store/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ContractIndex {
let memory = memory.read();
let memory_inner = memory.inner;

element.len = memory_inner.current_len;
element.len = memory_inner.mmap.len();

for (dirty_page, _, page_offset) in memory_inner.mmap.dirty_pages() {
element.offsets.insert(page_offset);
Expand Down
Loading