Skip to content

Commit

Permalink
Rollup merge of rust-lang#127789 - Sword-Destiny:master, r=petrochenkov
Browse files Browse the repository at this point in the history
delete #![allow(unsafe_op_in_unsafe_fn)] in teeos

deny unsafe_op_in_unsafe_fn for teeos
  • Loading branch information
matthiaskrgr authored Jul 16, 2024
2 parents 686f75b + 8b1d874 commit 5e4edba
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 27 deletions.
18 changes: 9 additions & 9 deletions std/src/sys/pal/teeos/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,37 +11,37 @@ unsafe impl GlobalAlloc for System {
// Also see <https://github.com/rust-lang/rust/issues/45955> and
// <https://github.com/rust-lang/rust/issues/62251#issuecomment-507580914>.
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
libc::malloc(layout.size()) as *mut u8
unsafe { libc::malloc(layout.size()) as *mut u8 }
} else {
aligned_malloc(&layout)
unsafe { aligned_malloc(&layout) }
}
}

#[inline]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
// See the comment above in `alloc` for why this check looks the way it does.
if layout.align() <= MIN_ALIGN && layout.align() <= layout.size() {
libc::calloc(layout.size(), 1) as *mut u8
unsafe { libc::calloc(layout.size(), 1) as *mut u8 }
} else {
let ptr = self.alloc(layout);
let ptr = unsafe { self.alloc(layout) };
if !ptr.is_null() {
ptr::write_bytes(ptr, 0, layout.size());
unsafe { ptr::write_bytes(ptr, 0, layout.size()) };
}
ptr
}
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
libc::free(ptr as *mut libc::c_void)
unsafe { libc::free(ptr as *mut libc::c_void) }
}

#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if layout.align() <= MIN_ALIGN && layout.align() <= new_size {
libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8
unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
} else {
realloc_fallback(self, ptr, layout, new_size)
unsafe { realloc_fallback(self, ptr, layout, new_size) }
}
}
}
Expand All @@ -52,6 +52,6 @@ unsafe fn aligned_malloc(layout: &Layout) -> *mut u8 {
// posix_memalign requires that the alignment be a multiple of `sizeof(void*)`.
// Since these are all powers of 2, we can just use max.
let align = layout.align().max(crate::mem::size_of::<usize>());
let ret = libc::posix_memalign(&mut out, align, layout.size());
let ret = unsafe { libc::posix_memalign(&mut out, align, layout.size()) };
if ret != 0 { ptr::null_mut() } else { out as *mut u8 }
}
2 changes: 1 addition & 1 deletion std/src/sys/pal/teeos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! This module contains the facade (aka platform-specific) implementations of
//! OS level functionality for Teeos.
#![allow(unsafe_op_in_unsafe_fn)]
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(unused_variables)]
#![allow(dead_code)]

Expand Down
28 changes: 15 additions & 13 deletions std/src/sys/pal/teeos/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,24 @@ impl Thread {
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
let p = Box::into_raw(Box::new(p));
let mut native: libc::pthread_t = mem::zeroed();
let mut attr: libc::pthread_attr_t = mem::zeroed();
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
let mut native: libc::pthread_t = unsafe { mem::zeroed() };
let mut attr: libc::pthread_attr_t = unsafe { mem::zeroed() };
assert_eq!(unsafe { libc::pthread_attr_init(&mut attr) }, 0);
assert_eq!(
libc::pthread_attr_settee(
&mut attr,
libc::TEESMP_THREAD_ATTR_CA_INHERIT,
libc::TEESMP_THREAD_ATTR_TASK_ID_INHERIT,
libc::TEESMP_THREAD_ATTR_HAS_SHADOW,
),
unsafe {
libc::pthread_attr_settee(
&mut attr,
libc::TEESMP_THREAD_ATTR_CA_INHERIT,
libc::TEESMP_THREAD_ATTR_TASK_ID_INHERIT,
libc::TEESMP_THREAD_ATTR_HAS_SHADOW,
)
},
0,
);

let stack_size = cmp::max(stack, min_stack_size(&attr));

match libc::pthread_attr_setstacksize(&mut attr, stack_size) {
match unsafe { libc::pthread_attr_setstacksize(&mut attr, stack_size) } {
0 => {}
n => {
assert_eq!(n, libc::EINVAL);
Expand All @@ -54,20 +56,20 @@ impl Thread {
let page_size = os::page_size();
let stack_size =
(stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
assert_eq!(unsafe { libc::pthread_attr_setstacksize(&mut attr, stack_size) }, 0);
}
};

let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
// Note: if the thread creation fails and this assert fails, then p will
// be leaked. However, an alternative design could cause double-free
// which is clearly worse.
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
assert_eq!(unsafe { libc::pthread_attr_destroy(&mut attr) }, 0);

return if ret != 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
// safe to reconstruct the box so that it gets deallocated.
drop(Box::from_raw(p));
drop(unsafe { Box::from_raw(p) });
Err(io::Error::from_raw_os_error(ret))
} else {
// The new thread will start running earliest after the next yield.
Expand Down
8 changes: 4 additions & 4 deletions std/src/sys/sync/condvar/teeos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,24 +76,24 @@ impl Condvar {

#[inline]
pub unsafe fn wait(&self, mutex: &Mutex) {
let mutex = mutex::raw(mutex);
let mutex = unsafe { mutex::raw(mutex) };
self.verify(mutex);
let r = libc::pthread_cond_wait(raw(self), mutex);
let r = unsafe { libc::pthread_cond_wait(raw(self), mutex) };
debug_assert_eq!(r, 0);
}

pub unsafe fn wait_timeout(&self, mutex: &Mutex, dur: Duration) -> bool {
use crate::sys::time::Timespec;

let mutex = mutex::raw(mutex);
let mutex = unsafe { mutex::raw(mutex) };
self.verify(mutex);

let timeout = Timespec::now(libc::CLOCK_MONOTONIC)
.checked_add_duration(&dur)
.and_then(|t| t.to_timespec())
.unwrap_or(TIMESPEC_MAX);

let r = pthread_cond_timedwait(raw(self), mutex, &timeout);
let r = unsafe { pthread_cond_timedwait(raw(self), mutex, &timeout) };
assert!(r == libc::ETIMEDOUT || r == 0);
r == 0
}
Expand Down

0 comments on commit 5e4edba

Please sign in to comment.