Skip to content

Commit c61406c

Browse files
Mark McCaskeymatklad
Mark McCaskey
authored andcommitted
Remove FuncRef registry
This backports wasmerio/wasmer#2437 to fix wasmerio/wasmer#2638 On a high-level, Store (global state for the whole wasmer runtime) used to store pointers to host functions. This was a partial implementation of the module linking proposal: for that, we need to be able to use functions *across* several instances, so we need to keep such functions in some sort of global state. This commit moves the data to the Instance. This breaks some module linking related tests, but we are not using that, and the support wasn't complete anyway. Note that the actual memory management remains the same: `Instance::imported_function_envs` is the thing that was and still is responsible for dropping data related to external functions.
1 parent 1b5b768 commit c61406c

File tree

15 files changed

+33
-199
lines changed

15 files changed

+33
-199
lines changed

lib/api/src/sys/externals/function.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ use std::sync::Arc;
1616
use wasmer_engine::{Export, ExportFunction, ExportFunctionMetadata};
1717
use wasmer_vm::{
1818
raise_user_trap, resume_panic, wasmer_call_trampoline, ImportInitializerFuncPtr,
19-
VMCallerCheckedAnyfunc, VMDynamicFunctionContext, VMFuncRef, VMFunction, VMFunctionBody,
20-
VMFunctionEnvironment, VMFunctionKind, VMTrampoline,
19+
VMDynamicFunctionContext, VMFuncRef, VMFunction, VMFunctionBody, VMFunctionEnvironment,
20+
VMFunctionKind, VMTrampoline,
2121
};
2222

2323
/// A WebAssembly `function` instance.
@@ -551,15 +551,12 @@ impl Function {
551551
}
552552
}
553553

554-
pub(crate) fn vm_funcref(&self) -> VMFuncRef {
554+
/*pub(crate) fn vm_funcref(&self) -> VMFuncRef {
555555
let engine = self.store.engine();
556556
let vmsignature = engine.register_signature(&self.exported.vm_function.signature);
557-
engine.register_function_metadata(VMCallerCheckedAnyfunc {
558-
func_ptr: self.exported.vm_function.address,
559-
type_index: vmsignature,
560-
vmctx: self.exported.vm_function.vmctx,
561-
})
562-
}
557+
// with the new changes this method is no longer possible... it should be though
558+
// so TODO: figure out how to make this work
559+
}*/
563560

564561
/// Transform this WebAssembly function into a function with the
565562
/// native ABI. See [`NativeFunc`] to learn more.

lib/api/src/sys/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl ValFuncRef for Val {
5353
}
5454
Ok(match self {
5555
Self::FuncRef(None) => VMFuncRef::null(),
56-
Self::FuncRef(Some(f)) => f.vm_funcref(),
56+
Self::FuncRef(Some(_)) => VMFuncRef::null(), //f.vm_funcref(),
5757
_ => return Err(RuntimeError::new("val is not func ref")),
5858
})
5959
}
@@ -100,7 +100,7 @@ impl ValFuncRef for Val {
100100
wasmer_vm::TableElement::ExternRef(extern_ref.clone().into())
101101
}
102102
Self::FuncRef(None) => wasmer_vm::TableElement::FuncRef(VMFuncRef::null()),
103-
Self::FuncRef(Some(f)) => wasmer_vm::TableElement::FuncRef(f.vm_funcref()),
103+
Self::FuncRef(Some(_)) => wasmer_vm::TableElement::FuncRef(VMFuncRef::null()),
104104
_ => return Err(RuntimeError::new("val is not reference")),
105105
})
106106
}

lib/engine-dylib/src/artifact.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ use wasmer_types::{
4141
SignatureIndex, TableIndex,
4242
};
4343
use wasmer_vm::{
44-
FuncDataRegistry, FunctionBodyPtr, MemoryStyle, TableStyle, VMFunctionBody,
45-
VMSharedSignatureIndex, VMTrampoline,
44+
FunctionBodyPtr, MemoryStyle, TableStyle, VMFunctionBody, VMSharedSignatureIndex, VMTrampoline,
4645
};
4746

