Skip to content

Commit

Permalink
Replace {environ,size}_{sizes_get,get} C code with Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsanthony committed May 2, 2019
1 parent d6b2fae commit dd3c923
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 92 deletions.
34 changes: 5 additions & 29 deletions wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,22 +454,6 @@ _Static_assert(_Alignof(__wasi_subscription_t) == 8, "non-wasi data layout");
#define WASMTIME_SSP_SYSCALL_NAME(name)
#endif

__wasi_errno_t wasmtime_ssp_args_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *arg_environ,
#endif
char **argv,
char *argv_buf
) WASMTIME_SSP_SYSCALL_NAME(args_get) __attribute__((__warn_unused_result__));

__wasi_errno_t wasmtime_ssp_args_sizes_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *arg_environ,
#endif
size_t *argc,
size_t *argv_buf_size
) WASMTIME_SSP_SYSCALL_NAME(args_sizes_get) __attribute__((__warn_unused_result__));

__wasi_errno_t wasmtime_ssp_clock_res_get(
__wasi_clockid_t clock_id,
__wasi_timestamp_t *resolution
Expand All @@ -481,21 +465,13 @@ __wasi_errno_t wasmtime_ssp_clock_time_get(
__wasi_timestamp_t *time
) WASMTIME_SSP_SYSCALL_NAME(clock_time_get) __attribute__((__warn_unused_result__));

__wasi_errno_t wasmtime_ssp_environ_get(
__wasi_errno_t wasmtime_ssp_fd_prestat_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *arg_environ,
#endif
char **environ,
char *environ_buf
) WASMTIME_SSP_SYSCALL_NAME(environ_get) __attribute__((__warn_unused_result__));

__wasi_errno_t wasmtime_ssp_environ_sizes_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *arg_environ,
struct fd_prestats *prestats,
#endif
size_t *environ_count,
size_t *environ_buf_size
) WASMTIME_SSP_SYSCALL_NAME(environ_sizes_get) __attribute__((__warn_unused_result__));
__wasi_fd_t fd,
__wasi_prestat_t *buf
) WASMTIME_SSP_SYSCALL_NAME(fd_prestat_get) __attribute__((__warn_unused_result__));

__wasi_errno_t wasmtime_ssp_fd_prestat_dir_name(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
Expand Down
54 changes: 0 additions & 54 deletions wasmtime-wasi/sandboxed-system-primitives/src/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2656,60 +2656,6 @@ __wasi_errno_t wasmtime_ssp_sock_shutdown(
return 0;
}

__wasi_errno_t wasmtime_ssp_args_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *argv_environ,
#endif
char **argv,
char *argv_buf
) {
for (size_t i = 0; i < argv_environ->argc; ++i) {
argv[i] = argv_buf + (argv_environ->argv[i] - argv_environ->argv_buf);
}
argv[argv_environ->argc] = NULL;
memcpy(argv_buf, argv_environ->argv_buf, argv_environ->argv_buf_size);
return __WASI_ESUCCESS;
}

__wasi_errno_t wasmtime_ssp_args_sizes_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *argv_environ,
#endif
size_t *argc,
size_t *argv_buf_size
) {
*argc = argv_environ->argc;
*argv_buf_size = argv_environ->argv_buf_size;
return __WASI_ESUCCESS;
}

__wasi_errno_t wasmtime_ssp_environ_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *argv_environ,
#endif
char **environ,
char *environ_buf
) {
for (size_t i = 0; i < argv_environ->environ_count; ++i) {
environ[i] = environ_buf + (argv_environ->environ[i] - argv_environ->environ_buf);
}
environ[argv_environ->environ_count] = NULL;
memcpy(environ_buf, argv_environ->environ_buf, argv_environ->environ_buf_size);
return __WASI_ESUCCESS;
}

__wasi_errno_t wasmtime_ssp_environ_sizes_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct argv_environ_values *argv_environ,
#endif
size_t *environ_count,
size_t *environ_buf_size
) {
*environ_count = argv_environ->environ_count;
*environ_buf_size = argv_environ->environ_buf_size;
return __WASI_ESUCCESS;
}

