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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::{
    ops::Deref,
    os::raw::{c_char, c_int},
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[repr(u32)]
#[non_exhaustive]
pub enum ErrorKind {
    /// An unidentified error.
    Other = 0,
    /// The WebAssembly isn't valid.
    ///
    /// This typically happens when the WebAssembly failed validation or isn't
    /// actually WebAssembly.
    InvalidWebAssembly = 1,
    /// There was an issue resolving imports.
    ///
    /// This typically occurs when the Rune is expecting different host
    /// functions or some of those functions have different signatures.
    BadImports = 2,
    /// The call into WebAssembly raised an error.
    CallFailed = 3,
}

/// An error that may be returned by the Rune native library.
///
/// # Error Handling
///
/// Fallible functions will return a `*mut Error` which *must* be checked before
/// continuing.
///
/// This might look like...
///
/// ```cpp
/// Runtime *runtime;
/// Config cfg = {...};
///
/// Error *error = rune_runtime_load(&cfg, &runtime);
///
/// if (error) {
///     const char *msg = rune_error_to_string(error);
///
///     printf("Unable to load the Rune: %s\n", msg);
///
///     free(msg);
///     rune_error_free(error);
///     exit(1);
/// }
/// ```
///
/// Additional "return" values are returned via output parameters (typically
/// named `xxx_out`). If an error occurs, the state of the output parameter is
/// unspecified, otherwise it is guaranteed to be in a valid state.
///
/// If an error is present, it is the caller's responsibility to free it
/// afterwards.
pub struct Error(anyhow::Error);

impl Error {
    pub fn boxed(error: impl Into<anyhow::Error>) -> *mut Error {
        let boxed = Box::new(Error(error.into()));
        Box::into_raw(boxed)
    }
}

impl From<anyhow::Error> for Error {
    fn from(e: anyhow::Error) -> Error { Error(e) }
}

impl Deref for Error {
    type Target = anyhow::Error;

    fn deref(&self) -> &Self::Target { &self.0 }
}

/// Create a new `Error` with the provided error message.
///
/// It is the caller's responsibility to free the `Error` using
/// `rune_error_free()` once they are done with it.
#[no_mangle]
pub unsafe extern "C" fn rune_error_new(
    msg: *const c_char,
    len: c_int,
) -> *mut Error {
    let msg = std::slice::from_raw_parts(msg as *const u8, len as usize);
    let error = anyhow::Error::msg(String::from_utf8_lossy(msg));

    Error::boxed(error)
}

/// Get a simple, oneline message describing the error.
///
/// Note: It is the caller's responsibility to free this string afterwards.
#[no_mangle]
pub unsafe extern "C" fn rune_error_to_string(e: *const Error) -> *mut c_char {
    let e = &*e;
    let msg = e.to_string();

    crate::c_str(&msg)
}

/// Print the error, plus any errors that may have caused it.
///
/// If the `RUST_BACKTRACE` environment variable is set, this will also include
/// a backtrace from where the error was first created.
///
/// Note: It is the caller's responsibility to free this string afterwards.
#[no_mangle]
pub unsafe extern "C" fn rune_error_to_string_verbose(
    e: *const Error,
) -> *mut c_char {
    let e = &*e;
    let msg = format!("{:?}", e.0);

    crate::c_str(&msg)
}

/// Free an error once you are done with it.
///
/// Note: Freeing the same `Error` twice is an error and may cause a crash at
/// runtime.
#[no_mangle]
pub unsafe extern "C" fn rune_error_free(e: *mut Error) {
    if e.is_null() {
        return;
    }

    let _ = Box::from_raw(e);
}

/// Programmatically find out what kind of error this is.
#[no_mangle]
pub unsafe extern "C" fn rune_error_kind(e: *const Error) -> ErrorKind {
    if e.is_null() {
        return ErrorKind::Other;
    }

    error_kind_for_anyhow(&*e)
}

fn error_kind_for_anyhow(error: &anyhow::Error) -> ErrorKind {
    for error in error.chain() {
        if let Some(error_kind) = specific_error_kind(error) {
            return error_kind;
        }
    }

    ErrorKind::Other
}

fn specific_error_kind(
    e: &(dyn std::error::Error + 'static),
) -> Option<ErrorKind> {
    if let Some(load_error) = e.downcast_ref::<hotg_rune_runtime::LoadError>() {
        if let Some(kind) = rune_load_error(load_error) {
            return Some(kind);
        }
    }

    #[cfg(feature = "wasmer")]
    if let Some(runtime_error) =
        e.downcast_ref::<hotg_rune_runtime::wasmer::RuntimeError>()
    {
        return Some(wasmer_runtime_error(runtime_error));
    }

    None
}

fn rune_load_error(
    load_error: &hotg_rune_runtime::LoadError,
) -> Option<ErrorKind> {
    match load_error {
        hotg_rune_runtime::LoadError::Other(anyhow) => {
            Some(error_kind_for_anyhow(anyhow))
        },
        #[cfg(feature = "wasmer")]
        hotg_rune_runtime::LoadError::WasmerInstantiation(e) => {
            Some(wasmer_instantiation_error(e))
        },
        #[cfg(feature = "wasmer")]
        hotg_rune_runtime::LoadError::WasmerCompile(e) => {
            Some(wasmer_compile_error(e))
        },
        _ => None,
    }
}

#[cfg(feature = "wasmer")]
fn wasmer_instantiation_error(
    error: &hotg_rune_runtime::wasmer::InstantiationError,
) -> ErrorKind {
    use hotg_rune_runtime::wasmer::{InstantiationError, LinkError};

    match error {
        InstantiationError::Link(LinkError::Import(..)) => {
            ErrorKind::BadImports
        },
        InstantiationError::Start(error) => wasmer_runtime_error(error),
        _ => ErrorKind::Other,
    }
}

#[cfg(feature = "wasmer")]
fn wasmer_runtime_error(
    _error: &hotg_rune_runtime::wasmer::RuntimeError,
) -> ErrorKind {
    // TODO: We need to add a bunch of downcasts here if we want more precise
    // error kinds.
    ErrorKind::CallFailed
}

#[cfg(feature = "wasmer")]
fn wasmer_compile_error(
    error: &hotg_rune_runtime::wasmer::CompileError,
) -> ErrorKind {
    use hotg_rune_runtime::wasmer::{CompileError, WasmError};

    match error {
        CompileError::Wasm(WasmError::InvalidWebAssembly { .. })
        | CompileError::Validate(_) => ErrorKind::InvalidWebAssembly,
        _ => ErrorKind::Other,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn errors_should_be_thin_pointers() {
        assert_eq!(
            std::mem::size_of::<*mut Error>(),
            std::mem::size_of::<*mut usize>()
        );
    }
}