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

Function deserialize_raw_avoids_copy

tests/all/module.rs:589–621  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

587#[test]
588#[cfg_attr(miri, ignore)]
589fn deserialize_raw_avoids_copy() {
590 // target pulley; executing code directly requires virtual memory
591 let mut config = Config::new();
592 let target = format!("{}", Triple::pulley_host());
593 config.target(&target).unwrap();
594 let engine = Engine::new(&config).unwrap();
595 let wat = String::from(
596 r#"
597 (module
598 (func (export "add") (param $lhs i32) (param $rhs i32) (result i32)
599 (i32.add (local.get $lhs) (local.get $rhs))
600 )
601 )
602 "#,
603 );
604 let module = Module::new(&engine, &wat).unwrap();
605 let mut serialized = module.serialize().expect("Serialize failed");
606 let serialized_ptr = std::ptr::slice_from_raw_parts(serialized.as_mut_ptr(), serialized.len());
607 let module_memory = std::ptr::NonNull::new(serialized_ptr.cast_mut()).unwrap();
608 let deserialized_module =
609 unsafe { Module::deserialize_raw(&engine, module_memory).expect("Deserialize Failed") };
610
611 // assert that addresses haven't changed, no full copy
612 // TODO: haven't been able to find a pub way of doing this
613
614 // basic verification that the loaded module works
615 let mut store = Store::new(&engine, ());
616 let instance = Instance::new(&mut store, &deserialized_module, &[]).unwrap();
617 let f = instance
618 .get_typed_func::<(i32, i32), i32>(&mut store, "add")
619 .unwrap();
620 assert_eq!(f.call(&mut store, (26, 50)).unwrap(), 76);
621}
622
623#[test]
624#[cfg_attr(miri, ignore)]

Callers

nothing calls this directly

Calls 9

fromFunction · 0.85
slice_from_raw_partsFunction · 0.85
newFunction · 0.50
unwrapMethod · 0.45
targetMethod · 0.45
expectMethod · 0.45
serializeMethod · 0.45
as_mut_ptrMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected