Skip to content

Commit

Permalink
Merge branch 'master' into feature/trampoline-in-artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
nlewycky committed Oct 20, 2020
2 parents 29c9cb1 + b04b4b0 commit f55121c
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- [#1710](https://github.com/wasmerio/wasmer/pull/1710) Memory for function call trampolines is now owned by the Artifact.
### Added

- [#1741](https://github.com/wasmerio/wasmer/pull/1741) Implement `wasm_memory_type` in the Wasm C API.
- [#1736](https://github.com/wasmerio/wasmer/pull/1736) Implement `wasm_global_type` in the Wasm C API.
- [#1699](https://github.com/wasmerio/wasmer/pull/1699) Update `wasm.h` to its latest version.
- [#1685](https://github.com/wasmerio/wasmer/pull/1685) Implement `wasm_exporttype_delete` in the Wasm C API.
Expand Down
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
8 changes: 4 additions & 4 deletions lib/c-api/src/wasm_c_api/externals/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ pub unsafe extern "C" fn wasm_memory_delete(_memory: Option<Box<wasm_memory_t>>)

// TODO: figure out if these should be deep or shallow copies
#[no_mangle]
pub unsafe extern "C" fn wasm_memory_copy(wasm_memory: &wasm_memory_t) -> Box<wasm_memory_t> {
pub unsafe extern "C" fn wasm_memory_copy(memory: &wasm_memory_t) -> Box<wasm_memory_t> {
// do shallow copy
Box::new(wasm_memory_t {
inner: wasm_memory.inner.clone(),
inner: memory.inner.clone(),
})
}

#[no_mangle]
pub unsafe extern "C" fn wasm_memory_type(_memory_ptr: &wasm_memory_t) -> *mut wasm_memorytype_t {
todo!("wasm_memory_type")
pub unsafe extern "C" fn wasm_memory_type(memory: &wasm_memory_t) -> Box<wasm_memorytype_t> {
Box::new(wasm_memorytype_t::new(memory.inner.ty().clone()))
}

// get a raw pointer into bytes
Expand Down
16 changes: 11 additions & 5 deletions lib/c-api/src/wasm_c_api/types/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ impl wasm_memorytype_t {
);
}
}

pub(crate) fn new(memory_type: MemoryType) -> Self {
Self {
extern_: wasm_externtype_t {
inner: ExternType::Memory(memory_type),
},
}
}
}

wasm_declare_vec!(memorytype);
Expand All @@ -42,11 +50,9 @@ pub unsafe extern "C" fn wasm_memorytype_new(limits: &wasm_limits_t) -> Box<wasm
Some(Pages(limits.max as _))
};

Box::new(wasm_memorytype_t {
extern_: wasm_externtype_t {
inner: ExternType::Memory(MemoryType::new(min_pages, max_pages, false)),
},
})
Box::new(wasm_memorytype_t::new(MemoryType::new(
min_pages, max_pages, false,
)))
}

#[no_mangle]
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
25 changes: 20 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 @@ -162,11 +164,24 @@ 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;
}

wasm_val_vec_t args = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_EMPTY_VEC;
own wasm_trap_t* trap = wasm_func_call(start_function, &args, &results);
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
2 changes: 1 addition & 1 deletion tests/integration/cli/tests/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn run_c_compile(

#[test]
fn object_file_engine_works() -> anyhow::Result<()> {
let temp_dir = tempfile::tempdir()?;
let temp_dir = tempfile::tempdir().context("Making a temp dir")?;
let operating_dir: PathBuf = temp_dir.path().to_owned();

let wasm_path = operating_dir.join(object_file_engine_test_wasm_path());
Expand Down
25 changes: 17 additions & 8 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 @@ -58,7 +60,6 @@ int main() {

wasm_extern_vec_t imports;
wasm_extern_vec_new_uninitialized(&imports, import_types.size);

wasm_importtype_vec_delete(&import_types);

bool get_imports_result = wasi_get_imports(store, module, wasi_env, &imports);
Expand All @@ -77,17 +78,25 @@ int main() {
return -1;
}
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 };
// WASI is now set up.
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);

wasm_extern_vec_delete(&imports);
wasm_val_vec_t args = WASM_EMPTY_VEC;
wasm_val_vec_t results = WASM_EMPTY_VEC;
own wasm_trap_t* trap = wasm_func_call(start_function, &args, &results);
if (trap) {
fprintf(stderr, "Trap is not NULL: TODO:\n");
return -1;
}

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

0 comments on commit f55121c

Please sign in to comment.