Skip to content

Add getcwd shim #958

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

Merged
merged 7 commits into from
Sep 24, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
32 changes: 31 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,33 @@ 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 {
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
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 than the path, we return null
if bytes.len() as u64 <= size {
// We need `size` bytes exactly
bytes.resize(size as usize, 0);
this.memory_mut().get_mut(buf.alloc_id)?.write_bytes(tcx, buf, &bytes)?;
return Ok(Scalar::Ptr(buf))
}
}
return Ok(Scalar::ptr_null(&*this.tcx));
}
throw_unsup_format!("Function not available when isolation is enabled")
}
}
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();
}