Deduplicate listings of traps in Wasmtime (#5299)

This commit replaces `wasmtime_environ::TrapCode` with `wasmtime::Trap`.
This is possible with past refactorings which slimmed down the `Trap`
definition in the `wasmtime` crate to a simple `enum`. This means that
there's one less place that all the various trap opcodes need to be
listed in Wasmtime.
This commit is contained in:
Alex Crichton
2022-11-18 16:04:38 -06:00
committed by GitHub
parent 9b7c5e316d
commit 7a31c5b07c
14 changed files with 178 additions and 248 deletions

View File

@@ -30,7 +30,7 @@ use wasmtime_environ::{
packed_option::ReservedValue, DataIndex, DefinedGlobalIndex, DefinedMemoryIndex,
DefinedTableIndex, ElemIndex, EntityIndex, EntityRef, EntitySet, FuncIndex, GlobalIndex,
GlobalInit, HostPtr, MemoryIndex, Module, PrimaryMap, SignatureIndex, TableIndex,
TableInitialization, TrapCode, VMOffsets, WasmType,
TableInitialization, Trap, VMOffsets, WasmType,
};
mod allocator;
@@ -580,7 +580,7 @@ impl Instance {
dst: u32,
src: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
// 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`.
@@ -602,7 +602,7 @@ impl Instance {
dst: u32,
src: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
// https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-table-init
let table = unsafe { &mut *self.get_table(table_index) };
@@ -612,7 +612,7 @@ impl Instance {
.and_then(|s| s.get(..usize::try_from(len).unwrap()))
{
Some(elements) => elements,
None => return Err(TrapCode::TableOutOfBounds),
None => return Err(Trap::TableOutOfBounds),
};
match table.element_type() {
@@ -662,7 +662,7 @@ impl Instance {
src_index: MemoryIndex,
src: u64,
len: u64,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
// https://webassembly.github.io/reference-types/core/exec/instructions.html#exec-memory-copy
let src_mem = self.get_memory(src_index);
@@ -684,8 +684,8 @@ impl Instance {
Ok(())
}
fn validate_inbounds(&self, max: usize, ptr: u64, len: u64) -> Result<usize, TrapCode> {
let oob = || TrapCode::HeapOutOfBounds;
fn validate_inbounds(&self, max: usize, ptr: u64, len: u64) -> Result<usize, Trap> {
let oob = || Trap::MemoryOutOfBounds;
let end = ptr
.checked_add(len)
.and_then(|i| usize::try_from(i).ok())
@@ -708,7 +708,7 @@ impl Instance {
dst: u64,
val: u8,
len: u64,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let memory = self.get_memory(memory_index);
let dst = self.validate_inbounds(memory.current_length(), dst, len)?;
@@ -738,7 +738,7 @@ impl Instance {
dst: u64,
src: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let range = match self.module().passive_data_map.get(&data_index).cloned() {
Some(range) if !self.dropped_data.contains(data_index) => range,
_ => 0..0,
@@ -757,7 +757,7 @@ impl Instance {
dst: u64,
src: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
// https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-memory-init
let memory = self.get_memory(memory_index);

View File

@@ -13,8 +13,8 @@ use std::sync::Arc;
use thiserror::Error;
use wasmtime_environ::{
DefinedMemoryIndex, DefinedTableIndex, HostPtr, InitMemory, MemoryInitialization,
MemoryInitializer, Module, PrimaryMap, TableInitialization, TableInitializer, TrapCode,
VMOffsets, WasmType, WASM_PAGE_SIZE,
MemoryInitializer, Module, PrimaryMap, TableInitialization, TableInitializer, Trap, VMOffsets,
WasmType, WASM_PAGE_SIZE,
};
#[cfg(feature = "pooling-allocator")]
@@ -105,7 +105,7 @@ pub enum InstantiationError {
/// A trap ocurred during instantiation, after linking.
#[error("Trap occurred during instantiation")]
Trap(TrapCode),
Trap(Trap),
/// A limit on how many instances are supported has been reached.
#[error("Limit of {0} concurrent instances has been reached")]
@@ -386,7 +386,7 @@ fn initialize_memories(instance: &mut Instance, module: &Module) -> Result<(), I
},
);
if !ok {
return Err(InstantiationError::Trap(TrapCode::HeapOutOfBounds));
return Err(InstantiationError::Trap(Trap::MemoryOutOfBounds));
}
Ok(())

View File

@@ -63,7 +63,7 @@ use anyhow::Result;
use std::mem;
use std::ptr::{self, NonNull};
use wasmtime_environ::{
DataIndex, ElemIndex, FuncIndex, GlobalIndex, MemoryIndex, TableIndex, TrapCode,
DataIndex, ElemIndex, FuncIndex, GlobalIndex, MemoryIndex, TableIndex, Trap,
};
/// Actually public trampolines which are used by the runtime as the entrypoint
@@ -228,7 +228,7 @@ unsafe fn table_fill(
// `VMCallerCheckedAnyfunc` until we look at the table's element type.
val: *mut u8,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let instance = (*vmctx).instance_mut();
let table_index = TableIndex::from_u32(table_index);
let table = &mut *instance.get_table(table_index);
@@ -259,7 +259,7 @@ unsafe fn table_copy(
dst: u32,
src: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let dst_table_index = TableIndex::from_u32(dst_table_index);
let src_table_index = TableIndex::from_u32(src_table_index);
let instance = (*vmctx).instance_mut();
@@ -278,7 +278,7 @@ unsafe fn table_init(
dst: u32,
src: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let table_index = TableIndex::from_u32(table_index);
let elem_index = ElemIndex::from_u32(elem_index);
let instance = (*vmctx).instance_mut();
@@ -300,7 +300,7 @@ unsafe fn memory_copy(
src_index: u32,
src: u64,
len: u64,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let src_index = MemoryIndex::from_u32(src_index);
let dst_index = MemoryIndex::from_u32(dst_index);
let instance = (*vmctx).instance_mut();
@@ -314,7 +314,7 @@ unsafe fn memory_fill(
dst: u64,
val: u32,
len: u64,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let memory_index = MemoryIndex::from_u32(memory_index);
let instance = (*vmctx).instance_mut();
instance.memory_fill(memory_index, dst, val as u8, len)
@@ -328,7 +328,7 @@ unsafe fn memory_init(
dst: u64,
src: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
let memory_index = MemoryIndex::from_u32(memory_index);
let data_index = DataIndex::from_u32(data_index);
let instance = (*vmctx).instance_mut();
@@ -498,14 +498,14 @@ unsafe fn validate_atomic_addr(
addr: u64,
access_size: u64,
access_alignment: u64,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
debug_assert!(access_alignment.is_power_of_two());
ensure!(addr % access_alignment == 0, TrapCode::HeapMisaligned);
ensure!(addr % access_alignment == 0, Trap::HeapMisaligned);
let length = u64::try_from(instance.get_memory(memory).current_length()).unwrap();
ensure!(
addr.saturating_add(access_size) < length,
TrapCode::HeapOutOfBounds
Trap::MemoryOutOfBounds
);
Ok(())

View File

@@ -8,7 +8,7 @@ use anyhow::{bail, format_err, Error, Result};
use std::convert::{TryFrom, TryInto};
use std::ops::Range;
use std::ptr;
use wasmtime_environ::{TablePlan, TrapCode, WasmType, FUNCREF_INIT_BIT, FUNCREF_MASK};
use wasmtime_environ::{TablePlan, Trap, WasmType, FUNCREF_INIT_BIT, FUNCREF_MASK};
/// An element going into or coming out of a table.
///
@@ -267,7 +267,7 @@ impl Table {
&mut self,
dst: u32,
items: impl ExactSizeIterator<Item = *mut VMCallerCheckedAnyfunc>,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
assert!(self.element_type() == TableElementType::Func);
let elements = match self
@@ -276,7 +276,7 @@ impl Table {
.and_then(|s| s.get_mut(..items.len()))
{
Some(elements) => elements,
None => return Err(TrapCode::TableOutOfBounds),
None => return Err(Trap::TableOutOfBounds),
};
for (item, slot) in items.zip(elements) {
@@ -290,14 +290,14 @@ impl Table {
/// Fill `table[dst..dst + len]` with `val`.
///
/// Returns a trap error on out-of-bounds accesses.
pub fn fill(&mut self, dst: u32, val: TableElement, len: u32) -> Result<(), TrapCode> {
pub fn fill(&mut self, dst: u32, val: TableElement, len: u32) -> Result<(), Trap> {
let start = dst as usize;
let end = start
.checked_add(len as usize)
.ok_or_else(|| TrapCode::TableOutOfBounds)?;
.ok_or_else(|| Trap::TableOutOfBounds)?;
if end > self.size() as usize {
return Err(TrapCode::TableOutOfBounds);
return Err(Trap::TableOutOfBounds);
}
debug_assert!(self.type_matches(&val));
@@ -412,7 +412,7 @@ impl Table {
dst_index: u32,
src_index: u32,
len: u32,
) -> Result<(), TrapCode> {
) -> Result<(), Trap> {
// https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-table-copy
if src_index
@@ -422,7 +422,7 @@ impl Table {
.checked_add(len)
.map_or(true, |m| m > (*dst_table).size())
{
return Err(TrapCode::TableOutOfBounds);
return Err(Trap::TableOutOfBounds);
}
debug_assert!(

View File

@@ -10,7 +10,6 @@ use std::cell::{Cell, UnsafeCell};
use std::mem::MaybeUninit;
use std::ptr;
use std::sync::Once;
use wasmtime_environ::TrapCode;
pub use self::backtrace::Backtrace;
pub use self::tls::{tls_eager_initialize, TlsRestore};
@@ -112,7 +111,7 @@ pub unsafe fn raise_user_trap(error: Error, needs_backtrace: bool) -> ! {
/// Only safe to call when wasm code is on the stack, aka `catch_traps` must
/// have been previously called. Additionally no Rust destructors can be on the
/// stack. They will be skipped and not executed.
pub unsafe fn raise_lib_trap(trap: TrapCode) -> ! {
pub unsafe fn raise_lib_trap(trap: wasmtime_environ::Trap) -> ! {
raise_trap(TrapReason::Wasm(trap))
}
@@ -153,7 +152,7 @@ pub enum TrapReason {
Jit(usize),
/// A trap raised from a wasm libcall
Wasm(TrapCode),
Wasm(wasmtime_environ::Trap),
}
impl TrapReason {
@@ -185,8 +184,8 @@ impl From<Error> for TrapReason {
}
}
impl From<TrapCode> for TrapReason {
fn from(code: TrapCode) -> Self {
impl From<wasmtime_environ::Trap> for TrapReason {
fn from(code: wasmtime_environ::Trap) -> Self {
TrapReason::Wasm(code)
}
}