Refactor the internals of traps in wasmtime_runtime (#4326)

This commit is a small refactoring of `wasmtime_runtime::Trap` and
various internals. The `Trap` structure is now a reason plus backtrace,
and the old `Trap` enum is mostly in `TrapReason` now. Additionally all
`Trap`-returning methods of `wasmtime_runtime` are changed to returning
a `TrapCode` to indicate that they never capture a backtrace. Finally
the `UnwindReason` internally now no longer duplicates the trap reasons,
instead only having two variants of "panic" and "trap".

The motivation for this commit is mostly just cleaning up trap internals
and removing the need for methods like
`wasmtime_runtime::Trap::insert_backtrace` to leave it only happening at
the `wasmtime` layer.
This commit is contained in:
Alex Crichton
2022-06-27 12:35:14 -05:00
committed by GitHub
parent 90876f717d
commit 77e06213b7
9 changed files with 81 additions and 130 deletions

View File

@@ -6,7 +6,6 @@ use crate::export::Export;
use crate::externref::VMExternRefActivationsTable;
use crate::memory::{Memory, RuntimeMemoryCreator};
use crate::table::{Table, TableElement, TableElementType};
use crate::traphandlers::Trap;
use crate::vmcontext::{
VMBuiltinFunctionsArray, VMCallerCheckedAnyfunc, VMContext, VMFunctionImport,
VMGlobalDefinition, VMGlobalImport, VMMemoryDefinition, VMMemoryImport, VMOpaqueContext,
@@ -561,7 +560,7 @@ impl Instance {
dst: u32,
src: u32,
len: u32,
) -> Result<(), Trap> {
) -> Result<(), TrapCode> {
// 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`.
@@ -583,7 +582,7 @@ impl Instance {
dst: u32,
src: u32,
len: u32,
) -> Result<(), Trap> {
) -> Result<(), TrapCode> {
// https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-table-init
let table = unsafe { &mut *self.get_table(table_index) };
@@ -593,7 +592,7 @@ impl Instance {
.and_then(|s| s.get(..usize::try_from(len).unwrap()))
{
Some(elements) => elements,
None => return Err(Trap::wasm(TrapCode::TableOutOfBounds)),
None => return Err(TrapCode::TableOutOfBounds),
};
match table.element_type() {
@@ -643,7 +642,7 @@ impl Instance {
src_index: MemoryIndex,
src: u64,
len: u64,
) -> Result<(), Trap> {
) -> Result<(), TrapCode> {
// https://webassembly.github.io/reference-types/core/exec/instructions.html#exec-memory-copy
let src_mem = self.get_memory(src_index);
@@ -665,8 +664,8 @@ impl Instance {
Ok(())
}
fn validate_inbounds(&self, max: usize, ptr: u64, len: u64) -> Result<usize, Trap> {
let oob = || Trap::wasm(TrapCode::HeapOutOfBounds);
fn validate_inbounds(&self, max: usize, ptr: u64, len: u64) -> Result<usize, TrapCode> {
let oob = || TrapCode::HeapOutOfBounds;
let end = ptr
.checked_add(len)
.and_then(|i| usize::try_from(i).ok())
@@ -689,7 +688,7 @@ impl Instance {
dst: u64,
val: u8,
len: u64,
) -> Result<(), Trap> {
) -> Result<(), TrapCode> {
let memory = self.get_memory(memory_index);
let dst = self.validate_inbounds(memory.current_length(), dst, len)?;
@@ -719,7 +718,7 @@ impl Instance {
dst: u64,
src: u32,
len: u32,
) -> Result<(), Trap> {
) -> Result<(), TrapCode> {
let range = match self.module().passive_data_map.get(&data_index).cloned() {
Some(range) if !self.dropped_data.contains(data_index) => range,
_ => 0..0,
@@ -738,7 +737,7 @@ impl Instance {
dst: u64,
src: u32,
len: u32,
) -> Result<(), Trap> {
) -> Result<(), TrapCode> {
// https://webassembly.github.io/bulk-memory-operations/core/exec/instructions.html#exec-memory-init
let memory = self.get_memory(memory_index);