Skip to content

Commit 2713dbd

Browse files
committed
Auto merge of #2718 - Pointerbender:o_tmpfile_flag, r=oli-obk
add graceful shim for the custom `O_TMPFILE` file opening flag plus test case I'm trying if I can get the [`tempfile`](https://crates.io/crates/tempfile) crate to work nicely with miri. Right now miri errors out due to an unsupported flag `O_TMPFILE` (= `0x410000`) passed to [`OpenOptions::custom_flags`](https://github.com/Stebalien/tempfile/blob/92ae3e9d6e82bdcfcf114be459d234d3602be2d4/src/file/imp/unix.rs#L71-L86). Interestingly, `tempfile` has a fallback in case the underlying file system does not support the `O_TMPFILE` flag, in which case `open`/`open64` is expected to return the error code `EOPNOTSUPP` (= `95`). This PR adds support for this scenario and also includes a test case (relevant [zulip](https://rust-lang.zulipchat.com/#narrow/stream/269128-miri/topic/miri.20and.20the.20.60tempfile.60.20crate) discussion).
2 parents b8585e9 + acb51b5 commit 2713dbd

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/shims/unix/fs.rs

+9
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,15 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
605605
// (Technically we do not support *not* setting this flag, but we ignore that.)
606606
mirror |= o_cloexec;
607607
}
608+
if this.tcx.sess.target.os == "linux" {
609+
let o_tmpfile = this.eval_libc_i32("O_TMPFILE")?;
610+
if flag & o_tmpfile != 0 {
611+
// if the flag contains `O_TMPFILE` then we return a graceful error
612+
let eopnotsupp = this.eval_libc("EOPNOTSUPP")?;
613+
this.set_last_error(eopnotsupp)?;
614+
return Ok(-1);
615+
}
616+
}
608617
// If `flag` is not equal to `mirror`, there is an unsupported option enabled in `flag`,
609618
// then we throw an error.
610619
if flag != mirror {

tests/pass-dep/shims/libc-fs.rs

+31-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use std::convert::TryInto;
88
use std::ffi::CString;
9-
use std::fs::{canonicalize, remove_file, File};
9+
use std::fs::{canonicalize, remove_dir_all, remove_file, File};
1010
use std::io::{Error, ErrorKind, Write};
1111
use std::os::unix::ffi::OsStrExt;
1212
use std::path::PathBuf;
@@ -18,6 +18,8 @@ fn main() {
1818
test_file_open_unix_allow_two_args();
1919
test_file_open_unix_needs_three_args();
2020
test_file_open_unix_extra_third_arg();
21+
#[cfg(target_os = "linux")]
22+
test_o_tmpfile_flag();
2123
}
2224

2325
fn tmp() -> PathBuf {
@@ -45,6 +47,15 @@ fn prepare(filename: &str) -> PathBuf {
4547
path
4648
}
4749

50+
/// Prepare directory: compute directory name and make sure it does not exist.
51+
#[allow(unused)]
52+
fn prepare_dir(dirname: &str) -> PathBuf {
53+
let path = tmp().join(&dirname);
54+
// Clean the directory for robustness.
55+
remove_dir_all(&path).ok();
56+
path
57+
}
58+
4859
/// Prepare like above, and also write some initial content to the file.
4960
fn prepare_with_content(filename: &str, content: &[u8]) -> PathBuf {
5061
let path = prepare(filename);
@@ -135,3 +146,22 @@ fn test_readlink() {
135146
assert_eq!(res, -1);
136147
assert_eq!(Error::last_os_error().kind(), ErrorKind::NotFound);
137148
}
149+
150+
#[cfg(target_os = "linux")]
151+
fn test_o_tmpfile_flag() {
152+
use std::fs::{create_dir, OpenOptions};
153+
use std::os::unix::fs::OpenOptionsExt;
154+
let dir_path = prepare_dir("miri_test_fs_dir");
155+
create_dir(&dir_path).unwrap();
156+
// test that the `O_TMPFILE` custom flag gracefully errors instead of stopping execution
157+
assert_eq!(
158+
Some(libc::EOPNOTSUPP),
159+
OpenOptions::new()
160+
.read(true)
161+
.write(true)
162+
.custom_flags(libc::O_TMPFILE)
163+
.open(dir_path)
164+
.unwrap_err()
165+
.raw_os_error(),
166+
);
167+
}

0 commit comments

Comments
 (0)