From 2d2d708500a670eb07b414c3b6fcd0fc55edecdd Mon Sep 17 00:00:00 2001 From: Mackenzie Clark Date: Fri, 15 Feb 2019 13:14:42 -0800 Subject: [PATCH] Validate descriptor max on creating new table or memory (#186) --- lib/runtime-core/src/error.rs | 6 ++++++ lib/runtime-core/src/memory/mod.rs | 9 +++++++++ lib/runtime-core/src/table/mod.rs | 8 ++++++++ 3 files changed, 23 insertions(+) diff --git a/lib/runtime-core/src/error.rs b/lib/runtime-core/src/error.rs index 98f0198f4..e88a5692c 100644 --- a/lib/runtime-core/src/error.rs +++ b/lib/runtime-core/src/error.rs @@ -266,6 +266,7 @@ impl std::fmt::Display for CallError { pub enum CreationError { UnableToCreateMemory, UnableToCreateTable, + InvalidDescriptor(String), } impl PartialEq for CreationError { @@ -279,6 +280,11 @@ impl std::fmt::Display for CreationError { match self { CreationError::UnableToCreateMemory => write!(f, "Unable to Create Memory"), CreationError::UnableToCreateTable => write!(f, "Unable to Create Table"), + CreationError::InvalidDescriptor(msg) => write!( + f, + "Unable to create because the supplied descriptor is invalid: \"{}\"", + msg + ), } } } diff --git a/lib/runtime-core/src/memory/mod.rs b/lib/runtime-core/src/memory/mod.rs index 30fab393e..56d4c643b 100644 --- a/lib/runtime-core/src/memory/mod.rs +++ b/lib/runtime-core/src/memory/mod.rs @@ -63,6 +63,15 @@ impl Memory { /// # } /// ``` pub fn new(desc: MemoryDescriptor) -> Result { + if let Some(max) = desc.maximum { + if max < desc.minimum { + return Err(CreationError::InvalidDescriptor( + "Max number of memory pages is less than the minimum number of pages" + .to_string(), + )); + } + } + let variant = if !desc.shared { MemoryVariant::Unshared(UnsharedMemory::new(desc)?) } else { diff --git a/lib/runtime-core/src/table/mod.rs b/lib/runtime-core/src/table/mod.rs index db4b943c8..1bad9a3c4 100644 --- a/lib/runtime-core/src/table/mod.rs +++ b/lib/runtime-core/src/table/mod.rs @@ -50,6 +50,14 @@ impl Table { /// # } /// ``` pub fn new(desc: TableDescriptor) -> Result { + if let Some(max) = desc.maximum { + if max < desc.minimum { + return Err(CreationError::InvalidDescriptor( + "Max table size is less than the minimum size".to_string(), + )); + } + } + let mut local = vm::LocalTable { base: ptr::null_mut(), count: 0,