4847
/// A compiled Wasm module, ready to be instantiated.
@@ -55,7 +54,6 @@ pub struct DylibArtifact {
5554
#[loupe(skip)]
5655
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
5756
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
58-
func_data_registry: Arc<FuncDataRegistry>,
5957
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
6058
frame_info_registration: Mutex<Option<GlobalFrameInfoRegistration>>,
6159
}
@@ -431,7 +429,6 @@ impl DylibArtifact {
431429
.into_boxed_slice(),
432430
finished_dynamic_function_trampolines: finished_dynamic_function_trampolines
433431
.into_boxed_slice(),
434-
func_data_registry: Arc::new(FuncDataRegistry::new()),
435432
signatures: signatures.into_boxed_slice(),
436433
frame_info_registration: Mutex::new(None),
437434
})
@@ -537,7 +534,6 @@ impl DylibArtifact {
537534
.into_boxed_slice(),
538535
finished_dynamic_function_trampolines: finished_dynamic_function_trampolines
539536
.into_boxed_slice(),
540-
func_data_registry: engine_inner.func_data().clone(),
541537
signatures: signatures.into_boxed_slice(),
542538
frame_info_registration: Mutex::new(None),
543539
})
@@ -819,10 +815,6 @@ impl Artifact for DylibArtifact {
819815
&self.signatures
820816
}
821817

822-
fn func_data_registry(&self) -> &FuncDataRegistry {
823-
&self.func_data_registry
824-
}
825-
826818
fn preinstantiate(&self) -> Result<(), InstantiationError> {
827819
Ok(())
828820
}

lib/engine-dylib/src/engine.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ use wasmer_engine::{Artifact, DeserializeError, Engine, EngineId, Tunables};
1313
#[cfg(feature = "compiler")]
1414
use wasmer_types::Features;
1515
use wasmer_types::FunctionType;
16-
use wasmer_vm::{
17-
FuncDataRegistry, SignatureRegistry, VMCallerCheckedAnyfunc, VMFuncRef, VMSharedSignatureIndex,
18-
};
16+
use wasmer_vm::{SignatureRegistry, VMSharedSignatureIndex};
1917

