Add chdir shim

This commit is contained in:
Christian Poveda 2019-09-24 14:42:38 -05:00
parent 2e17933eed
commit 0eed5e64de
4 changed files with 45 additions and 6 deletions

View File

@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::env;
use std::path::Path;
use crate::stacked_borrows::Tag;
use crate::*;
@ -151,4 +152,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
}
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)
}
}
}
}

View File

@ -441,6 +441,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
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()?;

View File

@ -0,0 +1,14 @@
// 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);
}

View File

@ -1,6 +0,0 @@
// ignore-windows: TODO the windows hook is not done yet
// compile-flags: -Zmiri-disable-isolation
fn main() {
std::env::current_dir().unwrap();
}