From ffc47de1b989ea4fe15162061c835ae13524d602 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 3 Oct 2019 09:33:36 -0500 Subject: [PATCH 1/3] Add unlink shim to delete files --- src/shims/foreign_items.rs | 5 +++++ src/shims/io.rs | 20 ++++++++++++++++++- .../{file_read.rs => file_manipulation.rs} | 5 +++-- 3 files changed, 27 insertions(+), 3 deletions(-) rename tests/run-pass/{file_read.rs => file_manipulation.rs} (89%) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index 3298eef853..a96ed35b67 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -502,6 +502,11 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?; } + "unlink" => { + let result = this.unlink(args[0])?; + this.write_scalar(Scalar::from_int(result, dest.layout.size), dest)?; + } + "strlen" => { let ptr = this.read_scalar(args[0])?.not_undef()?; let n = this.memory().read_c_str(ptr)?.len(); diff --git a/src/shims/io.rs b/src/shims/io.rs index ca3f500f5a..0893d0b4e0 100644 --- a/src/shims/io.rs +++ b/src/shims/io.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; -use std::fs::{File, OpenOptions}; +use std::fs::{File, OpenOptions, remove_file}; use std::io::{Read, Write}; use rustc::ty::layout::Size; @@ -205,6 +205,24 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx }) } + fn unlink( &mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> { + let this = self.eval_context_mut(); + + if !this.machine.communicate { + throw_unsup_format!("`write` not available when isolation is enabled") + } + + let path_bytes = this + .memory() + .read_c_str(this.read_scalar(path_op)?.not_undef()?)?; + let path = std::str::from_utf8(path_bytes) + .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?; + + let result = remove_file(path).map(|_| 0); + + this.consume_result(result) + } + /// Helper function that gets a `FileHandle` immutable reference and allows to manipulate it /// using the `f` closure. /// diff --git a/tests/run-pass/file_read.rs b/tests/run-pass/file_manipulation.rs similarity index 89% rename from tests/run-pass/file_read.rs rename to tests/run-pass/file_manipulation.rs index 666abd65c1..3980cc0f74 100644 --- a/tests/run-pass/file_read.rs +++ b/tests/run-pass/file_manipulation.rs @@ -1,11 +1,10 @@ // ignore-windows: File handling is not implemented yet // compile-flags: -Zmiri-disable-isolation -use std::fs::File; +use std::fs::{File, remove_file}; use std::io::{Read, Write}; fn main() { - // FIXME: remove the file and delete it when `rm` is implemented. let path = "./tests/hello.txt"; let bytes = b"Hello, World!\n"; // Test creating, writing and closing a file (closing is tested when `file` is dropped). @@ -22,4 +21,6 @@ fn main() { // Reading until EOF should get the whole text. file.read_to_end(&mut contents).unwrap(); assert_eq!(bytes, contents.as_slice()); + // Removing file should succeed + remove_file(path).unwrap(); } From 00792493ef65da150ce5d3e4cc7c15660ce1b54a Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 8 Oct 2019 08:56:36 -0500 Subject: [PATCH 2/3] Add tests for non-existing files --- tests/run-pass/file_manipulation.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/run-pass/file_manipulation.rs b/tests/run-pass/file_manipulation.rs index 3980cc0f74..fa17efd7e1 100644 --- a/tests/run-pass/file_manipulation.rs +++ b/tests/run-pass/file_manipulation.rs @@ -23,4 +23,8 @@ fn main() { assert_eq!(bytes, contents.as_slice()); // Removing file should succeed remove_file(path).unwrap(); + // Opening non-existing file should fail + assert!(File::open(path).is_err()); + // Removing non-existing file should fail + assert!(remove_file(path).is_err()); } From 187361996f6b85edc9001cfdc0081474986ed2b1 Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Tue, 8 Oct 2019 09:25:26 -0500 Subject: [PATCH 3/3] Add errno_location shim for MacOS --- src/shims/foreign_items.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shims/foreign_items.rs b/src/shims/foreign_items.rs index a96ed35b67..9cd84d0b88 100644 --- a/src/shims/foreign_items.rs +++ b/src/shims/foreign_items.rs @@ -418,7 +418,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx } } - "__errno_location" => { + "__errno_location" | "__error" => { let errno_scalar: Scalar = this.machine.last_error.unwrap().into(); this.write_scalar(errno_scalar, dest)?; }