1use crate::func_data_registry::VMFuncRef;
8use crate::global::Global;
9use crate::instance::Instance;
10use crate::memory::Memory;
11use crate::sig_registry::VMSharedSignatureIndex;
12use crate::table::Table;
13use crate::trap::{Trap, TrapCode};
14use crate::VMExternRef;
15use std::any::Any;
16use std::convert::TryFrom;
17use std::fmt;
18use std::ptr::{self, NonNull};
19use std::sync::Arc;
20use std::u32;
21
22#[derive(Copy, Clone, Eq)]
27pub union VMFunctionEnvironment {
28 pub vmctx: *mut VMContext,
30 pub host_env: *mut std::ffi::c_void,
32}
33
34impl VMFunctionEnvironment {
35 pub fn is_null(&self) -> bool {
37 unsafe { self.host_env.is_null() }
38 }
39}
40
41impl std::fmt::Debug for VMFunctionEnvironment {
42 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
43 f.debug_struct("VMFunctionEnvironment")
44 .field("vmctx_or_hostenv", unsafe { &self.host_env })
45 .finish()
46 }
47}
48
49impl std::cmp::PartialEq for VMFunctionEnvironment {
50 fn eq(&self, rhs: &Self) -> bool {
51 unsafe { self.host_env as usize == rhs.host_env as usize }
52 }
53}
54
55impl std::hash::Hash for VMFunctionEnvironment {
56 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
57 unsafe {
58 self.vmctx.hash(state);
59 }
60 }
61}
62
63#[derive(Debug)]
66#[repr(C)]
67pub struct FunctionExtent {
68 pub address: FunctionBodyPtr,
71 pub length: usize,
73}
74
75#[derive(Debug, Copy, Clone)]
77#[repr(C)]
78pub struct VMFunctionImport {
79 pub body: FunctionBodyPtr,
81
82 pub signature: VMSharedSignatureIndex,
84
85 pub trampoline: Option<VMTrampoline>,
87
88 pub environment: VMFunctionEnvironment,
90}
91
92#[cfg(test)]
93mod test_vmfunction_import {
94 use super::VMFunctionImport;
95 use crate::VMOffsets;
96 use memoffset::offset_of;
97 use std::mem::size_of;
98 use wasmer_types::ModuleInfo;
99
100 #[test]
101 fn check_vmfunction_import_offsets() {
102 let module = ModuleInfo::new();
103 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
104 assert_eq!(
105 size_of::<VMFunctionImport>(),
106 usize::from(offsets.size_of_vmfunction_import())
107 );
108 assert_eq!(
109 offset_of!(VMFunctionImport, body),
110 usize::from(offsets.vmfunction_import_body())
111 );
112 assert_eq!(
113 offset_of!(VMFunctionImport, environment),
114 usize::from(offsets.vmfunction_import_vmctx())
115 );
116 }
117}
118
119#[derive(Debug, Copy, Clone)]
121#[repr(C)]
122pub struct VMLocalFunction {
123 pub body: FunctionBodyPtr,
125
126 pub length: u32,
128
129 pub signature: VMSharedSignatureIndex,
131
132 pub trampoline: VMTrampoline,
134}
135
136#[repr(C)]
145pub struct VMDynamicFunctionContext<T: Sized + Send + Sync> {
146 pub address: *const VMFunctionBody,
151
152 pub ctx: T,
154}
155
156unsafe impl<T: Sized + Send + Sync> Send for VMDynamicFunctionContext<T> {}
159unsafe impl<T: Sized + Send + Sync> Sync for VMDynamicFunctionContext<T> {}
162
163impl<T: Sized + Clone + Send + Sync> Clone for VMDynamicFunctionContext<T> {
164 fn clone(&self) -> Self {
165 Self {
166 address: self.address,
167 ctx: self.ctx.clone(),
168 }
169 }
170}
171
172#[cfg(test)]
173mod test_vmdynamicfunction_import_context {
174 use super::VMDynamicFunctionContext;
175 use crate::VMOffsets;
176 use memoffset::offset_of;
177 use std::mem::size_of;
178 use wasmer_types::ModuleInfo;
179
180 #[test]
181 fn check_vmdynamicfunction_import_context_offsets() {
182 let module = ModuleInfo::new();
183 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
184 assert_eq!(
185 size_of::<VMDynamicFunctionContext<usize>>(),
186 usize::from(offsets.size_of_vmdynamicfunction_import_context())
187 );
188 assert_eq!(
189 offset_of!(VMDynamicFunctionContext<usize>, address),
190 usize::from(offsets.vmdynamicfunction_import_context_address())
191 );
192 assert_eq!(
193 offset_of!(VMDynamicFunctionContext<usize>, ctx),
194 usize::from(offsets.vmdynamicfunction_import_context_ctx())
195 );
196 }
197}
198
199#[repr(C)]
204pub struct VMFunctionBody(u8);
205
206#[cfg(test)]
207mod test_vmfunction_body {
208 use super::VMFunctionBody;
209 use std::mem::size_of;
210
211 #[test]
212 fn check_vmfunction_body_offsets() {
213 assert_eq!(size_of::<VMFunctionBody>(), 1);
214 }
215}
216
217#[derive(Clone, Copy, Debug)]
219#[repr(transparent)]
220pub struct FunctionBodyPtr(pub *const VMFunctionBody);
221
222impl std::ops::Deref for FunctionBodyPtr {
223 type Target = *const VMFunctionBody;
224
225 fn deref(&self) -> &Self::Target {
226 &self.0
227 }
228}
229
230unsafe impl Send for FunctionBodyPtr {}
233
234unsafe impl Sync for FunctionBodyPtr {}
237
238#[derive(Debug, Copy, Clone, PartialEq)]
240#[repr(C)]
241pub enum VMFunctionKind {
242 Static,
249
250 Dynamic,
256}
257
258#[derive(Debug, Clone)]
261#[repr(C)]
262pub struct VMTableImport {
263 pub definition: NonNull<VMTableDefinition>,
265
266 pub from: Arc<dyn Table>,
268}
269
270#[cfg(test)]
271mod test_vmtable_import {
272 use super::VMTableImport;
273 use crate::VMOffsets;
274 use memoffset::offset_of;
275 use std::mem::size_of;
276 use wasmer_types::ModuleInfo;
277
278 #[test]
279 fn check_vmtable_import_offsets() {
280 let module = ModuleInfo::new();
281 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
282 assert_eq!(
283 size_of::<VMTableImport>(),
284 usize::from(offsets.size_of_vmtable_import())
285 );
286 assert_eq!(
287 offset_of!(VMTableImport, definition),
288 usize::from(offsets.vmtable_import_definition())
289 );
290 assert_eq!(
291 offset_of!(VMTableImport, from),
292 usize::from(offsets.vmtable_import_from())
293 );
294 }
295}
296
297#[derive(Debug, Clone)]
300#[repr(C)]
301pub struct VMMemoryImport {
302 pub definition: NonNull<VMMemoryDefinition>,
304
305 pub from: Arc<dyn Memory>,
307}
308
309#[cfg(test)]
310mod test_vmmemory_import {
311 use super::VMMemoryImport;
312 use crate::VMOffsets;
313 use memoffset::offset_of;
314 use std::mem::size_of;
315 use wasmer_types::ModuleInfo;
316
317 #[test]
318 fn check_vmmemory_import_offsets() {
319 let module = ModuleInfo::new();
320 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
321 assert_eq!(
322 size_of::<VMMemoryImport>(),
323 usize::from(offsets.size_of_vmmemory_import())
324 );
325 assert_eq!(
326 offset_of!(VMMemoryImport, definition),
327 usize::from(offsets.vmmemory_import_definition())
328 );
329 assert_eq!(
330 offset_of!(VMMemoryImport, from),
331 usize::from(offsets.vmmemory_import_from())
332 );
333 }
334}
335
336#[derive(Debug, Clone)]
339#[repr(C)]
340pub struct VMGlobalImport {
341 pub definition: NonNull<VMGlobalDefinition>,
343
344 pub from: Arc<Global>,
346}
347
348unsafe impl Send for VMGlobalImport {}
353unsafe impl Sync for VMGlobalImport {}
359
360#[cfg(test)]
361mod test_vmglobal_import {
362 use super::VMGlobalImport;
363 use crate::VMOffsets;
364 use memoffset::offset_of;
365 use std::mem::size_of;
366 use wasmer_types::ModuleInfo;
367
368 #[test]
369 fn check_vmglobal_import_offsets() {
370 let module = ModuleInfo::new();
371 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
372 assert_eq!(
373 size_of::<VMGlobalImport>(),
374 usize::from(offsets.size_of_vmglobal_import())
375 );
376 assert_eq!(
377 offset_of!(VMGlobalImport, definition),
378 usize::from(offsets.vmglobal_import_definition())
379 );
380 assert_eq!(
381 offset_of!(VMGlobalImport, from),
382 usize::from(offsets.vmglobal_import_from())
383 );
384 }
385}
386
387#[derive(Debug, Copy, Clone)]
391#[repr(C)]
392pub struct VMMemoryDefinition {
393 pub base: *mut u8,
395
396 pub current_length: usize,
398}
399
400unsafe impl Send for VMMemoryDefinition {}
404unsafe impl Sync for VMMemoryDefinition {}
410
411impl VMMemoryDefinition {
412 pub(crate) unsafe fn memory_copy(&self, dst: u32, src: u32, len: u32) -> Result<(), Trap> {
424 if src
426 .checked_add(len)
427 .map_or(true, |n| usize::try_from(n).unwrap() > self.current_length)
428 || dst
429 .checked_add(len)
430 .map_or(true, |m| usize::try_from(m).unwrap() > self.current_length)
431 {
432 return Err(Trap::lib(TrapCode::HeapAccessOutOfBounds));
433 }
434
435 let dst = usize::try_from(dst).unwrap();
436 let src = usize::try_from(src).unwrap();
437
438 let dst = self.base.add(dst);
441 let src = self.base.add(src);
442 ptr::copy(src, dst, len as usize);
443
444 Ok(())
445 }
446
447 pub(crate) unsafe fn memory_fill(&self, dst: u32, val: u32, len: u32) -> Result<(), Trap> {
458 if dst
459 .checked_add(len)
460 .map_or(true, |m| usize::try_from(m).unwrap() > self.current_length)
461 {
462 return Err(Trap::lib(TrapCode::HeapAccessOutOfBounds));
463 }
464
465 let dst = isize::try_from(dst).unwrap();
466 let val = val as u8;
467
468 let dst = self.base.offset(dst);
471 ptr::write_bytes(dst, val, len as usize);
472
473 Ok(())
474 }
475}
476
477#[cfg(test)]
478mod test_vmmemory_definition {
479 use super::VMMemoryDefinition;
480 use crate::VMOffsets;
481 use memoffset::offset_of;
482 use std::mem::size_of;
483 use wasmer_types::ModuleInfo;
484
485 #[test]
486 fn check_vmmemory_definition_offsets() {
487 let module = ModuleInfo::new();
488 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
489 assert_eq!(
490 size_of::<VMMemoryDefinition>(),
491 usize::from(offsets.size_of_vmmemory_definition())
492 );
493 assert_eq!(
494 offset_of!(VMMemoryDefinition, base),
495 usize::from(offsets.vmmemory_definition_base())
496 );
497 assert_eq!(
498 offset_of!(VMMemoryDefinition, current_length),
499 usize::from(offsets.vmmemory_definition_current_length())
500 );
501 }
502}
503
504#[derive(Debug, Clone, Copy)]
507#[repr(C)]
508pub struct VMTableDefinition {
509 pub base: *mut u8,
511
512 pub current_elements: u32,
514}
515
516#[cfg(test)]
517mod test_vmtable_definition {
518 use super::VMTableDefinition;
519 use crate::VMOffsets;
520 use memoffset::offset_of;
521 use std::mem::size_of;
522 use wasmer_types::ModuleInfo;
523
524 #[test]
525 fn check_vmtable_definition_offsets() {
526 let module = ModuleInfo::new();
527 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
528 assert_eq!(
529 size_of::<VMTableDefinition>(),
530 usize::from(offsets.size_of_vmtable_definition())
531 );
532 assert_eq!(
533 offset_of!(VMTableDefinition, base),
534 usize::from(offsets.vmtable_definition_base())
535 );
536 assert_eq!(
537 offset_of!(VMTableDefinition, current_elements),
538 usize::from(offsets.vmtable_definition_current_elements())
539 );
540 }
541}
542
543#[derive(Clone, Copy)]
551#[repr(C, align(16))]
552pub union VMGlobalDefinitionStorage {
553 as_i32: i32,
554 as_u32: u32,
555 as_f32: f32,
556 as_i64: i64,
557 as_u64: u64,
558 as_f64: f64,
559 as_u128: u128,
560 as_funcref: VMFuncRef,
561 as_externref: VMExternRef,
562 bytes: [u8; 16],
563}
564
565impl fmt::Debug for VMGlobalDefinitionStorage {
566 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
567 f.debug_struct("VMGlobalDefinitionStorage")
568 .field("bytes", unsafe { &self.bytes })
569 .finish()
570 }
571}
572
573#[derive(Debug, Clone)]
578#[repr(C, align(16))]
579pub struct VMGlobalDefinition {
580 storage: VMGlobalDefinitionStorage,
581 }
583
584#[cfg(test)]
585mod test_vmglobal_definition {
586 use super::VMGlobalDefinition;
587 use crate::{VMFuncRef, VMOffsets};
588 use more_asserts::assert_ge;
589 use std::mem::{align_of, size_of};
590 use wasmer_types::ModuleInfo;
591
592 #[test]
593 fn check_vmglobal_definition_alignment() {
594 assert_ge!(align_of::<VMGlobalDefinition>(), align_of::<i32>());
595 assert_ge!(align_of::<VMGlobalDefinition>(), align_of::<i64>());
596 assert_ge!(align_of::<VMGlobalDefinition>(), align_of::<f32>());
597 assert_ge!(align_of::<VMGlobalDefinition>(), align_of::<f64>());
598 assert_ge!(align_of::<VMGlobalDefinition>(), align_of::<VMFuncRef>());
599 assert_ge!(align_of::<VMGlobalDefinition>(), align_of::<[u8; 16]>());
600 }
601
602 #[test]
603 fn check_vmglobal_definition_offsets() {
604 let module = ModuleInfo::new();
605 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
606 assert_eq!(
607 size_of::<*const VMGlobalDefinition>(),
608 usize::from(offsets.size_of_vmglobal_local())
609 );
610 }
611
612 #[test]
613 fn check_vmglobal_begins_aligned() {
614 let module = ModuleInfo::new();
615 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
616 assert_eq!(offsets.vmctx_globals_begin() % 16, 0);
617 }
618}
619
620impl VMGlobalDefinition {
621 pub fn new() -> Self {
623 Self {
624 storage: VMGlobalDefinitionStorage { bytes: [0; 16] },
625 }
626 }
627
628 pub fn to_i32(&self) -> i32 {
632 unsafe { self.storage.as_i32 }
633 }
634
635 pub unsafe fn as_i32_mut(&mut self) -> &mut i32 {
644 &mut self.storage.as_i32
645 }
646
647 pub fn to_u32(&self) -> u32 {
651 unsafe { self.storage.as_u32 }
652 }
653
654 pub unsafe fn as_u32_mut(&mut self) -> &mut u32 {
663 &mut self.storage.as_u32
664 }
665
666 pub fn to_i64(&self) -> i64 {
670 unsafe { self.storage.as_i64 }
671 }
672
673 pub unsafe fn as_i64_mut(&mut self) -> &mut i64 {
682 &mut self.storage.as_i64
683 }
684
685 pub fn to_u64(&self) -> u64 {
689 unsafe { self.storage.as_u64 }
690 }
691
692 pub unsafe fn as_u64_mut(&mut self) -> &mut u64 {
701 &mut self.storage.as_u64
702 }
703
704 pub fn to_f32(&self) -> f32 {
708 unsafe { self.storage.as_f32 }
709 }
710
711 pub unsafe fn as_f32_mut(&mut self) -> &mut f32 {
720 &mut self.storage.as_f32
721 }
722
723 pub fn to_f64(&self) -> f64 {
727 unsafe { self.storage.as_f64 }
728 }
729
730 pub unsafe fn as_f64_mut(&mut self) -> &mut f64 {
739 &mut self.storage.as_f64
740 }
741
742 pub fn to_funcref(&self) -> VMFuncRef {
746 unsafe { self.storage.as_funcref }
747 }
748
749 pub unsafe fn as_funcref_mut(&mut self) -> &mut VMFuncRef {
758 &mut self.storage.as_funcref
759 }
760
761 pub unsafe fn as_externref_mut(&mut self) -> &mut VMExternRef {
770 &mut self.storage.as_externref
771 }
772
773 pub fn to_externref(&self) -> VMExternRef {
777 unsafe { self.storage.as_externref }
778 }
779
780 pub fn to_u128(&self) -> u128 {
784 unsafe { self.storage.as_u128 }
785 }
786
787 pub unsafe fn as_u128_mut(&mut self) -> &mut u128 {
796 &mut self.storage.as_u128
797 }
798
799 pub fn to_bytes(&self) -> [u8; 16] {
801 unsafe { self.storage.bytes }
802 }
803
804 pub unsafe fn as_bytes_mut(&mut self) -> &mut [u8; 16] {
812 &mut self.storage.bytes
813 }
814}
815
816#[cfg(test)]
817mod test_vmshared_signature_index {
818 use super::VMSharedSignatureIndex;
819 use crate::vmoffsets::{TargetSharedSignatureIndex, VMOffsets};
820 use std::mem::size_of;
821 use wasmer_types::ModuleInfo;
822
823 #[test]
824 fn check_vmshared_signature_index() {
825 let module = ModuleInfo::new();
826 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
827 assert_eq!(
828 size_of::<VMSharedSignatureIndex>(),
829 usize::from(offsets.size_of_vmshared_signature_index())
830 );
831 }
832
833 #[test]
834 fn check_target_shared_signature_index() {
835 assert_eq!(
836 size_of::<VMSharedSignatureIndex>(),
837 size_of::<TargetSharedSignatureIndex>()
838 );
839 }
840}
841
842#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
846#[repr(C)]
847pub struct VMCallerCheckedAnyfunc {
848 pub func_ptr: *const VMFunctionBody,
850 pub type_index: VMSharedSignatureIndex,
852 pub vmctx: VMFunctionEnvironment,
854 }
856
857#[cfg(test)]
858mod test_vmcaller_checked_anyfunc {
859 use super::VMCallerCheckedAnyfunc;
860 use crate::VMOffsets;
861 use memoffset::offset_of;
862 use std::mem::size_of;
863 use wasmer_types::ModuleInfo;
864
865 #[test]
866 fn check_vmcaller_checked_anyfunc_offsets() {
867 let module = ModuleInfo::new();
868 let offsets = VMOffsets::new(size_of::<*mut u8>() as u8).with_module_info(&module);
869 assert_eq!(
870 size_of::<VMCallerCheckedAnyfunc>(),
871 usize::from(offsets.size_of_vmcaller_checked_anyfunc())
872 );
873 assert_eq!(
874 offset_of!(VMCallerCheckedAnyfunc, func_ptr),
875 usize::from(offsets.vmcaller_checked_anyfunc_func_ptr())
876 );
877 assert_eq!(
878 offset_of!(VMCallerCheckedAnyfunc, type_index),
879 usize::from(offsets.vmcaller_checked_anyfunc_type_index())
880 );
881 assert_eq!(
882 offset_of!(VMCallerCheckedAnyfunc, vmctx),
883 usize::from(offsets.vmcaller_checked_anyfunc_vmctx())
884 );
885 }
886}
887
888#[derive(Copy, Clone, Debug)]
890pub struct VMBuiltinFunctionIndex(u32);
891
892impl VMBuiltinFunctionIndex {
893 pub const fn get_memory32_grow_index() -> Self {
895 Self(0)
896 }
897 pub const fn get_imported_memory32_grow_index() -> Self {
899 Self(1)
900 }
901 pub const fn get_memory32_size_index() -> Self {
903 Self(2)
904 }
905 pub const fn get_imported_memory32_size_index() -> Self {
907 Self(3)
908 }
909 pub const fn get_table_copy_index() -> Self {
912 Self(4)
913 }
914 pub const fn get_table_init_index() -> Self {
916 Self(5)
917 }
918 pub const fn get_elem_drop_index() -> Self {
920 Self(6)
921 }
922 pub const fn get_memory_copy_index() -> Self {
924 Self(7)
925 }
926 pub const fn get_imported_memory_copy_index() -> Self {
928 Self(8)
929 }
930 pub const fn get_memory_fill_index() -> Self {
932 Self(9)
933 }
934 pub const fn get_imported_memory_fill_index() -> Self {
936 Self(10)
937 }
938 pub const fn get_memory_init_index() -> Self {
940 Self(11)
941 }
942 pub const fn get_data_drop_index() -> Self {
944 Self(12)
945 }
946 pub const fn get_raise_trap_index() -> Self {
948 Self(13)
949 }
950 pub const fn get_table_size_index() -> Self {
952 Self(14)
953 }
954 pub const fn get_imported_table_size_index() -> Self {
956 Self(15)
957 }
958 pub const fn get_table_grow_index() -> Self {
960 Self(16)
961 }
962 pub const fn get_imported_table_grow_index() -> Self {
964 Self(17)
965 }
966 pub const fn get_table_get_index() -> Self {
968 Self(18)
969 }
970 pub const fn get_imported_table_get_index() -> Self {
972 Self(19)
973 }
974 pub const fn get_table_set_index() -> Self {
976 Self(20)
977 }
978 pub const fn get_imported_table_set_index() -> Self {
980 Self(21)
981 }
982 pub const fn get_func_ref_index() -> Self {
984 Self(22)
985 }
986 pub const fn get_table_fill_index() -> Self {
988 Self(23)
989 }
990 pub const fn get_externref_inc_index() -> Self {
992 Self(24)
993 }
994 pub const fn get_externref_dec_index() -> Self {
996 Self(25)
997 }
998 pub const fn builtin_functions_total_number() -> u32 {
1000 26
1001 }
1002
1003 pub const fn index(self) -> u32 {
1005 self.0
1006 }
1007}
1008
1009#[repr(C)]
1012pub struct VMBuiltinFunctionsArray {
1013 ptrs: [usize; Self::len()],
1014}
1015
1016impl VMBuiltinFunctionsArray {
1017 pub const fn len() -> usize {
1018 VMBuiltinFunctionIndex::builtin_functions_total_number() as usize
1019 }
1020
1021 pub fn initialized() -> Self {
1022 use crate::libcalls::*;
1023
1024 let mut ptrs = [0; Self::len()];
1025
1026 ptrs[VMBuiltinFunctionIndex::get_memory32_grow_index().index() as usize] =
1027 wasmer_vm_memory32_grow as usize;
1028 ptrs[VMBuiltinFunctionIndex::get_imported_memory32_grow_index().index() as usize] =
1029 wasmer_vm_imported_memory32_grow as usize;
1030
1031 ptrs[VMBuiltinFunctionIndex::get_memory32_size_index().index() as usize] =
1032 wasmer_vm_memory32_size as usize;
1033 ptrs[VMBuiltinFunctionIndex::get_imported_memory32_size_index().index() as usize] =
1034 wasmer_vm_imported_memory32_size as usize;
1035
1036 ptrs[VMBuiltinFunctionIndex::get_table_copy_index().index() as usize] =
1037 wasmer_vm_table_copy as usize;
1038
1039 ptrs[VMBuiltinFunctionIndex::get_table_init_index().index() as usize] =
1040 wasmer_vm_table_init as usize;
1041 ptrs[VMBuiltinFunctionIndex::get_elem_drop_index().index() as usize] =
1042 wasmer_vm_elem_drop as usize;
1043
1044 ptrs[VMBuiltinFunctionIndex::get_memory_copy_index().index() as usize] =
1045 wasmer_vm_memory32_copy as usize;
1046 ptrs[VMBuiltinFunctionIndex::get_imported_memory_copy_index().index() as usize] =
1047 wasmer_vm_imported_memory32_copy as usize;
1048 ptrs[VMBuiltinFunctionIndex::get_memory_fill_index().index() as usize] =
1049 wasmer_vm_memory32_fill as usize;
1050 ptrs[VMBuiltinFunctionIndex::get_imported_memory_fill_index().index() as usize] =
1051 wasmer_vm_imported_memory32_fill as usize;
1052 ptrs[VMBuiltinFunctionIndex::get_memory_init_index().index() as usize] =
1053 wasmer_vm_memory32_init as usize;
1054 ptrs[VMBuiltinFunctionIndex::get_data_drop_index().index() as usize] =
1055 wasmer_vm_data_drop as usize;
1056 ptrs[VMBuiltinFunctionIndex::get_raise_trap_index().index() as usize] =
1057 wasmer_vm_raise_trap as usize;
1058 ptrs[VMBuiltinFunctionIndex::get_table_size_index().index() as usize] =
1059 wasmer_vm_table_size as usize;
1060 ptrs[VMBuiltinFunctionIndex::get_imported_table_size_index().index() as usize] =
1061 wasmer_vm_imported_table_size as usize;
1062 ptrs[VMBuiltinFunctionIndex::get_table_grow_index().index() as usize] =
1063 wasmer_vm_table_grow as usize;
1064 ptrs[VMBuiltinFunctionIndex::get_imported_table_grow_index().index() as usize] =
1065 wasmer_vm_imported_table_grow as usize;
1066 ptrs[VMBuiltinFunctionIndex::get_table_get_index().index() as usize] =
1067 wasmer_vm_table_get as usize;
1068 ptrs[VMBuiltinFunctionIndex::get_imported_table_get_index().index() as usize] =
1069 wasmer_vm_imported_table_get as usize;
1070 ptrs[VMBuiltinFunctionIndex::get_table_set_index().index() as usize] =
1071 wasmer_vm_table_set as usize;
1072 ptrs[VMBuiltinFunctionIndex::get_imported_table_set_index().index() as usize] =
1073 wasmer_vm_imported_table_set as usize;
1074 ptrs[VMBuiltinFunctionIndex::get_func_ref_index().index() as usize] =
1075 wasmer_vm_func_ref as usize;
1076 ptrs[VMBuiltinFunctionIndex::get_table_fill_index().index() as usize] =
1077 wasmer_vm_table_fill as usize;
1078 ptrs[VMBuiltinFunctionIndex::get_externref_inc_index().index() as usize] =
1079 wasmer_vm_externref_inc as usize;
1080 ptrs[VMBuiltinFunctionIndex::get_externref_dec_index().index() as usize] =
1081 wasmer_vm_externref_dec as usize;
1082
1083 debug_assert!(ptrs.iter().cloned().all(|p| p != 0));
1084
1085 Self { ptrs }
1086 }
1087}
1088
1089#[derive(Debug)]
1099#[repr(C, align(16))] pub struct VMContext {}
1101
1102impl VMContext {
1103 #[allow(clippy::cast_ptr_alignment)]
1109 #[inline]
1110 pub(crate) unsafe fn instance(&self) -> &Instance {
1111 &*((self as *const Self as *mut u8).offset(-Instance::vmctx_offset()) as *const Instance)
1112 }
1113
1114 #[inline]
1120 pub unsafe fn host_state(&self) -> &dyn Any {
1121 self.instance().host_state()
1122 }
1123}
1124
1125pub type VMTrampoline = unsafe extern "C" fn(
1127 *mut VMContext, *const VMFunctionBody, *mut u128, );
1131
1132#[derive(Clone, Copy, Debug)]
1134#[repr(transparent)]
1135pub struct SectionBodyPtr(pub *const u8);
1136
1137impl std::ops::Deref for SectionBodyPtr {
1138 type Target = *const u8;
1139
1140 fn deref(&self) -> &Self::Target {
1141 &self.0
1142 }
1143}