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);