Skip to content

Commit efdcce1

Browse files
authored
Rollup merge of rust-lang#62738 - nathanwhit:fix_mem_uninit_cloudabi, r=RalfJung
Remove uses of mem::uninitialized from std::sys::cloudabi Addresses rust-lang#62397 for std::sys::cloudabi, excluding the tests within cloudabi, which will be a separate PR
2 parents a7d9939 + b70f217 commit efdcce1

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

src/libstd/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@
273273
#![feature(link_args)]
274274
#![feature(linkage)]
275275
#![feature(maybe_uninit_ref)]
276+
#![feature(maybe_uninit_slice)]
276277
#![feature(mem_take)]
277278
#![feature(needs_panic_runtime)]
278279
#![feature(never_type)]

src/libstd/sys/cloudabi/abi/cloudabi.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,7 @@ pub unsafe fn clock_res_get(clock_id_: clockid, resolution_: &mut timestamp) ->
18841884
/// **time**:
18851885
/// The time value of the clock.
18861886
#[inline]
1887-
pub unsafe fn clock_time_get(clock_id_: clockid, precision_: timestamp, time_: &mut timestamp) -> errno {
1887+
pub unsafe fn clock_time_get(clock_id_: clockid, precision_: timestamp, time_: *mut timestamp) -> errno {
18881888
cloudabi_sys_clock_time_get(clock_id_, precision_, time_)
18891889
}
18901890

@@ -2643,7 +2643,7 @@ pub unsafe fn mem_unmap(mapping_: &mut [u8]) -> errno {
26432643
/// **nevents**:
26442644
/// The number of events stored.
26452645
#[inline]
2646-
pub unsafe fn poll(in_: *const subscription, out_: *mut event, nsubscriptions_: usize, nevents_: &mut usize) -> errno {
2646+
pub unsafe fn poll(in_: *const subscription, out_: *mut event, nsubscriptions_: usize, nevents_: *mut usize) -> errno {
26472647
cloudabi_sys_poll(in_, out_, nsubscriptions_, nevents_)
26482648
}
26492649

src/libstd/sys/cloudabi/condvar.rs

+20-9
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,21 @@ impl Condvar {
7979
},
8080
..mem::zeroed()
8181
};
82-
let mut event: abi::event = mem::uninitialized();
83-
let mut nevents: usize = mem::uninitialized();
84-
let ret = abi::poll(&subscription, &mut event, 1, &mut nevents);
82+
let mut event: mem::MaybeUninit<abi::event> = mem::MaybeUninit::uninit();
83+
let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit();
84+
let ret = abi::poll(
85+
&subscription,
86+
event.as_mut_ptr(),
87+
1,
88+
nevents.as_mut_ptr()
89+
);
8590
assert_eq!(
8691
ret,
8792
abi::errno::SUCCESS,
8893
"Failed to wait on condition variable"
8994
);
9095
assert_eq!(
91-
event.error,
96+
event.assume_init().error,
9297
abi::errno::SUCCESS,
9398
"Failed to wait on condition variable"
9499
);
@@ -131,21 +136,27 @@ impl Condvar {
131136
..mem::zeroed()
132137
},
133138
];
134-
let mut events: [abi::event; 2] = mem::uninitialized();
135-
let mut nevents: usize = mem::uninitialized();
136-
let ret = abi::poll(subscriptions.as_ptr(), events.as_mut_ptr(), 2, &mut nevents);
139+
let mut events: [mem::MaybeUninit<abi::event>; 2] = [mem::MaybeUninit::uninit(); 2];
140+
let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit();
141+
let ret = abi::poll(
142+
subscriptions.as_ptr(),
143+
mem::MaybeUninit::first_ptr_mut(&mut events),
144+
2,
145+
nevents.as_mut_ptr()
146+
);
137147
assert_eq!(
138148
ret,
139149
abi::errno::SUCCESS,
140150
"Failed to wait on condition variable"
141151
);
152+
let nevents = nevents.assume_init();
142153
for i in 0..nevents {
143154
assert_eq!(
144-
events[i].error,
155+
events[i].assume_init().error,
145156
abi::errno::SUCCESS,
146157
"Failed to wait on condition variable"
147158
);
148-
if events[i].type_ == abi::eventtype::CONDVAR {
159+
if events[i].assume_init().type_ == abi::eventtype::CONDVAR {
149160
return true;
150161
}
151162
}

src/libstd/sys/cloudabi/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ pub use libc::strlen;
6161

6262
pub fn hashmap_random_keys() -> (u64, u64) {
6363
unsafe {
64-
let mut v = mem::uninitialized();
65-
libc::arc4random_buf(&mut v as *mut _ as *mut libc::c_void, mem::size_of_val(&v));
66-
v
64+
let mut v: mem::MaybeUninit<(u64, u64)> = mem::MaybeUninit::uninit();
65+
libc::arc4random_buf(
66+
v.as_mut_ptr() as *mut libc::c_void,
67+
mem::size_of_val(&v)
68+
);
69+
v.assume_init()
6770
}
6871
}

src/libstd/sys/cloudabi/mutex.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::cell::UnsafeCell;
22
use crate::mem;
3+
use crate::mem::MaybeUninit;
34
use crate::sync::atomic::{AtomicU32, Ordering};
45
use crate::sys::cloudabi::abi;
56
use crate::sys::rwlock::{self, RWLock};
@@ -47,24 +48,27 @@ impl Mutex {
4748
}
4849

4950
pub struct ReentrantMutex {
50-
lock: UnsafeCell<AtomicU32>,
51-
recursion: UnsafeCell<u32>,
51+
lock: UnsafeCell<MaybeUninit<AtomicU32>>,
52+
recursion: UnsafeCell<MaybeUninit<u32>>,
5253
}
5354

5455
impl ReentrantMutex {
5556
pub unsafe fn uninitialized() -> ReentrantMutex {
56-
mem::uninitialized()
57+
ReentrantMutex {
58+
lock: UnsafeCell::new(MaybeUninit::uninit()),
59+
recursion: UnsafeCell::new(MaybeUninit::uninit())
60+
}
5761
}
5862

5963
pub unsafe fn init(&mut self) {
60-
self.lock = UnsafeCell::new(AtomicU32::new(abi::LOCK_UNLOCKED.0));
61-
self.recursion = UnsafeCell::new(0);
64+
self.lock = UnsafeCell::new(MaybeUninit::new(AtomicU32::new(abi::LOCK_UNLOCKED.0)));
65+
self.recursion = UnsafeCell::new(MaybeUninit::new(0));
6266
}
6367

6468
pub unsafe fn try_lock(&self) -> bool {
6569
// Attempt to acquire the lock.
66-
let lock = self.lock.get();
67-
let recursion = self.recursion.get();
70+
let lock = (*self.lock.get()).as_mut_ptr();
71+
let recursion = (*self.recursion.get()).as_mut_ptr();
6872
if let Err(old) = (*lock).compare_exchange(
6973
abi::LOCK_UNLOCKED.0,
7074
__pthread_thread_id.0 | abi::LOCK_WRLOCKED.0,
@@ -109,8 +113,8 @@ impl ReentrantMutex {
109113
}
110114

111115
pub unsafe fn unlock(&self) {
112-
let lock = self.lock.get();
113-
let recursion = self.recursion.get();
116+
let lock = (*self.lock.get()).as_mut_ptr();
117+
let recursion = (*self.recursion.get()).as_mut_ptr();
114118
assert_eq!(
115119
(*lock).load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0,
116120
__pthread_thread_id.0 | abi::LOCK_WRLOCKED.0,
@@ -136,8 +140,8 @@ impl ReentrantMutex {
136140
}
137141

138142
pub unsafe fn destroy(&self) {
139-
let lock = self.lock.get();
140-
let recursion = self.recursion.get();
143+
let lock = (*self.lock.get()).as_mut_ptr();
144+
let recursion = (*self.recursion.get()).as_mut_ptr();
141145
assert_eq!(
142146
(*lock).load(Ordering::Relaxed),
143147
abi::LOCK_UNLOCKED.0,

src/libstd/sys/cloudabi/time.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ pub fn checked_dur2intervals(dur: &Duration) -> Option<abi::timestamp> {
1818
impl Instant {
1919
pub fn now() -> Instant {
2020
unsafe {
21-
let mut t = mem::uninitialized();
22-
let ret = abi::clock_time_get(abi::clockid::MONOTONIC, 0, &mut t);
21+
let mut t: mem::MaybeUninit<abi::timestamp> = mem::MaybeUninit::uninit();
22+
let ret = abi::clock_time_get(abi::clockid::MONOTONIC, 0, t.as_mut_ptr());
2323
assert_eq!(ret, abi::errno::SUCCESS);
24-
Instant { t }
24+
Instant { t: t.assume_init() }
2525
}
2626
}
2727

@@ -59,10 +59,10 @@ pub struct SystemTime {
5959
impl SystemTime {
6060
pub fn now() -> SystemTime {
6161
unsafe {
62-
let mut t = mem::uninitialized();
63-
let ret = abi::clock_time_get(abi::clockid::REALTIME, 0, &mut t);
62+
let mut t: mem::MaybeUninit<abi::timestamp> = mem::MaybeUninit::uninit();
63+
let ret = abi::clock_time_get(abi::clockid::REALTIME, 0, t.as_mut_ptr());
6464
assert_eq!(ret, abi::errno::SUCCESS);
65-
SystemTime { t }
65+
SystemTime { t: t.assume_init() }
6666
}
6767
}
6868

0 commit comments

Comments
 (0)