Skip to content

Commit 0a0a15a

Browse files
committed
Auto merge of rust-lang#2380 - RalfJung:isatty, r=RalfJung
isatty tweaks `@saethlin` I realized we need to gate this behind the isolation flag, sorry. :/ The point of isolated mode is that it is 100% deterministic, so we cannot call host functions that might give different answers on different runs.
2 parents 8ec3425 + e30dd07 commit 0a0a15a

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/shims/unix/fs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
16941694
fn isatty(&mut self, miri_fd: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
16951695
let this = self.eval_context_mut();
16961696
#[cfg(unix)]
1697-
{
1697+
if matches!(this.machine.isolated_op, IsolatedOp::Allow) {
16981698
let miri_fd = this.read_scalar(miri_fd)?.to_i32()?;
16991699
if let Some(host_fd) =
17001700
this.machine.file_handler.handles.get(&miri_fd).and_then(|fd| fd.as_unix_host_fd())
@@ -1714,7 +1714,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
17141714
}
17151715
}
17161716
// We are attemping to use a Unix interface on a non-Unix platform, or we are on a Unix
1717-
// platform and the passed file descriptor is not open.
1717+
// platform and the passed file descriptor is not open, or isolation is enabled
17181718
// FIXME: It should be possible to emulate this at least on Windows by using
17191719
// GetConsoleMode.
17201720
let enotty = this.eval_libc("ENOTTY")?;

tests/pass/libc.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//@ignore-windows: No libc on Windows
22
//@compile-flags: -Zmiri-disable-isolation
3-
43
#![feature(rustc_private)]
54

5+
use std::fs::{remove_file, File};
6+
use std::os::unix::io::AsRawFd;
7+
68
extern crate libc;
79

8-
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
910
fn tmp() -> std::path::PathBuf {
1011
std::env::var("MIRI_TEMP")
1112
.map(std::path::PathBuf::from)
@@ -15,9 +16,7 @@ fn tmp() -> std::path::PathBuf {
1516
#[cfg(any(target_os = "linux", target_os = "freebsd"))]
1617
fn test_posix_fadvise() {
1718
use std::convert::TryInto;
18-
use std::fs::{remove_file, File};
1919
use std::io::Write;
20-
use std::os::unix::io::AsRawFd;
2120

2221
let path = tmp().join("miri_test_libc_posix_fadvise.txt");
2322
// Cleanup before test
@@ -44,9 +43,7 @@ fn test_posix_fadvise() {
4443

4544
#[cfg(any(target_os = "linux"))]
4645
fn test_sync_file_range() {
47-
use std::fs::{remove_file, File};
4846
use std::io::Write;
49-
use std::os::unix::io::AsRawFd;
5047

5148
let path = tmp().join("miri_test_libc_sync_file_range.txt");
5249
// Cleanup before test.
@@ -319,6 +316,19 @@ fn test_isatty() {
319316
libc::isatty(libc::STDIN_FILENO);
320317
libc::isatty(libc::STDOUT_FILENO);
321318
libc::isatty(libc::STDERR_FILENO);
319+
320+
// But when we open a file, it is definitely not a TTY.
321+
let path = tmp().join("notatty.txt");
322+
// Cleanup before test.
323+
remove_file(&path).ok();
324+
let file = File::create(&path).unwrap();
325+
326+
assert_eq!(libc::isatty(file.as_raw_fd()), 0);
327+
assert_eq!(std::io::Error::last_os_error().raw_os_error().unwrap(), libc::ENOTTY);
328+
329+
// Cleanup after test.
330+
drop(file);
331+
remove_file(&path).unwrap();
322332
}
323333
}
324334

0 commit comments

Comments
 (0)