Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add chdir shim #960

Merged
merged 2 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::env;
use std::path::Path;

use crate::stacked_borrows::Tag;
use crate::*;
Expand Down Expand Up @@ -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))?,
);
pvdrz marked this conversation as resolved.
Show resolved Hide resolved

match env::set_current_dir(path) {
Ok(()) => Ok(0),
Err(e) => {
this.machine.last_error = e.raw_os_error().unwrap() as u32;
Ok(-1)
}
}
}
}
5 changes: 5 additions & 0 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down
11 changes: 11 additions & 0 deletions tests/compile-fail/chdir_invalid_path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// compile-flags: -Zmiri-disable-isolation

extern {
pub fn chdir(dir: *const u8) -> i32;
}

fn main() {
let path = vec![0xc3u8, 0x28, 0];
// test that `chdir` errors with invalid utf-8 path
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
unsafe { chdir(path.as_ptr()) }; //~ ERROR is not a valid utf-8 string
}
14 changes: 14 additions & 0 deletions tests/run-pass/change_current_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// ignore-windows: TODO the windows hook is not done yet
// compile-flags: -Zmiri-disable-isolation
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
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);
pvdrz marked this conversation as resolved.
Show resolved Hide resolved
// 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);
}
6 changes: 0 additions & 6 deletions tests/run-pass/get_current_dir.rs

This file was deleted.