]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/stdio.rs
Rollup merge of #90625 - Milo123459:ref-unwind-safe, r=dtolnay
[rust.git] / library / std / src / sys / windows / stdio.rs
1 #![unstable(issue = "none", feature = "windows_stdio")]
2
3 use crate::char::decode_utf16;
4 use crate::cmp;
5 use crate::io;
6 use crate::os::windows::io::{FromRawHandle, IntoRawHandle};
7 use crate::ptr;
8 use crate::str;
9 use crate::sys::c;
10 use crate::sys::cvt;
11 use crate::sys::handle::Handle;
12 use core::str::utf8_char_width;
13
14 // Don't cache handles but get them fresh for every read/write. This allows us to track changes to
15 // the value over time (such as if a process calls `SetStdHandle` while it's running). See #40490.
16 pub struct Stdin {
17     surrogate: u16,
18 }
19 pub struct Stdout {
20     incomplete_utf8: IncompleteUtf8,
21 }
22
23 pub struct Stderr {
24     incomplete_utf8: IncompleteUtf8,
25 }
26
27 struct IncompleteUtf8 {
28     bytes: [u8; 4],
29     len: u8,
30 }
31
32 // Apparently Windows doesn't handle large reads on stdin or writes to stdout/stderr well (see
33 // #13304 for details).
34 //
35 // From MSDN (2011): "The storage for this buffer is allocated from a shared heap for the
36 // process that is 64 KB in size. The maximum size of the buffer will depend on heap usage."
37 //
38 // We choose the cap at 8 KiB because libuv does the same, and it seems to be acceptable so far.
39 const MAX_BUFFER_SIZE: usize = 8192;
40
41 // The standard buffer size of BufReader for Stdin should be able to hold 3x more bytes than there
42 // are `u16`'s in MAX_BUFFER_SIZE. This ensures the read data can always be completely decoded from
43 // UTF-16 to UTF-8.
44 pub const STDIN_BUF_SIZE: usize = MAX_BUFFER_SIZE / 2 * 3;
45
46 pub fn get_handle(handle_id: c::DWORD) -> io::Result<c::HANDLE> {
47     let handle = unsafe { c::GetStdHandle(handle_id) };
48     if handle == c::INVALID_HANDLE_VALUE {
49         Err(io::Error::last_os_error())
50     } else if handle.is_null() {
51         Err(io::Error::from_raw_os_error(c::ERROR_INVALID_HANDLE as i32))
52     } else {
53         Ok(handle)
54     }
55 }
56
57 fn is_console(handle: c::HANDLE) -> bool {
58     // `GetConsoleMode` will return false (0) if this is a pipe (we don't care about the reported
59     // mode). This will only detect Windows Console, not other terminals connected to a pipe like
60     // MSYS. Which is exactly what we need, as only Windows Console needs a conversion to UTF-16.
61     let mut mode = 0;
62     unsafe { c::GetConsoleMode(handle, &mut mode) != 0 }
63 }
64
65 fn write(
66     handle_id: c::DWORD,
67     data: &[u8],
68     incomplete_utf8: &mut IncompleteUtf8,
69 ) -> io::Result<usize> {
70     if data.is_empty() {
71         return Ok(0);
72     }
73
74     let handle = get_handle(handle_id)?;
75     if !is_console(handle) {
76         unsafe {
77             let handle = Handle::from_raw_handle(handle);
78             let ret = handle.write(data);
79             handle.into_raw_handle(); // Don't close the handle
80             return ret;
81         }
82     }
83
84     if incomplete_utf8.len > 0 {
85         assert!(
86             incomplete_utf8.len < 4,
87             "Unexpected number of bytes for incomplete UTF-8 codepoint."
88         );
89         if data[0] >> 6 != 0b10 {
90             // not a continuation byte - reject
91             incomplete_utf8.len = 0;
92             return Err(io::Error::new_const(
93                 io::ErrorKind::InvalidData,
94                 &"Windows stdio in console mode does not support writing non-UTF-8 byte sequences",
95             ));
96         }
97         incomplete_utf8.bytes[incomplete_utf8.len as usize] = data[0];
98         incomplete_utf8.len += 1;
99         let char_width = utf8_char_width(incomplete_utf8.bytes[0]);
100         if (incomplete_utf8.len as usize) < char_width {
101             // more bytes needed
102             return Ok(1);
103         }
104         let s = str::from_utf8(&incomplete_utf8.bytes[0..incomplete_utf8.len as usize]);
105         incomplete_utf8.len = 0;
106         match s {
107             Ok(s) => {
108                 assert_eq!(char_width, s.len());
109                 let written = write_valid_utf8_to_console(handle, s)?;
110                 assert_eq!(written, s.len()); // guaranteed by write_valid_utf8_to_console() for single codepoint writes
111                 return Ok(1);
112             }
113             Err(_) => {
114                 return Err(io::Error::new_const(
115                     io::ErrorKind::InvalidData,
116                     &"Windows stdio in console mode does not support writing non-UTF-8 byte sequences",
117                 ));
118             }
119         }
120     }
121
122     // As the console is meant for presenting text, we assume bytes of `data` are encoded as UTF-8,
123     // which needs to be encoded as UTF-16.
124     //
125     // If the data is not valid UTF-8 we write out as many bytes as are valid.
126     // If the first byte is invalid it is either first byte of a multi-byte sequence but the
127     // provided byte slice is too short or it is the first byte of an invalid multi-byte sequence.
128     let len = cmp::min(data.len(), MAX_BUFFER_SIZE / 2);
129     let utf8 = match str::from_utf8(&data[..len]) {
130         Ok(s) => s,
131         Err(ref e) if e.valid_up_to() == 0 => {
132             let first_byte_char_width = utf8_char_width(data[0]);
133             if first_byte_char_width > 1 && data.len() < first_byte_char_width {
134                 incomplete_utf8.bytes[0] = data[0];
135                 incomplete_utf8.len = 1;
136                 return Ok(1);
137             } else {
138                 return Err(io::Error::new_const(
139                     io::ErrorKind::InvalidData,
140                     &"Windows stdio in console mode does not support writing non-UTF-8 byte sequences",
141                 ));
142             }
143         }
144         Err(e) => str::from_utf8(&data[..e.valid_up_to()]).unwrap(),
145     };
146
147     write_valid_utf8_to_console(handle, utf8)
148 }
149
150 fn write_valid_utf8_to_console(handle: c::HANDLE, utf8: &str) -> io::Result<usize> {
151     let mut utf16 = [0u16; MAX_BUFFER_SIZE / 2];
152     let mut len_utf16 = 0;
153     for (chr, dest) in utf8.encode_utf16().zip(utf16.iter_mut()) {
154         *dest = chr;
155         len_utf16 += 1;
156     }
157     let utf16 = &utf16[..len_utf16];
158
159     let mut written = write_u16s(handle, &utf16)?;
160
161     // Figure out how many bytes of as UTF-8 were written away as UTF-16.
162     if written == utf16.len() {
163         Ok(utf8.len())
164     } else {
165         // Make sure we didn't end up writing only half of a surrogate pair (even though the chance
166         // is tiny). Because it is not possible for user code to re-slice `data` in such a way that
167         // a missing surrogate can be produced (and also because of the UTF-8 validation above),
168         // write the missing surrogate out now.
169         // Buffering it would mean we have to lie about the number of bytes written.
170         let first_char_remaining = utf16[written];
171         if first_char_remaining >= 0xDCEE && first_char_remaining <= 0xDFFF {
172             // low surrogate
173             // We just hope this works, and give up otherwise
174             let _ = write_u16s(handle, &utf16[written..written + 1]);
175             written += 1;
176         }
177         // Calculate the number of bytes of `utf8` that were actually written.
178         let mut count = 0;
179         for ch in utf16[..written].iter() {
180             count += match ch {
181                 0x0000..=0x007F => 1,
182                 0x0080..=0x07FF => 2,
183                 0xDCEE..=0xDFFF => 1, // Low surrogate. We already counted 3 bytes for the other.
184                 _ => 3,
185             };
186         }
187         debug_assert!(String::from_utf16(&utf16[..written]).unwrap() == utf8[..count]);
188         Ok(count)
189     }
190 }
191
192 fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result<usize> {
193     let mut written = 0;
194     cvt(unsafe {
195         c::WriteConsoleW(
196             handle,
197             data.as_ptr() as c::LPCVOID,
198             data.len() as u32,
199             &mut written,
200             ptr::null_mut(),
201         )
202     })?;
203     Ok(written as usize)
204 }
205
206 impl Stdin {
207     pub const fn new() -> Stdin {
208         Stdin { surrogate: 0 }
209     }
210 }
211
212 impl io::Read for Stdin {
213     fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
214         let handle = get_handle(c::STD_INPUT_HANDLE)?;
215         if !is_console(handle) {
216             unsafe {
217                 let handle = Handle::from_raw_handle(handle);
218                 let ret = handle.read(buf);
219                 handle.into_raw_handle(); // Don't close the handle
220                 return ret;
221             }
222         }
223
224         if buf.len() == 0 {
225             return Ok(0);
226         } else if buf.len() < 4 {
227             return Err(io::Error::new_const(
228                 io::ErrorKind::InvalidInput,
229                 &"Windows stdin in console mode does not support a buffer too small to \
230                  guarantee holding one arbitrary UTF-8 character (4 bytes)",
231             ));
232         }
233
234         let mut utf16_buf = [0u16; MAX_BUFFER_SIZE / 2];
235         // In the worst case, a UTF-8 string can take 3 bytes for every `u16` of a UTF-16. So
236         // we can read at most a third of `buf.len()` chars and uphold the guarantee no data gets
237         // lost.
238         let amount = cmp::min(buf.len() / 3, utf16_buf.len());
239         let read = read_u16s_fixup_surrogates(handle, &mut utf16_buf, amount, &mut self.surrogate)?;
240
241         utf16_to_utf8(&utf16_buf[..read], buf)
242     }
243 }
244
245 // We assume that if the last `u16` is an unpaired surrogate they got sliced apart by our
246 // buffer size, and keep it around for the next read hoping to put them together.
247 // This is a best effort, and might not work if we are not the only reader on Stdin.
248 fn read_u16s_fixup_surrogates(
249     handle: c::HANDLE,
250     buf: &mut [u16],
251     mut amount: usize,
252     surrogate: &mut u16,
253 ) -> io::Result<usize> {
254     // Insert possibly remaining unpaired surrogate from last read.
255     let mut start = 0;
256     if *surrogate != 0 {
257         buf[0] = *surrogate;
258         *surrogate = 0;
259         start = 1;
260         if amount == 1 {
261             // Special case: `Stdin::read` guarantees we can always read at least one new `u16`
262             // and combine it with an unpaired surrogate, because the UTF-8 buffer is at least
263             // 4 bytes.
264             amount = 2;
265         }
266     }
267     let mut amount = read_u16s(handle, &mut buf[start..amount])? + start;
268
269     if amount > 0 {
270         let last_char = buf[amount - 1];
271         if last_char >= 0xD800 && last_char <= 0xDBFF {
272             // high surrogate
273             *surrogate = last_char;
274             amount -= 1;
275         }
276     }
277     Ok(amount)
278 }
279
280 fn read_u16s(handle: c::HANDLE, buf: &mut [u16]) -> io::Result<usize> {
281     // Configure the `pInputControl` parameter to not only return on `\r\n` but also Ctrl-Z, the
282     // traditional DOS method to indicate end of character stream / user input (SUB).
283     // See #38274 and https://stackoverflow.com/questions/43836040/win-api-readconsole.
284     const CTRL_Z: u16 = 0x1A;
285     const CTRL_Z_MASK: c::ULONG = 1 << CTRL_Z;
286     let mut input_control = c::CONSOLE_READCONSOLE_CONTROL {
287         nLength: crate::mem::size_of::<c::CONSOLE_READCONSOLE_CONTROL>() as c::ULONG,
288         nInitialChars: 0,
289         dwCtrlWakeupMask: CTRL_Z_MASK,
290         dwControlKeyState: 0,
291     };
292
293     let mut amount = 0;
294     loop {
295         cvt(unsafe {
296             c::SetLastError(0);
297             c::ReadConsoleW(
298                 handle,
299                 buf.as_mut_ptr() as c::LPVOID,
300                 buf.len() as u32,
301                 &mut amount,
302                 &mut input_control as c::PCONSOLE_READCONSOLE_CONTROL,
303             )
304         })?;
305
306         // ReadConsoleW returns success with ERROR_OPERATION_ABORTED for Ctrl-C or Ctrl-Break.
307         // Explicitly check for that case here and try again.
308         if amount == 0 && unsafe { c::GetLastError() } == c::ERROR_OPERATION_ABORTED {
309             continue;
310         }
311         break;
312     }
313
314     if amount > 0 && buf[amount as usize - 1] == CTRL_Z {
315         amount -= 1;
316     }
317     Ok(amount as usize)
318 }
319
320 #[allow(unused)]
321 fn utf16_to_utf8(utf16: &[u16], utf8: &mut [u8]) -> io::Result<usize> {
322     let mut written = 0;
323     for chr in decode_utf16(utf16.iter().cloned()) {
324         match chr {
325             Ok(chr) => {
326                 chr.encode_utf8(&mut utf8[written..]);
327                 written += chr.len_utf8();
328             }
329             Err(_) => {
330                 // We can't really do any better than forget all data and return an error.
331                 return Err(io::Error::new_const(
332                     io::ErrorKind::InvalidData,
333                     &"Windows stdin in console mode does not support non-UTF-16 input; \
334                      encountered unpaired surrogate",
335                 ));
336             }
337         }
338     }
339     Ok(written)
340 }
341
342 impl IncompleteUtf8 {
343     pub const fn new() -> IncompleteUtf8 {
344         IncompleteUtf8 { bytes: [0; 4], len: 0 }
345     }
346 }
347
348 impl Stdout {
349     pub const fn new() -> Stdout {
350         Stdout { incomplete_utf8: IncompleteUtf8::new() }
351     }
352 }
353
354 impl io::Write for Stdout {
355     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
356         write(c::STD_OUTPUT_HANDLE, buf, &mut self.incomplete_utf8)
357     }
358
359     fn flush(&mut self) -> io::Result<()> {
360         Ok(())
361     }
362 }
363
364 impl Stderr {
365     pub const fn new() -> Stderr {
366         Stderr { incomplete_utf8: IncompleteUtf8::new() }
367     }
368 }
369
370 impl io::Write for Stderr {
371     fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
372         write(c::STD_ERROR_HANDLE, buf, &mut self.incomplete_utf8)
373     }
374
375     fn flush(&mut self) -> io::Result<()> {
376         Ok(())
377     }
378 }
379
380 pub fn is_ebadf(err: &io::Error) -> bool {
381     err.raw_os_error() == Some(c::ERROR_INVALID_HANDLE as i32)
382 }
383
384 pub fn panic_output() -> Option<impl io::Write> {
385     Some(Stderr::new())
386 }