void argv_environ_init(struct argv_environ_values *argv_environ,
const size_t *argv_offsets, size_t argv_offsets_len,
const char *argv_buf, size_t argv_buf_len,
Expand Down
76 changes: 74 additions & 2 deletions wasmtime-wasi/src/host_impls.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::host;
use super::wasm32;
use errno::{errno, Errno};
use std::slice;

/// Convert POSIX error code to host's WASI error code
fn convert_errno(error: Errno) -> host::__wasi_errno_t {
Expand Down Expand Up @@ -121,7 +121,79 @@ macro_rules! rwlock_unlock {
};
}

pub fn wasmtime_ssp_proc_exit(rval: wasm32::__wasi_exitcode_t) {
pub fn wasmtime_ssp_args_get(
argv_environ: &mut host::argv_environ_values,
argv: &mut [*mut host::char],
argv_buf: &mut [host::char],
) -> host::__wasi_errno_t {
for i in 0..argv_environ.argc {
let buf_off;
unsafe {
buf_off = usize::checked_sub(
argv_environ.argv.offset(i as isize) as _,
argv_environ.argv_buf as _,
)
.expect("argv[i] - argv_buf overflows");
}
argv[i] = argv_buf[buf_off..].as_mut_ptr();
}
argv[argv_environ.argc] = std::ptr::null_mut();
let argv_environ_buf;
unsafe {
argv_environ_buf =
slice::from_raw_parts_mut(argv_environ.argv_buf, argv_environ.argv_buf_size);
}
argv_buf.copy_from_slice(argv_environ_buf);
host::__WASI_ESUCCESS
}

pub fn wasmtime_ssp_args_sizes_get(
argv_environ: &mut host::argv_environ_values,
argc: &mut usize,
argv_buf_size: &mut usize,
) -> host::__wasi_errno_t {
*argc = argv_environ.argc;
*argv_buf_size = argv_environ.argv_buf_size;
host::__WASI_ESUCCESS
}

pub fn wasmtime_ssp_environ_get(
argv_environ: &mut host::argv_environ_values,
environ: &mut [*mut host::char],
environ_buf: &mut [host::char],
) -> host::__wasi_errno_t {
for i in 0..(*argv_environ).environ_count {
let buf_off;
unsafe {
buf_off = usize::checked_sub(
argv_environ.environ.offset(i as isize) as _,
argv_environ.environ_buf as _,
)
.expect("environ[i] - environ_buf overflows");
}
environ[i] = environ_buf[buf_off..].as_mut_ptr();
}
environ[argv_environ.environ_count] = std::ptr::null_mut();
let argv_environ_buf;
unsafe {
argv_environ_buf =
slice::from_raw_parts_mut(argv_environ.environ_buf, argv_environ.environ_buf_size);
}
environ_buf.copy_from_slice(argv_environ_buf);
host::__WASI_ESUCCESS
}

pub fn wasmtime_ssp_environ_sizes_get(
argv_environ: &mut host::argv_environ_values,
environ_count: &mut usize,
environ_buf_size: &mut usize,
) -> host::__wasi_errno_t {
*environ_count = argv_environ.environ_count;
*environ_buf_size = argv_environ.environ_buf_size;
host::__WASI_ESUCCESS
}

pub fn wasmtime_ssp_proc_exit(rval: host::__wasi_exitcode_t) {
::std::process::exit(rval as i32)
}

Expand Down
20 changes: 13 additions & 7 deletions wasmtime-wasi/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ syscalls! {
Err(_) => return wasm32::__WASI_ENOMEM,
};
let argv_buf_size = match cast::u32((*argv_environ).argv_buf_size) {
Ok(argc) => argc,
Ok(argv_buf_size) => argv_buf_size,
Err(_) => return wasm32::__WASI_ENOMEM,
};

Expand All @@ -192,7 +192,10 @@ syscalls! {
let mut host_argv = Vec::new();
host_argv.resize((*argv_environ).argc + 1, ptr::null_mut());

let e = host::wasmtime_ssp_args_get(argv_environ, host_argv.as_mut_ptr(), host_argv_buf);
let e = host_impls::wasmtime_ssp_args_get(
&mut *argv_environ,
host_argv.as_mut_slice(),
slice::from_raw_parts_mut(host_argv_buf, argv_buf_size as usize));

encode_charstar_slice(argv, host_argv, argv_buf, host_argv_buf);

Expand Down Expand Up @@ -223,7 +226,7 @@ syscalls! {
let vmctx = &mut *vmctx;
let argv_environ = get_argv_environ(vmctx);

let e = host::wasmtime_ssp_args_sizes_get(argv_environ, &mut host_argc, &mut host_argv_buf_size);
let e = host_impls::wasmtime_ssp_args_sizes_get(&mut *argv_environ, &mut host_argc, &mut host_argv_buf_size);

trace!(" | *argc={:?}", host_argc);
encode_usize_byref(vmctx, argc, host_argc);
Expand Down Expand Up @@ -311,8 +314,8 @@ syscalls! {
Err(_) => return wasm32::__WASI_ENOMEM,
};

let (host_environ_buf, _environ_buf_len) = match decode_char_slice(vmctx, environ_buf, environ_buf_size) {
Ok((environ_buf, environ_buf_len)) => (environ_buf, environ_buf_len),
let (host_environ_buf, _environ_buf_size) = match decode_char_slice(vmctx, environ_buf, environ_buf_size) {
Ok((environ_buf, environ_buf_size)) => (environ_buf, environ_buf_size),
Err(e) => return return_encoded_errno(e),
};
// Add 1 so that we can add an extra NULL pointer at the end.
Expand All @@ -323,7 +326,10 @@ syscalls! {
let mut host_environ = Vec::new();
host_environ.resize((*argv_environ).environ_count + 1, ptr::null_mut());

let e = host::wasmtime_ssp_environ_get(argv_environ, host_environ.as_mut_ptr(), host_environ_buf);
let e = host_impls::wasmtime_ssp_environ_get(
&mut *argv_environ,
host_environ.as_mut_slice(),
slice::from_raw_parts_mut(host_environ_buf, environ_buf_size as usize));

encode_charstar_slice(environ, host_environ, environ_buf, host_environ_buf);

Expand Down Expand Up @@ -354,7 +360,7 @@ syscalls! {
let vmctx = &mut *vmctx;
let argv_environ = get_argv_environ(vmctx);

let e = host::wasmtime_ssp_environ_sizes_get(argv_environ, &mut host_environ_count, &mut host_environ_buf_size);
let e = host_impls::wasmtime_ssp_environ_sizes_get(&mut *argv_environ, &mut host_environ_count, &mut host_environ_buf_size);

trace!(" | *environ_count={:?}", host_environ_count);
encode_usize_byref(vmctx, environ_count, host_environ_count);
Expand Down

0 comments on commit dd3c923

Please sign in to comment.