WASI paths as &str and String (#37)

* Check if RawString operates on valid encodings

* Use &str and String for WASI paths
This commit is contained in:
Jakub Konka
2019-07-19 20:09:27 +02:00
committed by Dan Gohman
parent c3994bf57b
commit 08aa61f066
8 changed files with 207 additions and 294 deletions

View File

@@ -3,8 +3,7 @@ use super::return_enc_errno;
use crate::ctx::WasiCtx;
use crate::fdentry::Descriptor;
use crate::memory::*;
use crate::sys::host_impl::RawString;
use crate::sys::{errno_from_host, hostcalls_impl};
use crate::sys::{errno_from_host, host_impl, hostcalls_impl};
use crate::{host, wasm32};
use log::trace;
use std::convert::identity;
@@ -593,14 +592,15 @@ pub fn path_create_directory(
);
let dirfd = dec_fd(dirfd);
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
Ok(slice) => RawString::from_bytes(slice),
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (path_ptr,path_len)={:?}", path);
trace!(" | (path_ptr,path_len)='{}'", path);
let ret = match hostcalls_impl::path_create_directory(wasi_ctx, dirfd, &path) {
let ret = match hostcalls_impl::path_create_directory(wasi_ctx, dirfd, path) {
Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e,
};
@@ -633,24 +633,28 @@ pub fn path_link(
let old_dirfd = dec_fd(old_dirfd);
let new_dirfd = dec_fd(new_dirfd);
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len) {
Ok(slice) => RawString::from_bytes(slice),
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len)
.and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len) {
Ok(slice) => RawString::from_bytes(slice),
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len)
.and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (old_path_ptr,old_path_len)={:?}", old_path);
trace!(" | (new_path_ptr,new_path_len)={:?}", new_path);
trace!(" | (old_path_ptr,old_path_len)='{}'", old_path);
trace!(" | (new_path_ptr,new_path_len)='{}'", new_path);
let ret = match hostcalls_impl::path_link(
wasi_ctx,
old_dirfd,
new_dirfd,
&old_path,
&new_path,
old_path,
new_path,
host::__WASI_RIGHT_PATH_LINK_SOURCE,
host::__WASI_RIGHT_PATH_LINK_TARGET,
) {
@@ -708,18 +712,19 @@ pub fn path_open(
let needed_base = host::__WASI_RIGHT_PATH_OPEN;
let needed_inheriting = fs_rights_base | fs_rights_inheriting;
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
Ok(slice) => RawString::from_bytes(slice),
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (path_ptr,path_len)={:?}", path);
trace!(" | (path_ptr,path_len)='{}'", path);
let ret = match hostcalls_impl::path_open(
wasi_ctx,
dirfd,
dirflags,
&path,
path,
oflags,
read,
write,
@@ -829,12 +834,12 @@ pub fn path_readlink(
Err(e) => return return_enc_errno(e),
};
let dirfd = dec_fd(dirfd);
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
Ok(slice) => RawString::from_bytes(slice),
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_vec) {
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (path_ptr,path_len)={:?}", path);
trace!(" | (path_ptr,path_len)='{}'", &path);
let mut buf = match dec_slice_of_mut::<u8>(memory, buf_ptr, buf_len) {
Ok(slice) => slice,
@@ -884,23 +889,27 @@ pub fn path_rename(
let old_dirfd = dec_fd(old_dirfd);
let new_dirfd = dec_fd(new_dirfd);
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len) {
Ok(slice) => RawString::from_bytes(slice),
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len)
.and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len) {
Ok(slice) => RawString::from_bytes(slice),
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len)
.and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (old_path_ptr,old_path_len)={:?}", old_path);
trace!(" | (new_path_ptr,new_path_len)={:?}", new_path);
trace!(" | (old_path_ptr,old_path_len)='{}'", old_path);
trace!(" | (new_path_ptr,new_path_len)='{}'", new_path);
let old_rights = host::__WASI_RIGHT_PATH_RENAME_SOURCE;
let new_rights = host::__WASI_RIGHT_PATH_RENAME_TARGET;
let ret = match hostcalls_impl::path_rename(
wasi_ctx, old_dirfd, &old_path, old_rights, new_dirfd, &new_path, new_rights,
wasi_ctx, old_dirfd, old_path, old_rights, new_dirfd, new_path, new_rights,
) {
Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e,
@@ -1025,14 +1034,15 @@ pub fn path_filestat_get(
let dirfd = dec_fd(dirfd);
let dirflags = dec_lookupflags(dirflags);
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
Ok(slice) => RawString::from_bytes(slice),
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (path_ptr,path_len)={:?}", path);
trace!(" | (path_ptr,path_len)='{}'", path);
let host_filestat = match hostcalls_impl::path_filestat_get(wasi_ctx, dirfd, dirflags, &path) {
let host_filestat = match hostcalls_impl::path_filestat_get(wasi_ctx, dirfd, dirflags, path) {
Ok(host_filestat) => host_filestat,
Err(e) => return return_enc_errno(e),
};
@@ -1071,12 +1081,13 @@ pub fn path_filestat_set_times(
let dirfd = dec_fd(dirfd);
let dirflags = dec_lookupflags(dirflags);
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
Ok(slice) => RawString::from_bytes(slice),
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (path_ptr,path_len)={:?}", path);
trace!(" | (path_ptr,path_len)='{}'", path);
let rights = host::__WASI_RIGHT_PATH_FILESTAT_SET_TIMES;
let st_atim = dec_timestamp(st_atim);
@@ -1084,7 +1095,7 @@ pub fn path_filestat_set_times(
let fst_flags = dec_fstflags(fst_flags);
let ret = match hostcalls_impl::path_filestat_set_times(
wasi_ctx, dirfd, dirflags, &path, rights, st_atim, st_mtim, fst_flags,
wasi_ctx, dirfd, dirflags, path, rights, st_atim, st_mtim, fst_flags,
) {
Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e,
@@ -1113,21 +1124,25 @@ pub fn path_symlink(
);
let dirfd = dec_fd(dirfd);
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len) {
Ok(slice) => RawString::from_bytes(slice),
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len)
.and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len) {
Ok(slice) => RawString::from_bytes(slice),
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len)
.and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (old_path_ptr,old_path_len)={:?}", old_path);
trace!(" | (new_path_ptr,new_path_len)={:?}", new_path);
trace!(" | (old_path_ptr,old_path_len)='{}'", old_path);
trace!(" | (new_path_ptr,new_path_len)='{}'", new_path);
let rights = host::__WASI_RIGHT_PATH_SYMLINK;
let ret = match hostcalls_impl::path_symlink(wasi_ctx, dirfd, rights, &old_path, &new_path) {
let ret = match hostcalls_impl::path_symlink(wasi_ctx, dirfd, rights, old_path, new_path) {
Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e,
};
@@ -1151,17 +1166,18 @@ pub fn path_unlink_file(
);
let dirfd = dec_fd(dirfd);
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
Ok(slice) => RawString::from_bytes(slice),
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (path_ptr,path_len)={:?}", path);
trace!(" | (path_ptr,path_len)='{}'", path);
let ret = match hostcalls_impl::path_unlink_file(
wasi_ctx,
dirfd,
&path,
path,
host::__WASI_RIGHT_PATH_UNLINK_FILE,
) {
Ok(()) => host::__WASI_ESUCCESS,
@@ -1187,16 +1203,17 @@ pub fn path_remove_directory(
);
let dirfd = dec_fd(dirfd);
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
Ok(slice) => RawString::from_bytes(slice),
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)
{
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
trace!(" | (path_ptr,path_len)={:?}", path);
trace!(" | (path_ptr,path_len)='{}'", path);
let rights = host::__WASI_RIGHT_PATH_REMOVE_DIRECTORY;
let ret = match hostcalls_impl::path_remove_directory(wasi_ctx, dirfd, &path, rights) {
let ret = match hostcalls_impl::path_remove_directory(wasi_ctx, dirfd, path, rights) {
Ok(()) => host::__WASI_ESUCCESS,
Err(e) => e,
};
@@ -1225,6 +1242,12 @@ pub fn fd_prestat_get(
if fe.fd_object.file_type != host::__WASI_FILETYPE_DIRECTORY {
return return_enc_errno(host::__WASI_ENOTDIR);
}
let path = match host_impl::path_from_host(po_path.as_os_str()) {
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
enc_prestat_byref(
memory,
prestat_ptr,
@@ -1232,7 +1255,7 @@ pub fn fd_prestat_get(
pr_type: host::__WASI_PREOPENTYPE_DIR,
u: host::__wasi_prestat_t___wasi_prestat_u {
dir: host::__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t {
pr_name_len: RawString::from(po_path.as_ref()).to_bytes().len(),
pr_name_len: path.len(),
},
},
},
@@ -1272,14 +1295,19 @@ pub fn fd_prestat_dir_name(
if fe.fd_object.file_type != host::__WASI_FILETYPE_DIRECTORY {
return return_enc_errno(host::__WASI_ENOTDIR);
}
let path_bytes = RawString::from(po_path.as_ref()).to_bytes();
if path_bytes.len() > dec_usize(path_len) {
let path = match host_impl::path_from_host(po_path.as_os_str()) {
Ok(path) => path,
Err(e) => return return_enc_errno(e),
};
if path.len() > dec_usize(path_len) {
return return_enc_errno(host::__WASI_ENAMETOOLONG);
}
trace!(" | (path_ptr,path_len)={:?}", po_path);
trace!(" | (path_ptr,path_len)='{}'", path);
enc_slice_of(memory, &path_bytes, path_ptr)
enc_slice_of(memory, path.as_bytes(), path_ptr)
.map(|_| host::__WASI_ESUCCESS)
.unwrap_or_else(identity)
} else {