From 3c7687fa33096f7d85b83c7452647716d65682c8 Mon Sep 17 00:00:00 2001 From: Anthony Romano Date: Sat, 27 Apr 2019 06:41:13 -0700 Subject: [PATCH] Replace {environ,size}_{sizes_get,get} C code with Rust --- .../include/wasmtime_ssp.h | 34 ++------- .../sandboxed-system-primitives/src/posix.c | 54 ------------- wasmtime-wasi/src/host_impls.rs | 76 ++++++++++++++++++- wasmtime-wasi/src/syscalls.rs | 18 +++-- 4 files changed, 91 insertions(+), 91 deletions(-) diff --git a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h b/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h index 660c4b2538ae..95717657c6c4 100644 --- a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h +++ b/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h @@ -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 @@ -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) diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c b/wasmtime-wasi/sandboxed-system-primitives/src/posix.c index 95b5846009b8..9c32b0c0f5ce 100644 --- a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c +++ b/wasmtime-wasi/sandboxed-system-primitives/src/posix.c @@ -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, diff --git a/wasmtime-wasi/src/host_impls.rs b/wasmtime-wasi/src/host_impls.rs index c73b1e845a14..38e1c609721a 100644 --- a/wasmtime-wasi/src/host_impls.rs +++ b/wasmtime-wasi/src/host_impls.rs @@ -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 { @@ -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(); + unsafe { + argv_buf.copy_from_slice(slice::from_raw_parts_mut( + (*argv_environ).argv_buf, + (*argv_environ).argv_buf_size, + )); + } + 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(); + unsafe { + environ_buf.copy_from_slice(slice::from_raw_parts_mut( + (*argv_environ).environ_buf, + (*argv_environ).environ_buf_size, + )); + } + 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) } diff --git a/wasmtime-wasi/src/syscalls.rs b/wasmtime-wasi/src/syscalls.rs index 0151095976e4..d6b325c60d03 100644 --- a/wasmtime-wasi/src/syscalls.rs +++ b/wasmtime-wasi/src/syscalls.rs @@ -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); @@ -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); @@ -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. @@ -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); @@ -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);