Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete #![allow(unsafe_op_in_unsafe_fn)] in teeos #127789

Merged
merged 5 commits into from
Jul 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions library/std/src/sys/pal/teeos/alloc.rs
Original file line number Diff line number Diff line change
@@ -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) }
}
}
}
@@ -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 library/std/src/sys/pal/teeos/mod.rs
Original file line number Diff line number Diff line change
@@ -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)]

28 changes: 15 additions & 13 deletions library/std/src/sys/pal/teeos/thread.rs
Original file line number Diff line number Diff line change
@@ -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);
@@ -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.
8 changes: 4 additions & 4 deletions library/std/src/sys/sync/condvar/teeos.rs
Original file line number Diff line number Diff line change
@@ -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
}
Loading