Skip to content

Commit ab00ae6

Browse files
authored
Rollup merge of rust-lang#128433 - hermit-os:hermit-unsafe_op_in_unsafe_fn, r=joboet
fix(hermit): `deny(unsafe_op_in_unsafe_fn)` Tracking issue: rust-lang#127747 r? workingjubilee CC: ``@stlankes``
2 parents 32894e2 + 589c0a0 commit ab00ae6

File tree

7 files changed

+58
-55
lines changed

7 files changed

+58
-55
lines changed

std/src/os/hermit/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![stable(feature = "rust1", since = "1.0.0")]
2+
#![deny(unsafe_op_in_unsafe_fn)]
23

34
#[allow(unused_extern_crates)]
45
#[stable(feature = "rust1", since = "1.0.0")]

std/src/sys/pal/hermit/alloc.rs

+11-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
use super::hermit_abi;
22
use crate::alloc::{GlobalAlloc, Layout, System};
3-
use crate::ptr;
43

54
#[stable(feature = "alloc_system_type", since = "1.28.0")]
65
unsafe impl GlobalAlloc for System {
76
#[inline]
87
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
9-
hermit_abi::malloc(layout.size(), layout.align())
10-
}
11-
12-
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
13-
let addr = hermit_abi::malloc(layout.size(), layout.align());
14-
15-
if !addr.is_null() {
16-
ptr::write_bytes(addr, 0x00, layout.size());
17-
}
18-
19-
addr
8+
let size = layout.size();
9+
let align = layout.align();
10+
unsafe { hermit_abi::malloc(size, align) }
2011
}
2112

2213
#[inline]
2314
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
24-
hermit_abi::free(ptr, layout.size(), layout.align())
15+
let size = layout.size();
16+
let align = layout.align();
17+
unsafe {
18+
hermit_abi::free(ptr, size, align);
19+
}
2520
}
2621

2722
#[inline]
2823
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
29-
hermit_abi::realloc(ptr, layout.size(), layout.align(), new_size)
24+
let size = layout.size();
25+
let align = layout.align();
26+
unsafe { hermit_abi::realloc(ptr, size, align, new_size) }
3027
}
3128
}

std/src/sys/pal/hermit/fd.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ impl FromInner<OwnedFd> for FileDesc {
111111

112112
impl FromRawFd for FileDesc {
113113
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
114-
Self { fd: FromRawFd::from_raw_fd(raw_fd) }
114+
let fd = unsafe { OwnedFd::from_raw_fd(raw_fd) };
115+
Self { fd }
115116
}
116117
}
117118

std/src/sys/pal/hermit/fs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,8 @@ impl IntoRawFd for File {
484484

485485
impl FromRawFd for File {
486486
unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
487-
Self(FromRawFd::from_raw_fd(raw_fd))
487+
let file_desc = unsafe { FileDesc::from_raw_fd(raw_fd) };
488+
Self(file_desc)
488489
}
489490
}
490491

std/src/sys/pal/hermit/mod.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
//! compiling for wasm. That way it's a compile time error for something that's
1414
//! guaranteed to be a runtime error!
1515
16-
#![allow(missing_docs, nonstandard_style, unsafe_op_in_unsafe_fn)]
16+
#![deny(unsafe_op_in_unsafe_fn)]
17+
#![allow(missing_docs, nonstandard_style)]
1718

1819
use crate::os::raw::c_char;
1920

@@ -49,9 +50,7 @@ pub fn unsupported_err() -> crate::io::Error {
4950
}
5051

5152
pub fn abort_internal() -> ! {
52-
unsafe {
53-
hermit_abi::abort();
54-
}
53+
unsafe { hermit_abi::abort() }
5554
}
5655

