1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use super::store::wasm_store_t;
use super::types::{wasm_byte_vec_t, wasm_message_t};
use super::types::{wasm_frame_t, wasm_frame_vec_t};
use std::ffi::CString;
use wasmer_api::RuntimeError;

// opaque type which is a `RuntimeError`
#[allow(non_camel_case_types)]
pub struct wasm_trap_t {
    pub(crate) inner: RuntimeError,
}

impl From<RuntimeError> for wasm_trap_t {
    fn from(other: RuntimeError) -> Self {
        Self { inner: other }
    }
}

/// Create a new trap message.
///
/// Be careful, the message is typed with `wasm_message_t` which
/// represents a null-terminated string.
///
/// # Example
///
/// See the module's documentation for a complete example.
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_new(
    _store: &mut wasm_store_t,
    message: &wasm_message_t,
) -> Option<Box<wasm_trap_t>> {
    let message_bytes = message.as_slice();

    // The trap message is typed with `wasm_message_t` which is a
    // typeref to `wasm_name_t` with the exception that it's a
    // null-terminated string. `RuntimeError` must contain a valid
    // Rust `String` that doesn't contain a null byte. We must ensure
    // this behavior.
    let runtime_error = match CString::new(message_bytes) {
        // The string is well-formed and doesn't contain a nul byte.
        Ok(cstring) => RuntimeError::new(cstring.into_string().ok()?),

        // The string is well-formed but is nul-terminated. Let's
        // create a `String` which is null-terminated too.
        Err(nul_error) if nul_error.nul_position() + 1 == message_bytes.len() => {
            let mut vec = nul_error.into_vec();
            vec.pop();

            RuntimeError::new(String::from_utf8(vec).ok()?)
        }

        // The string not well-formed.
        Err(_) => return None,
    };

    let trap = runtime_error.into();

    Some(Box::new(trap))
}

/// Deletes a trap.
///
/// # Example
///
/// See the module's documentation for a complete example.
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_delete(_trap: Option<Box<wasm_trap_t>>) {}

/// Gets the message attached to the trap.
///
/// # Example
///
/// ```rust
/// # use wasmer_inline_c::assert_c;
/// # fn main() {
/// #    (assert_c! {
/// # #include "tests/wasmer.h"
/// #
/// int main() {
///     // Create an engine and a store.
///     wasm_engine_t* engine = wasm_engine_new();
///     wasm_store_t* store = wasm_store_new(engine);
///
///     // Create the trap message.
///     wasm_message_t message;
///     wasm_name_new_from_string_nt(&message, "foobar");
///
///     // Create the trap with its message.
///     // The backtrace will be generated automatically.
///     wasm_trap_t* trap = wasm_trap_new(store, &message);
///     assert(trap);
///
///     // Get the trap's message back.
///     wasm_message_t retrieved_message;
///     wasm_trap_message(trap, &retrieved_message);
///     assert(retrieved_message.size == message.size);
///
///     // Free everything.
///     wasm_name_delete(&message);
///     wasm_name_delete(&retrieved_message);
///     wasm_trap_delete(trap);
///     wasm_store_delete(store);
///     wasm_engine_delete(engine);
///
///     return 0;
/// }
/// #    })
/// #    .success();
/// # }
/// ```
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_message(
    trap: &wasm_trap_t,
    // own
    out: &mut wasm_byte_vec_t,
) {
    let message = trap.inner.message();
    let mut byte_vec = message.into_bytes();
    byte_vec.push(0);

    out.set_buffer(byte_vec);
}

/// Gets the origin frame attached to the trap.
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_origin(trap: &wasm_trap_t) -> Option<Box<wasm_frame_t>> {
    trap.inner.trace().first().map(Into::into).map(Box::new)
}

/// Gets the trace (as a list of frames) attached to the trap.
#[no_mangle]
pub unsafe extern "C" fn wasm_trap_trace(
    trap: &wasm_trap_t,
    // own
    out: &mut wasm_frame_vec_t,
) {
    let frames = trap.inner.trace();
    out.set_buffer(
        frames
            .iter()
            .map(|frame| Some(Box::new(frame.into())))
            .collect(),
    );
}

#[cfg(test)]
mod tests {
    #[cfg(not(target_os = "windows"))]
    use inline_c::assert_c;
    #[cfg(target_os = "windows")]
    use wasmer_inline_c::assert_c;

    #[cfg_attr(coverage, ignore)]
    #[test]
    fn test_trap_message_null_terminated() {
        (assert_c! {
            #include "tests/wasmer.h"

            int main() {
                wasm_engine_t* engine = wasm_engine_new();
                wasm_store_t* store = wasm_store_new(engine);

                wasm_message_t original_message;
                wasm_name_new_from_string_nt(&original_message, "foobar");
                assert(original_message.size == 7); // 6 for `foobar` + 1 for nul byte.

                wasm_trap_t* trap = wasm_trap_new(store, &original_message);
                assert(trap);

                wasm_message_t retrieved_message;
                wasm_trap_message(trap, &retrieved_message);
                assert(retrieved_message.size == 7);

                wasm_name_delete(&original_message);
                wasm_name_delete(&retrieved_message);
                wasm_trap_delete(trap);
                wasm_store_delete(store);
                wasm_engine_delete(engine);

                return 0;
            }
        })
        .success();
    }

    #[cfg_attr(coverage, ignore)]
    #[test]
    fn test_trap_message_not_null_terminated() {
        (assert_c! {
            #include "tests/wasmer.h"

            int main() {
                wasm_engine_t* engine = wasm_engine_new();
                wasm_store_t* store = wasm_store_new(engine);

                wasm_message_t original_message;
                wasm_name_new_from_string(&original_message, "foobar");
                assert(original_message.size == 6); // 6 for `foobar` + 0 for nul byte.

                wasm_trap_t* trap = wasm_trap_new(store, &original_message);
                assert(trap);

                wasm_message_t retrieved_message;
                wasm_trap_message(trap, &retrieved_message);
                assert(retrieved_message.size == 7);

                wasm_name_delete(&original_message);
                wasm_name_delete(&retrieved_message);
                wasm_trap_delete(trap);
                wasm_store_delete(store);
                wasm_engine_delete(engine);

                return 0;
            }
        })
        .success();
    }
}