Skip to content

Commit 18e628e

Browse files
committed
Formatting and wording fixes.
1 parent 9e13218 commit 18e628e

File tree

4 files changed

+63
-77
lines changed

4 files changed

+63
-77
lines changed

src/eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub enum RejectOpWith {
3737
#[derive(Copy, Clone, Debug, PartialEq)]
3838
pub enum IsolatedOp {
3939
/// Reject op requiring communication with the host. Usually, miri
40-
/// generates a fake error for such op, and prints a warning
40+
/// returns an error code for such op, and prints a warning
4141
/// about it. Warning levels are controlled by `RejectOpWith` enum.
4242
Reject(RejectOpWith),
4343

src/shims/env.rs

Lines changed: 57 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -322,28 +322,26 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
322322
"`getcwd` is only available for the UNIX target family"
323323
);
324324

325-
match this.machine.isolated_op {
326-
IsolatedOp::Allow => {
327-
let buf = this.read_scalar(&buf_op)?.check_init()?;
328-
let size = this.read_scalar(&size_op)?.to_machine_usize(&*this.tcx)?;
329-
// If we cannot get the current directory, we return null
330-
match env::current_dir() {
331-
Ok(cwd) => {
332-
if this.write_path_to_c_str(&cwd, buf, size)?.0 {
333-
return Ok(buf);
334-
}
335-
let erange = this.eval_libc("ERANGE")?;
336-
this.set_last_error(erange)?;
325+
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
326+
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
327+
this.set_last_error_from_io_error(err)?;
328+
this.report_rejected_op("getcwd", reject_with);
329+
} else {
330+
let buf = this.read_scalar(&buf_op)?.check_init()?;
331+
let size = this.read_scalar(&size_op)?.to_machine_usize(&*this.tcx)?;
332+
// If we cannot get the current directory, we return null
333+
match env::current_dir() {
334+
Ok(cwd) => {
335+
if this.write_path_to_c_str(&cwd, buf, size)?.0 {
336+
return Ok(buf);
337337
}
338-
Err(e) => this.set_last_error_from_io_error(e)?,
338+
let erange = this.eval_libc("ERANGE")?;
339+
this.set_last_error(erange)?;
339340
}
340-
}
341-
IsolatedOp::Reject(reject_with) => {
342-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
343-
this.set_last_error_from_io_error(err)?;
344-
this.report_rejected_op("getcwd", reject_with);
341+
Err(e) => this.set_last_error_from_io_error(e)?,
345342
}
346343
}
344+
347345
Ok(Scalar::null_ptr(&*this.tcx))
348346
}
349347

@@ -356,24 +354,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
356354
let this = self.eval_context_mut();
357355
this.assert_target_os("windows", "GetCurrentDirectoryW");
358356

359-
match this.machine.isolated_op {
360-
IsolatedOp::Allow => {
361-
let size = u64::from(this.read_scalar(size_op)?.to_u32()?);
362-
let buf = this.read_scalar(buf_op)?.check_init()?;
363-
364-
// If we cannot get the current directory, we return 0
365-
match env::current_dir() {
366-
Ok(cwd) =>
367-
return Ok(windows_check_buffer_size(
368-
this.write_path_to_wide_str(&cwd, buf, size)?,
369-
)),
370-
Err(e) => this.set_last_error_from_io_error(e)?,
371-
}
372-
}
373-
IsolatedOp::Reject(reject_with) => {
374-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
375-
this.set_last_error_from_io_error(err)?;
376-
this.report_rejected_op("GetCurrentDirectoryW", reject_with);
357+
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
358+
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
359+
this.set_last_error_from_io_error(err)?;
360+
this.report_rejected_op("GetCurrentDirectoryW", reject_with);
361+
} else {
362+
let size = u64::from(this.read_scalar(size_op)?.to_u32()?);
363+
let buf = this.read_scalar(buf_op)?.check_init()?;
364+
365+
// If we cannot get the current directory, we return 0
366+
match env::current_dir() {
367+
Ok(cwd) =>
368+
return Ok(windows_check_buffer_size(
369+
this.write_path_to_wide_str(&cwd, buf, size)?,
370+
)),
371+
Err(e) => this.set_last_error_from_io_error(e)?,
377372
}
378373
}
379374
Ok(0)
@@ -387,24 +382,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
387382
"`getcwd` is only available for the UNIX target family"
388383
);
389384

390-
match this.machine.isolated_op {
391-
IsolatedOp::Allow => {
392-
let path = this.read_path_from_c_str(this.read_scalar(path_op)?.check_init()?)?;
385+
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
386+
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
387+
this.set_last_error_from_io_error(err)?;
388+
this.report_rejected_op("chdir", reject_with);
393389

394-
match env::set_current_dir(path) {
395-
Ok(()) => Ok(0),
396-
Err(e) => {
397-
this.set_last_error_from_io_error(e)?;
398-
Ok(-1)
399-
}
400-
}
401-
}
402-
IsolatedOp::Reject(reject_with) => {
403-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
404-
this.set_last_error_from_io_error(err)?;
405-
this.report_rejected_op("chdir", reject_with);
390+
Ok(-1)
391+
} else {
392+
let path = this.read_path_from_c_str(this.read_scalar(path_op)?.check_init()?)?;
406393

407-
Ok(-1)
394+
match env::set_current_dir(path) {
395+
Ok(()) => Ok(0),
396+
Err(e) => {
397+
this.set_last_error_from_io_error(e)?;
398+
Ok(-1)
399+
}
408400
}
409401
}
410402
}
@@ -419,25 +411,21 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
419411
let this = self.eval_context_mut();
420412
this.assert_target_os("windows", "SetCurrentDirectoryW");
421413

422-
match this.machine.isolated_op {
423-
IsolatedOp::Allow => {
424-
let path =
425-
this.read_path_from_wide_str(this.read_scalar(path_op)?.check_init()?)?;
414+
if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op {
415+
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
416+
this.set_last_error_from_io_error(err)?;
417+
this.report_rejected_op("SetCurrentDirectoryW", reject_with);
426418

427-
match env::set_current_dir(path) {
428-
Ok(()) => Ok(1),
429-
Err(e) => {
430-
this.set_last_error_from_io_error(e)?;
431-
Ok(0)
432-
}
433-
}
434-
}
435-
IsolatedOp::Reject(reject_with) => {
436-
let err = Error::new(ErrorKind::NotFound, "rejected due to isolation");
437-
this.set_last_error_from_io_error(err)?;
438-
this.report_rejected_op("SetCurrentDirectoryW", reject_with);
419+
Ok(0)
420+
} else {
421+
let path = this.read_path_from_wide_str(this.read_scalar(path_op)?.check_init()?)?;
439422

440-
Ok(0)
423+
match env::set_current_dir(path) {
424+
Ok(()) => Ok(1),
425+
Err(e) => {
426+
this.set_last_error_from_io_error(e)?;
427+
Ok(0)
428+
}
441429
}
442430
}
443431
}

src/shims/posix/fs.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -697,9 +697,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
697697
let mut bytes = vec![0; count as usize];
698698
// `File::read` never returns a value larger than `count`,
699699
// so this cannot fail.
700-
let result = file_descriptor
701-
.read(communicate, &mut bytes)?
702-
.map(|c| i64::try_from(c).unwrap());
700+
let result =
701+
file_descriptor.read(communicate, &mut bytes)?.map(|c| i64::try_from(c).unwrap());
703702

704703
match result {
705704
Ok(read_bytes) => {
@@ -738,9 +737,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
738737
let communicate = this.machine.communicate();
739738
if let Some(file_descriptor) = this.machine.file_handler.handles.get_mut(&fd) {
740739
let bytes = this.memory.read_bytes(buf, Size::from_bytes(count))?;
741-
let result = file_descriptor
742-
.write(communicate, &bytes)?
743-
.map(|c| i64::try_from(c).unwrap());
740+
let result =
741+
file_descriptor.write(communicate, &bytes)?.map(|c| i64::try_from(c).unwrap());
744742
this.try_unwrap_io_result(result)
745743
} else {
746744
this.handle_not_found()

tests/run-pass/current_dir_with_isolation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::env;
22
use std::io::ErrorKind;
33

44
fn main() {
5-
// Test that current dir operations return the dummy error instead
5+
// Test that current dir operations return a proper error instead
66
// of stopping the machine in isolation mode
77
assert_eq!(env::current_dir().unwrap_err().kind(), ErrorKind::NotFound);
88
for _i in 0..3 {

0 commit comments

Comments
 (0)