5756
pub fn hashmap_random_keys() -> (u64, u64) {
@@ -80,7 +79,9 @@ pub extern "C" fn __rust_abort() {
8079
// SAFETY: must be called only once during runtime initialization.
8180
// NOTE: this is not guaranteed to run, for example when Rust code is called externally.
8281
pub unsafe fn init(argc: isize, argv: *const *const u8, _sigpipe: u8) {
83-
args::init(argc, argv);
82+
unsafe {
83+
args::init(argc, argv);
84+
}
8485
}
8586

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

104-
let result = main(argc as isize, argv);
105+
let result = unsafe { main(argc as isize, argv) };
105106

106-
crate::sys::thread_local::destructors::run();
107-
hermit_abi::exit(result);
107+
unsafe {
108+
crate::sys::thread_local::destructors::run();
109+
}
110+
unsafe { hermit_abi::exit(result) }
108111
}
109112

110113
#[inline]

std/src/sys/pal/hermit/os.rs

+16-22
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,21 @@ pub fn current_exe() -> io::Result<PathBuf> {
6868
unsupported()
6969
}
7070

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

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

77-
if env.is_null() {
78-
return;
79-
}
77+
if env.is_null() {
78+
return;
79+
}
8080

81-
let mut guard = ENV.as_ref().unwrap().lock().unwrap();
81+
unsafe {
8282
let mut environ = env;
8383
while !(*environ).is_null() {
8484
if let Some((key, value)) = parse(CStr::from_ptr(*environ).to_bytes()) {
85-
guard.insert(key, value);
85+
map.insert(key, value);
8686
}
8787
environ = environ.add(1);
8888
}
@@ -154,30 +154,26 @@ impl Iterator for Env {
154154
/// Returns a vector of (variable, value) byte-vector pairs for all the
155155
/// environment variables of the current process.
156156
pub fn env() -> Env {
157-
unsafe {
158-
let guard = ENV.as_ref().unwrap().lock().unwrap();
159-
let mut result = Vec::new();
157+
let guard = ENV.lock().unwrap();
158+
let env = guard.as_ref().unwrap();
160159

161-
for (key, value) in guard.iter() {
162-
result.push((key.clone(), value.clone()));
163-
}
160+
let result = env.iter().map(|(key, value)| (key.clone(), value.clone())).collect::<Vec<_>>();
164161

165-
return Env { iter: result.into_iter() };
166-
}
162+
Env { iter: result.into_iter() }
167163
}
168164

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

173169
pub unsafe fn setenv(k: &OsStr, v: &OsStr) -> io::Result<()> {
174170
let (k, v) = (k.to_owned(), v.to_owned());
175-
ENV.as_ref().unwrap().lock().unwrap().insert(k, v);
171+
ENV.lock().unwrap().as_mut().unwrap().insert(k, v);
176172
Ok(())
177173
}
178174

179175
pub unsafe fn unsetenv(k: &OsStr) -> io::Result<()> {
180-
ENV.as_ref().unwrap().lock().unwrap().remove(k);
176+
ENV.lock().unwrap().as_mut().unwrap().remove(k);
181177
Ok(())
182178
}
183179

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

192188
pub fn exit(code: i32) -> ! {
193-
unsafe {
194-
hermit_abi::exit(code);
195-
}
189+
unsafe { hermit_abi::exit(code) }
196190
}
197191

198192
pub fn getpid() -> u32 {

std/src/sys/pal/hermit/thread.rs

+15-9
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,22 @@ impl Thread {
2525
core_id: isize,
2626
) -> io::Result<Thread> {
2727
let p = Box::into_raw(Box::new(p));
28-
let tid = hermit_abi::spawn2(
29-
thread_start,
30-
p.expose_provenance(),
31-
hermit_abi::Priority::into(hermit_abi::NORMAL_PRIO),
32-
stack,
33-
core_id,
34-
);
28+
let tid = unsafe {
29+
hermit_abi::spawn2(
30+
thread_start,
31+
p.expose_provenance(),
32+
hermit_abi::Priority::into(hermit_abi::NORMAL_PRIO),
33+
stack,
34+
core_id,
35+
)
36+
};
3537

3638
return if tid == 0 {
3739
// The thread failed to start and as a result p was not consumed. Therefore, it is
3840
// safe to reconstruct the box so that it gets deallocated.
39-
drop(Box::from_raw(p));
41+
unsafe {
42+
drop(Box::from_raw(p));
43+
}
4044
Err(io::const_io_error!(io::ErrorKind::Uncategorized, "Unable to create thread!"))
4145
} else {
4246
Ok(Thread { tid: tid })
@@ -54,7 +58,9 @@ impl Thread {
5458
}
5559

5660
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
57-
Thread::new_with_coreid(stack, p, -1 /* = no specific core */)
61+
unsafe {
62+
Thread::new_with_coreid(stack, p, -1 /* = no specific core */)
63+
}
5864
}
5965

6066
#[inline]

0 commit comments

Comments
 (0)