From 8a2dba534ac2be43ffb577f314acfb38bc64c49e Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Wed, 28 Aug 2019 13:19:06 -0700 Subject: [PATCH] Improve error handling, fix probable lifetime issue --- lib/wasi/src/lib.rs | 8 ++++---- lib/wasi/src/state/mod.rs | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index f8294f83e..ce2059c6f 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -41,20 +41,20 @@ pub fn generate_import_object( mapped_dirs: Vec<(String, PathBuf)>, ) -> ImportObject { let state_gen = move || { + // TODO: look into removing all these unnecessary clones fn state_destructor(data: *mut c_void) { - dbg!("IN DESTRUCTOR"); - dbg!(&data); unsafe { drop(Box::from_raw(data as *mut WasiState)); } } + let preopened_files = preopened_files.clone(); + let mapped_dirs = mapped_dirs.clone(); let state = Box::new(WasiState { - fs: WasiFs::new(&preopened_files, &mapped_dirs).unwrap(), + fs: WasiFs::new(&preopened_files, &mapped_dirs).expect("Could not create WASI FS"), args: args.clone(), envs: envs.clone(), }); - dbg!("IN CONSTRUCTOR"); ( Box::into_raw(state) as *mut c_void, diff --git a/lib/wasi/src/state/mod.rs b/lib/wasi/src/state/mod.rs index a4154733d..64a5aee84 100644 --- a/lib/wasi/src/state/mod.rs +++ b/lib/wasi/src/state/mod.rs @@ -168,7 +168,7 @@ impl WasiFs { let inode = wasi_fs.create_virtual_root(); let fd = wasi_fs .create_fd(root_rights, root_rights, 0, Fd::READ, inode) - .expect("Could not create root fd"); + .map_err(|e| format!("Could not create root fd: {}", e))?; wasi_fs.preopen_fds.push(fd); inode }; @@ -179,7 +179,13 @@ impl WasiFs { // TODO: think about this let default_rights = 0x1FFFFFFF; // all rights let cur_dir = PathBuf::from(dir); - let cur_dir_metadata = cur_dir.metadata().expect("Could not find directory"); + let cur_dir_metadata = cur_dir.metadata().map_err(|e| { + format!( + "Could not get metadata for file {:?}: {}", + dir, + e.to_string() + ) + })?; let kind = if cur_dir_metadata.is_dir() { Kind::Dir { parent: Some(root_inode), @@ -209,7 +215,7 @@ impl WasiFs { Fd::READ | Fd::WRITE, inode, ) - .expect("Could not open fd"); + .map_err(|e| format!("Could not open fd for file {:?}: {}", dir, e))?; if let Kind::Root { entries } = &mut wasi_fs.inodes[root_inode].kind { // todo handle collisions assert!(entries.insert(dir.to_string(), inode).is_none()) @@ -221,9 +227,13 @@ impl WasiFs { debug!("Attempting to open {:?} at {}", real_dir, alias); // TODO: think about this let default_rights = 0x1FFFFFFF; // all rights - let cur_dir_metadata = real_dir - .metadata() - .expect("mapped dir not at previously verified location"); + let cur_dir_metadata = real_dir.metadata().map_err(|e| { + format!( + "Could not get metadata for file {:?}: {}", + &real_dir, + e.to_string() + ) + })?; let kind = if cur_dir_metadata.is_dir() { Kind::Dir { parent: Some(root_inode), @@ -253,7 +263,7 @@ impl WasiFs { Fd::READ | Fd::WRITE, inode, ) - .expect("Could not open fd"); + .map_err(|e| format!("Could not open fd for file {:?}: {}", &real_dir, e))?; if let Kind::Root { entries } = &mut wasi_fs.inodes[root_inode].kind { // todo handle collisions assert!(entries.insert(alias.clone(), inode).is_none());