Skip to content

Commit e8578cf

Browse files
committed
Auto merge of rust-lang#124330 - fmease:rollup-a98y7jf, r=fmease
Rollup of 6 pull requests Successful merges: - rust-lang#123316 (Test `#[unix_sigpipe = "inherit"]` with both `SIG_DFL` and `SIG_IGN`) - rust-lang#123794 (More DefineOpaqueTypes::Yes) - rust-lang#123881 (Bump Fuchsia versions) - rust-lang#124281 (fix weak memory bug in TLS on Windows) - rust-lang#124282 (windows fill_utf16_buf: explain the expected return value) - rust-lang#124308 (Add diagnostic item for `std::iter::Enumerate`) r? `@ghost` `@rustbot` modify labels: rollup
2 parents e8ea161 + 0dcdc5d commit e8578cf

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

core/src/iter/adapters/enumerate.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::ops::Try;
1515
#[derive(Clone, Debug)]
1616
#[must_use = "iterators are lazy and do nothing unless consumed"]
1717
#[stable(feature = "rust1", since = "1.0.0")]
18+
#[cfg_attr(not(test), rustc_diagnostic_item = "Enumerate")]
1819
pub struct Enumerate<I> {
1920
iter: I,
2021
count: usize,

std/src/sys/pal/windows/mod.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,21 @@ pub fn to_u16s<S: AsRef<OsStr>>(s: S) -> crate::io::Result<Vec<u16>> {
201201
// currently reside in the buffer. This function is an abstraction over these
202202
// functions by making them easier to call.
203203
//
204-
// The first callback, `f1`, is yielded a (pointer, len) pair which can be
204+
// The first callback, `f1`, is passed a (pointer, len) pair which can be
205205
// passed to a syscall. The `ptr` is valid for `len` items (u16 in this case).
206-
// The closure is expected to return what the syscall returns which will be
207-
// interpreted by this function to determine if the syscall needs to be invoked
208-
// again (with more buffer space).
206+
// The closure is expected to:
207+
// - On success, return the actual length of the written data *without* the null terminator.
208+
// This can be 0. In this case the last_error must be left unchanged.
209+
// - On insufficient buffer space,
210+
// - either return the required length *with* the null terminator,
211+
// - or set the last-error to ERROR_INSUFFICIENT_BUFFER and return `len`.
212+
// - On other failure, return 0 and set last_error.
213+
//
214+
// This is how most but not all syscalls indicate the required buffer space.
215+
// Other syscalls may need translation to match this protocol.
209216
//
210217
// Once the syscall has completed (errors bail out early) the second closure is
211-
// yielded the data which has been read from the syscall. The return value
218+
// passed the data which has been read from the syscall. The return value
212219
// from this closure is then the return value of the function.
213220
pub fn fill_utf16_buf<F1, F2, T>(mut f1: F1, f2: F2) -> crate::io::Result<T>
214221
where

std/src/sys/pal/windows/os.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ fn home_dir_crt() -> Option<PathBuf> {
326326

327327
super::fill_utf16_buf(
328328
|buf, mut sz| {
329+
// GetUserProfileDirectoryW does not quite use the usual protocol for
330+
// negotiating the buffer size, so we have to translate.
329331
match c::GetUserProfileDirectoryW(
330332
ptr::without_provenance_mut(CURRENT_PROCESS_TOKEN),
331333
buf,

std/src/sys/pal/windows/thread_local_key.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,15 @@ impl StaticKey {
141141
panic!("out of TLS indexes");
142142
}
143143

144-
self.key.store(key + 1, Release);
145144
register_dtor(self);
146145

146+
// Release-storing the key needs to be the last thing we do.
147+
// This is because in `fn key()`, other threads will do an acquire load of the key,
148+
// and if that sees this write then it will entirely bypass the `InitOnce`. We thus
149+
// need to establish synchronization through `key`. In particular that acquire load
150+
// must happen-after the register_dtor above, to ensure the dtor actually runs!
151+
self.key.store(key + 1, Release);
152+
147153
let r = c::InitOnceComplete(self.once.get(), 0, ptr::null_mut());
148154
debug_assert_eq!(r, c::TRUE);
149155

@@ -313,17 +319,29 @@ unsafe fn run_dtors() {
313319
// Use acquire ordering to observe key initialization.
314320
let mut cur = DTORS.load(Acquire);
315321
while !cur.is_null() {
316-
let key = (*cur).key.load(Relaxed) - 1;
322+
let pre_key = (*cur).key.load(Acquire);
317323
let dtor = (*cur).dtor.unwrap();
324+
cur = (*cur).next.load(Relaxed);
325+
326+
// In StaticKey::init, we register the dtor before setting `key`.
327+
// So if one thread's `run_dtors` races with another thread executing `init` on the same
328+
// `StaticKey`, we can encounter a key of 0 here. That means this key was never
329+
// initialized in this thread so we can safely skip it.
330+
if pre_key == 0 {
331+
continue;
332+
}
333+
// If this is non-zero, then via the `Acquire` load above we synchronized with
334+
// everything relevant for this key. (It's not clear that this is needed, since the
335+
// release-acquire pair on DTORS also establishes synchronization, but better safe than
336+
// sorry.)
337+
let key = pre_key - 1;
318338

319339
let ptr = c::TlsGetValue(key);
320340
if !ptr.is_null() {
321341
c::TlsSetValue(key, ptr::null_mut());
322342
dtor(ptr as *mut _);
323343
any_run = true;
324344
}
325-
326-
cur = (*cur).next.load(Relaxed);
327345
}
328346

329347
if !any_run {

0 commit comments

Comments
 (0)