added memory/table import limits validation

This commit is contained in:
Svyatoslav Nikolsky
2017-09-04 12:00:01 +03:00
parent 8da7d22e37
commit a85162dcd1
12 changed files with 224 additions and 29 deletions

View File

@ -1,7 +1,7 @@
use std::u32;
use std::sync::Arc;
use parking_lot::RwLock;
use elements::MemoryType;
use elements::{MemoryType, ResizableLimits};
use interpreter::{Error, UserError};
use interpreter::module::check_limits;
@ -12,6 +12,8 @@ const LINEAR_MEMORY_MAX_PAGES: u32 = 65536;
/// Linear memory instance.
pub struct MemoryInstance<E: UserError> {
/// Memofy limits.
limits: ResizableLimits,
/// Linear memory buffer.
buffer: RwLock<Vec<u8>>,
/// Maximum buffer size.
@ -51,6 +53,7 @@ impl<E> MemoryInstance<E> where E: UserError {
.ok_or(Error::Memory(format!("initial memory size must be at most {} pages", LINEAR_MEMORY_MAX_PAGES)))?;
let memory = MemoryInstance {
limits: memory_type.limits().clone(),
buffer: RwLock::new(vec![0; initial_size as usize]),
maximum_size: maximum_size,
_dummy: Default::default(),
@ -59,6 +62,11 @@ impl<E> MemoryInstance<E> where E: UserError {
Ok(Arc::new(memory))
}
/// Return linear memory limits.
pub fn limits(&self) -> &ResizableLimits {
&self.limits
}
/// Return linear memory size (in pages).
pub fn size(&self) -> u32 {
self.buffer.read().len() as u32 / LINEAR_MEMORY_PAGE_SIZE