Skip to content

Commit f29126f

Browse files
committed
implement Windows stdout/stderr
1 parent f299b89 commit f29126f

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

appveyor.yml

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ test_script:
3434
- cargo miri setup
3535
- set MIRI_SYSROOT=%USERPROFILE%\AppData\Local\miri\miri\cache\HOST
3636
# Test miri
37+
- set MIRI_LOG=info
38+
- cargo run --release --all-features -- tests\run-pass\hello.rs
39+
- set MIRI_LOG=
3740
- cargo test --release --all-features
3841
# Test cargo integration
3942
- cd test-cargo-miri

src/fn_call.rs

+43-2
Original file line numberDiff line numberDiff line change
@@ -596,18 +596,59 @@ pub trait EvalContextExt<'a, 'mir, 'tcx: 'a+'mir>: crate::MiriEvalContextExt<'a,
596596
this.write_scalar(Scalar::from_uint(key, dest.layout.size), dest)?;
597597
}
598598
"TlsGetValue" => {
599-
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
599+
let key = this.read_scalar(args[0])?.to_u32()? as u128;
600600
let ptr = this.machine.tls.load_tls(key)?;
601601
this.write_scalar(ptr, dest)?;
602602
}
603603
"TlsSetValue" => {
604-
let key = this.read_scalar(args[0])?.to_bits(args[0].layout.size)?;
604+
let key = this.read_scalar(args[0])?.to_u32()? as u128;
605605
let new_ptr = this.read_scalar(args[1])?.not_undef()?;
606606
this.machine.tls.store_tls(key, new_ptr)?;
607607

608608
// Return success (1)
609609
this.write_scalar(Scalar::from_int(1, dest.layout.size), dest)?;
610610
}
611+
"GetStdHandle" => {
612+
let which = this.read_scalar(args[0])?.to_i32()?;
613+
// We just make this the identity function, so we know later in "WriteFile"
614+
// which one it is.
615+
this.write_scalar(Scalar::from_int(which, this.pointer_size()), dest)?;
616+
}
617+
"WriteFile" => {
618+
let handle = this.read_scalar(args[0])?.to_isize(this)?;
619+
let buf = this.read_scalar(args[1])?.not_undef()?;
620+
let n = this.read_scalar(args[2])?.to_u32()?;
621+
let written_place = this.deref_operand(args[3])?;
622+
this.write_null(written_place.into())?; // spec says we always write 0 first
623+
let written = if handle == -11 || handle == -12 {
624+
// stdout/stderr
625+
use std::io::{self, Write};
626+
627+
let buf_cont = this.memory().read_bytes(buf, Size::from_bytes(u64::from(n)))?;
628+
let res = if handle == -11 {
629+
io::stdout().write(buf_cont)
630+
} else {
631+
io::stderr().write(buf_cont)
632+
};
633+
res.ok().map(|n| n as u32)
634+
} else {
635+
eprintln!("Miri: Ignored output to handle {}", handle);
636+
Some(n) // pretend it all went well
637+
};
638+
// If there was no error, write back how much was written
639+
if let Some(n) = written {
640+
this.write_scalar(Scalar::from_uint(n, Size::from_bits(32)), written_place.into())?;
641+
}
642+
// Return whether this was a success
643+
this.write_scalar(
644+
Scalar::from_int(if written.is_some() { 1 } else { 0 }, dest.layout.size),
645+
dest,
646+
)?;
647+
}
648+
"GetConsoleMode" => {
649+
// Everything is a pipe
650+
this.write_null(dest)?;
651+
}
611652

612653
// We can't execute anything else
613654
_ => {

0 commit comments

Comments
 (0)