MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / test_sharing_of_shared_memory

Function test_sharing_of_shared_memory

tests/all/threads.rs:91–131  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

89#[test]
90#[cfg_attr(miri, ignore)]
91fn test_sharing_of_shared_memory() -> Result<()> {
92 let wat = r#"(module
93 (import "env" "memory" (memory 1 5 shared))
94 (func (export "first_word") (result i32) (i32.load (i32.const 0)))
95 )"#;
96 let Some(engine) = engine() else {
97 return Ok(());
98 };
99 let module = Module::new(&engine, wat)?;
100 let mut store = Store::new(&engine, ());
101 let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, 5))?;
102 let instance1 = Instance::new(&mut store, &module, &[shared_memory.clone().into()])?;
103 let instance2 = Instance::new(&mut store, &module, &[shared_memory.clone().into()])?;
104 let data = shared_memory.data();
105
106 // Modify the memory in one place.
107 unsafe {
108 *data[0].get() = 42;
109 }
110
111 // Verify that the memory is the same in all shared locations.
112 let shared_memory_first_word = i32::from_le_bytes(unsafe {
113 [
114 *data[0].get(),
115 *data[1].get(),
116 *data[2].get(),
117 *data[3].get(),
118 ]
119 });
120 let instance1_first_word = instance1
121 .get_typed_func::<(), i32>(&mut store, "first_word")?
122 .call(&mut store, ())?;
123 let instance2_first_word = instance2
124 .get_typed_func::<(), i32>(&mut store, "first_word")?
125 .call(&mut store, ())?;
126 assert_eq!(shared_memory_first_word, 42);
127 assert_eq!(instance1_first_word, 42);
128 assert_eq!(instance2_first_word, 42);
129
130 Ok(())
131}
132
133#[test]
134#[cfg_attr(miri, ignore)]

Callers

nothing calls this directly

Calls 7

OkFunction · 0.85
engineFunction · 0.70
newFunction · 0.50
cloneMethod · 0.45
dataMethod · 0.45
getMethod · 0.45
callMethod · 0.45

Tested by

no test coverage detected