about summary refs log tree commit diff
path: root/library/std/src/sys/windows/c.rs
AgeCommit message (Collapse)AuthorLines
2022-04-05Make `synchronous_write` safe to callChris Denton-7/+0
2022-04-05Complete reads and writes synchronously or abortChris Denton-0/+35
2022-04-05Correct definition of `IO_STATUS_BLOCK`Chris Denton-5/+11
2022-04-04Rollup merge of #95467 - ChrisDenton:async-read-pipe, r=joshtriplettDylan DPC-0/+21
Windows: Synchronize asynchronous pipe reads and writes On Windows, the pipes used for spawned processes are opened for asynchronous access but `read` and `write` are done using the standard methods that assume synchronous access. This means that the buffer (and variables on the stack) may be read/written to after the function returns. This PR ensures reads/writes complete before returning. Note that this only applies to pipes we create and does not affect the standard file read/write methods. Fixes #95411
2022-04-04Correct calling conventionChris Denton-1/+1
2022-03-30Synchronize asynchronous pipe reads and writesChris Denton-0/+21
2022-03-29Make the stdlib largely conform to strict provenance.Aria Beingessner-1/+1
Some things like the unwinders and system APIs are not fully conformant, this only covers a lot of low-hanging fruit.
2022-03-03Use `HandleOrNull` and `HandleOrInvalid` in the Windows FFI bindings.Dan Gohman-4/+5
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-14Maintain broken symlink behaviour for the Windows exe resolverChris Denton-0/+2
2022-01-24Use `NtCreateFile` instead of `NtOpenFile` to open a fileChris Denton-3/+9
2022-01-19Fix CVE-2022-21658 for WindowsChris Denton-10/+114
2021-12-09Rollup merge of #89999 - talagrand:GetTempPath2, r=m-ou-seMatthias Krüger-0/+6
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-10-31 Windows: Resolve Command program without using the current directoryChris Denton-0/+2
2021-10-30Auto merge of #89174 - ChrisDenton:automatic-verbatim-paths, r=dtolnaybors-0/+6
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
2021-10-26Clarify platform availability of GetTempPath2Eugene Talagrand-1/+1
Windows Server 2022 is a different version from Win11, breaking precent
2021-10-18Update std::env::temp_dir to use GetTempPath2 on Windows when available.Eugene Talagrand-0/+6
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.
2021-10-15Use BCryptGenRandom instead of RtlGenRandom on Windows.Mara Bos-15/+14
BCryptGenRandom isn't available on XP, but we dropped XP support a while ago.
2021-10-03Automatically convert paths to verbatimChris Denton-0/+6
This allows using longer paths for filesystem operations without the user needing to `canonicalize` or manually prefix paths. If the path is already verbatim than this has no effect.
2021-09-02Auto merge of #87580 - ChrisDenton:win-arg-parse-2008, r=m-ou-sebors-1/+1
Update Windows Argument Parsing Fixes #44650 The Windows command line is passed to applications [as a single string](https://docs.microsoft.com/en-us/archive/blogs/larryosterman/the-windows-command-line-is-just-a-string) which the application then parses to get a list of arguments. The standard rules (as used by C/C++) for parsing the command line have slightly changed over the years, most recently in 2008 which added new escaping rules. This PR implements the new rules as [described on MSDN](https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args?view=msvc-160#parsing-c-command-line-arguments) and [further detailed here](https://daviddeley.com/autohotkey/parameters/parameters.htm#WIN). It has been tested against the behaviour of C++ by calling a C++ program that outputs its raw command line and the contents of `argv`. See [my repo](https://github.com/ChrisDenton/winarg/tree/std) if anyone wants to reproduce my work. For an overview of how this PR changes argument parsing behavior and why we feel it is warranted see https://github.com/rust-lang/rust/pull/87580#issuecomment-893833893. For some examples see: https://github.com/rust-lang/rust/pull/87580#issuecomment-894299249
2021-08-30add `TcpStream::set_linger` and `TcpStream::linger`ibraheemdev-0/+8
2021-08-08Implement modern Windows arg parsingChris Denton-1/+1
As derived from extensive testing of `argv` in a C/C++ application. Co-Authored-By: Jane Lusby <jlusby42@gmail.com>
2021-07-04Auto merge of #85270 - ChrisDenton:win-env-case, r=m-ou-sebors-0/+12
When using `process::Command` on Windows, environment variable names must be case-preserving but case-insensitive When using `Command` to set the environment variables, the key should be compared as uppercase Unicode but when set it should preserve the original case. Fixes #85242
2021-07-03Auto merge of #79965 - ijackson:moreerrnos, r=joshtriplettbors-49/+4
More ErrorKinds for common errnos From the commit message of the main commit here (as revised): ``` There are a number of IO error situations which it would be very useful for Rust code to be able to recognise without having to resort to OS-specific code. Taking some Unix examples, `ENOTEMPTY` and `EXDEV` have obvious recovery strategies. Recently I was surprised to discover that `ENOSPC` came out as `ErrorKind::Other`. Since I am familiar with Unix I reviwed the list of errno values in https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/errno.h.html Here, I add those that most clearly seem to be needed. `@CraftSpider` provided information about Windows, and references, which I have tried to take into account. This has to be insta-stable because we can't sensibly have a different set of ErrorKinds depending on a std feature flag. I have *not* added these to the mapping tables for any operating systems other than Unix and Windows. I hope that it is OK to add them now for Unix and Windows now, and maybe add them to other OS's mapping tables as and when someone on that OS is able to consider the situation. I adopted the general principle that it was usually a bad idea to map two distinct error values to the same Rust error code. I notice that this principle is already violated in the case of `EACCES` and `EPERM`, which both map to `PermissionDenied`. I think this was probably a mistake but it would be quite hard to change now, so I don't propose to do anything about that. However, for Windows, there are sometimes different error codes for identical situations. Eg there are WSA* versions of some error codes as well as ERROR_* ones. Also Windows seems to have a great many more erorr codes. I don't know precisely what best practice would be for Windows. ``` <strike> ``` Errno values I wasn't sure about so *haven't* included: EMFILE ENFILE ENOBUFS ENOLCK: These are all fairly Unix-specific resource exhaustion situations. In practice it seemed not very likely to me that anyone would want to handle these differently to `Other`. ENOMEM ERANGE EDOM EOVERFLOW Normally these don't get exposed to the Rust callers I hope. They don't tend to come out of filesystem APIs. EILSEQ Hopefully Rust libraries open files in binary mode and do the converstion in Rust. So Rust code ought not to be exposed to EILSEQ. EIO The range of things that could cause this is troublesome. I found it difficult to describe. I do think it would be useful to add this at some point, because EIO on a filesystem operation is much more serious than most other errors. ENETDOWN I wasn't sure if this was useful or, indeed, if any modern systems use it. ENOEXEC It is not clear to me how a Rust program could respond to this. It seems rather niche. EPROTO ENETRESET ENODATA ENOMSG ENOPROTOOPT ENOSR ENOSTR ETIME ENOTRECOVERABLE EOWNERDEAD EBADMSG EPROTONOSUPPORT EPROTOTYPE EIDRM These are network or STREAMS related errors which I have never in my own Unix programming found the need to do anything with. I think someone who understands these better should be the one to try to find good Rust names and descriptions for them. ENOTTY ENXIO ENODEV EOPNOTSUPP ESRCH EALREADY ECANCELED ECHILD EINPROGRESS These are very hard to get unless you're already doing something very Unix-specific, in which case the raw_os_error interface is probably more suitable than relying on the Rust ErrorKind mapping. EFAULT EBADF These would seem to be the result of application UB. ``` </strike> <i>(omitted errnos are discussed below, especially in https://github.com/rust-lang/rust/pull/79965#issuecomment-810468334)
2021-06-21Move `available_concurrency` implementation to `sys`Christiaan Dirkx-0/+18
2021-06-18Windows error codes: Move to a separate moduleIan Jackson-49/+4
We're going to add many more of these. This commit is pure code motion, plus the necessary administrivia, as I have veried with the following runes: $ git-diff HEAD~ | grep '^+' |sort >plus $ git-diff HEAD~ | grep '^-' | perl -pe 's/^-/+/' |sort >min $ diff -ub min plus |less The output is precisely the expected `mod` and `use` directives. Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-06-18windows errors: Change type name for ERROR_SHARING_VIOLATIONIan Jackson-1/+1
DWORD is a type alias for u32, so this makes no difference. But this entry is anomalous and in my forthcoming commits I am going to import many errors wholesale, and I spotted that my wholesale import didn't match what was here. CC: Chris Denton <christophersdenton@gmail.com> Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-05-28Refactor windows sockets impl methodsMichael-0/+1
2021-05-23Rollup merge of #84758 - ChrisDenton:dllimport, r=dtolnayDylan DPC-152/+168
MSVC: Avoid using jmp stubs for dll function imports Windows import libraries contain two symbols for every function: `__imp_FunctionName` and `FunctionName` (where `FunctionName` is the name of the function to be imported). `__imp_FunctionName` contains the address of the imported function. This will be filled in by the Windows executable loader at runtime. `FunctionName` contains a jmp stub that simply jumps to the address given by `__imp_FunctionName`. E.g. it's a function that solely contains a single jmp instruction: ```asm jmp __imp_FunctionName ``` When using an external DLL function in Rust, by default the linker will link to FunctionName, causing a bit of indirection at runtime. In Microsoft's C++ it's possible to instead tell it to insert calls to the address in `__imp_FunctionName` by using the `__declspec(dllimport)` attribute. In Rust it's possible to get effectively the same behaviour using the `#[link]` attribute on `extern` blocks. ---- The second commit also merges multiple `extern` blocks into one block. This is because otherwise Rust will currently create duplicate linker arguments for each block. In this case having duplicates shouldn't matter much other than the noise when displaying the linker command.
2021-05-19Windows implementation of `fs::try_exists`Chris Denton-0/+1
2021-05-19Windows `Command` environment variables are case-preservingChris Denton-0/+12
But comparing is case-insensitive.
2021-05-10windows: provide NonZeroDWORDIan Jackson-0/+2
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
2021-05-06Use the proper import library namesChris Denton-154/+167
2021-05-06Add `#[link]` attributes to dll importsChris Denton-0/+3
This avoids using jmp stubs when calling functions exported from a dll.
2021-05-02Use ErrorKind::OutOfMemory in unix, windows, and wasiKornel-0/+2
2021-03-26Rework `std::sys::windows::alloc`Christiaan Dirkx-7/+0
Add documentation to the system functions and `SAFETY` comments. Refactored helper functions, fixing the correctness of `get_header`.
2021-02-14Fix typo in link to CreateSymbolicLinkW documentation.Mara Bos-1/+1
2021-01-29Resolve DLL imports at CRT startup, not on demandArlie Davis-0/+1
On Windows, libstd uses GetProcAddress to locate some DLL imports, so that libstd can run on older versions of Windows. If a given DLL import is not present, then libstd uses other behavior (such as fallback implementations). This commit uses a feature of the Windows CRT to do these DLL imports during module initialization, before main() (or DllMain()) is called. This is the ideal time to resolve imports, because the module is effectively single-threaded at that point; no other threads can touch the data or code of the module that is being initialized. This avoids several problems. First, it makes the cost of performing the DLL import lookups deterministic. Right now, the DLL imports are done on demand, which means that application threads _might_ have to do the DLL import during some time-sensitive operation. This is a small source of unpredictability. Since threads can race, it's even possible to have more than one thread running the same redundant DLL lookup. This commit also removes using the heap to allocate strings, during the DLL lookups.
2021-01-22Remove delay-binding for Win XP and VistaArlie Davis-53/+63
The minimum supported Windows version is now Windows 7. Windows XP and Windows Vista are no longer supported; both are already broken, and require extra steps to use. This commit removes the delayed-binding support for Windows API functions that are present on all supported Windows targets. This has several benefits: Removes needless complexity. Removes a load and dynamic call on hot paths in mutex acquire / release. This may have performance benefits. * "Drop official support for Windows XP" https://github.com/rust-lang/compiler-team/issues/378 * "Firefox has ended support for Windows XP and Vista" https://support.mozilla.org/en-US/kb/end-support-windows-xp-and-vista
2020-12-14Auto merge of #77618 - fusion-engineering-forks:windows-parker, r=Amanieubors-0/+47
Add fast futex-based thread parker for Windows. This adds a fast futex-based thread parker for Windows. It either uses WaitOnAddress+WakeByAddressSingle or NT Keyed Events (NtWaitForKeyedEvent+NtReleaseKeyedEvent), depending on which is available. Together, this makes this thread parker work for Windows XP and up. Before this change, park()/unpark() did not work on Windows XP: it needs condition variables, which only exist since Windows Vista. --- Unfortunately, NT Keyed Events are an undocumented Windows API. However: - This API is relatively simple with obvious behaviour, and there are several (unofficial) articles documenting the details. [1] - parking_lot has been using this API for years (on Windows versions before Windows 8). [2] Many big projects extensively use parking_lot, such as servo and the Rust compiler itself. - It is the underlying API used by Windows SRW locks and Windows critical sections. [3] [4] - The source code of the implementations of Wine, ReactOs, and Windows XP are available and match the expected behaviour. - The main risk with an undocumented API is that it might change in the future. But since we only use it for older versions of Windows, that's not a problem. - Even if these functions do not block or wake as we expect (which is unlikely, see all previous points), this implementation would still be memory safe. The NT Keyed Events API is only used to sleep/block in the right place. [1]\: http://www.locklessinc.com/articles/keyed_events/ [2]\: https://github.com/Amanieu/parking_lot/commit/43abbc964e [3]\: https://docs.microsoft.com/en-us/archive/msdn-magazine/2012/november/windows-with-c-the-evolution-of-synchronization-in-windows-and-c [4]\: Windows Internals, Part 1, ISBN 9780735671300 --- The choice of fallback API is inspired by parking_lot(_core), but the implementation of this thread parker is different. While parking_lot has no use for a fast path (park() directly returning if unpark() was already called), this implementation has a fast path that returns without even checking which waiting/waking API to use, as the same atomic variable with compatible states is used in all cases.
2020-10-09Remove some dead code in windows-gnu stdMateusz Mikuła-11/+0
2020-10-06Add Keyed Events API to sys::windows::c.Mara Bos-0/+32
2020-10-06Add WaitOnAddress/WakeByAddress API to sys::windows::c.Mara Bos-0/+15
2020-10-01Improve std::sys::windows::compat.Mara Bos-1/+1
- Module name can now be any string, not just an ident. (Not all Windows api modules are valid Rust identifiers.) - Adds c::FuncName::is_available() for checking if a function is really available without having to do a duplicate lookup. - Add comment explaining the lack of locking. - Use `$_:block` to simplify the macro_rules. - Apply allow(unused_variables) only to the fallback instead of everything.
2020-07-27mv std libs to library/mark-0/+1098