Skip to content

Commit

Permalink
Rollup merge of rust-lang#62738 - nathanwhit:fix_mem_uninit_cloudabi,…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
Centril authored Jul 24, 2019
2 parents a7d9939 + b70f217 commit efdcce1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 31 deletions.
1 change: 1 addition & 0 deletions src/libstd/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@
#![feature(link_args)]
#![feature(linkage)]
#![feature(maybe_uninit_ref)]
#![feature(maybe_uninit_slice)]
#![feature(mem_take)]
#![feature(needs_panic_runtime)]
#![feature(never_type)]
Expand Down
4 changes: 2 additions & 2 deletions src/libstd/sys/cloudabi/abi/cloudabi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,7 @@ pub unsafe fn clock_res_get(clock_id_: clockid, resolution_: &mut timestamp) ->
/// **time**:
/// The time value of the clock.
#[inline]
pub unsafe fn clock_time_get(clock_id_: clockid, precision_: timestamp, time_: &mut timestamp) -> errno {
pub unsafe fn clock_time_get(clock_id_: clockid, precision_: timestamp, time_: *mut timestamp) -> errno {
cloudabi_sys_clock_time_get(clock_id_, precision_, time_)
}

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

Expand Down
29 changes: 20 additions & 9 deletions src/libstd/sys/cloudabi/condvar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ impl Condvar {
},
..mem::zeroed()
};
let mut event: abi::event = mem::uninitialized();
let mut nevents: usize = mem::uninitialized();
let ret = abi::poll(&subscription, &mut event, 1, &mut nevents);
let mut event: mem::MaybeUninit<abi::event> = mem::MaybeUninit::uninit();
let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit();
let ret = abi::poll(
&subscription,
event.as_mut_ptr(),
1,
nevents.as_mut_ptr()
);
assert_eq!(
ret,
abi::errno::SUCCESS,
"Failed to wait on condition variable"
);
assert_eq!(
event.error,
event.assume_init().error,
abi::errno::SUCCESS,
"Failed to wait on condition variable"
);
Expand Down Expand Up @@ -131,21 +136,27 @@ impl Condvar {
..mem::zeroed()
},
];
let mut events: [abi::event; 2] = mem::uninitialized();
let mut nevents: usize = mem::uninitialized();
let ret = abi::poll(subscriptions.as_ptr(), events.as_mut_ptr(), 2, &mut nevents);
let mut events: [mem::MaybeUninit<abi::event>; 2] = [mem::MaybeUninit::uninit(); 2];
let mut nevents: mem::MaybeUninit<usize> = mem::MaybeUninit::uninit();
let ret = abi::poll(
subscriptions.as_ptr(),
mem::MaybeUninit::first_ptr_mut(&mut events),
2,
nevents.as_mut_ptr()
);
assert_eq!(
ret,
abi::errno::SUCCESS,
"Failed to wait on condition variable"
);
let nevents = nevents.assume_init();
for i in 0..nevents {
assert_eq!(
events[i].error,
events[i].assume_init().error,
abi::errno::SUCCESS,
"Failed to wait on condition variable"
);
if events[i].type_ == abi::eventtype::CONDVAR {
if events[i].assume_init().type_ == abi::eventtype::CONDVAR {
return true;
}
}
Expand Down
9 changes: 6 additions & 3 deletions src/libstd/sys/cloudabi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ pub use libc::strlen;

pub fn hashmap_random_keys() -> (u64, u64) {
unsafe {
let mut v = mem::uninitialized();
libc::arc4random_buf(&mut v as *mut _ as *mut libc::c_void, mem::size_of_val(&v));
v
let mut v: mem::MaybeUninit<(u64, u64)> = mem::MaybeUninit::uninit();
libc::arc4random_buf(
v.as_mut_ptr() as *mut libc::c_void,
mem::size_of_val(&v)
);
v.assume_init()
}
}
26 changes: 15 additions & 11 deletions src/libstd/sys/cloudabi/mutex.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::cell::UnsafeCell;
use crate::mem;
use crate::mem::MaybeUninit;
use crate::sync::atomic::{AtomicU32, Ordering};
use crate::sys::cloudabi::abi;
use crate::sys::rwlock::{self, RWLock};
Expand Down Expand Up @@ -47,24 +48,27 @@ impl Mutex {
}

pub struct ReentrantMutex {
lock: UnsafeCell<AtomicU32>,
recursion: UnsafeCell<u32>,
lock: UnsafeCell<MaybeUninit<AtomicU32>>,
recursion: UnsafeCell<MaybeUninit<u32>>,
}

impl ReentrantMutex {
pub unsafe fn uninitialized() -> ReentrantMutex {
mem::uninitialized()
ReentrantMutex {
lock: UnsafeCell::new(MaybeUninit::uninit()),
recursion: UnsafeCell::new(MaybeUninit::uninit())
}
}

pub unsafe fn init(&mut self) {
self.lock = UnsafeCell::new(AtomicU32::new(abi::LOCK_UNLOCKED.0));
self.recursion = UnsafeCell::new(0);
self.lock = UnsafeCell::new(MaybeUninit::new(AtomicU32::new(abi::LOCK_UNLOCKED.0)));
self.recursion = UnsafeCell::new(MaybeUninit::new(0));
}

pub unsafe fn try_lock(&self) -> bool {
// Attempt to acquire the lock.
let lock = self.lock.get();
let recursion = self.recursion.get();
let lock = (*self.lock.get()).as_mut_ptr();
let recursion = (*self.recursion.get()).as_mut_ptr();
if let Err(old) = (*lock).compare_exchange(
abi::LOCK_UNLOCKED.0,
__pthread_thread_id.0 | abi::LOCK_WRLOCKED.0,
Expand Down Expand Up @@ -109,8 +113,8 @@ impl ReentrantMutex {
}

pub unsafe fn unlock(&self) {
let lock = self.lock.get();
let recursion = self.recursion.get();
let lock = (*self.lock.get()).as_mut_ptr();
let recursion = (*self.recursion.get()).as_mut_ptr();
assert_eq!(
(*lock).load(Ordering::Relaxed) & !abi::LOCK_KERNEL_MANAGED.0,
__pthread_thread_id.0 | abi::LOCK_WRLOCKED.0,
Expand All @@ -136,8 +140,8 @@ impl ReentrantMutex {
}

pub unsafe fn destroy(&self) {
let lock = self.lock.get();
let recursion = self.recursion.get();
let lock = (*self.lock.get()).as_mut_ptr();
let recursion = (*self.recursion.get()).as_mut_ptr();
assert_eq!(
(*lock).load(Ordering::Relaxed),
abi::LOCK_UNLOCKED.0,
Expand Down
12 changes: 6 additions & 6 deletions src/libstd/sys/cloudabi/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub fn checked_dur2intervals(dur: &Duration) -> Option<abi::timestamp> {
impl Instant {
pub fn now() -> Instant {
unsafe {
let mut t = mem::uninitialized();
let ret = abi::clock_time_get(abi::clockid::MONOTONIC, 0, &mut t);
let mut t: mem::MaybeUninit<abi::timestamp> = mem::MaybeUninit::uninit();
let ret = abi::clock_time_get(abi::clockid::MONOTONIC, 0, t.as_mut_ptr());
assert_eq!(ret, abi::errno::SUCCESS);
Instant { t }
Instant { t: t.assume_init() }
}
}

Expand Down Expand Up @@ -59,10 +59,10 @@ pub struct SystemTime {
impl SystemTime {
pub fn now() -> SystemTime {
unsafe {
let mut t = mem::uninitialized();
let ret = abi::clock_time_get(abi::clockid::REALTIME, 0, &mut t);
let mut t: mem::MaybeUninit<abi::timestamp> = mem::MaybeUninit::uninit();
let ret = abi::clock_time_get(abi::clockid::REALTIME, 0, t.as_mut_ptr());
assert_eq!(ret, abi::errno::SUCCESS);
SystemTime { t }
SystemTime { t: t.assume_init() }
}
}

Expand Down

0 comments on commit efdcce1

Please sign in to comment.