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

fix(hermit): deny(unsafe_op_in_unsafe_fn) #128433

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions library/std/src/os/hermit/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![stable(feature = "rust1", since = "1.0.0")]
#![deny(unsafe_op_in_unsafe_fn)]

#[allow(unused_extern_crates)]
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
25 changes: 11 additions & 14 deletions library/std/src/sys/pal/hermit/alloc.rs
Original file line number Diff line number Diff line change
@@ -1,31 +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())
}

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
let addr = hermit_abi::malloc(layout.size(), layout.align());

if !addr.is_null() {
ptr::write_bytes(addr, 0x00, layout.size());
}

addr
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
19 changes: 11 additions & 8 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 @@ -49,9 +50,7 @@ pub fn unsupported_err() -> crate::io::Error {
}

pub fn abort_internal() -> ! {
unsafe {
hermit_abi::abort();
}
unsafe { hermit_abi::abort() }
}

pub fn hashmap_random_keys() -> (u64, u64) {
Expand Down Expand Up @@ -80,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 @@ -101,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
38 changes: 16 additions & 22 deletions library/std/src/sys/pal/hermit/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,21 +68,21 @@ pub fn current_exe() -> io::Result<PathBuf> {
unsupported()
}

static mut ENV: Option<Mutex<HashMap<OsString, OsString>>> = None;
static ENV: Mutex<Option<HashMap<OsString, OsString>>> = Mutex::new(None);

pub fn init_environment(env: *const *const i8) {
unsafe {
ENV = Some(Mutex::new(HashMap::new()));
let mut guard = ENV.lock().unwrap();
let map = guard.insert(HashMap::new());

if env.is_null() {
return;
}
if env.is_null() {
return;
}

let mut guard = ENV.as_ref().unwrap().lock().unwrap();
unsafe {
let mut environ = env;
while !(*environ).is_null() {
if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
guard.insert(key, value);
map.insert(key, value);
}
environ = environ.add(1);
}
Expand Down Expand Up @@ -154,30 +154,26 @@ impl Iterator for Env {
/// Returns a vector of (variable, value) byte-vector pairs for all the
/// environment variables of the current process.
pub fn env() -> Env {
unsafe {
let guard = ENV.as_ref().unwrap().lock().unwrap();
let mut result = Vec::new();
let guard = ENV.lock().unwrap();
let env = guard.as_ref().unwrap();

for (key, value) in guard.iter() {
result.push((key.clone(), value.clone()));
}
let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect::<Vec<_>>();

return Env { iter: result.into_iter() };
}
Env { iter: result.into_iter() }
}

pub fn getenv(k: &OsStr) -> Option<OsString> {
unsafe { ENV.as_ref().unwrap().lock().unwrap().get_mut(k).cloned() }
ENV.lock().unwrap().as_ref().unwrap().get(k).cloned()
}

pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
let (k, v) = (k.to_owned(), v.to_owned());
ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
ENV.lock().unwrap().as_mut().unwrap().insert(k, v);
Ok(())
}

pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
ENV.as_ref().unwrap().lock().unwrap().remove(k);
ENV.lock().unwrap().as_mut().unwrap().remove(k);
Ok(())
}

Expand All @@ -190,9 +186,7 @@ pub fn home_dir() -> Option<PathBuf> {
}

pub fn exit(code: i32) -> ! {
unsafe {
hermit_abi::exit(code);
}
unsafe { hermit_abi::exit(code) }
}

pub fn getpid() -> u32 {
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
Loading