]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/windows/mod.rs
Auto merge of #107843 - bjorn3:sync_cg_clif-2023-02-09, r=bjorn3
[rust.git] / library / std / src / sys / windows / mod.rs
1 #![allow(missing_docs, nonstandard_style)]
2
3 use crate::ffi::{CStr, OsStr, OsString};
4 use crate::io::ErrorKind;
5 use crate::mem::MaybeUninit;
6 use crate::os::windows::ffi::{OsStrExt, OsStringExt};
7 use crate::path::PathBuf;
8 use crate::time::Duration;
9
10 pub use self::rand::hashmap_random_keys;
11
12 #[macro_use]
13 pub mod compat;
14
15 pub mod alloc;
16 pub mod args;
17 pub mod c;
18 pub mod cmath;
19 pub mod env;
20 pub mod fs;
21 pub mod handle;
22 pub mod io;
23 pub mod locks;
24 pub mod memchr;
25 pub mod net;
26 pub mod os;
27 pub mod os_str;
28 pub mod path;
29 pub mod pipe;
30 pub mod process;
31 pub mod rand;
32 pub mod stdio;
33 pub mod thread;
34 pub mod thread_local_dtor;
35 pub mod thread_local_key;
36 pub mod thread_parking;
37 pub mod time;
38 cfg_if::cfg_if! {
39     if #[cfg(not(target_vendor = "uwp"))] {
40         pub mod stack_overflow;
41     } else {
42         pub mod stack_overflow_uwp;
43         pub use self::stack_overflow_uwp as stack_overflow;
44     }
45 }
46
47 // SAFETY: must be called only once during runtime initialization.
48 // NOTE: this is not guaranteed to run, for example when Rust code is called externally.
49 pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {
50     stack_overflow::init();
51
52     // Normally, `thread::spawn` will call `Thread::set_name` but since this thread already
53     // exists, we have to call it ourselves.
54     thread::Thread::set_name(&CStr::from_bytes_with_nul_unchecked(b"main\0"));
55 }
56
57 // SAFETY: must be called only once during runtime cleanup.
58 // NOTE: this is not guaranteed to run, for example when the program aborts.
59 pub unsafe fn cleanup() {
60     net::cleanup();
61 }
62
63 pub fn decode_error_kind(errno: i32) -> ErrorKind {
64     use ErrorKind::*;
65
66     match errno as c::DWORD {
67         c::ERROR_ACCESS_DENIED => return PermissionDenied,
68         c::ERROR_ALREADY_EXISTS => return AlreadyExists,
69         c::ERROR_FILE_EXISTS => return AlreadyExists,
70         c::ERROR_BROKEN_PIPE => return BrokenPipe,
71         c::ERROR_FILE_NOT_FOUND => return NotFound,
72         c::ERROR_PATH_NOT_FOUND => return NotFound,
73         c::ERROR_NO_DATA => return BrokenPipe,
74         c::ERROR_INVALID_NAME => return InvalidFilename,
75         c::ERROR_INVALID_PARAMETER => return InvalidInput,
76         c::ERROR_NOT_ENOUGH_MEMORY | c::ERROR_OUTOFMEMORY => return OutOfMemory,
77         c::ERROR_SEM_TIMEOUT
78         | c::WAIT_TIMEOUT
79         | c::ERROR_DRIVER_CANCEL_TIMEOUT
80         | c::ERROR_OPERATION_ABORTED
81         | c::ERROR_SERVICE_REQUEST_TIMEOUT
82         | c::ERROR_COUNTER_TIMEOUT
83         | c::ERROR_TIMEOUT
84         | c::ERROR_RESOURCE_CALL_TIMED_OUT
85         | c::ERROR_CTX_MODEM_RESPONSE_TIMEOUT
86         | c::ERROR_CTX_CLIENT_QUERY_TIMEOUT
87         | c::FRS_ERR_SYSVOL_POPULATE_TIMEOUT
88         | c::ERROR_DS_TIMELIMIT_EXCEEDED
89         | c::DNS_ERROR_RECORD_TIMED_OUT
90         | c::ERROR_IPSEC_IKE_TIMED_OUT
91         | c::ERROR_RUNLEVEL_SWITCH_TIMEOUT
92         | c::ERROR_RUNLEVEL_SWITCH_AGENT_TIMEOUT => return TimedOut,
93         c::ERROR_CALL_NOT_IMPLEMENTED => return Unsupported,
94         c::ERROR_HOST_UNREACHABLE => return HostUnreachable,
95         c::ERROR_NETWORK_UNREACHABLE => return NetworkUnreachable,
96         c::ERROR_DIRECTORY => return NotADirectory,
97         c::ERROR_DIRECTORY_NOT_SUPPORTED => return IsADirectory,
98         c::ERROR_DIR_NOT_EMPTY => return DirectoryNotEmpty,
99         c::ERROR_WRITE_PROTECT => return ReadOnlyFilesystem,
100         c::ERROR_DISK_FULL | c::ERROR_HANDLE_DISK_FULL => return StorageFull,
101         c::ERROR_SEEK_ON_DEVICE => return NotSeekable,
102         c::ERROR_DISK_QUOTA_EXCEEDED => return FilesystemQuotaExceeded,
103         c::ERROR_FILE_TOO_LARGE => return FileTooLarge,
104         c::ERROR_BUSY => return ResourceBusy,
105         c::ERROR_POSSIBLE_DEADLOCK => return Deadlock,
106         c::ERROR_NOT_SAME_DEVICE => return CrossesDevices,
107         c::ERROR_TOO_MANY_LINKS => return TooManyLinks,
108         c::ERROR_FILENAME_EXCED_RANGE => return InvalidFilename,
109         _ => {}
110     }
111
112     match errno {
113         c::WSAEACCES => PermissionDenied,
114         c::WSAEADDRINUSE => AddrInUse,
115         c::WSAEADDRNOTAVAIL => AddrNotAvailable,
116         c::WSAECONNABORTED => ConnectionAborted,
117         c::WSAECONNREFUSED => ConnectionRefused,
118         c::WSAECONNRESET => ConnectionReset,
119         c::WSAEINVAL => InvalidInput,
120         c::WSAENOTCONN => NotConnected,
121         c::WSAEWOULDBLOCK => WouldBlock,
122         c::WSAETIMEDOUT => TimedOut,
123         c::WSAEHOSTUNREACH => HostUnreachable,
124         c::WSAENETDOWN => NetworkDown,
125         c::WSAENETUNREACH => NetworkUnreachable,
126
127         _ => Uncategorized,
128     }
129 }
130
131 pub fn unrolled_find_u16s(needle: u16, haystack: &[u16]) -> Option<usize> {
132     let ptr = haystack.as_ptr();
133     let mut start = &haystack[..];
134
135     // For performance reasons unfold the loop eight times.
136     while start.len() >= 8 {
137         macro_rules! if_return {
138             ($($n:literal,)+) => {
139                 $(
140                     if start[$n] == needle {
141                         return Some(((&start[$n] as *const u16).addr() - ptr.addr()) / 2);
142                     }
143                 )+
144             }
145         }
146
147         if_return!(0, 1, 2, 3, 4, 5, 6, 7,);
148
149         start = &start[8..];
150     }
151
152     for c in start {
153         if *c == needle {
154             return Some(((c as *const u16).addr() - ptr.addr()) / 2);
155         }
156     }
157     None
158 }
159
160 pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
161     fn inner(s: &OsStr) -> crate::io::Result<Vec<u16>> {
162         // Most paths are ASCII, so reserve capacity for as much as there are bytes
163         // in the OsStr plus one for the null-terminating character. We are not
164         // wasting bytes here as paths created by this function are primarily used
165         // in an ephemeral fashion.
166         let mut maybe_result = Vec::with_capacity(s.len() + 1);
167         maybe_result.extend(s.encode_wide());
168
169         if unrolled_find_u16s(0, &maybe_result).is_some() {
170             return Err(crate::io::const_io_error!(
171                 ErrorKind::InvalidInput,
172                 "strings passed to WinAPI cannot contain NULs",
173             ));
174         }
175         maybe_result.push(0);
176         Ok(maybe_result)
177     }
178     inner(s.as_ref())
179 }
180
181 // Many Windows APIs follow a pattern of where we hand a buffer and then they
182 // will report back to us how large the buffer should be or how many bytes
183 // currently reside in the buffer. This function is an abstraction over these
184 // functions by making them easier to call.
185 //
186 // The first callback, `f1`, is yielded a (pointer, len) pair which can be
187 // passed to a syscall. The `ptr` is valid for `len` items (u16 in this case).
188 // The closure is expected to return what the syscall returns which will be
189 // interpreted by this function to determine if the syscall needs to be invoked
190 // again (with more buffer space).
191 //
192 // Once the syscall has completed (errors bail out early) the second closure is
193 // yielded the data which has been read from the syscall. The return value
194 // from this closure is then the return value of the function.
195 fn fill_utf16_buf<F1, F2, T>(mut f1: F1, f2: F2) -> crate::io::Result<T>
196 where
197     F1: FnMut(*mut u16, c::DWORD) -> c::DWORD,
198     F2: FnOnce(&[u16]) -> T,
199 {
200     // Start off with a stack buf but then spill over to the heap if we end up
201     // needing more space.
202     //
203     // This initial size also works around `GetFullPathNameW` returning
204     // incorrect size hints for some short paths:
205     // https://github.com/dylni/normpath/issues/5
206     let mut stack_buf: [MaybeUninit<u16>; 512] = MaybeUninit::uninit_array();
207     let mut heap_buf: Vec<MaybeUninit<u16>> = Vec::new();
208     unsafe {
209         let mut n = stack_buf.len();
210         loop {
211             let buf = if n <= stack_buf.len() {
212                 &mut stack_buf[..]
213             } else {
214                 let extra = n - heap_buf.len();
215                 heap_buf.reserve(extra);
216                 // We used `reserve` and not `reserve_exact`, so in theory we
217                 // may have gotten more than requested. If so, we'd like to use
218                 // it... so long as we won't cause overflow.
219                 n = heap_buf.capacity().min(c::DWORD::MAX as usize);
220                 // Safety: MaybeUninit<u16> does not need initialization
221                 heap_buf.set_len(n);
222                 &mut heap_buf[..]
223             };
224
225             // This function is typically called on windows API functions which
226             // will return the correct length of the string, but these functions
227             // also return the `0` on error. In some cases, however, the
228             // returned "correct length" may actually be 0!
229             //
230             // To handle this case we call `SetLastError` to reset it to 0 and
231             // then check it again if we get the "0 error value". If the "last
232             // error" is still 0 then we interpret it as a 0 length buffer and
233             // not an actual error.
234             c::SetLastError(0);
235             let k = match f1(buf.as_mut_ptr().cast::<u16>(), n as c::DWORD) {
236                 0 if c::GetLastError() == 0 => 0,
237                 0 => return Err(crate::io::Error::last_os_error()),
238                 n => n,
239             } as usize;
240             if k == n && c::GetLastError() == c::ERROR_INSUFFICIENT_BUFFER {
241                 n = n.saturating_mul(2).min(c::DWORD::MAX as usize);
242             } else if k > n {
243                 n = k;
244             } else if k == n {
245                 // It is impossible to reach this point.
246                 // On success, k is the returned string length excluding the null.
247                 // On failure, k is the required buffer length including the null.
248                 // Therefore k never equals n.
249                 unreachable!();
250             } else {
251                 // Safety: First `k` values are initialized.
252                 let slice: &[u16] = MaybeUninit::slice_assume_init_ref(&buf[..k]);
253                 return Ok(f2(slice));
254             }
255         }
256     }
257 }
258
259 fn os2path(s: &[u16]) -> PathBuf {
260     PathBuf::from(OsString::from_wide(s))
261 }
262
263 pub fn truncate_utf16_at_nul(v: &[u16]) -> &[u16] {
264     match unrolled_find_u16s(0, v) {
265         // don't include the 0
266         Some(i) => &v[..i],
267         None => v,
268     }
269 }
270
271 pub trait IsZero {
272     fn is_zero(&self) -> bool;
273 }
274
275 macro_rules! impl_is_zero {
276     ($($t:ident)*) => ($(impl IsZero for $t {
277         fn is_zero(&self) -> bool {
278             *self == 0
279         }
280     })*)
281 }
282
283 impl_is_zero! { i8 i16 i32 i64 isize u8 u16 u32 u64 usize }
284
285 pub fn cvt<I: IsZero>(i: I) -> crate::io::Result<I> {
286     if i.is_zero() { Err(crate::io::Error::last_os_error()) } else { Ok(i) }
287 }
288
289 pub fn dur2timeout(dur: Duration) -> c::DWORD {
290     // Note that a duration is a (u64, u32) (seconds, nanoseconds) pair, and the
291     // timeouts in windows APIs are typically u32 milliseconds. To translate, we
292     // have two pieces to take care of:
293     //
294     // * Nanosecond precision is rounded up
295     // * Greater than u32::MAX milliseconds (50 days) is rounded up to INFINITE
296     //   (never time out).
297     dur.as_secs()
298         .checked_mul(1000)
299         .and_then(|ms| ms.checked_add((dur.subsec_nanos() as u64) / 1_000_000))
300         .and_then(|ms| ms.checked_add(if dur.subsec_nanos() % 1_000_000 > 0 { 1 } else { 0 }))
301         .map(|ms| if ms > <c::DWORD>::MAX as u64 { c::INFINITE } else { ms as c::DWORD })
302         .unwrap_or(c::INFINITE)
303 }
304
305 /// Use `__fastfail` to abort the process
306 ///
307 /// This is the same implementation as in libpanic_abort's `__rust_start_panic`. See
308 /// that function for more information on `__fastfail`
309 #[allow(unreachable_code)]
310 pub fn abort_internal() -> ! {
311     #[allow(unused)]
312     const FAST_FAIL_FATAL_APP_EXIT: usize = 7;
313     #[cfg(not(miri))] // inline assembly does not work in Miri
314     unsafe {
315         cfg_if::cfg_if! {
316             if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
317                 core::arch::asm!("int $$0x29", in("ecx") FAST_FAIL_FATAL_APP_EXIT);
318                 crate::intrinsics::unreachable();
319             } else if #[cfg(all(target_arch = "arm", target_feature = "thumb-mode"))] {
320                 core::arch::asm!(".inst 0xDEFB", in("r0") FAST_FAIL_FATAL_APP_EXIT);
321                 crate::intrinsics::unreachable();
322             } else if #[cfg(target_arch = "aarch64")] {
323                 core::arch::asm!("brk 0xF003", in("x0") FAST_FAIL_FATAL_APP_EXIT);
324                 crate::intrinsics::unreachable();
325             }
326         }
327     }
328     crate::intrinsics::abort();
329 }
330
331 /// Align the inner value to 8 bytes.
332 ///
333 /// This is enough for almost all of the buffers we're likely to work with in
334 /// the Windows APIs we use.
335 #[repr(C, align(8))]
336 #[derive(Copy, Clone)]
337 pub(crate) struct Align8<T: ?Sized>(pub T);