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 Apr 28, 2019
1 parent 69e44ca commit da5916e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 78 deletions.
32 changes: 0 additions & 32 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,22 +465,6 @@ __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(
#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,
#endif
size_t *environ_count,
size_t *environ_buf_size
) WASMTIME_SSP_SYSCALL_NAME(environ_sizes_get) __attribute__((__warn_unused_result__));

__wasi_errno_t wasmtime_ssp_fd_prestat_get(
#if !defined(WASMTIME_SSP_STATIC_CURFDS)
struct fd_prestats *prestats,
Expand Down
42 changes: 0 additions & 42 deletions wasmtime-wasi/sandboxed-system-primitives/src/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -2690,48 +2690,6 @@ __wasi_errno_t wasmtime_ssp_sched_yield(void) {
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,
Expand Down
60 changes: 60 additions & 0 deletions wasmtime-wasi/src/host_impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,65 @@
use super::host;
use super::wasm32;

pub unsafe fn wasmtime_ssp_args_get(
argv_environ: &mut host::argv_environ_values,
argv: &mut [*mut host::char],
argv_buf: *mut host::char,
) -> wasm32::__wasi_errno_t {
for i in 0..(*argv_environ).argc {
let buf_off = isize::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.offset(buf_off);
}
argv[(*argv_environ).argc] = std::ptr::null_mut();
argv_buf.copy_from((*argv_environ).argv_buf, (*argv_environ).argv_buf_size);
wasm32::__WASI_ESUCCESS
}

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

pub unsafe fn wasmtime_ssp_environ_get(
argv_environ: &mut host::argv_environ_values,
environ: &mut [*mut host::char],
environ_buf: *mut host::char,
) -> wasm32::__wasi_errno_t {
for i in 0..(*argv_environ).environ_count {
let buf_off = isize::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.offset(buf_off);
}
environ[(*argv_environ).environ_count] = std::ptr::null_mut();
environ_buf.copy_from(
(*argv_environ).environ_buf,
(*argv_environ).environ_buf_size,
);
wasm32::__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,
) -> wasm32::__wasi_errno_t {
*environ_count = (*argv_environ).environ_count;
*environ_buf_size = (*argv_environ).environ_buf_size;
wasm32::__WASI_ESUCCESS
}

pub fn wasmtime_ssp_proc_exit(rval: wasm32::__wasi_exitcode_t) {
::std::process::exit(rval as i32)
}
8 changes: 4 additions & 4 deletions wasmtime-wasi/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ 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(), host_argv_buf);

encode_charstar_slice(argv, host_argv, argv_buf, host_argv_buf);

Expand Down Expand Up @@ -223,7 +223,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 @@ -323,7 +323,7 @@ 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(), host_environ_buf);

encode_charstar_slice(environ, host_environ, environ_buf, host_environ_buf);

Expand Down Expand Up @@ -354,7 +354,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 da5916e

Please sign in to comment.