Add env and args syscalls

This commit is contained in:
Lachlan Sneff 2019-03-28 12:56:11 -07:00
parent 94674e9d80
commit dbc4176964
3 changed files with 88 additions and 13 deletions

View File

@ -7,7 +7,7 @@ use std::ffi::c_void;
use wasmer_runtime_core::{func, import::ImportObject, imports};
pub fn generate_import_object(args: Vec<u8>, envs: Vec<u8>) -> ImportObject {
pub fn generate_import_object(args: Vec<Vec<u8>>, envs: Vec<Vec<u8>>) -> ImportObject {
let state_gen = move || {
fn state_dtor(data: *mut c_void) {
unsafe {

View File

@ -1,5 +1,5 @@
pub struct WasiState<'a> {
// vfs: Vfs,
pub args: &'a [u8],
pub envs: &'a [u8],
pub args: &'a [Vec<u8>],
pub envs: &'a [Vec<u8>],
}

View File

@ -1,28 +1,103 @@
use crate::state::WasiState;
use wasmer_runtime_core::vm::Ctx;
use wasmer_runtime_core::{memory::Memory, vm::Ctx};
fn get_wasi_state(ctx: &mut Ctx) -> &mut WasiState {
fn get_wasi_state(ctx: &Ctx) -> &mut WasiState {
unsafe { &mut *(ctx.data as *mut WasiState) }
}
pub fn __wasi_args_get(ctx: &mut Ctx) {
unimplemented!()
fn write_buffer_array(
memory: &Memory,
from: &[Vec<u8>],
ptr_buffer_offset: u32,
buffer_offset: u32,
) {
let mut current_buffer_offset = buffer_offset;
for (i, sub_buffer) in from.iter().enumerate() {
memory.view::<u32>()[(ptr_buffer_offset as usize)..][i].set(current_buffer_offset);
for (cell, &byte) in memory.view()[(current_buffer_offset as usize)..]
.iter()
.zip(sub_buffer.iter())
{
cell.set(byte);
}
current_buffer_offset += sub_buffer.len() as u32;
}
}
pub fn __wasi_args_sizes_get(ctx: &mut Ctx) {
unimplemented!()
/// ### `__wasi_args_get()`
/// Read command-line argument data.
/// The sizes of the buffers should match that returned by [`__wasi_args_sizes_get()`](#args_sizes_get).
/// Inputs:
/// - `char **argv`
/// A pointer to a buffer to write the argument pointers.
/// - `char *argv_buf`
/// A pointer to a buffer to write the argument string data.
///
pub fn __wasi_args_get(ctx: &mut Ctx, ptr_buffer_offset: u32, buffer_offset: u32) {
let state = get_wasi_state(ctx);
let memory = ctx.memory(0);
write_buffer_array(memory, &*state.args, ptr_buffer_offset, buffer_offset);
}
/// ### `__wasi_args_sizes_get()`
/// Return command-line argument data sizes.
/// Outputs:
/// - `size_t *argc`
/// The number of arguments.
/// - `size_t *argv_buf_size`
/// The size of the argument string data.
pub fn __wasi_args_sizes_get(ctx: &mut Ctx, argc_out: u32, argv_buf_size_out: u32) {
let state = get_wasi_state(ctx);
let memory = ctx.memory(0);
let arg_count = state.args.len();
let total_arg_size: usize = state.args.iter().map(|v| v.len()).sum();
memory.view::<u32>()[(argc_out / 4) as usize].set(arg_count as u32);
memory.view::<u32>()[(argv_buf_size_out / 4) as usize].set(total_arg_size as u32);
}
pub fn __wasi_clock_res_get(ctx: &mut Ctx) {
unimplemented!()
}
pub fn __wasi_clock_time_get(ctx: &mut Ctx) {
unimplemented!()
}
pub fn __wasi_environ_get(ctx: &mut Ctx) {
unimplemented!()
/// ### `__wasi_environ_get()`
/// Read environment variable data.
/// The sizes of the buffers should match that returned by [`__wasi_environ_sizes_get()`](#environ_sizes_get).
/// Inputs:
/// - `char **environ`
/// A pointer to a buffer to write the environment variable pointers.
/// - `char *environ_buf`
/// A pointer to a buffer to write the environment variable string data.
pub fn __wasi_environ_get(ctx: &mut Ctx, environ: u32, environ_buf: u32) {
let state = get_wasi_state(ctx);
let memory = ctx.memory(0);
write_buffer_array(memory, &*state.args, environ, environ_buf);
}
pub fn __wasi_environ_sizes_get(ctx: &mut Ctx) {
unimplemented!()
/// ### `__wasi_environ_sizes_get()`
/// Return command-line argument data sizes.
/// Outputs:
/// - `size_t *environ_count`
/// The number of environment variables.
/// - `size_t *environ_buf_size`
/// The size of the environment variable string data.
pub fn __wasi_environ_sizes_get(ctx: &mut Ctx, environ_count_out: u32, environ_buf_size_out: u32) {
let state = get_wasi_state(ctx);
let memory = ctx.memory(0);
let env_count = state.envs.len();
let total_env_size: usize = state.envs.iter().map(|v| v.len()).sum();
memory.view::<u32>()[(environ_count_out / 4) as usize].set(env_count as u32);
memory.view::<u32>()[(environ_buf_size_out / 4) as usize].set(total_env_size as u32);
}
pub fn __wasi_fd_advise(ctx: &mut Ctx) {
unimplemented!()
}