summary refs log tree commit diff
path: root/library/std/src/sys_common
AgeCommit message (Collapse)AuthorLines
2022-07-31Auto merge of #78802 - faern:simplify-socketaddr, r=joshtriplettbors-13/+48
Implement network primitives with ideal Rust layout, not C system layout This PR is the result of this internals forum thread: https://internals.rust-lang.org/t/why-are-socketaddrv4-socketaddrv6-based-on-low-level-sockaddr-in-6/13321. Instead of basing `std:::net::{Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6}` on system (C) structs, they are encoded in a more optimal and idiomatic Rust way. This changes the public API of std by introducing structural equality impls for all four types here, which means that `match ipv4addr { SOME_CONSTANT => ... }` will now compile, whereas previously this was an error. No other intentional changes are introduced to public API. It's possible to observe the current layout of these types (e.g., by pointer casting); most but not all libraries which were found by Crater to do this have had updates issued and affected versions yanked. See report below. ### Benefits of this change - It will become possible to move these fundamental network types from `std` into `core` ([RFC](https://github.com/rust-lang/rfcs/pull/2832)). - Some methods that can't be made `const fn`s today can be made `const fn`s with this change. - `SocketAddrV4` only occupies 6 bytes instead of 16 bytes. - These simple primitives become easier to read and uses less `unsafe`. - Makes these types support structural equality, which means you can now (for instance) match an `Ipv4Addr` against a constant ### ~Remaining~ Previous problems This change obviously changes the memory layout of the types. And it turns out some libraries invalidly assumes the memory layout and does very dangerous pointer casts to convert them. These libraries will have undefined behaviour and perform invalid memory access until patched. - [x] - `mio` - Issue: https://github.com/tokio-rs/mio/issues/1386. - [x] `0.7` branch https://github.com/tokio-rs/mio/pull/1388 - [x] `0.7.6` published https://github.com/tokio-rs/mio/pull/1398 - [x] Yank all `0.7` versions older than `0.7.6` - [x] Report `<0.7.6` to RustSec Advisory Database https://rustsec.org/advisories/RUSTSEC-2020-0081.html - [x] - `socket2` - Issue: https://github.com/rust-lang/socket2-rs/issues/119. - [x] `0.3.x` branch https://github.com/rust-lang/socket2-rs/pull/120 - [x] `0.3.16` published - [x] `master` branch https://github.com/rust-lang/socket2-rs/pull/122 - [x] Yank all `0.3` versions older than `0.3.16` - [x] Report `<0.3.16` to RustSec Advisory Database https://rustsec.org/advisories/RUSTSEC-2020-0079.html - [x] - `net2` - Issue: https://github.com/deprecrated/net2-rs/issues/105 - [x] https://github.com/deprecrated/net2-rs/pull/106 - [x] `0.2.36` published - [x] Yank all `0.2` versions older than `0.2.36` - [x] Report `<0.2.36` to RustSec Advisory Database https://rustsec.org/advisories/RUSTSEC-2020-0078.html - [x] - `miow` - Issue: https://github.com/yoshuawuyts/miow/issues/38 - [x] `0.3.x` - https://github.com/yoshuawuyts/miow/pull/39 - [x] `0.3.6` published - [x] `0.2.x` - https://github.com/yoshuawuyts/miow/pull/40 - [x] `0.2.2` published - [x] Yanked all `0.2` versions older than `0.2.2` - [x] Yanked all `0.3` versions older than `0.3.6` - [x] Report `<0.2.2` and `<0.3.6` to RustSec Advisory Database https://rustsec.org/advisories/RUSTSEC-2020-0080.html - [x] - `quinn master` (aka what became 0.7) - https://github.com/quinn-rs/quinn/issues/968 https://github.com/quinn-rs/quinn/pull/987 - [x] - `quinn 0.6` - https://github.com/quinn-rs/quinn/pull/1045 - [x] - `quinn 0.5` - https://github.com/quinn-rs/quinn/pull/1046 - [x] - Release `0.7.0`, `0.6.2` and `0.5.4` - [x] - `nb-connect` - https://github.com/smol-rs/nb-connect/issues/1 - [x] - Release `1.0.3` - [x] - Yank all versions older than `1.0.3` - [x] - `shadowsocks-rust` - https://github.com/shadowsocks/shadowsocks-rust/issues/462 - [ ] - `rio` - https://github.com/spacejam/rio/issues/44 - [ ] - `seaslug` - https://github.com/spacejam/seaslug/issues/1 #### Fixed crate versions All crates I have found that assumed the memory layout have been fixed and published. The crates and versions that will continue working even as/if this PR is merged is (please upgrade these to help unblock this PR): * `net2 0.2.36` * `socket2 0.3.16` * `miow 0.2.2` * `miow 0.3.6` * `mio 0.7.6` * `mio 0.6.23` - Never had the invalid assumption itself, but has now been bumped to only allow fixed dependencies (`net2` + `miow`) * `nb-connect 1.0.3` * `quinn 0.5.4` * `quinn 0.6.2` ### Release notes draft This release changes the memory layout of `Ipv4Addr`, `Ipv6Addr`, `SocketAddrV4` and `SocketAddrV6`. The standard library no longer implements these as the corresponding `libc` structs (`sockaddr_in`, `sockaddr_in6` etc.). This internal representation was never exposed, but some crates relied on it anyway by unsafely transmuting. This change will cause those crates to make invalid memory accesses. Notably `net2 <0.2.36`, `socket2 <0.3.16`, `mio <0.7.6`, `miow <0.3.6` and a few other crates are affected. All known affected crates have been patched and have had fixed versions published over a year ago. If any affected crate is still in your dependency tree, you need to upgrade them before using this version of Rust.
2022-07-20Rollup merge of #98101 - vladimir-ea:stdlib_watch_os, r=thomccDylan DPC-1/+1
stdlib support for Apple WatchOS This is a follow-up to https://github.com/rust-lang/rust/pull/95243 (Add Apple WatchOS compiler targets) that adds stdlib support for Apple WatchOS. `@deg4uss3r` `@nagisa`
2022-07-20Library changes for Apple WatchOSVladimir Michael Eatwell-1/+1
2022-07-17Move SocketAddrCRepr to sys_commonLinus Färnstrand-0/+35
2022-06-26Rollup merge of #97140 - joboet:solid_parker, r=m-ou-seMatthias Krüger-3/+106
std: use an event-flag-based thread parker on SOLID `Mutex` and `Condvar` are being replaced by more efficient implementations, which need thread parking themselves (see #93740). Therefore, the generic `Parker` needs to be replaced on all platforms where the new lock implementation will be used, which, after #96393, are SOLID, SGX and Hermit (more PRs coming soon). SOLID, conforming to the [μITRON specification](http://www.ertl.jp/ITRON/SPEC/FILE/mitron-400e.pdf), has event flags, which are a thread parking primitive very similar to `Parker`. However, they do not make any atomic ordering guarantees (even though those can probably be assumed) and necessitate a system call even when the thread token is already available. Hence, this `Parker`, like the Windows parker, uses an extra atomic state variable. I future-proofed the code by wrapping the event flag in a `WaitFlag` structure, as both SGX and Hermit can share the Parker implementation, they just have slightly different primitives (SGX uses signals and Hermit has a thread blocking API). `````@kawadakk````` I assume you are the target maintainer? Could you test this for me?
2022-06-25Rollup merge of #98194 - m-ou-se:leak-locked-pthread-mutex, r=AmanieuMatthias Krüger-3/+16
Leak pthread_{mutex,rwlock}_t if it's dropped while locked. Fixes https://github.com/rust-lang/rust/issues/85434.
2022-06-23Implement IpV{4,6}Addr structs with native Rust encodingLinus Färnstrand-3/+3
2022-06-23Represent SocketAddrV4 and SocketAddrV6 as Rust native encodingLinus Färnstrand-10/+10
2022-06-22Rollup merge of #96768 - m-ou-se:futex-fuchsia, r=tmandryYuki Okushi-0/+1
Use futex based thread parker on Fuchsia.
2022-06-21Use futex based thread parker on Fuchsia.Mara Bos-0/+1
2022-06-20Remove lies in comments.Mara Bos-7/+6
2022-06-19Auto merge of #97791 - m-ou-se:const-locks, r=m-ou-sebors-3/+8
Make {Mutex, Condvar, RwLock}::new() const. This makes it possible to have `static M: Mutex<_> = Mutex::new(..);` 🎉 Our implementations [on Linux](https://github.com/rust-lang/rust/pull/95035), [on Windows](https://github.com/rust-lang/rust/pull/77380), and various BSDs and some tier 3 platforms have already been using a non-allocating const-constructible implementation. As of https://github.com/rust-lang/rust/pull/97647, the remaining platforms (most notably macOS) now have a const-constructible implementation as well. This means we can finally make these functions publicly const. Tracking issue: https://github.com/rust-lang/rust/issues/93740
2022-06-16Leak pthreax_mutex_t when it's dropped while locked.Mara Bos-3/+16
2022-06-15std: relax memory orderings in `Parker`joboet-8/+7
Co-authored-by: Tomoaki Kawada <kawada@kmckk.co.jp>
2022-06-13Lower listen backlog to fix accept crashesAzureMarker-6/+13
See https://github.com/Meziu/rust-horizon/pull/1
2022-06-13Horizon OS STD supportMeziu-0/+5
Co-authored-by: Ian Chamberlain <ian.h.chamberlain@gmail.com> Co-authored-by: Mark Drobnak <mark.drobnak@gmail.com>
2022-06-06Make {Mutex, Condvar, RwLock}::new() const.Mara Bos-2/+2
2022-06-06Make all {Mutex, Condvar, RwLock}::new #[inline].Mara Bos-1/+6
2022-06-05std: solve priority issue for Parkerjoboet-24/+31
2022-06-03Lazily allocate+initialize locks.Mara Bos-8/+83
2022-06-03Use Drop instead of destroy() for locks.Mara Bos-25/+0
2022-05-30Remove "sys isn't exported yet" phraseest31-2/+2
The oldest occurence is from 9e224c2bf18ebf8f871efb2e1aba43ed7970ebb7, which is from the pre-1.0 days. In the years since then, std::sys still hasn't been exported, and the last attempt was met with strong criticism: https://github.com/rust-lang/rust/pull/97151 Thus, removing the "yet" part makes a lot of sense.
2022-05-19std: fix deadlock in `Parker`joboet-4/+4
2022-05-18std: use an event flag based thread parker on SOLIDjoboet-3/+100
2022-05-09Use Rust 2021 prelude in std itself.Mara Bos-2/+1
2022-05-06Auto merge of #96510 - m-ou-se:futex-bsd, r=Amanieubors-0/+3
Use futex-based locks and thread parker on {Free, Open, DragonFly}BSD. This switches *BSD to our futex-based locks and thread parker. Tracking issue: https://github.com/rust-lang/rust/issues/93740 This is a draft, because this still needs a new version of the `libc` crate to be published that includes https://github.com/rust-lang/libc/pull/2770. r? `@Amanieu`
2022-05-05Rollup merge of #96619 - akiekintveld:same_mutex_check_relaxed_ordering, ↵Yuki Okushi-2/+8
r=m-ou-se Relax memory ordering used in SameMutexCheck `SameMutexCheck` only requires atomicity for `self.addr`, but does not need ordering of other memory accesses in either the success or failure case. Using `Relaxed`, the code still correctly handles the case when two threads race to store an address.
2022-05-03Don't use futexes on netbsd.Mara Bos-1/+0
The latest NetBSD release doesn't include the futex syscall yet.
2022-05-01Add commentAustin Kiekintveld-0/+2
2022-05-01Fix formattingAustin Kiekintveld-2/+6
2022-05-01Relax memory ordering used in SameMutexCheckAustin Kiekintveld-1/+1
`SameMutexCheck` only requires atomicity for `self.addr`, but does not need ordering of other memory accesses in either the success or failure case. Using `Relaxed`, the code still correctly handles the case when two threads race to store an address.
2022-05-01Relax memory ordering used in `min_stack`Austin Kiekintveld-2/+2
`min_stack` does not provide any synchronization guarantees to its callers, and only requires atomicity for `MIN` itself, so relaxed memory ordering is sufficient.
2022-04-29Use futex-based locks and thread parker on FreeBSD.Mara Bos-0/+1
2022-04-29Use futex-based locks and thread parker on DragonFlyBSD.Mara Bos-0/+1
2022-04-29Use futex-based locks and thread parker on NetBSD.Mara Bos-0/+1
2022-04-29Use futex-based locks and thread parker on OpenBSD.Mara Bos-0/+1
2022-04-28Auto merge of #96393 - joboet:pthread_parker, r=thomccbors-16/+28
std: directly use pthread in UNIX parker implementation `Mutex` and `Condvar` are being replaced by more efficient implementations, which need thread parking themselves (see #93740). Therefore we should use the `pthread` synchronization primitives directly. Also, we can avoid allocating the mutex and condition variable because the `Parker` struct is being placed in an `Arc` anyways. This basically is just a copy of the current `Mutex` and `Condvar` code, which will however be removed (again, see #93740). An alternative implementation could be to use dedicated private `OsMutex` and `OsCondvar` types, but all the other platforms supported by std actually have their own thread parking primitives. I used `Pin` to guarantee a stable address for the `Parker` struct, while the current implementation does not, rather using extra unsafe declaration. Since the thread struct is shared anyways, I assumed this would not add too much clutter while being clearer.
2022-04-25Make EncodeWide implement FusedIteratorAron Parker-1/+4
2022-04-25std: directly use pthread in UNIX parker implementationjoboet-16/+28
Mutex and Condvar are being replaced by more efficient implementations, which need thread parking themselves (see #93740). Therefore use the pthread synchronization primitives directly. Also, avoid allocating because the Parker struct is being placed in an Arc anyways.
2022-04-16Use a single ReentrantMutex implementation on all platforms.Mara Bos-13/+86
2022-04-14Use u32 instead of i32 for futexes.Mara Bos-6/+6
2022-04-06Rename RWLock to RwLock in std::sys.Mara Bos-18/+18
2022-03-29Make the stdlib largely conform to strict provenance.Aria Beingessner-5/+7
Some things like the unwinders and system APIs are not fully conformant, this only covers a lot of low-hanging fruit.
2022-03-21Move pthread locks to own module.Mara Bos-10/+9
2022-03-10Use implicit capture syntax in format_argsT-O-R-U-S-6/+6
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-02-20Remove assertion on output length for `getsockopt`.Chris Copeland-1/+0
POSIX allows `getsockopt` to set `*option_len` to a smaller value if necessary. Windows will set `*option_len` to 1 for boolean options even when the caller passes a `BOOL` (`int`) with `*option_len` as 4.
2022-02-20Fix `setsockopt` and `getsockopt` parameter names.Chris Copeland-9/+25
Previously `level` was named `opt` and `option_name` was named `val`, then extra names of `payload` or `slot` were used for the option value. This change aligns the wrapper parameters with their names in POSIX. Winsock uses similar but more abbreviated names: `level`, `optname`, `optval`, `optlen`.
2022-02-04Hide Repr details from io::Error, and rework `io::Error::new_const`.Thom Chiovoloni-5/+5
2022-02-02Configure panic hook backtrace behaviorMark Rousskov-57/+0
2022-01-25Avoid double panics when using `TempDir` in testsChris Denton-1/+7