Get rid of memory_units dependency in validation
This commit is contained in:
parent
9935df3307
commit
fc36931c06
|
@ -109,10 +109,10 @@ extern crate std as alloc;
|
|||
#[macro_use]
|
||||
extern crate core;
|
||||
|
||||
#[cfg(test)]
|
||||
extern crate wabt;
|
||||
#[cfg(test)]
|
||||
extern crate assert_matches;
|
||||
#[cfg(test)]
|
||||
extern crate wabt;
|
||||
|
||||
#[cfg(not(feature = "std"))]
|
||||
extern crate hashbrown;
|
||||
|
|
|
@ -108,7 +108,23 @@ impl MemoryInstance {
|
|||
///
|
||||
/// [`LINEAR_MEMORY_PAGE_SIZE`]: constant.LINEAR_MEMORY_PAGE_SIZE.html
|
||||
pub fn alloc(initial: Pages, maximum: Option<Pages>) -> Result<MemoryRef, Error> {
|
||||
validation::validate_memory(initial, maximum).map_err(Error::Memory)?;
|
||||
{
|
||||
use std::convert::TryInto;
|
||||
let initial_u32: u32 = initial
|
||||
.0
|
||||
.try_into()
|
||||
.map_err(|_| Error::Memory("initial can't be coerced to u32".into()))?;
|
||||
let maximum_u32: Option<u32> = match maximum {
|
||||
Some(pages) => Some(
|
||||
pages
|
||||
.0
|
||||
.try_into()
|
||||
.map_err(|_| Error::Memory("maximum can't be coerced to u32".into()))?,
|
||||
),
|
||||
None => None,
|
||||
};
|
||||
validation::validate_memory(initial_u32, maximum_u32).map_err(Error::Memory)?;
|
||||
}
|
||||
|
||||
let memory = MemoryInstance::new(initial, maximum);
|
||||
Ok(MemoryRef(Rc::new(memory)))
|
||||
|
@ -268,7 +284,9 @@ impl MemoryInstance {
|
|||
}
|
||||
|
||||
let new_size: Pages = size_before_grow + additional;
|
||||
let maximum = self.maximum.unwrap_or(validation::LINEAR_MEMORY_MAX_PAGES);
|
||||
let maximum = self
|
||||
.maximum
|
||||
.unwrap_or(Pages(validation::LINEAR_MEMORY_MAX_PAGES as usize));
|
||||
if new_size > maximum {
|
||||
return Err(Error::Memory(format!(
|
||||
"Trying to grow memory by {} pages when already have {}",
|
||||
|
|
|
@ -6,7 +6,6 @@ edition = "2018"
|
|||
|
||||
[dependencies]
|
||||
parity-wasm = { version = "0.31", default-features = false }
|
||||
memory_units_crate = { package = "memory_units", version = "0.3.0" }
|
||||
hashbrown = { version = "0.1.8", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
|
|
|
@ -19,7 +19,7 @@ pub const DEFAULT_MEMORY_INDEX: u32 = 0;
|
|||
pub const DEFAULT_TABLE_INDEX: u32 = 0;
|
||||
|
||||
/// Maximal number of pages that a wasm instance supports.
|
||||
pub const LINEAR_MEMORY_MAX_PAGES: Pages = Pages(65536);
|
||||
pub const LINEAR_MEMORY_MAX_PAGES: u32 = 65536;
|
||||
|
||||
#[allow(unused_imports)]
|
||||
use alloc::prelude::v1::*;
|
||||
|
@ -32,14 +32,6 @@ use hashbrown::HashSet;
|
|||
#[cfg(feature = "std")]
|
||||
use std::collections::HashSet;
|
||||
|
||||
/// WebAssembly-specific sizes and units.
|
||||
pub mod memory_units {
|
||||
pub use memory_units_crate::wasm32::*;
|
||||
pub use memory_units_crate::{size_of, ByteSize, Bytes, RoundUpTo};
|
||||
}
|
||||
|
||||
use memory_units::Pages;
|
||||
|
||||
use self::context::ModuleContextBuilder;
|
||||
use parity_wasm::elements::{
|
||||
BlockType, External, GlobalEntry, GlobalType, InitExpr, Instruction, Internal, MemoryType,
|
||||
|
@ -367,30 +359,30 @@ fn validate_limits(limits: &ResizableLimits) -> Result<(), Error> {
|
|||
}
|
||||
|
||||
fn validate_memory_type(memory_type: &MemoryType) -> Result<(), Error> {
|
||||
let initial: Pages = Pages(memory_type.limits().initial() as usize);
|
||||
let maximum: Option<Pages> = memory_type.limits().maximum().map(|m| Pages(m as usize));
|
||||
let initial = memory_type.limits().initial();
|
||||
let maximum: Option<u32> = memory_type.limits().maximum();
|
||||
validate_memory(initial, maximum).map_err(Error)
|
||||
}
|
||||
|
||||
pub fn validate_memory(initial: Pages, maximum: Option<Pages>) -> Result<(), String> {
|
||||
pub fn validate_memory(initial: u32, maximum: Option<u32>) -> Result<(), String> {
|
||||
if initial > LINEAR_MEMORY_MAX_PAGES {
|
||||
return Err(format!(
|
||||
"initial memory size must be at most {} pages",
|
||||
LINEAR_MEMORY_MAX_PAGES.0
|
||||
LINEAR_MEMORY_MAX_PAGES
|
||||
));
|
||||
}
|
||||
if let Some(maximum) = maximum {
|
||||
if initial > maximum {
|
||||
return Err(format!(
|
||||
"maximum limit {} is less than minimum {}",
|
||||
maximum.0, initial.0,
|
||||
maximum, initial,
|
||||
));
|
||||
}
|
||||
|
||||
if maximum > LINEAR_MEMORY_MAX_PAGES {
|
||||
return Err(format!(
|
||||
"maximum memory size must be at most {} pages",
|
||||
LINEAR_MEMORY_MAX_PAGES.0
|
||||
LINEAR_MEMORY_MAX_PAGES
|
||||
));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue