Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(runtime-c-api) Implement wasmer_trap #1133

Merged
merged 20 commits into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ec37859
chore(runtime-c-api) Update headers.
Hywan Jan 10, 2020
e913e89
chore(runtime-c-api) Fix CS.
Hywan Jan 10, 2020
35459c2
feat(runtime-c-api) Implement `wasmer_import_trap`.
Hywan Jan 10, 2020
6846204
chore(runtime-c-api) Update headers.
Hywan Jan 10, 2020
c7a4825
doc(runtime-core) Update documentation.
Hywan Jan 10, 2020
4bf3d6d
test(runtime-c-api) Test `wasmer_import_trap`.
Hywan Jan 10, 2020
8790f6d
feat(runtime-c-api) Check pointers aren't null in `wasmer_import_trap`.
Hywan Jan 13, 2020
b45ead2
test(runtime-c-api) Test `wasmer_import_trap`.
Hywan Jan 13, 2020
a506411
doc(runtime-c-api) Improve documentation of `wasmer_import_trap`.
Hywan Jan 13, 2020
b494bd8
doc(runtime-c-api) Improve `wasmer_import_func_new`'s documentation.
Hywan Jan 13, 2020
6e7d5ba
chore(runtime-c-api) Update C/C++ headers.
Hywan Jan 13, 2020
b5e96b8
doc(changelog) Add #1133.
Hywan Jan 13, 2020
176152e
test(runtime-c-api) Remove a hardcoded value.
Hywan Jan 13, 2020
c0b439e
feat(runtime-c-api) Rename `wasmer_import_trap` to `wasmer_trap`.
Hywan Jan 15, 2020
4cdf868
doc(runtime-c-api) Explain why code is unreachable in `wasmer_trap`.
Hywan Jan 15, 2020
bcbde69
doc(runtime-core) Fix a typo.
Hywan Jan 15, 2020
cd16a7d
chore(runtime-c-api) Update C/C++ headers.
Hywan Jan 15, 2020
ce3fb49
Merge branch 'master' into feat-runtime-c-api-import-trap
Hywan Jan 15, 2020
f4ae606
doc(changelog) Update #1133.
Hywan Jan 15, 2020
fb06ee3
test(runtime-c-api) Rename `wasmer_import_trap` to `wasmer_trap`.
Hywan Jan 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/runtime-c-api/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ pub unsafe extern "C" fn wasmer_export_func_call(

let instance = &*named_export.instance;
let result = instance.call(&named_export.name, &params[..]);

match result {
Ok(results_vec) => {
if !results_vec.is_empty() {
Expand Down
29 changes: 27 additions & 2 deletions lib/runtime-c-api/src/import/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@
use crate::{
error::{update_last_error, CApiError},
export::{wasmer_import_export_kind, wasmer_import_export_value},
instance::wasmer_instance_context_t,
module::wasmer_module_t,
value::wasmer_value_tag,
wasmer_byte_array, wasmer_result_t,
};
use libc::c_uint;
use std::{convert::TryFrom, ffi::c_void, ptr, slice, sync::Arc};
use wasmer_runtime::{Global, Memory, Module, Table};
use std::{
convert::TryFrom,
ffi::{c_void, CStr},
os::raw::c_char,
ptr, slice,
sync::Arc,
};
use wasmer_runtime::{Ctx, Global, Memory, Module, Table};
use wasmer_runtime_core::{
export::{Context, Export, FuncPointer},
import::{ImportObject, ImportObjectIterator},
Expand Down Expand Up @@ -661,6 +668,24 @@ pub unsafe extern "C" fn wasmer_import_func_new(
Box::into_raw(export) as *mut wasmer_import_func_t
}

/// Hello
#[no_mangle]
#[allow(clippy::cast_ptr_alignment)]
pub unsafe extern "C" fn wasmer_import_trap(
ctx: *const wasmer_instance_context_t,
error_message: *const c_char,
) -> c_uint {
let ctx = &*(ctx as *const Ctx);
let error_message = CStr::from_ptr(error_message).to_str().unwrap();
Hywan marked this conversation as resolved.
Show resolved Hide resolved

(&*ctx.module)
Hywan marked this conversation as resolved.
Show resolved Hide resolved
.runnable_module
.do_early_trap(Box::new(error_message)); // never returns

#[allow(unreachable_code)]
0
}

/// Sets the params buffer to the parameter types of the given wasmer_import_func_t
///
/// Returns `wasmer_result_t::WASMER_OK` upon success.
Expand Down
5 changes: 4 additions & 1 deletion lib/runtime-c-api/tests/test-import-function.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ static int memory_len = 0;
static int ptr_len = 0;
static char actual_str[14] = {};
static int actual_context_data_value = 0;
static const char *trap_error_message = "hello";

typedef struct {
int value;
Expand All @@ -31,12 +32,14 @@ void print_str(wasmer_instance_context_t *ctx, int32_t ptr, int32_t len)
actual_str[idx] = mem_bytes[ptr + idx];
}
actual_str[13] = '\0';
printf("In print_str, memory len: %d, ptr_len: %d\n, str %s", mem_len, len, actual_str);
printf("In print_str, memory len: %d, ptr_len: %d, str %s\n", mem_len, len, actual_str);
print_str_called = true;
memory_len = mem_len;
ptr_len = len;

actual_context_data_value = ((context_data *) wasmer_instance_context_data_get(ctx))->value;

wasmer_import_trap(ctx, trap_error_message);
}

int main()
Expand Down
7 changes: 6 additions & 1 deletion lib/runtime-c-api/wasmer.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ enum Version {
Unknown = 0,
/**
* Latest version. See `wasmer_wasi::WasiVersion::Latest` to
* leran more.
* learn more.
*/
Latest = 1,
/**
Expand Down Expand Up @@ -661,6 +661,11 @@ wasmer_import_object_iter_t *wasmer_import_object_iterate_functions(const wasmer
*/
wasmer_import_object_t *wasmer_import_object_new(void);

/**
* Hello
*/
unsigned int wasmer_import_trap(const wasmer_instance_context_t *ctx, const char *error_message);

/**
* Calls an instances exported function by `name` with the provided parameters.
* Results are set using the provided `results` pointer.
Expand Down
5 changes: 4 additions & 1 deletion lib/runtime-c-api/wasmer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ enum class Version : uint8_t {
/// Version cannot be detected or is unknown.
Unknown = 0,
/// Latest version. See `wasmer_wasi::WasiVersion::Latest` to
/// leran more.
/// learn more.
Latest = 1,
/// `wasi_unstable`.
Snapshot0 = 2,
Expand Down Expand Up @@ -528,6 +528,9 @@ wasmer_import_object_iter_t *wasmer_import_object_iterate_functions(const wasmer
/// See also `wasmer_import_object_append`
wasmer_import_object_t *wasmer_import_object_new();

/// Hello
unsigned int wasmer_import_trap(const wasmer_instance_context_t *ctx, const char *error_message);

/// Calls an instances exported function by `name` with the provided parameters.
/// Results are set using the provided `results` pointer.
///
Expand Down
3 changes: 2 additions & 1 deletion lib/runtime-core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,11 @@ pub trait RunnableModule: Send + Sync {
}

/// A wasm trampoline contains the necessary data to dynamically call an exported wasm function.
/// Given a particular signature index, we are returned a trampoline that is matched with that
/// Given a particular signature index, we are returning a trampoline that is matched with that
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer returned to returning here;

, we return a trampoline... is probably best though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought it was an English typo.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it's grammatically correct and called the "passive voice", it's often discouraged in English though because it takes more words and ends up being less specific. For example, I generally prefer direct writing like, "This function takes a function signature and returns a corresponding trampoline".

/// signature and an invoke function that can call the trampoline.
fn get_trampoline(&self, info: &ModuleInfo, sig_index: SigIndex) -> Option<Wasm>;

/// Trap an error.
unsafe fn do_early_trap(&self, data: Box<dyn Any + Send>) -> !;

/// Returns the machine code associated with this module.
Expand Down