From da134efb8015d2f69bc897907d67e9600a3d5365 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 18 Oct 2023 13:10:39 +0200 Subject: [PATCH 1/2] Pull out testing wats --- packages/vm/src/modules/in_memory_cache.rs | 99 +++++++--------------- 1 file changed, 29 insertions(+), 70 deletions(-) diff --git a/packages/vm/src/modules/in_memory_cache.rs b/packages/vm/src/modules/in_memory_cache.rs index 95991c0e5a..7bc596bb72 100644 --- a/packages/vm/src/modules/in_memory_cache.rs +++ b/packages/vm/src/modules/in_memory_cache.rs @@ -117,6 +117,28 @@ mod tests { // Based on `examples/module_size.sh` const TESTING_WASM_SIZE_FACTOR: usize = 18; + const WAT1: &str = r#"(module + (type $t0 (func (param i32) (result i32))) + (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) + get_local $p0 + i32.const 1 + i32.add) + )"#; + const WAT2: &str = r#"(module + (type $t0 (func (param i32) (result i32))) + (func $add_one (export "add_two") (type $t0) (param $p0 i32) (result i32) + get_local $p0 + i32.const 2 + i32.add) + )"#; + const WAT3: &str = r#"(module + (type $t0 (func (param i32) (result i32))) + (func $add_one (export "add_three") (type $t0) (param $p0 i32) (result i32) + get_local $p0 + i32.const 3 + i32.add) + )"#; + #[test] fn check_element_sizes() { let key_size = mem::size_of::(); @@ -135,16 +157,7 @@ mod tests { let mut cache = InMemoryCache::new(Size::mebi(200)); // Create module - let wasm = wat::parse_str( - r#"(module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 1 - i32.add) - )"#, - ) - .unwrap(); + let wasm = wat::parse_str(WAT1).unwrap(); let checksum = Checksum::generate(&wasm); // Module does not exist @@ -188,38 +201,11 @@ mod tests { let mut cache = InMemoryCache::new(Size::mebi(2)); // Create module - let wasm1 = wat::parse_str( - r#"(module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 1 - i32.add) - )"#, - ) - .unwrap(); + let wasm1 = wat::parse_str(WAT1).unwrap(); let checksum1 = Checksum::generate(&wasm1); - let wasm2 = wat::parse_str( - r#"(module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_two") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 2 - i32.add) - )"#, - ) - .unwrap(); + let wasm2 = wat::parse_str(WAT2).unwrap(); let checksum2 = Checksum::generate(&wasm2); - let wasm3 = wat::parse_str( - r#"(module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_three") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 3 - i32.add) - )"#, - ) - .unwrap(); + let wasm3 = wat::parse_str(WAT3).unwrap(); let checksum3 = Checksum::generate(&wasm3); assert_eq!(cache.len(), 0); @@ -248,38 +234,11 @@ mod tests { let mut cache = InMemoryCache::new(Size::mebi(2)); // Create module - let wasm1 = wat::parse_str( - r#"(module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_one") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 1 - i32.add) - )"#, - ) - .unwrap(); + let wasm1 = wat::parse_str(WAT1).unwrap(); let checksum1 = Checksum::generate(&wasm1); - let wasm2 = wat::parse_str( - r#"(module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_two") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 2 - i32.add) - )"#, - ) - .unwrap(); + let wasm2 = wat::parse_str(WAT2).unwrap(); let checksum2 = Checksum::generate(&wasm2); - let wasm3 = wat::parse_str( - r#"(module - (type $t0 (func (param i32) (result i32))) - (func $add_one (export "add_three") (type $t0) (param $p0 i32) (result i32) - get_local $p0 - i32.const 3 - i32.add) - )"#, - ) - .unwrap(); + let wasm3 = wat::parse_str(WAT3).unwrap(); let checksum3 = Checksum::generate(&wasm3); assert_eq!(cache.size(), 0); From d6ad28feb6df6b7d6bc11ae5b496901848f10182 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 18 Oct 2023 13:11:27 +0200 Subject: [PATCH 2/2] Test zero-size InMemoryCache --- packages/vm/src/modules/in_memory_cache.rs | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/packages/vm/src/modules/in_memory_cache.rs b/packages/vm/src/modules/in_memory_cache.rs index 7bc596bb72..0fa221c130 100644 --- a/packages/vm/src/modules/in_memory_cache.rs +++ b/packages/vm/src/modules/in_memory_cache.rs @@ -261,4 +261,39 @@ mod tests { cache.store(&checksum3, module, 1_500_000).unwrap(); assert_eq!(cache.size(), 1_500_032); } + + #[test] + fn in_memory_cache_works_for_zero_size() { + // A cache size of 0 practically disabled the cache. It must work + // like any cache with insufficient space. + // We test all common methods here. + + let mut cache = InMemoryCache::new(Size::mebi(0)); + + // Create module + let wasm = wat::parse_str(WAT1).unwrap(); + let checksum = Checksum::generate(&wasm); + + // Module does not exist + let cache_entry = cache.load(&checksum).unwrap(); + assert!(cache_entry.is_none()); + assert_eq!(cache.len(), 0); + assert_eq!(cache.size(), 0); + + // Compile module + let engine = make_compiling_engine(TESTING_MEMORY_LIMIT); + let original = compile(&engine, &wasm).unwrap(); + + // Store module + let size = wasm.len() * TESTING_WASM_SIZE_FACTOR; + cache.store(&checksum, original, size).unwrap(); + assert_eq!(cache.len(), 0); + assert_eq!(cache.size(), 0); + + // Load module + let cached = cache.load(&checksum).unwrap(); + assert!(cached.is_none()); + assert_eq!(cache.len(), 0); + assert_eq!(cache.size(), 0); + } }