about summary refs log tree commit diff
path: root/library/std/src/sys/windows
AgeCommit message (Collapse)AuthorLines
2022-03-29Make the stdlib largely conform to strict provenance.Aria Beingessner-16/+19
Some things like the unwinders and system APIs are not fully conformant, this only covers a lot of low-hanging fruit.
2022-03-23Auto merge of #95173 - m-ou-se:sys-locks-module, r=dtolnaybors-5/+9
Move std::sys::{mutex, condvar, rwlock} to std::sys::locks. This cleans up the the std::sys modules a bit by putting the locks in a single module called `locks` rather than spread over the three modules `mutex`, `condvar`, and `rwlock`. This makes it easier to organise lock implementations, which helps with https://github.com/rust-lang/rust/issues/93740.
2022-03-22Move std::sys::{mutex, condvar, rwlock} to std::sys::locks.Mara Bos-5/+9
2022-03-19Skip a test if symlink creation is not possibleChris Denton-2/+9
2022-03-19Rollup merge of #92519 - ChrisDenton:command-maybe-verbatim, r=dtolnayDylan DPC-30/+44
Use verbatim paths for `process::Command` if necessary In #89174, the standard library started using verbatim paths so longer paths are usable by default. However, `Command` was originally left out because of the way `CreateProcessW` was being called. This was changed as a side effect of #87704 so now `Command` paths can be converted to verbatim too (if necessary).
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-5/+3
This updates the standard library's documentation to use the new syntax. The documentation is worthwhile to update as it should be more idiomatic (particularly for features like this, which are nice for users to get acquainted with). The general codebase is likely more hassle than benefit to update: it'll hurt git blame, and generally updates can be done by folks updating the code if (and when) that makes things more readable with the new format. A few places in the compiler and library code are updated (mostly just due to already having been done when this commit was first authored).
2022-03-09Use `unreachable!` for an unreachable code pathChris Denton-1/+7
2022-03-04Rollup merge of #94618 - lewisclark:remove-stack-size-rounding, r=yaahcDylan DPC-4/+1
Don't round stack size up for created threads in Windows Fixes #94454 Windows does the rounding itself, so there isn't a need to explicity do the rounding beforehand, as mentioned by ```@ChrisDenton``` in #94454 > The operating system rounds up the specified size to the nearest multiple of the system's allocation granularity (typically 64 KB). To retrieve the allocation granularity of the current system, use the [GetSystemInfo](https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo) function. https://docs.microsoft.com/en-us/windows/win32/procthread/thread-stack-size
2022-03-04Don't round stack size up for created threadsLewis Clark-4/+1
2022-03-03Use `HandleOrNull` and `HandleOrInvalid` in the Windows FFI bindings.Dan Gohman-17/+21
Use the new `HandleOrNull` and `HandleOrInvalid` types that were introduced as part of [I/O safety] in a few functions in the Windows FFI bindings. This factors out an `unsafe` block and two `unsafe` function calls in the Windows implementation code. And, it helps test `HandleOrNull` and `HandleOrInvalid`, which indeed turned up a bug: `OwnedHandle` also needs to be `#[repr(transparent)]`, as it's used inside of `HandleOrNull` and `HandleOrInvalid` which are also `#[repr(transparent)]`. [I/O safety]: https://github.com/rust-lang/rust/issues/87074
2022-03-01Provide C FFI types via core::ffi, not just in stdJosh Triplett-1/+1
The ability to interoperate with C code via FFI is not limited to crates using std; this allows using these types without std. The existing types in `std::os::raw` become type aliases for the ones in `core::ffi`. This uses type aliases rather than re-exports, to allow the std types to remain stable while the core types are unstable. This also moves the currently unstable `NonZero_` variants and `c_size_t`/`c_ssize_t`/`c_ptrdiff_t` types to `core::ffi`, while leaving them unstable.
2022-02-20use `BOOL` for `TCP_NODELAY` `setsockopt` value on WindowsChris Copeland-2/+2
This issue was found by the Wine project and mitigated there [1]. Windows' documented interface for `setsockopt` expects a `BOOL` (a `typedef` for `int`) for `TCP_NODELAY` [2]. Windows is forgiving and will accept any positive length and interpret the first byte of `*option_value` as the value, so this bug does not affect Windows itself, but does affect systems implementing Windows' interface more strictly, such as Wine. Wine was previously passing this through to the host's `setsockopt`, where, e.g., Linux requires that `option_len` be correct for the chosen option, and `TCP_NODELAY` expects an `int`. [1]: https://source.winehq.org/git/wine.git/commit/d6ea38f32dfd3edbe107a255c37e9f7f3da06ae7 [2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt
2022-02-17Keep the path after `program_exists` succeedsChris Denton-22/+23
2022-02-17Use verbatim paths for `process::Command` if necessaryChris Denton-13/+26
2022-02-14Maintain broken symlink behaviour for the Windows exe resolverChris Denton-2/+30
2022-02-13Auto merge of #91673 - ChrisDenton:path-absolute, r=Mark-Simulacrumbors-0/+16
`std::path::absolute` Implements #59117 by adding a `std::path::absolute` function that creates an absolute path without reading the filesystem. This is intended to be a drop-in replacement for [`std::fs::canonicalize`](https://doc.rust-lang.org/std/fs/fn.canonicalize.html) in cases where it isn't necessary to resolve symlinks. It can be used on paths that don't exist or where resolving symlinks is unwanted. It can also be used to avoid circumstances where `canonicalize` might otherwise fail. On Windows this is a wrapper around [`GetFullPathNameW`](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew). On Unix it partially implements the POSIX [pathname resolution](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13) specification, stopping just short of actually resolving symlinks.
2022-02-13make Instant::{duration_since, elapsed, sub} saturating and remove workaroundsThe8472-8/+0
This removes all mutex/atomics based workarounds for non-monotonic clocks and makes the previously panicking methods saturating instead. Effectively this moves the monotonization from `Instant` construction to the comparisons. This has some observable effects, especially on platforms without monotonic clocks: * Incorrectly ordered Instant comparisons no longer panic. This may hide some programming errors until someone actually looks at the resulting `Duration` * `checked_duration_since` will now return `None` in more cases. Previously it only happened when one compared instants obtained in the wrong order or manually created ones. Now it also does on backslides. The upside is reduced complexity and lower overhead of `Instant::now`.
2022-02-10Rename to `InvalidFilename`Yuki Okushi-2/+2
2022-02-10Map `ERROR_INVALID_NAME` to `FilenameInvalid`Yuki Okushi-1/+2
2022-02-10Rename `FilenameTooLong` to `FilenameInvalid`Yuki Okushi-1/+1
2022-02-10windows: Map `ERROR_INVALID_NAME` as `InvalidInput`Yuki Okushi-1/+1
2022-02-09Rollup merge of #93445 - yaahc:exitcode-constructor, r=dtolnayYuki Okushi-0/+6
Add From<u8> for ExitCode This should cover a mostly cross-platform subset of supported exit codes. We decided to stick with `u8` initially since its the common subset between all platforms that we support (excluding wasm which I think only works with `true` or `false`). Posix is supposed to take i32s, but in practice many unix platforms mask out all but the low 8 bits or in some cases the 8-15th bits. Windows takes a u32 instead of an i32. Bourne-compatible shells also report signals as exitcode 128 + `signal_no`, so there's some ambiguity there when returning exit codes > 127, but it is possible to disambiguate them on the other side so we decided against restricting the possible codes further than to `u8`. ## Related - Detailed analysis of exit code support on various platforms: https://internals.rust-lang.org/t/mini-pre-rfc-redesigning-process-exitstatus/5426 - https://github.com/rust-lang/rust/issues/48711 - https://github.com/rust-lang/rust/issues/43301 - https://rust-lang.zulipchat.com/#narrow/stream/219381-t-libs/topic/Termination.2FExit.20Status.20Stabilization
2022-02-08Rollup merge of #93206 - ChrisDenton:ntopenfile, r=nagisaMatthias Krüger-6/+17
Use `NtCreateFile` instead of `NtOpenFile` to open a file Generally the internal `Nt*` functions should be avoided but when we do need to use one we should stick to the most commonly used for the job. To that end, this PR replaces `NtOpenFile` with `NtCreateFile`. NOTE: The initial version of this comment hypothesised that this may help with some recent false positives from malware scanners. This hypothesis proved wrong. Sorry for the distraction.
2022-02-08`std::path::absolute`Chris Denton-0/+16
2022-02-06Add From<u8> for ExitCodeJane Lusby-0/+6
This should cover a mostly cross-platform subset of supported exit codes.
2022-02-04Hide Repr details from io::Error, and rework `io::Error::new_const`.Thom Chiovoloni-27/+25
2022-01-25make Windows abort_internal Miri-compatibleRalf Jung-0/+1
2022-01-25Rollup merge of #88794 - sunfishcode:sunfishcode/try-clone, r=joshtriplettMatthias Krüger-76/+9
Add a `try_clone()` function to `OwnedFd`. As suggested in #88564. This adds a `try_clone()` to `OwnedFd` by refactoring the code out of the existing `File`/`Socket` code. r? ``@joshtriplett``
2022-01-24Use `NtCreateFile` instead of `NtOpenFile` to open a fileChris Denton-6/+17
2022-01-19Fix CVE-2022-21658 for WindowsChris Denton-27/+419
2022-01-05Rollup merge of #92517 - ChrisDenton:explicit-path, r=dtolnayMatthias Krüger-33/+37
Explicitly pass `PATH` to the Windows exe resolver This allows for testing different `PATH`s without using the actual environment.
2022-01-04Rollup merge of #91754 - Patrick-Poitras:rm-4byte-minimum-stdio-windows, ↵Matthias Krüger-18/+54
r=Mark-Simulacrum Modifications to `std::io::Stdin` on Windows so that there is no longer a 4-byte buffer minimum in read(). This is an attempted fix of issue #91722, where a too-small buffer was passed to the read function of stdio on Windows. This caused an error to be returned when `read_to_end` or `read_to_string` were called. Both delegate to `std::io::default_read_to_end`, which creates a buffer that is of length >0, and forwards it to `std::io::Stdin::read()`. The latter method returns an error if the length of the buffer is less than 4, as there might not be enough space to allocate a UTF-16 character. This creates a problem when the buffer length is in `0 < N < 4`, causing the bug. The current modification creates an internal buffer, much like the one used for the write functions I'd also like to acknowledge the help of ``@agausmann`` and ``@hkratz`` in detecting and isolating the bug, and for suggestions that made the fix possible. Couple disclaimers: - Firstly, I didn't know where to put code to replicate the bug found in the issue. It would probably be wise to add that case to the testing suite, but I'm afraid that I don't know _where_ that test should be added. - Secondly, the code is fairly fundamental to IO operations, so my fears are that this may cause some undesired side effects ~or performance loss in benchmarks.~ The testing suite runs on my computer, and it does fix the issue noted in #91722. - Thirdly, I left the "surrogate" field in the Stdin struct, but from a cursory glance, it seems to be serving the same purpose for other functions. Perhaps merging the two would be appropriate. Finally, this is my first pull request to the rust language, and as such some things may be weird/unidiomatic/plain out bad. If there are any obvious improvements I could do to the code, or any other suggestions, I would appreciate them. Edit: Closes #91722
2022-01-03Explicitly pass `PATH` to the Windows exe resolverChris Denton-33/+37
2021-12-28Fix windows buildXuanwo-0/+9
Signed-off-by: Xuanwo <github@xuanwo.io>
2021-12-23Rollup merge of #92208 - ChrisDenton:win-bat-cmd, r=dtolnayMatthias Krüger-0/+16
Quote bat script command line Fixes #91991 [`CreateProcessW`](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#parameters) should only be used to run exe files but it does have some (undocumented) special handling for files with `.bat` and `.cmd` extensions. Essentially those magic extensions will cause the parameters to be automatically rewritten. Example pseudo Rust code (note that `CreateProcess` starts with an optional application name followed by the application arguments): ```rust // These arguments... CreateProcess(None, `@"foo.bat` "hello world""`@,` ...); // ...are rewritten as CreateProcess(Some(r"C:\Windows\System32\cmd.exe"), `@""foo.bat` "hello world"""`@,` ...); ``` However, when setting the first parameter (the application name) as we now do, it will omit the extra level of quotes around the arguments: ```rust // These arguments... CreateProcess(Some("foo.bat"), `@"foo.bat` "hello world""`@,` ...); // ...are rewritten as CreateProcess(Some(r"C:\Windows\System32\cmd.exe"), `@"foo.bat` "hello world""`@,` ...); ``` This means the arguments won't be passed to the script as intended. Note that running batch files this way is undocumented but people have relied on this so we probably shouldn't break it.
2021-12-22Fix testsChris Denton-0/+4
2021-12-16Quote bat script command lineChris Denton-0/+16
2021-12-15Modifications to buffer UTF-16 internally so that there is no longer a ↵PFPoitras-18/+54
4-byte buffer minimum. Include suggestions from @agausmann and @Mark-Simulacrum.
2021-12-15Rollup merge of #91916 - steffahn:fix-typos, r=dtolnayMatthias Krüger-2/+2
Fix a bunch of typos I hope that none of these files is not supposed to be modified. FYI, I opened separate PRs for typos in submodules, in the respective repositories * https://github.com/rust-lang/stdarch/pull/1267 * https://github.com/rust-lang/backtrace-rs/pull/455
2021-12-14Fix a bunch of typosFrank Steffahn-2/+2
2021-12-12Stabilize asm! and global_asm!Amanieu d'Antras-3/+3
They are also removed from the prelude as per the decision in https://github.com/rust-lang/rust/issues/87228. stdarch and compiler-builtins are updated to work with the new, stable asm! and global_asm! macros.
2021-12-09Auto merge of #81156 - DrMeepster:read_buf, r=joshtriplettbors-2/+39
Implement most of RFC 2930, providing the ReadBuf abstraction This replaces the `Initializer` abstraction for permitting reading into uninitialized buffers, closing #42788. This leaves several APIs described in the RFC out of scope for the initial implementation: * read_buf_vectored * `ReadBufs` Closes #42788, by removing the relevant APIs.
2021-12-09Rollup merge of #89999 - talagrand:GetTempPath2, r=m-ou-seMatthias Krüger-1/+7
Update std::env::temp_dir to use GetTempPath2 on Windows when available. As a security measure, Windows 11 introduces a new temporary directory API, GetTempPath2. When the calling process is running as SYSTEM, a separate temporary directory will be returned inaccessible to non-SYSTEM processes. For non-SYSTEM processes the behavior will be the same as before. This can help mitigate against attacks such as this one: https://medium.com/csis-techblog/cve-2020-1088-yet-another-arbitrary-delete-eop-a00b97d8c3e2 Compatibility risk: Software which relies on temporary files to communicate between SYSTEM and non-SYSTEM processes may be affected by this change. In many cases, such patterns may be vulnerable to the very attacks the new API was introduced to harden against. I'm unclear on the Rust project's tolerance for such change-of-behavior in the standard library. If anything, this PR is meant to raise awareness of the issue and hopefully start the conversation. How tested: Taking the example code from the documentation and running it through psexec (from SysInternals) on Win10 and Win11. On Win10: C:\test>psexec -s C:\test\main.exe <...> Temporary directory: C:\WINDOWS\TEMP\ On Win11: C:\test>psexec -s C:\test\main.exe <...> Temporary directory: C:\Windows\SystemTemp\
2021-11-20Auto merge of #87704 - ChrisDenton:win-resolve-exe, r=yaahcbors-29/+216
Windows: Resolve `process::Command` program without using the current directory Currently `std::process::Command` searches many directories for the executable to run, including the current directory. This has lead to a [CVE for `ripgrep`](https://cve.circl.lu/cve/CVE-2021-3013) but presumably other command line utilities could be similarly vulnerable if they run commands. This was [discussed on the internals forum](https://internals.rust-lang.org/t/std-command-resolve-to-avoid-security-issues-on-windows/14800). Also discussed was [which directories should be searched](https://internals.rust-lang.org/t/windows-where-should-command-new-look-for-executables/15015). EDIT: This PR originally removed all implicit paths. They've now been added back as laid out in the rest of this comment. ## Old Search Strategy The old search strategy is [documented here][1]. Additionally Rust adds searching the child's paths (see also #37519). So the full list of paths that were searched was: 1. The directories that are listed in the child's `PATH` environment variable. 2. The directory from which the application loaded. 3. The current directory for the parent process. 4. The 32-bit Windows system directory. 5. The 16-bit Windows system directory. 6. The Windows directory. 7. The directories that are listed in the PATH environment variable. ## New Search Strategy The new strategy removes the current directory from the searched paths. 1. The directories that are listed in the child's PATH environment variable. 2. The directory from which the application loaded. 3. The 32-bit Windows system directory. 4. The Windows directory. 5. The directories that are listed in the parent's PATH environment variable. Note that it also removes the 16-bit system directory, mostly because there isn't a function to get it. I do not anticipate this being an issue in modern Windows. ## Impact Removing the current directory should fix CVE's like the one linked above. However, it's possible some Windows users of affected Rust CLI applications have come to expect the old behaviour. This change could also affect small Windows-only script-like programs that assumed the current directory would be used. The user would need to use `.\file.exe` instead of the bare application name. This PR could break tests, especially those that test the exact output of error messages (e.g. Cargo) as this does change the error messages is some cases. [1]: https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa#parameters
2021-11-17windows: Return the "Not Found" error when a path is emptyYuki Okushi-4/+3
2021-11-02more efficent File::read_buf impl for windows and unixDrMeepster-2/+39
2021-11-01Run destructors from existing tls callbackChris Denton-23/+14
2021-10-31 Windows: Resolve Command program without using the current directoryChris Denton-29/+216
2021-10-31Windows thread-local keyless dropChris Denton-1/+36
`#[thread_local]` allows us to maintain a per-thread list of destructors. This also avoids the need to synchronize global data (which is particularly tricky within the TLS callback function).
2021-10-30Auto merge of #89174 - ChrisDenton:automatic-verbatim-paths, r=dtolnaybors-13/+174
Automatically convert paths to verbatim for filesystem operations that support it This allows using longer paths without the user needing to `canonicalize` or manually prefix paths. If the path is already verbatim then this has no effect. Fixes: #32689