From bdef57ea45594752f904983effc81fe938fbdfe9 Mon Sep 17 00:00:00 2001 From: Samrat Man Singh Date: Tue, 4 Aug 2020 20:40:48 +0530 Subject: [PATCH] Flush to stdout from FileDescriptor::write for `Stdout` Also, remove unnecessary `-Zmiri-disable-isolation` in test --- src/shims/posix/foreign_items.rs | 5 ----- src/shims/posix/fs.rs | 10 +++++++++- tests/compile-fail/fs/write_to_stdin.rs | 1 - 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/shims/posix/foreign_items.rs b/src/shims/posix/foreign_items.rs index 594f58d26461..151ab95f1e3c 100644 --- a/src/shims/posix/foreign_items.rs +++ b/src/shims/posix/foreign_items.rs @@ -1,5 +1,3 @@ -use std::io::{self, Write}; - use log::trace; use rustc_middle::mir; @@ -76,9 +74,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx let count = this.read_scalar(n)?.to_machine_usize(this)?; trace!("Called write({:?}, {:?}, {:?})", fd, buf, count); let result = this.write(fd, buf, count)?; - if fd == 1 { - io::stdout().flush().unwrap(); - } // Now, `result` is the value we return back to the program. this.write_scalar(Scalar::from_machine_isize(result, this), dest)?; } diff --git a/src/shims/posix/fs.rs b/src/shims/posix/fs.rs index 65d50aa504d3..3e1ba3976f77 100644 --- a/src/shims/posix/fs.rs +++ b/src/shims/posix/fs.rs @@ -76,7 +76,15 @@ impl<'tcx> FileDescriptor<'tcx> for io::Stdout { } fn write(&mut self, bytes: &[u8]) -> InterpResult<'tcx, io::Result> { - Ok(Write::write(self, bytes)) + let result = Write::write(self, bytes); + // Stdout is buffered, flush to make sure it appears on the + // screen. This is the write() syscall of the interpreted + // program, we want it to correspond to a write() syscall on + // the host -- there is no good in adding extra buffering + // here. + io::stdout().flush().unwrap(); + + Ok(result) } fn seek(&mut self, _offset: SeekFrom) -> InterpResult<'tcx, io::Result> { diff --git a/tests/compile-fail/fs/write_to_stdin.rs b/tests/compile-fail/fs/write_to_stdin.rs index 30d24b5dc444..c2754636c860 100644 --- a/tests/compile-fail/fs/write_to_stdin.rs +++ b/tests/compile-fail/fs/write_to_stdin.rs @@ -1,4 +1,3 @@ -// compile-flags: -Zmiri-disable-isolation // ignore-windows: No libc on Windows #![feature(rustc_private)]