Wasmtime
trap.hh
Go to the documentation of this file.
1
5#ifndef WASMTIME_TRAP_HH
6#define WASMTIME_TRAP_HH
7
8#include <wasmtime/error.hh>
9#include <wasmtime/trap.h>
10
11namespace wasmtime {
12
20class FrameRef {
21 wasm_frame_t *frame;
22
23public:
26 uint32_t func_index() const { return wasm_frame_func_index(frame); }
29 size_t func_offset() const { return wasm_frame_func_offset(frame); }
32 size_t module_offset() const { return wasm_frame_module_offset(frame); }
33
38 std::optional<std::string_view> func_name() const {
39 const auto *name = wasmtime_frame_func_name(frame);
40 if (name != nullptr) {
41 return std::string_view(name->data, name->size);
42 }
43 return std::nullopt;
44 }
45
50 std::optional<std::string_view> module_name() const {
51 const auto *name = wasmtime_frame_module_name(frame);
52 if (name != nullptr) {
53 return std::string_view(name->data, name->size);
54 }
55 return std::nullopt;
56 }
57};
58
66class Trace {
67 friend class Trap;
68 friend class Error;
69
71
72 Trace(wasm_frame_vec_t vec) : vec(vec) {}
73
74public:
75 ~Trace() { wasm_frame_vec_delete(&vec); }
76
77 Trace(const Trace &other) = delete;
78 Trace(Trace &&other) = delete;
79 Trace &operator=(const Trace &other) = delete;
80 Trace &operator=(Trace &&other) = delete;
81
83 typedef const FrameRef *iterator;
84
86 iterator begin() const {
87 return reinterpret_cast<FrameRef *>(&vec.data[0]); // NOLINT
88 }
90 iterator end() const {
91 return reinterpret_cast<FrameRef *>(&vec.data[vec.size]); // NOLINT
92 }
94 size_t size() const { return vec.size; }
95};
96
97inline Trace Error::trace() const {
98 wasm_frame_vec_t frames;
99 wasmtime_error_wasm_trace(ptr.get(), &frames);
100 return Trace(frames);
101}
102
113class Trap {
114 WASMTIME_OWN_WRAPPER(Trap, wasm_trap);
115
117 explicit Trap(std::string_view msg)
118 : Trap(wasmtime_trap_new(msg.data(), msg.size())) {}
119
122
124 std::string message() const {
125 wasm_byte_vec_t msg;
126 wasm_trap_message(ptr.get(), &msg);
127 std::string ret(msg.data, msg.size - 1);
129 return ret;
130 }
131
135 Trace trace() const {
136 wasm_frame_vec_t frames;
137 wasm_trap_trace(ptr.get(), &frames);
138 return Trace(frames);
139 }
140
143 std::optional<wasmtime_trap_code_t> code() const {
145 bool present = wasmtime_trap_code(ptr.get(), &code);
146 if (present)
147 return code;
148 return std::nullopt;
149 }
150};
151
153struct TrapError {
155 std::variant<Trap, Error> data;
156
158 TrapError(Trap t) : data(std::move(t)) {}
160 TrapError(Error e) : data(std::move(e)) {}
161
163 std::string message() const {
164 if (const auto *trap = std::get_if<Trap>(&data)) {
165 return trap->message();
166 }
167 if (const auto *error = std::get_if<Error>(&data)) {
168 return std::string(error->message());
169 }
170 std::abort();
171 }
172};
173
176template <typename T> using TrapResult = Result<T, TrapError>;
177
178} // namespace wasmtime
179
180#endif // WASMTIME_TRAP_HH
Errors coming from Wasmtime.
Definition: error.hh:26
Non-owning reference to a WebAssembly function frame as part of a Trace
Definition: trap.hh:20
size_t module_offset() const
Definition: trap.hh:32
std::optional< std::string_view > module_name() const
Definition: trap.hh:50
uint32_t func_index() const
Definition: trap.hh:26
std::optional< std::string_view > func_name() const
Definition: trap.hh:38
size_t func_offset() const
Definition: trap.hh:29
Fallible result type used for Wasmtime.
Definition: error.hh:70
An owned vector of FrameRef instances representing the WebAssembly call-stack on a trap.
Definition: trap.hh:66
iterator begin() const
Returns the start of iteration.
Definition: trap.hh:86
const FrameRef * iterator
Iterator used to iterate over this trace.
Definition: trap.hh:83
size_t size() const
Returns the size of this trace, or how many frames it contains.
Definition: trap.hh:94
iterator end() const
Returns the end of iteration.
Definition: trap.hh:90
Information about a WebAssembly trap.
Definition: trap.hh:113
WASM_API_EXTERN void wasmtime_error_wasm_trace(const wasmtime_error_t *, wasm_frame_vec_t *out)
Attempts to extract a WebAssembly trace from this error.
A list of bytes.
Definition: wasm.h:102
size_t size
Length of this vector.
Definition: wasm.h:102
wasm_byte_t * data
Pointer to the base of this vector.
Definition: wasm.h:102
Opaque struct representing a frame of a wasm stack trace.
A list of wasm_frame_t frameues.
Definition: wasm.h:373
wasm_frame_t ** data
Pointer to the base of this vector.
Definition: wasm.h:373
size_t size
Length of this vector.
Definition: wasm.h:373
Structure used to represent either a Trap or an Error.
Definition: trap.hh:153
TrapError(Trap t)
Creates a new TrapError from a Trap
Definition: trap.hh:158
TrapError(Error e)
Creates a new TrapError from an Error
Definition: trap.hh:160
std::string message() const
Dispatches internally to return the message associated with this error.
Definition: trap.hh:163
std::variant< Trap, Error > data
Storage for what this trap represents.
Definition: trap.hh:155
WASM_API_EXTERN const wasm_name_t * wasmtime_frame_module_name(const wasm_frame_t *)
Returns a human-readable name for this frame's module.
WASM_API_EXTERN const wasm_name_t * wasmtime_frame_func_name(const wasm_frame_t *)
Returns a human-readable name for this frame's function.
WASM_API_EXTERN wasm_trap_t * wasmtime_trap_new_code(wasmtime_trap_code_t code)
Creates a new trap from the given trap code.
WASM_API_EXTERN bool wasmtime_trap_code(const wasm_trap_t *, wasmtime_trap_code_t *code)
Attempts to extract the trap code from this trap.
wasmtime_trap_code_enum
Trap codes for instruction traps.
Definition: trap.h:26
uint8_t wasmtime_trap_code_t
Code of an instruction trap.
Definition: trap.h:21
WASM_API_EXTERN wasm_trap_t * wasmtime_trap_new(const char *msg, size_t msg_len)
Creates a new trap with the given message.
uint32_t wasm_frame_func_index(const wasm_frame_t *)
Returns the function index in the original wasm module that this frame corresponds to.
void wasm_trap_message(const wasm_trap_t *, wasm_message_t *out)
Retrieves the message associated with this trap.
void wasm_trap_trace(const wasm_trap_t *, wasm_frame_vec_t *out)
Returns the trace of wasm frames for this trap.
size_t wasm_frame_module_offset(const wasm_frame_t *)
Returns the byte offset from the beginning of the original wasm file to the instruction this frame po...
void wasm_byte_vec_delete(wasm_byte_vec_t *)
Deletes a byte vector.
void wasm_frame_vec_delete(wasm_frame_vec_t *)
Deallocates export for a vector.
size_t wasm_frame_func_offset(const wasm_frame_t *)
Returns the byte offset from the beginning of the function in the original wasm file to the instructi...