Skip to content

Commit

Permalink
Support allocating 4GB of memory (#452)
Browse files Browse the repository at this point in the history
* Support allocating 4GB of memory

* Support 32bit archs as well

* Feedback

* Update core/src/vmem.rs
  • Loading branch information
bkchr authored Sep 20, 2022
1 parent b0d6c7f commit 4e85c57
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmi"
version = "0.13.0"
version = "0.13.1"
edition = "2021"
authors = ["Parity Technologies <[email protected]>", "Nikolay Volf <[email protected]>", "Svyatoslav Nikolsky <[email protected]>", "Sergey Pepyakin <[email protected]>"]
license = "MIT/Apache-2.0"
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "wasmi_core"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
authors = ["Parity Technologies <[email protected]>"]
license = "MIT/Apache-2.0"
Expand Down
14 changes: 12 additions & 2 deletions core/src/vmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ impl Debug for VirtualMemory {
}

impl VirtualMemory {
/// The maximum allocation size for a `wasmi` virtual memory.
const MAX_ALLOCATION_SIZE: usize = u32::MAX as usize;
/// The maximum allocation size for a `wasmi` virtual memory on 16-bit.
#[cfg(target_pointer_width = "16")]
const MAX_ALLOCATION_SIZE: usize =
compile_error!("16-bit architectures are current unsupported by wasmi");

/// The maximum allocation size for a `wasmi` virtual memory on 32-bit.
#[cfg(target_pointer_width = "32")]
const MAX_ALLOCATION_SIZE: usize = i32::MAX as usize + 1; // 2GB

/// The maximum allocation size for a `wasmi` virtual memory on 64-bit.
#[cfg(target_pointer_width = "64")]
const MAX_ALLOCATION_SIZE: usize = u32::MAX as usize + 1; // 4GB

/// Create a new virtual memory allocation.
///
Expand Down
9 changes: 8 additions & 1 deletion src/memory/mmap_bytebuf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ impl ByteBuf {
/// In this implementation we won't reallocate the virtually allocated
/// buffer and instead simply adjust the `len` field of the `ByteBuf`
/// wrapper in order to efficiently grow the virtual memory.
const ALLOCATION_SIZE: usize = u32::MAX as usize;
const ALLOCATION_SIZE: usize =
validation::LINEAR_MEMORY_MAX_PAGES as usize * super::LINEAR_MEMORY_PAGE_SIZE.0;

/// Creates a new byte buffer with the given initial length.
pub fn new(len: usize) -> Result<Self, String> {
Expand Down Expand Up @@ -96,4 +97,10 @@ mod tests {
let mut byte_buf = ByteBuf::new(100).unwrap();
assert!(byte_buf.realloc(ByteBuf::ALLOCATION_SIZE + 1).is_err());
}

#[test]
fn allocate_maximum_number_of_pages() {
let mut byte_buf = ByteBuf::new(100).unwrap();
byte_buf.realloc(ByteBuf::ALLOCATION_SIZE).unwrap();
}
}

0 comments on commit 4e85c57

Please sign in to comment.