Skip to content

Commit 402129c

Browse files
committed
Auto merge of #960 - christianpoveda:chdir-shim, r=oli-obk
Add `chdir` shim r? @oli-obk
2 parents 2e17933 + 145a582 commit 402129c

File tree

5 files changed

+56
-6
lines changed

5 files changed

+56
-6
lines changed

src/shims/env.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::HashMap;
22
use std::env;
3+
use std::path::Path;
34

45
use crate::stacked_borrows::Tag;
56
use crate::*;
@@ -151,4 +152,29 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
151152
}
152153
Ok(Scalar::ptr_null(&*this.tcx))
153154
}
155+
156+
fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
157+
let this = self.eval_context_mut();
158+
159+
if !this.machine.communicate {
160+
throw_unsup_format!("`chdir` not available when isolation is enabled")
161+
}
162+
163+
let path_bytes = this
164+
.memory()
165+
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
166+
167+
let path = Path::new(
168+
std::str::from_utf8(path_bytes)
169+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?,
170+
);
171+
172+
match env::set_current_dir(path) {
173+
Ok(()) => Ok(0),
174+
Err(e) => {
175+
this.machine.last_error = e.raw_os_error().unwrap() as u32;
176+
Ok(-1)
177+
}
178+
}
179+
}
154180
}

src/shims/foreign_items.rs

+5
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
441441
this.write_scalar(result, dest)?;
442442
}
443443

444+
"chdir" => {
445+
let result = this.chdir(args[0])?;
446+
this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?;
447+
}
448+
444449
"write" => {
445450
let fd = this.read_scalar(args[0])?.to_i32()?;
446451
let buf = this.read_scalar(args[1])?.not_undef()?;
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// compile-flags: -Zmiri-disable-isolation
2+
3+
extern {
4+
pub fn chdir(dir: *const u8) -> i32;
5+
}
6+
7+
fn main() {
8+
let path = vec![0xc3u8, 0x28, 0];
9+
// test that `chdir` errors with invalid utf-8 path
10+
unsafe { chdir(path.as_ptr()) }; //~ ERROR is not a valid utf-8 string
11+
}

tests/run-pass/change_current_dir.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// ignore-windows: TODO the windows hook is not done yet
2+
// compile-flags: -Zmiri-disable-isolation
3+
use std::env;
4+
use std::path::Path;
5+
6+
fn main() {
7+
// test that `getcwd` is available
8+
let cwd = env::current_dir().unwrap();
9+
let parent = cwd.parent().unwrap_or(&cwd);
10+
// test that `chdir` is available
11+
assert!(env::set_current_dir(&Path::new("..")).is_ok());
12+
// test that `..` goes to the parent directory
13+
assert_eq!(env::current_dir().unwrap(), parent);
14+
}

tests/run-pass/get_current_dir.rs

-6
This file was deleted.

0 commit comments

Comments
 (0)