Skip to content

Commit

Permalink
Merge #1728
Browse files Browse the repository at this point in the history
1728: Fix bugs in object file engine r=syrusakbary a=MarkMcCaskey

Still fixing some bugs, work in progress

# Review

- [ ] Add a short description of the the change to the CHANGELOG.md file


Co-authored-by: Mark McCaskey <mark@wasmer.io>
Co-authored-by: Syrus <me@syrusakbary.com>
  • Loading branch information
3 people authored Oct 17, 2020
2 parents 114a82a + bb1b89f commit 2646b94
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 25 deletions.
1 change: 0 additions & 1 deletion lib/c-api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,6 @@ fn exclude_items_from_wasm_c_api(builder: Builder) -> Builder {
.exclude_item("wasi_version_t")
.exclude_item("wasm_config_set_compiler")
.exclude_item("wasm_config_set_engine")
.exclude_item("wasm_instance_get_vmctx_ptr")
.exclude_item("wasm_module_name")
.exclude_item("wasm_module_set_name")
.exclude_item("wasmer_compiler_t")
Expand Down
7 changes: 0 additions & 7 deletions lib/c-api/src/wasm_c_api/wasmer.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
//! Wasmer-specific extensions to the Wasm C API.
use super::instance::wasm_instance_t;
use super::module::wasm_module_t;
use super::types::wasm_name_t;
use std::ffi::c_void;
use std::str;
use std::sync::Arc;

#[no_mangle]
pub unsafe extern "C" fn wasm_instance_get_vmctx_ptr(instance: &wasm_instance_t) -> *mut c_void {
instance.inner.vmctx_ptr() as _
}

#[no_mangle]
pub unsafe extern "C" fn wasm_module_name(module: &wasm_module_t, out: &mut wasm_name_t) {
let name = match module.inner.name() {
Expand Down
2 changes: 0 additions & 2 deletions lib/c-api/wasmer_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,6 @@ void wasm_config_set_compiler(wasm_config_t *config, wasmer_compiler_t compiler)

void wasm_config_set_engine(wasm_config_t *config, wasmer_engine_t engine);

void *wasm_instance_get_vmctx_ptr(const wasm_instance_t *instance);

void wasm_module_name(const wasm_module_t *module, wasm_name_t *out);

bool wasm_module_set_name(wasm_module_t *module, const wasm_name_t *name);
Expand Down
22 changes: 17 additions & 5 deletions lib/cli/src/commands/wasmer_create_exe_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extern "C" {
#include <stdio.h>
#include <stdlib.h>

#define own

// TODO: make this define templated so that the Rust code can toggle it on/off
#define WASI

Expand Down Expand Up @@ -158,11 +160,21 @@ int main(int argc, char* argv[]) {
wasi_env_set_instance(wasi_env, instance);
#endif

void* vmctx = wasm_instance_get_vmctx_ptr(instance);
wasm_val_t* inout[2] = { NULL, NULL };

// We're able to call our compiled function directly through a trampoline.
wasmer_trampoline_function_call__1(vmctx, wasmer_function__1, &inout);
#ifdef WASI
own wasm_func_t* start_function = wasi_get_start_function(instance);
if (!start_function) {
fprintf(stderr, "`_start` function not found\n");
print_wasmer_error();
return -1;
}
own wasm_trap_t* trap = wasm_func_call(start_function, NULL, NULL);
if (trap) {
fprintf(stderr, "Trap is not NULL: TODO:\n");
return -1;
}
#endif

// TODO: handle non-WASI start (maybe with invoke?)

wasm_instance_delete(instance);
wasm_module_delete(module);
Expand Down
18 changes: 13 additions & 5 deletions lib/engine-object-file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern "C" {
#include <stdio.h>
#include <stdlib.h>

#define own

#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -100,13 +102,19 @@ int main() {
wasi_env_set_instance(wasi_env, instance);

// WASI is now set up.

void* vmctx = wasm_instance_get_vmctx_ptr(instance);
wasm_val_t* inout[2] = { NULL, NULL };
own wasm_func_t* start_function = wasi_get_start_function(instance);
if (!start_function) {
fprintf(stderr, "`_start` function not found\n");
print_wasmer_error();
return -1;
}

fflush(stdout);
// We're able to call our compiled function directly through a trampoline.
wasmer_trampoline_function_call__1(vmctx, wasmer_function__1, &inout);
own wasm_trap_t* trap = wasm_func_call(start_function, NULL, NULL);
if (trap) {
fprintf(stderr, "Trap is not NULL: TODO:\n");
return -1;
}

wasm_instance_delete(instance);
wasm_module_delete(module);
Expand Down
5 changes: 5 additions & 0 deletions lib/vm/src/libcalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,11 @@ impl LibCall {
Self::FloorF64 => "wasmer_f64_floor",
Self::NearestF32 => "wasmer_f32_nearest",
Self::NearestF64 => "wasmer_f64_nearest",
// We have to do this because macOS requires a leading `_` and it's not
// a normal function, it's a static variable, so we have to do it manually.
#[cfg(target_os = "macos")]
Self::Probestack => "_wasmer_probestack",
#[cfg(not(target_os = "macos"))]
Self::Probestack => "wasmer_probestack",
Self::RaiseTrap => "wasmer_raise_trap",
Self::TruncF32 => "wasmer_f32_trunc",
Expand Down
18 changes: 13 additions & 5 deletions tests/integration/cli/tests/object_file_engine_test_c_source.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ extern "C" {
#include <stdio.h>
#include <stdlib.h>

#define own

#ifdef __cplusplus
}
#endif
Expand Down Expand Up @@ -73,13 +75,19 @@ int main() {
wasi_env_set_instance(wasi_env, instance);

// WASI is now set up.

void* vmctx = wasm_instance_get_vmctx_ptr(instance);
wasm_val_t* inout[2] = { NULL, NULL };
own wasm_func_t* start_function = wasi_get_start_function(instance);
if (!start_function) {
fprintf(stderr, "`_start` function not found\n");
print_wasmer_error();
return -1;
}

fflush(stdout);
// We're able to call our compiled function directly through a trampoline.
wasmer_trampoline_function_call__1(vmctx, wasmer_function__1, &inout);
own wasm_trap_t* trap = wasm_func_call(start_function, NULL, NULL);
if (trap) {
fprintf(stderr, "Trap is not NULL: TODO:\n");
return -1;
}

wasm_instance_delete(instance);
wasm_module_delete(module);
Expand Down

0 comments on commit 2646b94

Please sign in to comment.