Skip to content

Commit

Permalink
Rename the wasmtime::runtime::vm::Instance::module method to `env_m…
Browse files Browse the repository at this point in the history
…odule` (#9261)

Because it returns the `wasmtime_environ::Module`.

This paves the way for a new method to get the `wasmtime::Module`.
  • Loading branch information
fitzgen authored Sep 16, 2024
1 parent 3926a00 commit 2bdae8b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ impl SharedMemory {
) -> Self {
#[cfg_attr(not(feature = "threads"), allow(unused_variables, unreachable_code))]
crate::runtime::vm::Instance::from_vmctx(wasmtime_export.vmctx, |handle| {
let memory_index = handle.module().memory_index(wasmtime_export.index);
let memory_index = handle.env_module().memory_index(wasmtime_export.index);
let page_size = handle.memory_page_size(memory_index);
debug_assert!(page_size.is_power_of_two());
let page_size_log2 = u8::try_from(page_size.ilog2()).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ impl<T> Store<T> {
let shim = ModuleRuntimeInfo::bare(module);
let allocator = OnDemandInstanceAllocator::default();
allocator
.validate_module(shim.module(), shim.offsets())
.validate_module(shim.env_module(), shim.offsets())
.unwrap();
let mut instance = unsafe {
allocator
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/trampoline/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ unsafe impl InstanceAllocatorImpl for SingleMemoryInstance<'_> {
) -> Result<(MemoryAllocationIndex, Memory)> {
#[cfg(debug_assertions)]
{
let module = request.runtime_info.module();
let module = request.runtime_info.env_module();
let offsets = request.runtime_info.offsets();
self.validate_module_impl(module, offsets)
.expect("should have already validated the module before allocating memory");
Expand Down
2 changes: 1 addition & 1 deletion crates/wasmtime/src/runtime/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl ModuleRuntimeInfo {
}

/// The underlying Module.
pub(crate) fn module(&self) -> &Arc<wasmtime_environ::Module> {
pub(crate) fn env_module(&self) -> &Arc<wasmtime_environ::Module> {
match self {
ModuleRuntimeInfo::Module(m) => m.env_module(),
ModuleRuntimeInfo::Bare(b) => &b.module,
Expand Down
4 changes: 2 additions & 2 deletions crates/wasmtime/src/runtime/vm/debug_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ static mut VMCTX_AND_MEMORY: (*mut VMContext, usize) = (std::ptr::null_mut(), 0)
pub unsafe extern "C" fn resolve_vmctx_memory(ptr: usize) -> *const u8 {
Instance::from_vmctx(VMCTX_AND_MEMORY.0, |handle| {
assert!(
VMCTX_AND_MEMORY.1 < handle.module().memory_plans.len(),
VMCTX_AND_MEMORY.1 < handle.env_module().memory_plans.len(),
"memory index for debugger is out of bounds"
);
let index = MemoryIndex::new(VMCTX_AND_MEMORY.1);
Expand All @@ -29,7 +29,7 @@ pub unsafe extern "C" fn resolve_vmctx_memory_ptr(p: *const u32) -> *const u8 {
);
Instance::from_vmctx(VMCTX_AND_MEMORY.0, |handle| {
assert!(
VMCTX_AND_MEMORY.1 < handle.module().memory_plans.len(),
VMCTX_AND_MEMORY.1 < handle.env_module().memory_plans.len(),
"memory index for debugger is out of bounds"
);
let index = MemoryIndex::new(VMCTX_AND_MEMORY.1);
Expand Down
70 changes: 35 additions & 35 deletions crates/wasmtime/src/runtime/vm/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ impl Instance {
}
let ptr = ptr.cast::<Instance>();

let module = req.runtime_info.module();
let module = req.runtime_info.env_module();
let dropped_elements = EntitySet::with_capacity(module.passive_elements.len());
let dropped_data = EntitySet::with_capacity(module.passive_data_map.len());

Expand Down Expand Up @@ -259,8 +259,8 @@ impl Instance {
.cast()
}

pub(crate) fn module(&self) -> &Arc<Module> {
self.runtime_info.module()
pub(crate) fn env_module(&self) -> &Arc<Module> {
self.runtime_info.env_module()
}

/// Translate a module-level interned type index into an engine-level
Expand Down Expand Up @@ -314,7 +314,7 @@ impl Instance {

/// Get a locally defined or imported memory.
pub(crate) fn get_memory(&self, index: MemoryIndex) -> VMMemoryDefinition {
if let Some(defined_index) = self.module().defined_memory_index(index) {
if let Some(defined_index) = self.env_module().defined_memory_index(index) {
self.memory(defined_index)
} else {
let import = self.imported_memory(index);
Expand All @@ -325,7 +325,7 @@ impl Instance {
/// Get a locally defined or imported memory.
#[cfg(feature = "threads")]
pub(crate) fn get_runtime_memory(&mut self, index: MemoryIndex) -> &mut Memory {
if let Some(defined_index) = self.module().defined_memory_index(index) {
if let Some(defined_index) = self.env_module().defined_memory_index(index) {
unsafe { &mut *self.get_defined_memory(defined_index) }
} else {
let import = self.imported_memory(index);
Expand Down Expand Up @@ -367,7 +367,7 @@ impl Instance {
&mut self,
index: GlobalIndex,
) -> *mut VMGlobalDefinition {
if let Some(index) = self.module().defined_global_index(index) {
if let Some(index) = self.env_module().defined_global_index(index) {
self.global_ptr(index)
} else {
self.imported_global(index).from
Expand All @@ -384,14 +384,14 @@ impl Instance {
pub fn all_globals<'a>(
&'a mut self,
) -> impl ExactSizeIterator<Item = (GlobalIndex, ExportGlobal)> + 'a {
let module = self.module().clone();
let module = self.env_module().clone();
module.globals.keys().map(move |idx| {
(
idx,
ExportGlobal {
definition: self.defined_or_imported_global_ptr(idx),
vmctx: self.vmctx(),
global: self.module().globals[idx],
global: self.env_module().globals[idx],
},
)
})
Expand All @@ -401,7 +401,7 @@ impl Instance {
pub fn defined_globals<'a>(
&'a mut self,
) -> impl ExactSizeIterator<Item = (DefinedGlobalIndex, ExportGlobal)> + 'a {
let module = self.module().clone();
let module = self.env_module().clone();
module
.globals
.keys()
Expand All @@ -411,7 +411,7 @@ impl Instance {
let global = ExportGlobal {
definition: self.global_ptr(def_idx),
vmctx: self.vmctx(),
global: self.module().globals[global_idx],
global: self.env_module().globals[global_idx],
};
(def_idx, global)
})
Expand Down Expand Up @@ -531,23 +531,23 @@ impl Instance {
}

fn get_exported_table(&mut self, index: TableIndex) -> ExportTable {
let (definition, vmctx) = if let Some(def_index) = self.module().defined_table_index(index)
{
(self.table_ptr(def_index), self.vmctx())
} else {
let import = self.imported_table(index);
(import.from, import.vmctx)
};
let (definition, vmctx) =
if let Some(def_index) = self.env_module().defined_table_index(index) {
(self.table_ptr(def_index), self.vmctx())
} else {
let import = self.imported_table(index);
(import.from, import.vmctx)
};
ExportTable {
definition,
vmctx,
table: self.module().table_plans[index].clone(),
table: self.env_module().table_plans[index].clone(),
}
}

fn get_exported_memory(&mut self, index: MemoryIndex) -> ExportMemory {
let (definition, vmctx, def_index) =
if let Some(def_index) = self.module().defined_memory_index(index) {
if let Some(def_index) = self.env_module().defined_memory_index(index) {
(self.memory_ptr(def_index), self.vmctx(), def_index)
} else {
let import = self.imported_memory(index);
Expand All @@ -556,20 +556,20 @@ impl Instance {
ExportMemory {
definition,
vmctx,
memory: self.module().memory_plans[index].clone(),
memory: self.env_module().memory_plans[index].clone(),
index: def_index,
}
}

fn get_exported_global(&mut self, index: GlobalIndex) -> ExportGlobal {
ExportGlobal {
definition: if let Some(def_index) = self.module().defined_global_index(index) {
definition: if let Some(def_index) = self.env_module().defined_global_index(index) {
self.global_ptr(def_index)
} else {
self.imported_global(index).from
},
vmctx: self.vmctx(),
global: self.module().globals[index],
global: self.env_module().globals[index],
}
}

Expand All @@ -579,7 +579,7 @@ impl Instance {
/// are export names, and the values are export declarations which can be
/// resolved `lookup_by_declaration`.
pub fn exports(&self) -> wasmparser::collections::index_map::Iter<String, EntityIndex> {
self.module().exports.iter()
self.env_module().exports.iter()
}

/// Return a reference to the custom state attached to this instance.
Expand All @@ -603,7 +603,7 @@ impl Instance {

/// Get the given memory's page size, in bytes.
pub(crate) fn memory_page_size(&self, index: MemoryIndex) -> usize {
usize::try_from(self.module().memory_plans[index].memory.page_size()).unwrap()
usize::try_from(self.env_module().memory_plans[index].memory.page_size()).unwrap()
}

/// Grow memory by the specified amount of pages.
Expand All @@ -616,7 +616,7 @@ impl Instance {
index: MemoryIndex,
delta: u64,
) -> Result<Option<usize>, Error> {
match self.module().defined_memory_index(index) {
match self.env_module().defined_memory_index(index) {
Some(idx) => self.defined_memory_grow(idx, delta),
None => {
let import = self.imported_memory(index);
Expand Down Expand Up @@ -721,7 +721,7 @@ impl Instance {
*base.add(sig.index())
};

let func_ref = if let Some(def_index) = self.module().defined_func_index(index) {
let func_ref = if let Some(def_index) = self.env_module().defined_func_index(index) {
VMFuncRef {
array_call: self
.runtime_info
Expand Down Expand Up @@ -786,7 +786,7 @@ impl Instance {
// expensive, so it's better for instantiation performance
// if we don't have to track "is-initialized" state at
// all!
let func = &self.module().functions[index];
let func = &self.env_module().functions[index];
let sig = func.signature;
let func_ref: *mut VMFuncRef = self
.vmctx_plus_offset_mut::<VMFuncRef>(self.offsets().vmctx_func_ref(func.func_ref));
Expand Down Expand Up @@ -814,7 +814,7 @@ impl Instance {
// TODO: this `clone()` shouldn't be necessary but is used for now to
// inform `rustc` that the lifetime of the elements here are
// disconnected from the lifetime of `self`.
let module = self.module().clone();
let module = self.env_module().clone();

// NB: fall back to an expressions-based list of elements which doesn't
// have static type information (as opposed to `Functions`) since we
Expand Down Expand Up @@ -846,7 +846,7 @@ impl Instance {
let table = unsafe { &mut *self.get_table(table_index) };
let src = usize::try_from(src).map_err(|_| Trap::TableOutOfBounds)?;
let len = usize::try_from(len).map_err(|_| Trap::TableOutOfBounds)?;
let module = self.module().clone();
let module = self.env_module().clone();

match elements {
TableSegmentElements::Functions(funcs) => {
Expand Down Expand Up @@ -1015,7 +1015,7 @@ impl Instance {
src: u32,
len: u32,
) -> Result<(), Trap> {
let range = match self.module().passive_data_map.get(&data_index).cloned() {
let range = match self.env_module().passive_data_map.get(&data_index).cloned() {
Some(range) if !self.dropped_data.contains(data_index) => range,
_ => 0..0,
};
Expand Down Expand Up @@ -1117,7 +1117,7 @@ impl Instance {
// determine the function that is going to be initialized. Note
// that `i` may be outside the limits of the static
// initialization so it's a fallible `get` instead of an index.
let module = self.module();
let module = self.env_module();
let precomputed = match &module.table_initialization.initial_values[idx] {
TableInitialValue::Null { precomputed } => precomputed,
TableInitialValue::Expr(_) => unreachable!(),
Expand Down Expand Up @@ -1155,7 +1155,7 @@ impl Instance {
index: TableIndex,
f: impl FnOnce(DefinedTableIndex, &mut Instance) -> R,
) -> R {
if let Some(defined_table_index) = self.module().defined_table_index(index) {
if let Some(defined_table_index) = self.env_module().defined_table_index(index) {
f(defined_table_index, self)
} else {
let import = self.imported_table(index);
Expand All @@ -1181,7 +1181,7 @@ impl Instance {
store: StorePtr,
imports: Imports,
) {
assert!(ptr::eq(module, self.module().as_ref()));
assert!(ptr::eq(module, self.env_module().as_ref()));

*self.vmctx_plus_offset_mut(offsets.ptr.vmctx_magic()) = VMCONTEXT_MAGIC;
self.set_callee(None);
Expand Down Expand Up @@ -1328,7 +1328,7 @@ impl InstanceHandle {

/// Return a reference to a module.
pub fn module(&self) -> &Arc<Module> {
self.instance().module()
self.instance().env_module()
}

/// Lookup a function by index.
Expand Down Expand Up @@ -1387,7 +1387,7 @@ impl InstanceHandle {
index: DefinedTableIndex,
range: impl Iterator<Item = u64>,
) -> *mut Table {
let index = self.instance().module().table_index(index);
let index = self.instance().env_module().table_index(index);
self.instance_mut().get_table_with_lazy_init(index, range)
}

Expand Down
13 changes: 8 additions & 5 deletions crates/wasmtime/src/runtime/vm/instance/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ pub trait InstanceAllocator: InstanceAllocatorImpl {
&self,
mut request: InstanceAllocationRequest,
) -> Result<InstanceHandle> {
let module = request.runtime_info.module();
let module = request.runtime_info.env_module();

#[cfg(debug_assertions)]
InstanceAllocatorImpl::validate_module_impl(self, module, request.runtime_info.offsets())
Expand Down Expand Up @@ -438,7 +438,7 @@ pub trait InstanceAllocator: InstanceAllocatorImpl {
request: &mut InstanceAllocationRequest,
memories: &mut PrimaryMap<DefinedMemoryIndex, (MemoryAllocationIndex, Memory)>,
) -> Result<()> {
let module = request.runtime_info.module();
let module = request.runtime_info.env_module();

#[cfg(debug_assertions)]
InstanceAllocatorImpl::validate_module_impl(self, module, request.runtime_info.offsets())
Expand Down Expand Up @@ -490,7 +490,7 @@ pub trait InstanceAllocator: InstanceAllocatorImpl {
request: &mut InstanceAllocationRequest,
tables: &mut PrimaryMap<DefinedTableIndex, (TableAllocationIndex, Table)>,
) -> Result<()> {
let module = request.runtime_info.module();
let module = request.runtime_info.env_module();

#[cfg(debug_assertions)]
InstanceAllocatorImpl::validate_module_impl(self, module, request.runtime_info.offsets())
Expand Down Expand Up @@ -636,7 +636,7 @@ fn get_memory_init_start(
let mut context = ConstEvalContext::new(instance, module);
let mut const_evaluator = ConstExprEvaluator::default();
unsafe { const_evaluator.eval(&mut context, &init.offset) }.map(|v| {
match instance.module().memory_plans[init.memory_index]
match instance.env_module().memory_plans[init.memory_index]
.memory
.idx_type
{
Expand Down Expand Up @@ -706,7 +706,10 @@ fn initialize_memories(instance: &mut Instance, module: &Module) -> Result<()> {
let val = unsafe { self.const_evaluator.eval(&mut context, expr) }
.expect("const expression should be valid");
Some(
match self.instance.module().memory_plans[memory].memory.idx_type {
match self.instance.env_module().memory_plans[memory]
.memory
.idx_type
{
wasmtime_environ::IndexType::I32 => val.get_u32().into(),
wasmtime_environ::IndexType::I64 => val.get_u64(),
},
Expand Down

0 comments on commit 2bdae8b

Please sign in to comment.