use std::collections::HashMap;
use std::env;
+use std::path::Path;
use crate::stacked_borrows::Tag;
use crate::*;
}
Ok(Scalar::ptr_null(&*this.tcx))
}
+
+ fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
+ let this = self.eval_context_mut();
+
+ if !this.machine.communicate {
+ throw_unsup_format!("`chdir` not available when isolation is enabled")
+ }
+
+ let path_bytes = this
+ .memory()
+ .read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
+
+ let path = Path::new(
+ std::str::from_utf8(path_bytes)
+ .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?,
+ );
+
+ match env::set_current_dir(path) {
+ Ok(()) => Ok(0),
+ Err(e) => {
+ this.machine.last_error = e.raw_os_error().unwrap() as u32;
+ Ok(-1)
+ }
+ }
+ }
}
this.write_scalar(result, dest)?;
}
+ "chdir" => {
+ let result = this.chdir(args[0])?;
+ this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
+ }
+
"write" => {
let fd = this.read_scalar(args[0])?.to_i32()?;
let buf = this.read_scalar(args[1])?.not_undef()?;
--- /dev/null
+// ignore-windows: TODO the windows hook is not done yet
+// compile-flags: -Zmiri-disable-isolation
+use std::env;
+use std::path::Path;
+
+fn main() {
+ // test that `getcwd` is available
+ let cwd = env::current_dir().unwrap();
+ let parent = cwd.parent().unwrap_or(&cwd);
+ // test that `chdir` is available
+ assert!(env::set_current_dir(&Path::new("..")).is_ok());
+ // test that `..` goes to the parent directory
+ assert_eq!(env::current_dir().unwrap(), parent);
+}
+++ /dev/null
-// ignore-windows: TODO the windows hook is not done yet
-// compile-flags: -Zmiri-disable-isolation
-
-fn main() {
- std::env::current_dir().unwrap();
-}