2018
/// A WebAssembly `Dylib` Engine.
2119
#[derive(Clone, MemoryUsage)]
@@ -37,7 +35,6 @@ impl DylibEngine {
3735
inner: Arc::new(Mutex::new(DylibEngineInner {
3836
compiler: Some(compiler),
3937
signatures: SignatureRegistry::new(),
40-
func_data: Arc::new(FuncDataRegistry::new()),
4138
prefixer: None,
4239
features,
4340
is_cross_compiling,
@@ -70,7 +67,6 @@ impl DylibEngine {
7067
#[cfg(feature = "compiler")]
7168
features: Features::default(),
7269
signatures: SignatureRegistry::new(),
73-
func_data: Arc::new(FuncDataRegistry::new()),
7470
prefixer: None,
7571
is_cross_compiling: false,
7672
linker: Linker::None,
@@ -124,11 +120,6 @@ impl Engine for DylibEngine {
124120
compiler.signatures().register(func_type)
125121
}
126122

127-
fn register_function_metadata(&self, func_data: VMCallerCheckedAnyfunc) -> VMFuncRef {
128-
let compiler = self.inner();
129-
compiler.func_data().register(func_data)
130-
}
131-
132123
/// Lookup a signature
133124
fn lookup_signature(&self, sig: VMSharedSignatureIndex) -> Option<FunctionType> {
134125
let compiler = self.inner();
@@ -247,11 +238,6 @@ pub struct DylibEngineInner {
247238
/// performantly.
248239
signatures: SignatureRegistry,
249240

250-
/// The backing storage of `VMFuncRef`s. This centralized store ensures that 2
251-
/// functions with the same `VMCallerCheckedAnyfunc` will have the same `VMFuncRef`.
252-
/// It also guarantees that the `VMFuncRef`s stay valid until the engine is dropped.
253-
func_data: Arc<FuncDataRegistry>,
254-
255241
/// The prefixer returns the a String to prefix each of
256242
/// the functions in the shared object generated by the `DylibEngine`,
257243
/// so we can assure no collisions.
@@ -315,11 +301,6 @@ impl DylibEngineInner {
315301
&self.signatures
316302
}
317303

318-
/// Shared func metadata registry.
319-
pub(crate) fn func_data(&self) -> &Arc<FuncDataRegistry> {
320-
&self.func_data
321-
}
322-
323304
pub(crate) fn is_cross_compiling(&self) -> bool {
324305
self.is_cross_compiling
325306
}

lib/engine-staticlib/src/artifact.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ use wasmer_types::{
2727
FunctionIndex, LocalFunctionIndex, MemoryIndex, ModuleInfo, OwnedDataInitializer,
2828
SignatureIndex, TableIndex,
2929
};
30-
use wasmer_vm::{
31-
FuncDataRegistry, FunctionBodyPtr, MemoryStyle, TableStyle, VMSharedSignatureIndex,
32-
VMTrampoline,
33-
};
30+
use wasmer_vm::{FunctionBodyPtr, MemoryStyle, TableStyle, VMSharedSignatureIndex, VMTrampoline};
3431

3532
/// A compiled wasm module, ready to be instantiated.
3633
#[derive(MemoryUsage)]
@@ -42,7 +39,6 @@ pub struct StaticlibArtifact {
4239
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
4340
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
4441
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
45-
func_data_registry: Arc<FuncDataRegistry>,
4642
/// Length of the serialized metadata
4743
metadata_length: usize,
4844
symbol_registry: ModuleMetadataSymbolRegistry,
@@ -292,7 +288,6 @@ impl StaticlibArtifact {
292288
finished_dynamic_function_trampolines: finished_dynamic_function_trampolines
293289
.into_boxed_slice(),
294290
signatures: signatures.into_boxed_slice(),
295-
func_data_registry: engine_inner.func_data().clone(),
296291
metadata_length,
297292
symbol_registry,
298293
})
@@ -332,7 +327,6 @@ impl StaticlibArtifact {
332327

333328
let engine_inner = engine.inner();
334329
let signature_registry = engine_inner.signatures();
335-
let func_data_registry = engine_inner.func_data().clone();
336330
let mut sig_map: BTreeMap<SignatureIndex, VMSharedSignatureIndex> = BTreeMap::new();
337331

338332
let num_imported_functions = metadata.compile_info.module.num_imported_functions;
@@ -412,7 +406,6 @@ impl StaticlibArtifact {
412406
finished_dynamic_function_trampolines: finished_dynamic_function_trampolines
413407
.into_boxed_slice(),
414408
signatures: signatures.into_boxed_slice(),
415-
func_data_registry,
416409
metadata_length: 0,
417410
symbol_registry,
418411
})
@@ -478,10 +471,6 @@ impl Artifact for StaticlibArtifact {
478471
&self.signatures
479472
}
480473

481-
fn func_data_registry(&self) -> &FuncDataRegistry {
482-
&self.func_data_registry
483-
}
484-
485474
fn preinstantiate(&self) -> Result<(), InstantiationError> {
486475
Ok(())
487476
}

lib/engine-staticlib/src/engine.rs

+1-20
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ use wasmer_engine::{Artifact, DeserializeError, Engine, EngineId, Tunables};
1010
#[cfg(feature = "compiler")]
1111
use wasmer_types::Features;
1212
use wasmer_types::FunctionType;
13-
use wasmer_vm::{
14-
FuncDataRegistry, SignatureRegistry, VMCallerCheckedAnyfunc, VMFuncRef, VMSharedSignatureIndex,
15-
};
13+
use wasmer_vm::{SignatureRegistry, VMSharedSignatureIndex};
1614

1715
/// A WebAssembly `Staticlib` Engine.
1816
#[derive(Clone, MemoryUsage)]
@@ -31,7 +29,6 @@ impl StaticlibEngine {
3129
inner: Arc::new(Mutex::new(StaticlibEngineInner {
3230
compiler: Some(compiler),
3331
signatures: SignatureRegistry::new(),
34-
func_data: Arc::new(FuncDataRegistry::new()),
3532
prefixer: None,
3633
features,
3734
})),
@@ -61,7 +58,6 @@ impl StaticlibEngine {
6158
#[cfg(feature = "compiler")]
6259
features: Features::default(),
6360
signatures: SignatureRegistry::new(),
64-
func_data: Arc::new(FuncDataRegistry::new()),
6561
prefixer: None,
6662
})),
6763
target: Arc::new(Target::default()),
@@ -114,11 +110,6 @@ impl Engine for StaticlibEngine {
114110
compiler.signatures().register(func_type)
115111
}
116112

117-
fn register_function_metadata(&self, func_data: VMCallerCheckedAnyfunc) -> VMFuncRef {
118-
let compiler = self.inner();
119-
compiler.func_data().register(func_data)
120-
}
121-
122113
/// Lookup a signature
123114
fn lookup_signature(&self, sig: VMSharedSignatureIndex) -> Option<FunctionType> {
124115
let compiler = self.inner();
@@ -197,11 +188,6 @@ pub struct StaticlibEngineInner {
197188
/// performantly.
198189
signatures: SignatureRegistry,
199190

200-
/// The backing storage of `VMFuncRef`s. This centralized store ensures that 2
201-
/// functions with the same `VMCallerCheckedAnyfunc` will have the same `VMFuncRef`.
202-
/// It also guarantees that the `VMFuncRef`s stay valid until the engine is dropped.
203-
func_data: Arc<FuncDataRegistry>,
204-
205191
/// The prefixer returns the a String to prefix each of the
206192
/// functions in the static object generated by the
207193
/// `StaticlibEngine`, so we can assure no collisions.
@@ -254,9 +240,4 @@ impl StaticlibEngineInner {
254240
pub fn signatures(&self) -> &SignatureRegistry {
255241
&self.signatures
256242
}
257-
258-
/// Shared func metadata registry.
259-
pub(crate) fn func_data(&self) -> &Arc<FuncDataRegistry> {
260-
&self.func_data
261-
}
262243
}

lib/engine-universal/src/artifact.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ use wasmer_types::{
2222
FunctionIndex, LocalFunctionIndex, MemoryIndex, ModuleInfo, OwnedDataInitializer,
2323
SignatureIndex, TableIndex,
2424
};
25-
use wasmer_vm::{
26-
FuncDataRegistry, FunctionBodyPtr, MemoryStyle, TableStyle, VMSharedSignatureIndex,
27-
VMTrampoline,
28-
};
25+
use wasmer_vm::{FunctionBodyPtr, MemoryStyle, TableStyle, VMSharedSignatureIndex, VMTrampoline};
2926

3027
const SERIALIZED_METADATA_LENGTH_OFFSET: usize = 22;
3128
const SERIALIZED_METADATA_CONTENT_OFFSET: usize = 32;
@@ -39,7 +36,6 @@ pub struct UniversalArtifact {
3936
finished_function_call_trampolines: BoxedSlice<SignatureIndex, VMTrampoline>,
4037
finished_dynamic_function_trampolines: BoxedSlice<FunctionIndex, FunctionBodyPtr>,
4138
signatures: BoxedSlice<SignatureIndex, VMSharedSignatureIndex>,
42-
func_data_registry: Arc<FuncDataRegistry>,
4339
frame_info_registration: Mutex<Option<GlobalFrameInfoRegistration>>,
4440
finished_function_lengths: BoxedSlice<LocalFunctionIndex, usize>,
4541
}
@@ -241,7 +237,6 @@ impl UniversalArtifact {
241237
let finished_dynamic_function_trampolines =
242238
finished_dynamic_function_trampolines.into_boxed_slice();
243239
let signatures = signatures.into_boxed_slice();
244-
let func_data_registry = inner_engine.func_data().clone();
245240

246241
Ok(Self {
247242
serializable,
@@ -251,7 +246,6 @@ impl UniversalArtifact {
251246
signatures,
252247
frame_info_registration: Mutex::new(None),
253248
finished_function_lengths,
254-
func_data_registry,
255249
})
256250
}
257251

@@ -332,9 +326,6 @@ impl Artifact for UniversalArtifact {
332326
&self.signatures
333327
}
334328

335-
fn func_data_registry(&self) -> &FuncDataRegistry {
336-
&self.func_data_registry
337-
}
338329
fn serialize(&self) -> Result<Vec<u8>, SerializeError> {
339330
// Prepend the header.
340331
let mut serialized = Self::MAGIC_HEADER.to_vec();

lib/engine-universal/src/engine.rs

+2-18
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ use wasmer_types::{
1414
Features, FunctionIndex, FunctionType, LocalFunctionIndex, ModuleInfo, SignatureIndex,
1515
};
1616
use wasmer_vm::{
17-
FuncDataRegistry, FunctionBodyPtr, SectionBodyPtr, SignatureRegistry, VMCallerCheckedAnyfunc,
18-
VMFuncRef, VMFunctionBody, VMSharedSignatureIndex, VMTrampoline,
17+
FunctionBodyPtr, SectionBodyPtr, SignatureRegistry, VMFunctionBody, VMSharedSignatureIndex,
18+
VMTrampoline,
1919
};
2020

2121
/// A WebAssembly `Universal` Engine.
@@ -36,7 +36,6 @@ impl UniversalEngine {
3636
compiler: Some(compiler),
3737
code_memory: vec![],
3838
signatures: SignatureRegistry::new(),
39-
func_data: Arc::new(FuncDataRegistry::new()),
4039
features,
4140
})),
4241
target: Arc::new(target),
@@ -64,7 +63,6 @@ impl UniversalEngine {
6463
compiler: None,
6564
code_memory: vec![],
6665
signatures: SignatureRegistry::new(),
67-
func_data: Arc::new(FuncDataRegistry::new()),
6866
features: Features::default(),
6967
})),
7068
target: Arc::new(Target::default()),
@@ -98,11 +96,6 @@ impl Engine for UniversalEngine {
9896
compiler.use_signals()
9997
}
10098

101-
fn register_function_metadata(&self, func_data: VMCallerCheckedAnyfunc) -> VMFuncRef {
102-
let compiler = self.inner();
103-
compiler.func_data().register(func_data)
104-
}
105-
10699
/// Lookup a signature
107100
fn lookup_signature(&self, sig: VMSharedSignatureIndex) -> Option<FunctionType> {
108101
let compiler = self.inner();
@@ -165,10 +158,6 @@ pub struct UniversalEngineInner {
165158
/// The signature registry is used mainly to operate with trampolines
166159
/// performantly.
167160
signatures: SignatureRegistry,
168-
/// The backing storage of `VMFuncRef`s. This centralized store ensures that 2
169-
/// functions with the same `VMCallerCheckedAnyfunc` will have the same `VMFuncRef`.
170-
/// It also guarantees that the `VMFuncRef`s stay valid until the engine is dropped.
171-
func_data: Arc<FuncDataRegistry>,
172161
}
173162

174163
impl UniversalEngineInner {
@@ -327,9 +316,4 @@ impl UniversalEngineInner {
327316
pub fn signatures(&self) -> &SignatureRegistry {
328317
&self.signatures
329318
}
330-
331-
/// Shared func metadata registry.
332-
pub(crate) fn func_data(&self) -> &Arc<FuncDataRegistry> {
333-
&self.func_data
334-
}
335319
}

0 commit comments

Comments
 (0)