std\sys\pal\windows/pipe.rs
1use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut};
2use crate::ops::Neg;
3use crate::os::windows::prelude::*;
4use crate::sys::handle::Handle;
5use crate::sys::{api, c};
6use crate::sys_common::{FromInner, IntoInner};
7use crate::{mem, ptr};
8
9////////////////////////////////////////////////////////////////////////////////
10// Anonymous pipes
11////////////////////////////////////////////////////////////////////////////////
12
13pub struct AnonPipe {
14 inner: Handle,
15}
16
17impl IntoInner<Handle> for AnonPipe {
18 fn into_inner(self) -> Handle {
19 self.inner
20 }
21}
22
23impl FromInner<Handle> for AnonPipe {
24 fn from_inner(inner: Handle) -> AnonPipe {
25 Self { inner }
26 }
27}
28
29pub struct Pipes {
30 pub ours: AnonPipe,
31 pub theirs: AnonPipe,
32}
33
34/// Although this looks similar to `anon_pipe` in the Unix module it's actually
35/// subtly different. Here we'll return two pipes in the `Pipes` return value,
36/// but one is intended for "us" where as the other is intended for "someone
37/// else".
38///
39/// Currently the only use case for this function is pipes for stdio on
40/// processes in the standard library, so "ours" is the one that'll stay in our
41/// process whereas "theirs" will be inherited to a child.
42///
43/// The ours/theirs pipes are *not* specifically readable or writable. Each
44/// one only supports a read or a write, but which is which depends on the
45/// boolean flag given. If `ours_readable` is `true`, then `ours` is readable and
46/// `theirs` is writable. Conversely, if `ours_readable` is `false`, then `ours`
47/// is writable and `theirs` is readable.
48///
49/// Also note that the `ours` pipe is always a handle opened up in overlapped
50/// mode. This means that technically speaking it should only ever be used
51/// with `OVERLAPPED` instances, but also works out ok if it's only ever used
52/// once at a time (which we do indeed guarantee).
53pub fn anon_pipe(ours_readable: bool, their_handle_inheritable: bool) -> io::Result<Pipes> {
54 // A 64kb pipe capacity is the same as a typical Linux default.
55 const PIPE_BUFFER_CAPACITY: u32 = 64 * 1024;
56
57 // Note that we specifically do *not* use `CreatePipe` here because
58 // unfortunately the anonymous pipes returned do not support overlapped
59 // operations. Instead, we use `NtCreateNamedPipeFile` to create the
60 // anonymous pipe with overlapped support.
61 //
62 // Once we do this, we connect to it via `NtOpenFile`, and then
63 // we return those reader/writer halves. Note that the `ours` pipe return
64 // value is always the named pipe, whereas `theirs` is just the normal file.
65 // This should hopefully shield us from child processes which assume their
66 // stdout is a named pipe, which would indeed be odd!
67 unsafe {
68 let mut io_status = c::IO_STATUS_BLOCK::default();
69 let mut object_attributes = c::OBJECT_ATTRIBUTES::default();
70 object_attributes.Length = size_of::<c::OBJECT_ATTRIBUTES>() as u32;
71
72 // Open a handle to the pipe filesystem (`\??\PIPE\`).
73 // This will be used when creating a new annon pipe.
74 let pipe_fs = {
75 let path = api::unicode_str!(r"\??\PIPE\");
76 object_attributes.ObjectName = path.as_ptr();
77 let mut pipe_fs = ptr::null_mut();
78 let status = c::NtOpenFile(
79 &mut pipe_fs,
80 c::SYNCHRONIZE | c::GENERIC_READ,
81 &object_attributes,
82 &mut io_status,
83 c::FILE_SHARE_READ | c::FILE_SHARE_WRITE,
84 c::FILE_SYNCHRONOUS_IO_NONALERT, // synchronous access
85 );
86 if c::nt_success(status) {
87 Handle::from_raw_handle(pipe_fs)
88 } else {
89 return Err(io::Error::from_raw_os_error(c::RtlNtStatusToDosError(status) as i32));
90 }
91 };
92
93 // From now on we're using handles instead of paths to create and open pipes.
94 // So set the `ObjectName` to a zero length string.
95 // As a (perhaps overzealous) mitigation for #143078, we use the null pointer
96 // for empty.Buffer instead of unicode_str!("").
97 // There's no difference to the OS itself but it's possible that third party
98 // DLLs which hook in to processes could be relying on the exact form of this string.
99 let empty = c::UNICODE_STRING::default();
100 object_attributes.ObjectName = &raw const empty;
101
102 // Create our side of the pipe for async access.
103 let ours = {
104 // Use the pipe filesystem as the root directory.
105 // With no name provided, an anonymous pipe will be created.
106 object_attributes.RootDirectory = pipe_fs.as_raw_handle();
107
108 // A negative timeout value is a relative time (rather than an absolute time).
109 // The time is given in 100's of nanoseconds so this is 50 milliseconds.
110 // This value was chosen to be consistent with the default timeout set by `CreateNamedPipeW`
111 // See: https://learn.microsoft.com/en-us/windows/win32/api/namedpipeapi/nf-namedpipeapi-createnamedpipew
112 let timeout = (50_i64 * 10000).neg() as u64;
113
114 let mut ours = ptr::null_mut();
115 let status = c::NtCreateNamedPipeFile(
116 &mut ours,
117 c::SYNCHRONIZE | if ours_readable { c::GENERIC_READ } else { c::GENERIC_WRITE },
118 &object_attributes,
119 &mut io_status,
120 if ours_readable { c::FILE_SHARE_WRITE } else { c::FILE_SHARE_READ },
121 c::FILE_CREATE,
122 0,
123 c::FILE_PIPE_BYTE_STREAM_TYPE,
124 c::FILE_PIPE_BYTE_STREAM_MODE,
125 c::FILE_PIPE_QUEUE_OPERATION,
126 // only allow one client pipe
127 1,
128 PIPE_BUFFER_CAPACITY,
129 PIPE_BUFFER_CAPACITY,
130 &timeout,
131 );
132 if c::nt_success(status) {
133 Handle::from_raw_handle(ours)
134 } else {
135 return Err(io::Error::from_raw_os_error(c::RtlNtStatusToDosError(status) as i32));
136 }
137 };
138
139 // Open their side of the pipe for synchronous access.
140 let theirs = {
141 // We can reopen the anonymous pipe without a name by setting
142 // RootDirectory to the pipe handle and not setting a path name,
143 object_attributes.RootDirectory = ours.as_raw_handle();
144
145 if their_handle_inheritable {
146 object_attributes.Attributes |= c::OBJ_INHERIT;
147 }
148 let mut theirs = ptr::null_mut();
149 let status = c::NtOpenFile(
150 &mut theirs,
151 c::SYNCHRONIZE
152 | if ours_readable {
153 c::GENERIC_WRITE | c::FILE_READ_ATTRIBUTES
154 } else {
155 c::GENERIC_READ
156 },
157 &object_attributes,
158 &mut io_status,
159 0,
160 c::FILE_NON_DIRECTORY_FILE | c::FILE_SYNCHRONOUS_IO_NONALERT,
161 );
162 if c::nt_success(status) {
163 Handle::from_raw_handle(theirs)
164 } else {
165 return Err(io::Error::from_raw_os_error(c::RtlNtStatusToDosError(status) as i32));
166 }
167 };
168
169 Ok(Pipes { ours: AnonPipe { inner: ours }, theirs: AnonPipe { inner: theirs } })
170 }
171}
172
173/// Takes an asynchronous source pipe and returns a synchronous pipe suitable
174/// for sending to a child process.
175///
176/// This is achieved by creating a new set of pipes and spawning a thread that
177/// relays messages between the source and the synchronous pipe.
178pub fn spawn_pipe_relay(
179 source: &AnonPipe,
180 ours_readable: bool,
181 their_handle_inheritable: bool,
182) -> io::Result<AnonPipe> {
183 // We need this handle to live for the lifetime of the thread spawned below.
184 let source = source.try_clone()?;
185
186 // create a new pair of anon pipes.
187 let Pipes { theirs, ours } = anon_pipe(ours_readable, their_handle_inheritable)?;
188
189 // Spawn a thread that passes messages from one pipe to the other.
190 // Any errors will simply cause the thread to exit.
191 let (reader, writer) = if ours_readable { (ours, source) } else { (source, ours) };
192 crate::thread::spawn(move || {
193 let mut buf = [0_u8; 4096];
194 'reader: while let Ok(len) = reader.read(&mut buf) {
195 if len == 0 {
196 break;
197 }
198 let mut start = 0;
199 while let Ok(written) = writer.write(&buf[start..len]) {
200 start += written;
201 if start == len {
202 continue 'reader;
203 }
204 }
205 break;
206 }
207 });
208
209 // Return the pipe that should be sent to the child process.
210 Ok(theirs)
211}
212
213impl AnonPipe {
214 pub fn handle(&self) -> &Handle {
215 &self.inner
216 }
217 pub fn into_handle(self) -> Handle {
218 self.inner
219 }
220
221 pub fn try_clone(&self) -> io::Result<Self> {
222 self.inner.duplicate(0, false, c::DUPLICATE_SAME_ACCESS).map(|inner| AnonPipe { inner })
223 }
224
225 pub fn read(&self, buf: &mut [u8]) -> io::Result<usize> {
226 let result = unsafe {
227 let len = crate::cmp::min(buf.len(), u32::MAX as usize) as u32;
228 let ptr = buf.as_mut_ptr();
229 self.alertable_io_internal(|overlapped, callback| {
230 c::ReadFileEx(self.inner.as_raw_handle(), ptr, len, overlapped, callback)
231 })
232 };
233
234 match result {
235 // The special treatment of BrokenPipe is to deal with Windows
236 // pipe semantics, which yields this error when *reading* from
237 // a pipe after the other end has closed; we interpret that as
238 // EOF on the pipe.
239 Err(ref e) if e.kind() == io::ErrorKind::BrokenPipe => Ok(0),
240 _ => result,
241 }
242 }
243
244 pub fn read_buf(&self, mut buf: BorrowedCursor<'_>) -> io::Result<()> {
245 let result = unsafe {
246 let len = crate::cmp::min(buf.capacity(), u32::MAX as usize) as u32;
247 let ptr = buf.as_mut().as_mut_ptr().cast::<u8>();
248 self.alertable_io_internal(|overlapped, callback| {
249 c::ReadFileEx(self.inner.as_raw_handle(), ptr, len, overlapped, callback)
250 })
251 };
252
253 match result {
254 // The special treatment of BrokenPipe is to deal with Windows
255 // pipe semantics, which yields this error when *reading* from
256 // a pipe after the other end has closed; we interpret that as
257 // EOF on the pipe.
258 Err(ref e) if e.kind() == io::ErrorKind::BrokenPipe => Ok(()),
259 Err(e) => Err(e),
260 Ok(n) => {
261 unsafe {
262 buf.advance_unchecked(n);
263 }
264 Ok(())
265 }
266 }
267 }
268
269 pub fn read_vectored(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
270 self.inner.read_vectored(bufs)
271 }
272
273 #[inline]
274 pub fn is_read_vectored(&self) -> bool {
275 self.inner.is_read_vectored()
276 }
277
278 pub fn read_to_end(&self, buf: &mut Vec<u8>) -> io::Result<usize> {
279 self.handle().read_to_end(buf)
280 }
281
282 pub fn write(&self, buf: &[u8]) -> io::Result<usize> {
283 unsafe {
284 let len = crate::cmp::min(buf.len(), u32::MAX as usize) as u32;
285 self.alertable_io_internal(|overlapped, callback| {
286 c::WriteFileEx(self.inner.as_raw_handle(), buf.as_ptr(), len, overlapped, callback)
287 })
288 }
289 }
290
291 pub fn write_vectored(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
292 self.inner.write_vectored(bufs)
293 }
294
295 #[inline]
296 pub fn is_write_vectored(&self) -> bool {
297 self.inner.is_write_vectored()
298 }
299
300 /// Synchronizes asynchronous reads or writes using our anonymous pipe.
301 ///
302 /// This is a wrapper around [`ReadFileEx`] or [`WriteFileEx`] that uses
303 /// [Asynchronous Procedure Call] (APC) to synchronize reads or writes.
304 ///
305 /// Note: This should not be used for handles we don't create.
306 ///
307 /// # Safety
308 ///
309 /// `buf` must be a pointer to a buffer that's valid for reads or writes
310 /// up to `len` bytes. The `AlertableIoFn` must be either `ReadFileEx` or `WriteFileEx`
311 ///
312 /// [`ReadFileEx`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-readfileex
313 /// [`WriteFileEx`]: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-writefileex
314 /// [Asynchronous Procedure Call]: https://docs.microsoft.com/en-us/windows/win32/sync/asynchronous-procedure-calls
315 unsafe fn alertable_io_internal(
316 &self,
317 io: impl FnOnce(&mut c::OVERLAPPED, c::LPOVERLAPPED_COMPLETION_ROUTINE) -> c::BOOL,
318 ) -> io::Result<usize> {
319 // Use "alertable I/O" to synchronize the pipe I/O.
320 // This has four steps.
321 //
322 // STEP 1: Start the asynchronous I/O operation.
323 // This simply calls either `ReadFileEx` or `WriteFileEx`,
324 // giving it a pointer to the buffer and callback function.
325 //
326 // STEP 2: Enter an alertable state.
327 // The callback set in step 1 will not be called until the thread
328 // enters an "alertable" state. This can be done using `SleepEx`.
329 //
330 // STEP 3: The callback
331 // Once the I/O is complete and the thread is in an alertable state,
332 // the callback will be run on the same thread as the call to
333 // `ReadFileEx` or `WriteFileEx` done in step 1.
334 // In the callback we simply set the result of the async operation.
335 //
336 // STEP 4: Return the result.
337 // At this point we'll have a result from the callback function
338 // and can simply return it. Note that we must not return earlier,
339 // while the I/O is still in progress.
340
341 // The result that will be set from the asynchronous callback.
342 let mut async_result: Option<AsyncResult> = None;
343 struct AsyncResult {
344 error: u32,
345 transferred: u32,
346 }
347
348 // STEP 3: The callback.
349 unsafe extern "system" fn callback(
350 dwErrorCode: u32,
351 dwNumberOfBytesTransferred: u32,
352 lpOverlapped: *mut c::OVERLAPPED,
353 ) {
354 // Set `async_result` using a pointer smuggled through `hEvent`.
355 // SAFETY:
356 // At this point, the OVERLAPPED struct will have been written to by the OS,
357 // except for our `hEvent` field which we set to a valid AsyncResult pointer (see below)
358 unsafe {
359 let result =
360 AsyncResult { error: dwErrorCode, transferred: dwNumberOfBytesTransferred };
361 *(*lpOverlapped).hEvent.cast::<Option<AsyncResult>>() = Some(result);
362 }
363 }
364
365 // STEP 1: Start the I/O operation.
366 let mut overlapped: c::OVERLAPPED = unsafe { crate::mem::zeroed() };
367 // `hEvent` is unused by `ReadFileEx` and `WriteFileEx`.
368 // Therefore the documentation suggests using it to smuggle a pointer to the callback.
369 overlapped.hEvent = (&raw mut async_result) as *mut _;
370
371 // Asynchronous read of the pipe.
372 // If successful, `callback` will be called once it completes.
373 let result = io(&mut overlapped, Some(callback));
374 if result == c::FALSE {
375 // We can return here because the call failed.
376 // After this we must not return until the I/O completes.
377 return Err(io::Error::last_os_error());
378 }
379
380 // Wait indefinitely for the result.
381 let result = loop {
382 // STEP 2: Enter an alertable state.
383 // The second parameter of `SleepEx` is used to make this sleep alertable.
384 unsafe { c::SleepEx(c::INFINITE, c::TRUE) };
385 if let Some(result) = async_result {
386 break result;
387 }
388 };
389 // STEP 4: Return the result.
390 // `async_result` is always `Some` at this point
391 match result.error {
392 c::ERROR_SUCCESS => Ok(result.transferred as usize),
393 error => Err(io::Error::from_raw_os_error(error as _)),
394 }
395 }
396}
397
398pub fn read2(p1: AnonPipe, v1: &mut Vec<u8>, p2: AnonPipe, v2: &mut Vec<u8>) -> io::Result<()> {
399 let p1 = p1.into_handle();
400 let p2 = p2.into_handle();
401
402 let mut p1 = AsyncPipe::new(p1, v1)?;
403 let mut p2 = AsyncPipe::new(p2, v2)?;
404 let objs = [p1.event.as_raw_handle(), p2.event.as_raw_handle()];
405
406 // In a loop we wait for either pipe's scheduled read operation to complete.
407 // If the operation completes with 0 bytes, that means EOF was reached, in
408 // which case we just finish out the other pipe entirely.
409 //
410 // Note that overlapped I/O is in general super unsafe because we have to
411 // be careful to ensure that all pointers in play are valid for the entire
412 // duration of the I/O operation (where tons of operations can also fail).
413 // The destructor for `AsyncPipe` ends up taking care of most of this.
414 loop {
415 let res = unsafe { c::WaitForMultipleObjects(2, objs.as_ptr(), c::FALSE, c::INFINITE) };
416 if res == c::WAIT_OBJECT_0 {
417 if !p1.result()? || !p1.schedule_read()? {
418 return p2.finish();
419 }
420 } else if res == c::WAIT_OBJECT_0 + 1 {
421 if !p2.result()? || !p2.schedule_read()? {
422 return p1.finish();
423 }
424 } else {
425 return Err(io::Error::last_os_error());
426 }
427 }
428}
429
430struct AsyncPipe<'a> {
431 pipe: Handle,
432 event: Handle,
433 overlapped: Box<c::OVERLAPPED>, // needs a stable address
434 dst: &'a mut Vec<u8>,
435 state: State,
436}
437
438#[derive(PartialEq, Debug)]
439enum State {
440 NotReading,
441 Reading,
442 Read(usize),
443}
444
445impl<'a> AsyncPipe<'a> {
446 fn new(pipe: Handle, dst: &'a mut Vec<u8>) -> io::Result<AsyncPipe<'a>> {
447 // Create an event which we'll use to coordinate our overlapped
448 // operations, this event will be used in WaitForMultipleObjects
449 // and passed as part of the OVERLAPPED handle.
450 //
451 // Note that we do a somewhat clever thing here by flagging the
452 // event as being manually reset and setting it initially to the
453 // signaled state. This means that we'll naturally fall through the
454 // WaitForMultipleObjects call above for pipes created initially,
455 // and the only time an even will go back to "unset" will be once an
456 // I/O operation is successfully scheduled (what we want).
457 let event = Handle::new_event(true, true)?;
458 let mut overlapped: Box<c::OVERLAPPED> = unsafe { Box::new(mem::zeroed()) };
459 overlapped.hEvent = event.as_raw_handle();
460 Ok(AsyncPipe { pipe, overlapped, event, dst, state: State::NotReading })
461 }
462
463 /// Executes an overlapped read operation.
464 ///
465 /// Must not currently be reading, and returns whether the pipe is currently
466 /// at EOF or not. If the pipe is not at EOF then `result()` must be called
467 /// to complete the read later on (may block), but if the pipe is at EOF
468 /// then `result()` should not be called as it will just block forever.
469 fn schedule_read(&mut self) -> io::Result<bool> {
470 assert_eq!(self.state, State::NotReading);
471 let amt = unsafe {
472 if self.dst.capacity() == self.dst.len() {
473 let additional = if self.dst.capacity() == 0 { 16 } else { 1 };
474 self.dst.reserve(additional);
475 }
476 self.pipe.read_overlapped(self.dst.spare_capacity_mut(), &mut *self.overlapped)?
477 };
478
479 // If this read finished immediately then our overlapped event will
480 // remain signaled (it was signaled coming in here) and we'll progress
481 // down to the method below.
482 //
483 // Otherwise the I/O operation is scheduled and the system set our event
484 // to not signaled, so we flag ourselves into the reading state and move
485 // on.
486 self.state = match amt {
487 Some(0) => return Ok(false),
488 Some(amt) => State::Read(amt),
489 None => State::Reading,
490 };
491 Ok(true)
492 }
493
494 /// Wait for the result of the overlapped operation previously executed.
495 ///
496 /// Takes a parameter `wait` which indicates if this pipe is currently being
497 /// read whether the function should block waiting for the read to complete.
498 ///
499 /// Returns values:
500 ///
501 /// * `true` - finished any pending read and the pipe is not at EOF (keep
502 /// going)
503 /// * `false` - finished any pending read and pipe is at EOF (stop issuing
504 /// reads)
505 fn result(&mut self) -> io::Result<bool> {
506 let amt = match self.state {
507 State::NotReading => return Ok(true),
508 State::Reading => self.pipe.overlapped_result(&mut *self.overlapped, true)?,
509 State::Read(amt) => amt,
510 };
511 self.state = State::NotReading;
512 unsafe {
513 let len = self.dst.len();
514 self.dst.set_len(len + amt);
515 }
516 Ok(amt != 0)
517 }
518
519 /// Finishes out reading this pipe entirely.
520 ///
521 /// Waits for any pending and schedule read, and then calls `read_to_end`
522 /// if necessary to read all the remaining information.
523 fn finish(&mut self) -> io::Result<()> {
524 while self.result()? && self.schedule_read()? {
525 // ...
526 }
527 Ok(())
528 }
529}
530
531impl<'a> Drop for AsyncPipe<'a> {
532 fn drop(&mut self) {
533 match self.state {
534 State::Reading => {}
535 _ => return,
536 }
537
538 // If we have a pending read operation, then we have to make sure that
539 // it's *done* before we actually drop this type. The kernel requires
540 // that the `OVERLAPPED` and buffer pointers are valid for the entire
541 // I/O operation.
542 //
543 // To do that, we call `CancelIo` to cancel any pending operation, and
544 // if that succeeds we wait for the overlapped result.
545 //
546 // If anything here fails, there's not really much we can do, so we leak
547 // the buffer/OVERLAPPED pointers to ensure we're at least memory safe.
548 if self.pipe.cancel_io().is_err() || self.result().is_err() {
549 let buf = mem::take(self.dst);
550 let overlapped = Box::new(unsafe { mem::zeroed() });
551 let overlapped = mem::replace(&mut self.overlapped, overlapped);
552 mem::forget((buf, overlapped));
553 }
554 }
555}