message: Test memory limit in make_compile_time_store/make_runtime_store
diff: @@ -87,6 +87,13 @@ fn limit_to_pages(limit: Size) -> Pages {
  #[cfg(test)]
  mod tests {
  use super::*;
  + use wasmer::{ImportObject, Instance, Memory, Module};
  +
  + /// A Wasm module with an exported memory (min: 4 pages, max: none)
  + const EXPORTED_MEMORY_WAT: &str = r#"(module
  + (memory 4)
  + (export "memory" (memory 0))
  + )"#;
  #[test]
  fn limit_to_pages_works() {
  @@ -101,4 +108,71 @@ mod tests {
  assert_eq!(limit_to_pages(Size::gibi(4)), Pages(65536));
  assert_eq!(limit_to_pages(Size::gibi(5)), Pages(65536));
  }
  +
  + #[test]
  + fn make_compile_time_store_applies_memory_limit() {
  + let wasm = wat::parse_str(EXPORTED_MEMORY_WAT).unwrap();
  +
  + // No limit
  + let store = make_compile_time_store(None);
  + let module = Module::new(&store, &wasm).unwrap();
  + let module_memory = module.info().memories.last().unwrap();
  + assert_eq!(module_memory.minimum, Pages(4));
  + assert_eq!(module_memory.maximum, None);
  + let instance = Instance::new(&module, &ImportObject::new()).unwrap();
  + let instance_memory: Memory = instance
  + .exports
  + .iter()
  + .memories()
  + .map(|pair| pair.1.clone())
  + .next()
  + .unwrap();
  + assert_eq!(instance_memory.ty().minimum, Pages(4));
  + assert_eq!(instance_memory.ty().maximum, None);
  +
  + // Set limit
  + let store = make_compile_time_store(Some(Size::kibi(23 * 64)));
  + let module = Module::new(&store, &wasm).unwrap();
  + let module_memory = module.info().memories.last().unwrap();
  + assert_eq!(module_memory.minimum, Pages(4));
  + assert_eq!(module_memory.maximum, None);
  + let instance = Instance::new(&module, &ImportObject::new()).unwrap();
  + let instance_memory: Memory = instance
  + .exports
  + .iter()
  + .memories()
  + .map(|pair| pair.1.clone())
  + .next()
  + .unwrap();
  + assert_eq!(instance_memory.ty().minimum, Pages(4));
  + assert_eq!(instance_memory.ty().maximum, Some(Pages(23)));
  + }
  +
  + #[test]
  + fn make_runtime_store_applies_memory_limit() {
  + // Compile
  + let serialized = {
  + let wasm = wat::parse_str(EXPORTED_MEMORY_WAT).unwrap();
  + let store = make_compile_time_store(None);
  + let module = Module::new(&store, &wasm).unwrap();
  + module.serialize().unwrap()
  + };
  +
  + // Instantiate with limit
  + let store = make_runtime_store(Size::kibi(23 * 64));
  + let module = unsafe { Module::deserialize(&store, &serialized) }.unwrap();
  + let module_memory = module.info().memories.last().unwrap();
  + assert_eq!(module_memory.minimum, Pages(4));
  + assert_eq!(module_memory.maximum, None);
  + let instance = Instance::new(&module, &ImportObject::new()).unwrap();
  + let instance_memory: Memory = instance
  + .exports
  + .iter()
  + .memories()
  + .map(|pair| pair.1.clone())
  + .next()
  + .unwrap();
  + assert_eq!(instance_memory.ty().minimum, Pages(4));
  + assert_eq!(instance_memory.ty().maximum, Some(Pages(23)));
  + }
  }
  