diff --git a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h b/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h index 49af8252508b..086a63a36743 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,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, diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c b/wasmtime-wasi/sandboxed-system-primitives/src/posix.c index 11e88fbec514..f836779fdb03 100644 --- a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c +++ b/wasmtime-wasi/sandboxed-system-primitives/src/posix.c @@ -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, diff --git a/wasmtime-wasi/src/host_impls.rs b/wasmtime-wasi/src/host_impls.rs index 8383a41372b7..e3824839b250 100644 --- a/wasmtime-wasi/src/host_impls.rs +++ b/wasmtime-wasi/src/host_impls.rs @@ -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) } diff --git a/wasmtime-wasi/src/syscalls.rs b/wasmtime-wasi/src/syscalls.rs index 7901f5531a76..5f8141967fda 100644 --- a/wasmtime-wasi/src/syscalls.rs +++ b/wasmtime-wasi/src/syscalls.rs @@ -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); @@ -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); @@ -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); @@ -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);