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 getcwd shim #958

Merged
merged 7 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
36 changes: 35 additions & 1 deletion src/shims/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::env;

use rustc::ty::layout::{Size};
use rustc_mir::interpret::{Pointer, Memory};
Expand All @@ -21,7 +22,7 @@ impl EnvVars {
excluded_env_vars.push("TERM".to_owned());

if ecx.machine.communicate {
for (name, value) in std::env::vars() {
for (name, value) in env::vars() {
if !excluded_env_vars.contains(&name) {
let var_ptr = alloc_env_var(name.as_bytes(), value.as_bytes(), ecx.memory_mut());
ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
Expand Down Expand Up @@ -111,4 +112,37 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
Ok(-1)
}
}

fn getcwd(
&mut self,
buf_op: OpTy<'tcx, Tag>,
size_op: OpTy<'tcx, Tag>,
) -> InterpResult<'tcx, Scalar<Tag>> {
let this = self.eval_context_mut();

if !this.machine.communicate {
throw_unsup_format!("Function not available when isolation is enabled")
}

let tcx = &{this.tcx.tcx};

let buf = this.force_ptr(this.read_scalar(buf_op)?.not_undef()?)?;
let size = this.read_scalar(size_op)?.to_usize(&*this.tcx)?;
// If we cannot get the current directory, we return null
// FIXME: Technically we have to set the `errno` global too
if let Ok(cwd) = env::current_dir() {
// It is not clear what happens with non-utf8 paths here
let mut bytes = cwd.display().to_string().into_bytes();
// If the buffer is smaller or equal than the path, we return null.
// FIXME: Technically we have to set the `errno` global too
if (bytes.len() as u64) < size {
// We add a `/0` terminator
bytes.push(0);
// This is ok because the buffer is larger than the path with the null terminator.
this.memory_mut().get_mut(buf.alloc_id)?.write_bytes(tcx, buf, &bytes)?;
return Ok(Scalar::Ptr(buf))
}
}
Ok(Scalar::ptr_null(&*this.tcx))
}
}
5 changes: 5 additions & 0 deletions src/shims/foreign_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
}

"getcwd" => {
let result = this.getcwd(args[0], args[1])?;
this.write_scalar(result, dest)?;
}

"write" => {
let fd = this.read_scalar(args[0])?.to_i32()?;
let buf = this.read_scalar(args[1])?.not_undef()?;
Expand Down
6 changes: 6 additions & 0 deletions tests/run-pass/get_current_dir.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// ignore-windows: TODO the windows hook is not done yet
// compile-flags: -Zmiri-disable-isolation

fn main() {
std::env::current_dir().unwrap();
}