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

Method initialize_vmctx

crates/wasmtime/src/runtime/vm/instance.rs:1180–1420  ·  view source on GitHub ↗

Initialize the VMContext data associated with this Instance. The `VMContext` memory is assumed to be uninitialized; any field that we need in a certain state will be explicitly written by this function.

(self: Pin<&mut Self>, store: &StoreOpaque, imports: Imports)

Source from the content-addressed store, hash-verified

1178 /// that we need in a certain state will be explicitly written by this
1179 /// function.
1180 unsafe fn initialize_vmctx(self: Pin<&mut Self>, store: &StoreOpaque, imports: Imports) {
1181 let (module, mut instance) = self.module_and_self();
1182
1183 // SAFETY: the type of the magic field is indeed `u32` and this function
1184 // is initializing its value.
1185 unsafe {
1186 let offsets = instance.runtime_info.offsets();
1187 instance
1188 .vmctx_plus_offset_raw::<u32>(offsets.ptr.vmctx_magic())
1189 .write(VMCONTEXT_MAGIC);
1190 }
1191
1192 // SAFETY: it's up to the caller to provide a valid store pointer here.
1193 unsafe {
1194 instance.as_mut().set_store(store);
1195 }
1196
1197 // Initialize shared types
1198 //
1199 // SAFETY: validity of the vmctx means it should be safe to write to it
1200 // here.
1201 unsafe {
1202 let types = NonNull::from(instance.runtime_info.type_ids());
1203 instance.type_ids_array().write(types.cast().into());
1204 }
1205
1206 // Initialize the built-in functions
1207 //
1208 // SAFETY: the type of the builtin functions field is indeed a pointer
1209 // and the pointer being filled in here, plus the vmctx is valid to
1210 // write to during initialization.
1211 unsafe {
1212 static BUILTINS: VMBuiltinFunctionsArray = VMBuiltinFunctionsArray::INIT;
1213 let ptr = BUILTINS.expose_provenance();
1214 let offsets = instance.runtime_info.offsets();
1215 instance
1216 .vmctx_plus_offset_raw(offsets.ptr.vmctx_builtin_functions())
1217 .write(VmPtr::from(ptr));
1218 }
1219
1220 // Initialize the imports
1221 //
1222 // SAFETY: the vmctx is safe to initialize during this function and
1223 // validity of each item itself is a contract the caller must uphold.
1224 debug_assert_eq!(imports.functions.len(), module.num_imported_funcs);
1225 unsafe {
1226 let offsets = instance.runtime_info.offsets();
1227 ptr::copy_nonoverlapping(
1228 imports.functions.as_ptr(),
1229 instance
1230 .vmctx_plus_offset_raw(offsets.vmctx_imported_functions_begin())
1231 .as_ptr(),
1232 imports.functions.len(),
1233 );
1234 debug_assert_eq!(imports.tables.len(), module.num_imported_tables);
1235 ptr::copy_nonoverlapping(
1236 imports.tables.as_ptr(),
1237 instance

Callers 1

newMethod · 0.45

Calls 15

fromFunction · 0.85
module_and_selfMethod · 0.80
vmctx_magicMethod · 0.80
set_storeMethod · 0.80
type_idsMethod · 0.80
type_ids_arrayMethod · 0.80
vmctx_plus_offset_rawMethod · 0.80

Tested by

no test coverage detected