Skip to content

Commit

Permalink
introduce proc_exec2
Browse files Browse the repository at this point in the history
  • Loading branch information
maminrayej committed May 27, 2024
1 parent 8bc7267 commit 35186ad
Show file tree
Hide file tree
Showing 6 changed files with 344 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/wasix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ fn wasix_exports_32(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>)
"proc_join" => Function::new_typed_with_env(&mut store, env, proc_join::<Memory32>),
"proc_signal" => Function::new_typed_with_env(&mut store, env, proc_signal::<Memory32>),
"proc_exec" => Function::new_typed_with_env(&mut store, env, proc_exec::<Memory32>),
"proc_exec2" => Function::new_typed_with_env(&mut store, env, proc_exec2::<Memory32>),
"proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
"proc_raise_interval" => Function::new_typed_with_env(&mut store, env, proc_raise_interval),
"proc_spawn" => Function::new_typed_with_env(&mut store, env, proc_spawn::<Memory32>),
Expand Down Expand Up @@ -652,6 +653,7 @@ fn wasix_exports_64(mut store: &mut impl AsStoreMut, env: &FunctionEnv<WasiEnv>)
"proc_join" => Function::new_typed_with_env(&mut store, env, proc_join::<Memory64>),
"proc_signal" => Function::new_typed_with_env(&mut store, env, proc_signal::<Memory64>),
"proc_exec" => Function::new_typed_with_env(&mut store, env, proc_exec::<Memory64>),
"proc_exec2" => Function::new_typed_with_env(&mut store, env, proc_exec2::<Memory64>),
"proc_raise" => Function::new_typed_with_env(&mut store, env, proc_raise),
"proc_raise_interval" => Function::new_typed_with_env(&mut store, env, proc_raise_interval),
"proc_spawn" => Function::new_typed_with_env(&mut store, env, proc_spawn::<Memory64>),
Expand Down
51 changes: 49 additions & 2 deletions lib/wasix/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ pub(crate) use self::types::{
},
*,
};
use self::{state::WasiInstanceGuardMemory, utils::WasiDummyWaker};
use self::{
state::{conv_env_vars, WasiInstanceGuardMemory},
utils::WasiDummyWaker,
};
pub(crate) use crate::os::task::{
process::{WasiProcessId, WasiProcessWait},
thread::{WasiThread, WasiThreadId},
Expand Down Expand Up @@ -1471,12 +1474,56 @@ where
}

// Function to prepare the WASI environment
pub(crate) fn _prepare_wasi(wasi_env: &mut WasiEnv, args: Option<Vec<String>>) {
pub(crate) fn _prepare_wasi(
wasi_env: &mut WasiEnv,
args: Option<Vec<String>>,
envs: Option<Vec<(String, String)>>,
) {
let mut state_is_forked = false;

// Swap out the arguments with the new ones
if let Some(args) = args {
let mut wasi_state = wasi_env.state.fork();
wasi_state.args = args;
wasi_env.state = Arc::new(wasi_state);
state_is_forked = true;
}

// Append the new env vars to the old ones
if let Some(envs) = envs {
if !state_is_forked {
let mut wasi_state = wasi_env.state.fork();
wasi_env.state = Arc::new(wasi_state);
state_is_forked = true;
}

// append new env vars and replace old ones if they exist
let mut guard = wasi_env.state.envs.lock().unwrap();

let mut env_map = guard
.iter()
.map(|b| {
let string = String::from_utf8_lossy(b);
let (key, val) = string.split_once('=').expect("env var is malformed");

(key.to_string(), val.to_string())
})
.collect::<HashMap<_, _>>();

for (key, val) in envs {
env_map.insert(key, val);
}

let envs = env_map
.into_iter()
.map(|(k, v)| (k, v.as_bytes().to_vec()))
.collect();

let envs = conv_env_vars(envs);

*guard = envs;

drop(guard)
}

// Close any files after the STDERR that are not preopened
Expand Down
7 changes: 7 additions & 0 deletions lib/wasix/src/syscalls/wasi/environ_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@ pub fn environ_get<M: MemorySize>(
let (memory, mut state) = unsafe { env.get_memory_and_wasi_state(&ctx, 0) };

let envs = state.envs.lock().unwrap();
println!(
"environ_get: {:?}",
envs.deref()
.iter()
.map(|e| String::from_utf8_lossy(e.as_slice()))
.collect::<Vec<_>>()
);
Ok(write_buffer_array(&memory, &envs, environ, environ_buf))
}
2 changes: 2 additions & 0 deletions lib/wasix/src/syscalls/wasix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod port_route_list;
mod port_route_remove;
mod port_unbridge;
mod proc_exec;
mod proc_exec2;
mod proc_fork;
mod proc_id;
mod proc_join;
Expand Down Expand Up @@ -90,6 +91,7 @@ pub use port_route_list::*;
pub use port_route_remove::*;
pub use port_unbridge::*;
pub use proc_exec::*;
pub use proc_exec2::*;
pub use proc_fork::*;
pub use proc_id::*;
pub use proc_join::*;
Expand Down
5 changes: 3 additions & 2 deletions lib/wasix/src/syscalls/wasix/proc_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub fn proc_exec<M: MemorySize>(
args: WasmPtr<u8, M>,
args_len: M::Offset,
) -> Result<(), WasiError> {
println!("proc_exec");
WasiEnv::process_signals_and_exit(&mut ctx)?;

// If we were just restored the stack then we were woken after a deep sleep
Expand Down Expand Up @@ -89,7 +90,7 @@ pub fn proc_exec<M: MemorySize>(
std::mem::swap(vfork.env.as_mut(), ctx.data_mut());
let mut wasi_env = *vfork.env;
wasi_env.owned_handles.push(vfork.handle);
_prepare_wasi(&mut wasi_env, Some(args));
_prepare_wasi(&mut wasi_env, Some(args), None);

// Recrod the stack offsets before we give up ownership of the wasi_env
let stack_lower = wasi_env.layout.stack_lower;
Expand Down Expand Up @@ -185,7 +186,7 @@ pub fn proc_exec<M: MemorySize>(
else {
// Prepare the environment
let mut wasi_env = ctx.data().clone();
_prepare_wasi(&mut wasi_env, Some(args));
_prepare_wasi(&mut wasi_env, Some(args), None);

// Get a reference to the runtime
let bin_factory = ctx.data().bin_factory.clone();
Expand Down
Loading

0 comments on commit 35186ad

Please sign in to comment.