about summary refs log tree commit diff
path: root/library/std/src/sys/windows
AgeCommit message (Collapse)AuthorLines
2022-12-30std: rename `Parker::new` to `Parker::new_in_place`, add safe `Parker::new` ↵joboet-1/+1
constructor for SGX
2022-12-29std: unify id-based thread parking implementationsjoboet-1/+1
2022-12-11Implement blocking outputAyush Singh-0/+5
This allows decoupling `Command::spawn` and `Command::output`. This is useful for targets which do support launching programs in blocking mode but do not support multitasking (Eg: UEFI). This was originally conceived when working on https://github.com/rust-lang/rust/pull/100316 Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2022-12-08Add read_to_end for AnonPipeAyush Singh-1/+5
Add `read_to_end` method for `sys::{target}::pipe::AnonPipe`. This allows having a more optimized version of `read_to_end` for ChildStdout. Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2022-12-07Auto merge of #103459 - ChrisDenton:propagate-nulls, r=thomccbors-14/+20
Pass on null handle values to child process Fixes #101645 In Windows, stdio handles are (semantically speaking) `Option<Handle>` where `Handle` is a non-zero value. When spawning a process with `Stdio::Inherit`, Rust currently turns zero values into `-1` values. This has the unfortunate effect of breaking console subprocesses (which typically need stdio) that are spawned from gui applications (that lack stdio by default) because the console process won't be assigned handles from the newly created console (as they usually would in that situation). Worse, `-1` is actually [a valid handle](https://doc.rust-lang.org/std/os/windows/io/struct.OwnedHandle.html) which means "the current process". So if a console process, for example, waits on stdin and it has a `-1` value then the process will end up waiting on itself. This PR fixes it by propagating the nulls instead of converting them to `-1`. While I think the current behaviour is a mistake, changing it (however justified) is an API change so I think this PR should at least have some input from t-libs-api. So choosing at random... r? `@joshtriplett`
2022-12-06Don't set `STARTF_USESTDHANDLES` if none are setChris Denton-7/+15
2022-12-01Auto merge of #104160 - Ayush1325:windows-args, r=m-ou-sebors-52/+2
Extract WStrUnits to sys_common::wstr This commit extracts WStrUnits from sys::windows::args to sys_common::wstr. This allows using the same structure for other targets which use wtf8 (example UEFI). This was originally a part of https://github.com/rust-lang/rust/pull/100316 Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2022-11-28Extract WStrUnits to sys_common::wstrAyush Singh-52/+2
This commit extracts WStrUnits from sys::windows::args to sys_common::wstr. This allows using the same structure for other targets which use wtf8 (example UEFI). This was originally a part of https://github.com/rust-lang/rust/pull/100316 Signed-off-by: Ayush Singh <ayushsingh1325@gmail.com>
2022-11-18Handle the case that even the filename array is unaligned.Thom Chiovoloni-5/+14
2022-11-17Don't assume `FILE_ID_BOTH_DIR_INFO` will be alignedThom Chiovoloni-5/+9
2022-11-06std: remove lock wrappers in `sys_common`joboet-26/+21
2022-11-05Rollup merge of #103995 - SUPERCILEX:typos, r=Dylan-DPCDylan DPC-5/+6
Small round of typo fixes
2022-11-04Small round of typo fixesAlex Saveau-5/+6
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
2022-10-27Use stdio in UWP appsChris Denton-120/+28
This has been supported since Windows 10.0.16299. See https://learn.microsoft.com/en-us/uwp/win32-and-com/win32-apis#apis-from-api-ms-win-core-console-l1-1-0dll
2022-10-24Pass on null handle values to child processChris Denton-7/+5
2022-10-21Reduce false positives in msys2 detectionChris Denton-2/+12
This checks that: * the handle is a pipe * the pipe's file name starts with "msys-" or "cygwin-" rather than looking in the full path.
2022-10-15Auto merge of #100579 - joboet:sync_mutex_everywhere, r=thomccbors-3/+3
std: use `sync::Mutex` for internal statics Since `sync::Mutex` is now `const`-constructible, it can be used for internal statics, removing the need for `sys_common::StaticMutex`. This adds some extra allocations on platforms which need to box their mutexes (currently SGX and some UNIX), but these will become unnecessary with the lock improvements tracked in #93740. I changed the program argument implementation on Hermit, it does not need `Mutex` but can use atomics like some UNIX systems (ping `@mkroening` `@stlankes).`
2022-10-15Use Align8 to avoid misalignment if the allocator or Vec doesn't align ↵Josh Triplett-7/+7
allocations
2022-10-15Rewrite FILE_NAME_INFO handling to avoid enlarging slice referenceJosh Triplett-4/+4
Rather than referencing a slice's pointer and then creating a new slice with a longer length, offset from the base structure pointer instead. This makes some choices of Rust semantics happier.
2022-10-15Make is_terminal fail fast if a process has no console at allJosh Triplett-1/+9
If a process has no console, it'll have NULL in place of a console handle, so return early with `false` in that case without making any OS calls.
2022-10-15Add `IsTerminal` trait to determine if a descriptor or handle is a terminalJosh Triplett-0/+67
The UNIX and WASI implementations use `isatty`. The Windows implementation uses the same logic the `atty` crate uses, including the hack needed to detect msys terminals. Implement this trait for `File` and for `Stdin`/`Stdout`/`Stderr` and their locked counterparts on all platforms. On UNIX and WASI, implement it for `BorrowedFd`/`OwnedFd`. On Windows, implement it for `BorrowedHandle`/`OwnedHandle`. Based on https://github.com/rust-lang/rust/pull/91121 Co-authored-by: Matt Wilkinson <mattwilki17@gmail.com>
2022-10-13std: use `sync::Mutex` for internal staticsjoboet-3/+3
2022-10-13Auto merge of #102655 - joboet:windows_tls_opt, r=ChrisDentonbors-73/+193
Optimize TLS on Windows This implements the suggestion in the current TLS code to embed the linked list of destructors in the `StaticKey` structure to save allocations. Additionally, locking is avoided when no destructor needs to be run. By using one Windows-provided `Once` per key instead of a global lock, locking is more finely-grained (this unblocks #100579).
2022-10-08std: optimize TLS on Windowsjoboet-73/+193
2022-10-01`SetFileTime` doesn't allow setting the file time to `0xFFFF_FFFF_FFFF_FFFF`beetrees-0/+8
2022-09-24Rollup merge of #102044 - ChrisDenton:BCrypt-system-rand, r=thomccMatthias Krüger-53/+29
Remove `RtlGenRandom` (take two) First try to use the system preferred RNG but if that fails (e.g. due to a broken system configuration) then fallback to manually opening an algorithm handle.
2022-09-20Auto merge of #100581 - joboet:sync_rwlock_everywhere, r=thomccbors-1/+1
std: use `sync::RwLock` for internal statics Since `sync::RwLock` is now `const`-constructible, it can be used for internal statics, removing the need for `sys_common::StaticRwLock`. This adds some extra allocations on platforms which need to box their locks (currently SGX and some UNIX), but these will become unnecessary with the lock improvements tracked in #93740.
2022-09-20Remove `RtlGenRandom` (take two)Chris Denton-53/+29
First try to use the system preferred RNG but if that fails (e.g. due to a broken system configuration) then fallback to manually opening an algorithm handle.
2022-09-19std: use `sync::RwLock` for internal staticsjoboet-1/+1
2022-09-10Explicitly link kernel32.lib from allocChris Denton-0/+1
2022-09-09Rollup merge of #101366 - ChrisDenton:unc-forward-slash, r=m-ou-seDylan DPC-18/+20
Restore old behaviour on broken UNC paths This fixes #101358 by restoring the behaviour from previous stable Rust versions. I'm not convinced this is ultimately right but I think it's less wrong and maybe this should be backported to beta? r? libs
2022-09-07Auto merge of #101476 - ChrisDenton:BCryptRandom-fix, r=thomccbors-20/+88
Open a BCrypt algorithm handle Fixes #101474, supplants #101456. Replaces use of a pseduo handle with manually opening a algorithm handle. Most interesting thing here is the atomics. r? `@thomcc`
2022-09-07Don't break windows/rand for miriChris Denton-0/+12
2022-09-06Open a BCrypt algorithm handleChris Denton-20/+76
2022-09-06Fix compile errors for uwp-windows-msvc targetsChris Denton-1/+1
2022-09-04Auto merge of #100576 - joboet:movable_const_remutex, r=Mark-Simulacrumbors-2/+0
Make `ReentrantMutex` movable and `const` As `MovableMutex` is now `const`, it can be used to simplify the implementation and interface of the internal reentrant mutex type. Consequently, the standard error stream does not need to be wrapped in `OnceLock` and `OnceLock::get_or_init_pin()` can be removed.
2022-09-03std: make `ReentrantMutex` movable and `const`; simplify `Stdout` initializationjoboet-2/+0
2022-09-03Restore old behaviour on broken UNC pathsChris Denton-18/+20
2022-09-03Rollup merge of #101325 - ChrisDenton:BCRYPT_RNG_ALG_HANDLE, r=thomccDylan DPC-5/+38
Windows RNG: Use `BCRYPT_RNG_ALG_HANDLE` by default This only changes a small amount of actual code, the rest is documentation outlining the history of this module as I feel it will be relevant to any future issues that might crop up. The code change is to use the `BCRYPT_RNG_ALG_HANDLE` [pseudo-handle](https://docs.microsoft.com/en-us/windows/win32/seccng/cng-algorithm-pseudo-handles) by default, which simply uses the default RNG. Previously we used `BCRYPT_USE_SYSTEM_PREFERRED_RNG` which has to load the system configuration and then find and load that RNG. I suspect this was the cause of failures on some systems (e.g. due to corrupted config). However, this is admittedly speculation as I can't reproduce the issue myself (and it does seem quite rare even in the wild). Still, removing a possible point of failure is likely worthwhile in any case. r? libs
2022-09-02Auto merge of #97802 - Enselic:add-no_ignore_sigkill-feature, r=joshtriplettbors-1/+1
Support `#[unix_sigpipe = "inherit|sig_dfl"]` on `fn main()` to prevent ignoring `SIGPIPE` When enabled, programs don't have to explicitly handle `ErrorKind::BrokenPipe` any longer. Currently, the program ```rust fn main() { loop { println!("hello world"); } } ``` will print an error if used with a short-lived pipe, e.g. % ./main | head -n 1 hello world thread 'main' panicked at 'failed printing to stdout: Broken pipe (os error 32)', library/std/src/io/stdio.rs:1016:9 note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace by enabling `#[unix_sigpipe = "sig_dfl"]` like this ```rust #![feature(unix_sigpipe)] #[unix_sigpipe = "sig_dfl"] fn main() { loop { println!("hello world"); } } ``` there is no error, because `SIGPIPE` will not be ignored and thus the program will be killed appropriately: % ./main | head -n 1 hello world The current libstd behaviour of ignoring `SIGPIPE` before `fn main()` can be explicitly requested by using `#[unix_sigpipe = "sig_ign"]`. With `#[unix_sigpipe = "inherit"]`, no change at all is made to `SIGPIPE`, which typically means the behaviour will be the same as `#[unix_sigpipe = "sig_dfl"]`. See https://github.com/rust-lang/rust/issues/62569 and referenced issues for discussions regarding the `SIGPIPE` problem itself See the [this](https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Proposal.3A.20First.20step.20towards.20solving.20the.20SIGPIPE.20problem) Zulip topic for more discussions, including about this PR. Tracking issue: https://github.com/rust-lang/rust/issues/97889
2022-09-02Rollup merge of #101260 - ChrisDenton:attribute-tag, r=thomccMatthias Krüger-8/+24
Use `FILE_ATTRIBUTE_TAG_INFO` to get reparse tag I've been looking at this code recently and it just occurred to me we don't actually use the full reparse data at this point, only the tag. [`GetFileInformationByHandleEx`](https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-getfileinformationbyhandleex) can do exactly that by filling a [`FILE_ATTRIBUTE_TAG_INFO`](https://docs.microsoft.com/en-us/windows/win32/api/winbase/ns-winbase-file_attribute_tag_info) struct. r? ```````@thomcc``````` since you've made changes here recently (which is why I have this code on my mind atm)
2022-09-02Use `BCRYPT_RNG_ALG_HANDLE` by defaultChris Denton-5/+38
Also briefly document the history of `sys/windows/rand.rs` as they may be relevant to any future changes.
2022-09-01Auto merge of #100707 - dzvon:fix-typo, r=davidtwcobors-2/+2
Fix a bunch of typo This PR will fix some typos detected by [typos]. I only picked the ones I was sure were spelling errors to fix, mostly in the comments. [typos]: https://github.com/crate-ci/typos
2022-09-01Use `FILE_ATTRIBUTE_TAG_INFO` to get reparse tagChris Denton-8/+24
This avoid unnecessarily getting the full reparse data when all we need is the tag.
2022-08-31Avoid needless buffer zeroing in `std::sys::windows::fs`Thom Chiovoloni-14/+24
2022-08-31Correct typoDezhi Wu-1/+1
2022-08-31Fix a bunch of typoDezhi Wu-2/+2
This PR will fix some typos detected by [typos]. I only picked the ones I was sure were spelling errors to fix, mostly in the comments. [typos]: https://github.com/crate-ci/typos
2022-08-31Rollup merge of #101171 - thomcc:fix-winfs-ub, r=ChrisDentonMatthias Krüger-28/+55
Fix UB from misalignment and provenance widening in `std::sys::windows` This fixes two types of UB: 1. Reading past the end of a reference in types like `&c::REPARSE_DATA_BUFFER` (see https://github.com/rust-lang/unsafe-code-guidelines/issues/256). This is fixed by using `addr_of!`. I think there are probably a couple more cases where we do this for other structures, and will look into it in a bit. 2. Failing to ensure that a `[u8; N]` on the stack is sufficiently aligned to convert to a `REPARSE_DATA_BUFFER`. ~~This was done by introducing a new `AlignedAs` struct that allows aligning one type to the alignment of another type. I expect there are other places where we have this issue too, or I wouldn't introduce this type, but will get to them after this lands.~~ ~~Worth noting, it *is* implemented in a way that can cause problems depending on how we fix #81996, but this would be caught by the test I added (and presumably if we decide to fix that in a way that would break this code, we'd also introduce a `#[repr(simple)]` or `#[repr(linear)]` as a replacement for this usage of `#[repr(C)]`).~~ Edit: None of that is still in the code, I just went with a `Align8` since that's all we'll need for almost everything we want to call. These are more or less "potential UB" since it's likely at the moment everything works fine, although the alignment not causing issues might just be down to luck (and x86 being forgiving). ~~NB: I've only ensured this check builds, but will run tests soon.~~ All tests pass, including stage2 compiler tests. r? ``@ChrisDenton``
2022-08-31Rollup merge of #100984 - ChrisDenton:reinstate-init, r=Mark-SimulacrumMatthias Krüger-33/+52
Reinstate preloading of some dll imports I've now come around to the conclusion that there is a justification for pre-loading the synchronization functions `WaitOnAddress` and `WakeByAddressSingle`. I've found this to have a particularly impact in testing frameworks that may have short lived processes which immediately spawn lots of threads. Also, because pre-main initializers imply a single-threaded environment, we can switch back to using relaxed atomics which might be a minor perf improvement on some platforms (though I doubt it's particularly notable). r? ``@Mark-Simulacrum`` and sorry for the churn here. For convenience I'll summarise previous issues with preloading and the solutions that are included in this PR (if any): **Issue:** User pre-main initializers may be run before std's **Solution:** The std now uses initializers that are guaranteed to run earlier than the old initializers. A note is also added that users should not copy std's behaviour if they want to ensure they run their initializers after std. **Issue:** Miri does not understand pre-main initializers. **Solution:** For miri only, run the function loading lazily instead. **Issue:** We should ideally use `LoadLibrary` to get "api-ms-win-core-synch-l1-2-0". Only "ntdll" and "kernel32" are guaranteed to always be loaded. **Solution:** None. We can't use `LoadLibrary` pre-main. However, in the past `GetModuleHandle` has always worked in practice so this should hopefully not be a problem. If/when Windows 7 support is dropped, we can finally remove all this for good and just use normal imports.
2022-08-30Avoid `MaybeUninit::uninit_array()`Thom Chiovoloni-4/+4