Skip to content

Commit 590f34e

Browse files
committed
cli: initialize wasienv memory in create-exe
1 parent 19e0fbf commit 590f34e

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

lib/c-api/src/wasm_c_api/wasi/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
pub use super::unstable::wasi::wasi_get_unordered_imports;
66
use super::{
7-
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t},
7+
externals::{wasm_extern_t, wasm_extern_vec_t, wasm_func_t, wasm_memory_t},
88
instance::wasm_instance_t,
99
module::wasm_module_t,
1010
store::{wasm_store_t, StoreRef},
@@ -197,6 +197,14 @@ pub unsafe extern "C" fn wasi_env_new(
197197
#[no_mangle]
198198
pub extern "C" fn wasi_env_delete(_state: Option<Box<wasi_env_t>>) {}
199199

200+
/// Set the memory on a [`wasi_env_t`].
201+
#[no_mangle]
202+
pub unsafe extern "C" fn wasi_env_set_memory(env: &mut wasi_env_t, memory: &wasm_memory_t) {
203+
let mut store_mut = env.store.store_mut();
204+
let wasi_env = env.inner.data_mut(&mut store_mut);
205+
wasi_env.set_memory(memory.extern_.memory());
206+
}
207+
200208
#[no_mangle]
201209
pub unsafe extern "C" fn wasi_env_read_stdout(
202210
env: &mut wasi_env_t,

lib/cli/src/commands/create_exe.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ impl Default for LinkCode {
242242

243243
impl LinkCode {
244244
fn run(&self) -> anyhow::Result<()> {
245+
let libwasmer_path = self
246+
.libwasmer_path
247+
.canonicalize()
248+
.context("Failed to find libwasmer")?;
249+
println!(
250+
"Using path `{}` as libwasmer path.",
251+
libwasmer_path.display()
252+
);
245253
let mut command = Command::new(&self.linker_path);
246254
let command = command
247255
.arg(&self.optimization_flag)
@@ -250,12 +258,7 @@ impl LinkCode {
250258
.iter()
251259
.map(|path| path.canonicalize().unwrap()),
252260
)
253-
.arg(
254-
&self
255-
.libwasmer_path
256-
.canonicalize()
257-
.context("Failed to find libwasmer")?,
258-
);
261+
.arg(&libwasmer_path);
259262
let command = if let Some(target) = &self.target {
260263
command.arg("-target").arg(format!("{}", target))
261264
} else {
@@ -272,11 +275,11 @@ impl LinkCode {
272275
// On unix we need dlopen-related symbols, libmath for a few things, and pthreads.
273276
#[cfg(not(windows))]
274277
let command = command.arg("-ldl").arg("-lm").arg("-pthread");
275-
let link_aganist_extra_libs = self
278+
let link_against_extra_libs = self
276279
.additional_libraries
277280
.iter()
278281
.map(|lib| format!("-l{}", lib));
279-
let command = command.args(link_aganist_extra_libs);
282+
let command = command.args(link_against_extra_libs);
280283
let output = command.arg("-o").arg(&self.output_path).output()?;
281284

282285
if !output.status.success() {

lib/cli/src/commands/wasmer_create_exe_main.c

+22-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ int main(int argc, char *argv[]) {
149149
}
150150

151151
#ifdef WASI
152+
// Read the exports.
153+
wasm_extern_vec_t exports;
154+
wasm_instance_exports(instance, &exports);
155+
wasm_memory_t* mem = NULL;
156+
for (size_t i = 0; i < exports.size; i++) {
157+
mem = wasm_extern_as_memory(exports.data[i]);
158+
if (mem) {
159+
break;
160+
}
161+
}
162+
163+
if (!mem) {
164+
fprintf(stderr, "Failed to create instance: Could not find memory in exports\n");
165+
print_wasmer_error();
166+
return -1;
167+
}
168+
wasi_env_set_memory(wasi_env, mem);
169+
152170
own wasm_func_t *start_function = wasi_get_start_function(instance);
153171
if (!start_function) {
154172
fprintf(stderr, "`_start` function not found\n");
@@ -163,11 +181,14 @@ int main(int argc, char *argv[]) {
163181
fprintf(stderr, "Trap is not NULL: TODO:\n");
164182
return -1;
165183
}
166-
wasi_env_delete(wasi_env);
167184
#endif
168185

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

188+
#ifdef WASI
189+
wasi_env_delete(wasi_env);
190+
wasm_extern_vec_delete(&exports);
191+
#endif
171192
wasm_instance_delete(instance);
172193
wasm_module_delete(module);
173194
wasm_store_delete(store);

0 commit comments

Comments
 (0)