Skip to content

Commit 8123eb4

Browse files
committed
Move NtWriteFile miri shim to foreign_items
1 parent 77de724 commit 8123eb4

File tree

2 files changed

+68
-69
lines changed

2 files changed

+68
-69
lines changed

src/tools/miri/src/shims/windows/dlsym.rs

-69
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use rustc_middle::mir;
2-
use rustc_target::abi::Size;
32
use rustc_target::spec::abi::Abi;
43

54
use log::trace;
@@ -11,7 +10,6 @@ use crate::*;
1110

1211
#[derive(Debug, Copy, Clone)]
1312
pub enum Dlsym {
14-
NtWriteFile,
1513
SetThreadDescription,
1614
WaitOnAddress,
1715
WakeByAddressSingle,
@@ -23,7 +21,6 @@ impl Dlsym {
2321
pub fn from_str<'tcx>(name: &str) -> InterpResult<'tcx, Option<Dlsym>> {
2422
Ok(match name {
2523
"GetSystemTimePreciseAsFileTime" => None,
26-
"NtWriteFile" => Some(Dlsym::NtWriteFile),
2724
"SetThreadDescription" => Some(Dlsym::SetThreadDescription),
2825
"WaitOnAddress" => Some(Dlsym::WaitOnAddress),
2926
"WakeByAddressSingle" => Some(Dlsym::WakeByAddressSingle),
@@ -49,72 +46,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
4946
this.check_abi(abi, Abi::System { unwind: false })?;
5047

5148
match dlsym {
52-
Dlsym::NtWriteFile => {
53-
if !this.frame_in_std() {
54-
throw_unsup_format!(
55-
"`NtWriteFile` support is crude and just enough for stdout to work"
56-
);
57-
}
58-
59-
let [
60-
handle,
61-
_event,
62-
_apc_routine,
63-
_apc_context,
64-
io_status_block,
65-
buf,
66-
n,
67-
byte_offset,
68-
_key,
69-
] = check_arg_count(args)?;
70-
let handle = this.read_target_isize(handle)?;
71-
let buf = this.read_pointer(buf)?;
72-
let n = this.read_scalar(n)?.to_u32()?;
73-
let byte_offset = this.read_target_usize(byte_offset)?; // is actually a pointer
74-
let io_status_block = this.deref_operand(io_status_block)?;
75-
76-
if byte_offset != 0 {
77-
throw_unsup_format!(
78-
"`NtWriteFile` `ByteOffset` paremeter is non-null, which is unsupported"
79-
);
80-
}
81-
82-
let written = if handle == -11 || handle == -12 {
83-
// stdout/stderr
84-
use std::io::{self, Write};
85-
86-
let buf_cont =
87-
this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(u64::from(n)))?;
88-
let res = if this.machine.mute_stdout_stderr {
89-
Ok(buf_cont.len())
90-
} else if handle == -11 {
91-
io::stdout().write(buf_cont)
92-
} else {
93-
io::stderr().write(buf_cont)
94-
};
95-
// We write at most `n` bytes, which is a `u32`, so we cannot have written more than that.
96-
res.ok().map(|n| u32::try_from(n).unwrap())
97-
} else {
98-
throw_unsup_format!(
99-
"on Windows, writing to anything except stdout/stderr is not supported"
100-
)
101-
};
102-
// We have to put the result into io_status_block.
103-
if let Some(n) = written {
104-
let io_status_information =
105-
this.mplace_field_named(&io_status_block, "Information")?;
106-
this.write_scalar(
107-
Scalar::from_target_usize(n.into(), this),
108-
&io_status_information.into(),
109-
)?;
110-
}
111-
// Return whether this was a success. >= 0 is success.
112-
// For the error code we arbitrarily pick 0xC0000185, STATUS_IO_DEVICE_ERROR.
113-
this.write_scalar(
114-
Scalar::from_u32(if written.is_some() { 0 } else { 0xC0000185u32 }),
115-
dest,
116-
)?;
117-
}
11849
Dlsym::SetThreadDescription => {
11950
let [handle, name] = check_arg_count(args)?;
12051

src/tools/miri/src/shims/windows/foreign_items.rs

+68
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,74 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
6969
this.write_scalar(result, dest)?;
7070
}
7171

72+
// File related shims
73+
"NtWriteFile" => {
74+
if !this.frame_in_std() {
75+
throw_unsup_format!(
76+
"`NtWriteFile` support is crude and just enough for stdout to work"
77+
);
78+
}
79+
80+
let [
81+
handle,
82+
_event,
83+
_apc_routine,
84+
_apc_context,
85+
io_status_block,
86+
buf,
87+
n,
88+
byte_offset,
89+
_key,
90+
] = this.check_shim(abi, Abi::System { unwind: false }, link_name, args)?;
91+
let handle = this.read_target_isize(handle)?;
92+
let buf = this.read_pointer(buf)?;
93+
let n = this.read_scalar(n)?.to_u32()?;
94+
let byte_offset = this.read_target_usize(byte_offset)?; // is actually a pointer
95+
let io_status_block = this.deref_operand(io_status_block)?;
96+
97+
if byte_offset != 0 {
98+
throw_unsup_format!(
99+
"`NtWriteFile` `ByteOffset` paremeter is non-null, which is unsupported"
100+
);
101+
}
102+
103+
let written = if handle == -11 || handle == -12 {
104+
// stdout/stderr
105+
use std::io::{self, Write};
106+
107+
let buf_cont =
108+
this.read_bytes_ptr_strip_provenance(buf, Size::from_bytes(u64::from(n)))?;
109+
let res = if this.machine.mute_stdout_stderr {
110+
Ok(buf_cont.len())
111+
} else if handle == -11 {
112+
io::stdout().write(buf_cont)
113+
} else {
114+
io::stderr().write(buf_cont)
115+
};
116+
// We write at most `n` bytes, which is a `u32`, so we cannot have written more than that.
117+
res.ok().map(|n| u32::try_from(n).unwrap())
118+
} else {
119+
throw_unsup_format!(
120+
"on Windows, writing to anything except stdout/stderr is not supported"
121+
)
122+
};
123+
// We have to put the result into io_status_block.
124+
if let Some(n) = written {
125+
let io_status_information =
126+
this.mplace_field_named(&io_status_block, "Information")?;
127+
this.write_scalar(
128+
Scalar::from_target_usize(n.into(), this),
129+
&io_status_information.into(),
130+
)?;
131+
}
132+
// Return whether this was a success. >= 0 is success.
133+
// For the error code we arbitrarily pick 0xC0000185, STATUS_IO_DEVICE_ERROR.
134+
this.write_scalar(
135+
Scalar::from_u32(if written.is_some() { 0 } else { 0xC0000185u32 }),
136+
dest,
137+
)?;
138+
}
139+
72140
// Allocation
73141
"HeapAlloc" => {
74142
let [handle, flags, size] =

0 commit comments

Comments
 (0)