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 friend class Linker;
115 friend class Instance;
116 friend class Func;
117 template <typename Params, typename Results> friend class TypedFunc;
118
119 struct deleter {
120 void operator()(wasm_trap_t *p) const { wasm_trap_delete(p); }
121 };
122
123 std::unique_ptr<wasm_trap_t, deleter> ptr;
124
125 Trap(wasm_trap_t *ptr) : ptr(ptr) {}
126
127public:
129 explicit Trap(std::string_view msg)
130 : Trap(wasmtime_trap_new(msg.data(), msg.size())) {}
131
133 std::string message() const {
134 wasm_byte_vec_t msg;
135 wasm_trap_message(ptr.get(), &msg);
136 std::string ret(msg.data, msg.size - 1);
138 return ret;
139 }
140
144 Trace trace() const {
145 wasm_frame_vec_t frames;
146 wasm_trap_trace(ptr.get(), &frames);
147 return Trace(frames);
148 }
149
152 std::optional<wasmtime_trap_code_t> code() const {
154 bool present = wasmtime_trap_code(ptr.get(), &code);
155 if (present)
156 return code;
157 return std::nullopt;
158 }
159};
160
162struct TrapError {
164 std::variant<Trap, Error> data;
165
167 TrapError(Trap t) : data(std::move(t)) {}
169 TrapError(Error e) : data(std::move(e)) {}
170
172 std::string message() const {
173 if (const auto *trap = std::get_if<Trap>(&data)) {
174 return trap->message();
175 }
176 if (const auto *error = std::get_if<Error>(&data)) {
177 return std::string(error->message());
178 }
179 std::abort();
180 }
181};
182
185template <typename T> using TrapResult = Result<T, TrapError>;
186
187} // namespace wasmtime
188
189#endif // WASMTIME_TRAP_HH
Errors coming from Wasmtime.
Definition: error.hh:24
Trace trace() const
Definition: trap.hh:97
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
Representation of a WebAssembly function.
Definition: wasmtime.hh:347
A WebAssembly instance.
Definition: wasmtime.hh:929
Helper class for linking modules together with name-based resolution.
Definition: wasmtime.hh:1061
Fallible result type used for Wasmtime.
Definition: error.hh:83
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
std::optional< wasmtime_trap_code_t > code() const
Returns the trap code associated with this trap, or nothing if it was a manually created trap.
Definition: trap.hh:152
Trap(std::string_view msg)
Creates a new host-defined trap with the specified message.
Definition: trap.hh:129
std::string message() const
Returns the descriptive message associated with this trap.
Definition: trap.hh:133
Trace trace() const
Definition: trap.hh:144
A version of a WebAssembly Func where the type signature of the function is statically known.
Definition: wasmtime.hh:606
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
Opaque struct representing a wasm trap.
Structure used to represent either a Trap or an Error.
Definition: trap.hh:162
TrapError(Trap t)
Creates a new TrapError from a Trap
Definition: trap.hh:167
TrapError(Error e)
Creates a new TrapError from an Error
Definition: trap.hh:169
std::string message() const
Dispatches internally to return the message associated with this error.
Definition: trap.hh:172
std::variant< Trap, Error > data
Storage for what this trap represents.
Definition: trap.hh:164
const wasm_name_t * wasmtime_frame_module_name(const wasm_frame_t *)
Returns a human-readable name for this frame's module.
bool wasmtime_trap_code(const wasm_trap_t *, wasmtime_trap_code_t *code)
Attempts to extract the trap code from this trap.
wasm_trap_t * wasmtime_trap_new(const char *msg, size_t msg_len)
Creates a new trap.
const wasm_name_t * wasmtime_frame_func_name(const wasm_frame_t *)
Returns a human-readable name for this frame's function.
uint8_t wasmtime_trap_code_t
Code of an instruction trap.
Definition: trap.h:21
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.
void wasm_trap_delete(wasm_trap_t *)
Deletes a trap.
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...