225 lines
5.8 KiB
Rust
Raw Normal View History

pub mod types;
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub mod unix;
#[cfg(any(target_os = "windows"))]
pub mod windows;
2019-03-28 12:18:05 -07:00
use crate::state::WasiState;
use types::*;
2019-03-28 12:56:11 -07:00
use wasmer_runtime_core::{memory::Memory, vm::Ctx};
2019-03-28 11:54:22 -07:00
#[cfg(any(target_os = "linux", target_os = "macos"))]
pub use unix::*;
#[cfg(any(target_os = "windows"))]
pub use windows::*;
2019-03-28 13:10:22 -07:00
#[allow(clippy::mut_from_ref)]
2019-03-28 12:56:11 -07:00
fn get_wasi_state(ctx: &Ctx) -> &mut WasiState {
2019-03-28 12:18:05 -07:00
unsafe { &mut *(ctx.data as *mut WasiState) }
}
2019-03-28 12:56:11 -07:00
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;
}
2019-03-28 11:54:22 -07:00
}
2019-03-28 12:56:11 -07:00
2019-03-28 15:17:52 -07:00
/// ### `args_get()`
2019-03-28 12:56:11 -07:00
/// Read command-line argument data.
2019-03-28 15:17:52 -07:00
/// The sizes of the buffers should match that returned by [`args_sizes_get()`](#args_sizes_get).
2019-03-28 12:56:11 -07:00
/// 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.
///
2019-03-28 15:17:52 -07:00
pub fn args_get(ctx: &mut Ctx, ptr_buffer_offset: u32, buffer_offset: u32) {
2019-03-28 12:56:11 -07:00
let state = get_wasi_state(ctx);
let memory = ctx.memory(0);
write_buffer_array(memory, &*state.args, ptr_buffer_offset, buffer_offset);
2019-03-28 11:54:22 -07:00
}
2019-03-28 12:56:11 -07:00
2019-03-28 15:17:52 -07:00
/// ### `args_sizes_get()`
2019-03-28 12:56:11 -07:00
/// 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.
2019-03-28 15:17:52 -07:00
pub fn args_sizes_get(ctx: &mut Ctx, argc_out: u32, argv_buf_size_out: u32) {
2019-03-28 12:56:11 -07:00
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);
}
2019-03-28 15:17:52 -07:00
/// ### `environ_get()`
2019-03-28 12:56:11 -07:00
/// Read environment variable data.
2019-03-28 15:17:52 -07:00
/// The sizes of the buffers should match that returned by [`environ_sizes_get()`](#environ_sizes_get).
2019-03-28 12:56:11 -07:00
/// 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.
2019-03-28 15:17:52 -07:00
pub fn environ_get(ctx: &mut Ctx, environ: u32, environ_buf: u32) {
2019-03-28 12:56:11 -07:00
let state = get_wasi_state(ctx);
let memory = ctx.memory(0);
write_buffer_array(memory, &*state.args, environ, environ_buf);
2019-03-28 11:54:22 -07:00
}
2019-03-28 12:56:11 -07:00
2019-03-28 15:17:52 -07:00
/// ### `environ_sizes_get()`
2019-03-28 12:56:11 -07:00
/// 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.
2019-03-28 15:17:52 -07:00
pub fn environ_sizes_get(ctx: &mut Ctx, environ_count_out: u32, environ_buf_size_out: u32) {
2019-03-28 12:56:11 -07:00
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);
2019-03-28 11:54:22 -07:00
}
2019-03-28 12:56:11 -07:00
2019-03-28 15:17:52 -07:00
pub fn fd_advise(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_allocate(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_close(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_datasync(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_fdstat_get(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_fdstat_set_flags(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_fdstat_set_rights(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_filestat_get(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_filestat_set_size(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_filestat_set_times(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_pread(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_prestat_get(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_prestat_dir_name(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_pwrite(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_read(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_readdir(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_renumber(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_seek(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_sync(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_tell(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn fd_write(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_create_directory(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_filestat_get(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_filestat_set_times(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_link(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_open(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_readlink(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_remove_directory(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_rename(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_symlink(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn path_unlink_file(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn poll_oneoff(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn proc_exit(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn proc_raise(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn random_get(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn sched_yield(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn sock_recv(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn sock_send(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}
2019-03-28 15:17:52 -07:00
pub fn sock_shutdown(ctx: &mut Ctx) {
2019-03-28 11:54:22 -07:00
unimplemented!()
}