Skip to content

Commit

Permalink
fix(pal/hermit): deny(unsafe_op_in_unsafe_fn)
Browse files Browse the repository at this point in the history
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
  • Loading branch information
mkroening committed Aug 1, 2024
1 parent b21af62 commit 4da966c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
15 changes: 11 additions & 4 deletions library/std/src/sys/pal/hermit/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
use super::hermit_abi;
use crate::alloc::{GlobalAlloc, Layout, System};
use crate::ptr;

#[stable(feature = "alloc_system_type", since = "1.28.0")]
unsafe impl GlobalAlloc for System {
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
hermit_abi::malloc(layout.size(), layout.align())
let size = layout.size();
let align = layout.align();
unsafe { hermit_abi::malloc(size, align) }
}

#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
hermit_abi::free(ptr, layout.size(), layout.align())
let size = layout.size();
let align = layout.align();
unsafe {
hermit_abi::free(ptr, size, align);
}
}

#[inline]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
hermit_abi::realloc(ptr, layout.size(), layout.align(), new_size)
let size = layout.size();
let align = layout.align();
unsafe { hermit_abi::realloc(ptr, size, align, new_size) }
}
}
3 changes: 2 additions & 1 deletion library/std/src/sys/pal/hermit/fd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ impl FromInner<OwnedFd> for FileDesc {

impl FromRawFd for FileDesc {
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
Self { fd: FromRawFd::from_raw_fd(raw_fd) }
let fd = unsafe { OwnedFd::from_raw_fd(raw_fd) };
Self { fd }
}
}

Expand Down
3 changes: 2 additions & 1 deletion library/std/src/sys/pal/hermit/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ impl IntoRawFd for File {

impl FromRawFd for File {
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
Self(FromRawFd::from_raw_fd(raw_fd))
let file_desc = unsafe { FileDesc::from_raw_fd(raw_fd) };
Self(file_desc)
}
}

Expand Down
15 changes: 10 additions & 5 deletions library/std/src/sys/pal/hermit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
//! compiling for wasm. That way it's a compile time error for something that's
//! guaranteed to be a runtime error!
#![allow(missing_docs, nonstandard_style, unsafe_op_in_unsafe_fn)]
#![deny(unsafe_op_in_unsafe_fn)]
#![allow(missing_docs, nonstandard_style)]

use crate::os::raw::c_char;

Expand Down Expand Up @@ -78,7 +79,9 @@ pub extern "C" fn __rust_abort() {
// SAFETY: must be called only once during runtime initialization.
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
pub unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {
args::init(argc, argv);
unsafe {
args::init(argc, argv);
}
}

// SAFETY: must be called only once during runtime cleanup.
Expand All @@ -99,10 +102,12 @@ pub unsafe extern "C" fn runtime_entry(
// initialize environment
os::init_environment(env as *const *const i8);

let result = main(argc as isize, argv);
let result = unsafe { main(argc as isize, argv) };

crate::sys::thread_local::destructors::run();
hermit_abi::exit(result)
unsafe {
crate::sys::thread_local::destructors::run();
}
unsafe { hermit_abi::exit(result) }
}

#[inline]
Expand Down
24 changes: 15 additions & 9 deletions library/std/src/sys/pal/hermit/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,22 @@ impl Thread {
core_id: isize,
) -> io::Result<Thread> {
let p = Box::into_raw(Box::new(p));
let tid = hermit_abi::spawn2(
thread_start,
p.expose_provenance(),
hermit_abi::Priority::into(hermit_abi::NORMAL_PRIO),
stack,
core_id,
);
let tid = unsafe {
hermit_abi::spawn2(
thread_start,
p.expose_provenance(),
hermit_abi::Priority::into(hermit_abi::NORMAL_PRIO),
stack,
core_id,
)
};

return if tid == 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));
unsafe {
drop(Box::from_raw(p));
}
Err(io::const_io_error!(io::ErrorKind::Uncategorized, "Unable to create thread!"))
} else {
Ok(Thread { tid: tid })
Expand All @@ -54,7 +58,9 @@ impl Thread {
}

pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
Thread::new_with_coreid(stack, p, -1 /* = no specific core */)
unsafe {
Thread::new_with_coreid(stack, p, -1 /* = no specific core */)
}
}

#[inline]
Expand Down

0 comments on commit 4da966c

Please sign in to comment.