Skip to content

Commit

Permalink
cli: initialize wasienv memory in create-exe
Browse files Browse the repository at this point in the history
  • Loading branch information
epilys committed Jul 29, 2022
1 parent a02ff59 commit 8215b65
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
10 changes: 9 additions & 1 deletion lib/c-api/src/wasm_c_api/wasi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub use super::unstable::wasi::wasi_get_unordered_imports;
use super::{
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t},
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t, wasm_memory_t},
instance::wasm_instance_t,
module::wasm_module_t,
store::{wasm_store_t, StoreRef},
Expand Down Expand Up @@ -197,6 +197,14 @@ pub unsafe extern "C" fn wasi_env_new(
#[no_mangle]
pub extern "C" fn wasi_env_delete(_state: Option<Box<wasi_env_t>>) {}

/// Set the memory on a [`wasi_env_t`].
#[no_mangle]
pub unsafe extern "C" fn wasi_env_set_memory(env: &mut wasi_env_t, memory: &wasm_memory_t) {
let mut store_mut = env.store.store_mut();
let wasi_env = env.inner.data_mut(&mut store_mut);
wasi_env.set_memory(memory.extern_.memory());
}

#[no_mangle]
pub unsafe extern "C" fn wasi_env_read_stdout(
env: &mut wasi_env_t,
Expand Down
19 changes: 11 additions & 8 deletions lib/cli/src/commands/create_exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,14 @@ impl Default for LinkCode {

impl LinkCode {
fn run(&self) -> anyhow::Result<()> {
let libwasmer_path = self
.libwasmer_path
.canonicalize()
.context("Failed to find libwasmer")?;
println!(
"Using path `{}` as libwasmer path.",
libwasmer_path.display()
);
let mut command = Command::new(&self.linker_path);
let command = command
.arg(&self.optimization_flag)
Expand All @@ -250,12 +258,7 @@ impl LinkCode {
.iter()
.map(|path| path.canonicalize().unwrap()),
)
.arg(
&self
.libwasmer_path
.canonicalize()
.context("Failed to find libwasmer")?,
);
.arg(&libwasmer_path);
let command = if let Some(target) = &self.target {
command.arg("-target").arg(format!("{}", target))
} else {
Expand All @@ -272,11 +275,11 @@ impl LinkCode {
// On unix we need dlopen-related symbols, libmath for a few things, and pthreads.
#[cfg(not(windows))]
let command = command.arg("-ldl").arg("-lm").arg("-pthread");
let link_aganist_extra_libs = self
let link_against_extra_libs = self
.additional_libraries
.iter()
.map(|lib| format!("-l{}", lib));
let command = command.args(link_aganist_extra_libs);
let command = command.args(link_against_extra_libs);
let output = command.arg("-o").arg(&self.output_path).output()?;

if !output.status.success() {
Expand Down
23 changes: 22 additions & 1 deletion lib/cli/src/commands/wasmer_create_exe_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,24 @@ int main(int argc, char *argv[]) {
}

#ifdef WASI
// Read the exports.
wasm_extern_vec_t exports;
wasm_instance_exports(instance, &exports);
wasm_memory_t* mem = NULL;
for (size_t i = 0; i < exports.size; i++) {
mem = wasm_extern_as_memory(exports.data[i]);
if (mem) {
break;
}
}

if (!mem) {
fprintf(stderr, "Failed to create instance: Could not find memory in exports\n");
print_wasmer_error();
return -1;
}
wasi_env_set_memory(wasi_env, mem);

own wasm_func_t *start_function = wasi_get_start_function(instance);
if (!start_function) {
fprintf(stderr, "`_start` function not found\n");
Expand All @@ -163,11 +181,14 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Trap is not NULL: TODO:\n");
return -1;
}
wasi_env_delete(wasi_env);
#endif

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

#ifdef WASI
wasi_env_delete(wasi_env);
wasm_extern_vec_delete(&exports);
#endif
wasm_instance_delete(instance);
wasm_module_delete(module);
wasm_store_delete(store);
Expand Down

0 comments on commit 8215b65

Please sign in to comment.