MCPcopy Create free account
hub / github.com/bytecodealliance/wasmtime / trap

Method trap

crates/wasmtime/src/runtime/vm/interpreter.rs:457–500  ·  view source on GitHub ↗

Handles an interpreter trap. This will initialize the trap state stored in TLS via the `test_if_trap` helper below by reading the pc/fp of the interpreter and seeing if that's a valid opcode to trap at.

(&mut self, pc: NonNull<u8>, kind: Option<TrapKind>)

Source from the content-addressed store, hash-verified

455 /// in TLS via the `test_if_trap` helper below by reading the pc/fp of the
456 /// interpreter and seeing if that's a valid opcode to trap at.
457 fn trap(&mut self, pc: NonNull<u8>, kind: Option<TrapKind>) -> NonNull<u8> {
458 let regs = TrapRegisters {
459 pc: pc.as_ptr() as usize,
460 fp: self.vm().fp() as usize,
461 };
462 let handler = tls::with(|s| {
463 let s = s.unwrap();
464 match kind {
465 Some(kind) => {
466 let trap = match kind {
467 TrapKind::IntegerOverflow => Trap::IntegerOverflow.into(),
468 TrapKind::DivideByZero => Trap::IntegerDivisionByZero.into(),
469 TrapKind::BadConversionToInteger => Trap::BadConversionToInteger.into(),
470 TrapKind::MemoryOutOfBounds => Trap::MemoryOutOfBounds.into(),
471 TrapKind::DisabledOpcode => Trap::DisabledOpcode.into(),
472 TrapKind::StackOverflow => Trap::StackOverflow.into(),
473 };
474 s.set_jit_trap(regs, None, trap);
475 s.entry_trap_handler()
476 }
477 None => {
478 match s.test_if_trap(regs, None, |_| false) {
479 // This shouldn't be possible, so this is a fatal error
480 // if it happens.
481 TrapTest::NotWasm => {
482 panic!("pulley trap at {pc:?} without trap code registered")
483 }
484
485 // Not possible with our closure above returning `false`.
486 #[cfg(has_native_signals)]
487 TrapTest::HandledByEmbedder => unreachable!(),
488
489 // Trap was handled, yay! Configure interpreter state
490 // to resume at the exception handler.
491 TrapTest::Trap(handler) => handler,
492 }
493 }
494 }
495 });
496 unsafe {
497 self.resume_to_exception_handler(&handler, 0, 0);
498 }
499 self.take_resume_at_pc()
500 }
501
502 fn take_resume_at_pc(&mut self) -> NonNull<u8> {
503 let pc = self.vm_state().resume_at_pc.take().unwrap();

Callers 1

callMethod · 0.45

Calls 10

withFunction · 0.85
vmMethod · 0.80
set_jit_trapMethod · 0.80
entry_trap_handlerMethod · 0.80
test_if_trapMethod · 0.80
take_resume_at_pcMethod · 0.80
as_ptrMethod · 0.45
fpMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected