Skip to content

Commit 7cae9e8

Browse files
#![deny(unsafe_op_in_unsafe_fn)] in sys/hermit
1 parent ac91673 commit 7cae9e8

File tree

10 files changed

+147
-75
lines changed

10 files changed

+147
-75
lines changed

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

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::alloc::{GlobalAlloc, Layout, System};
24
use crate::ptr;
35
use crate::sys::hermit::abi;
@@ -6,26 +8,33 @@ use crate::sys::hermit::abi;
68
unsafe impl GlobalAlloc for System {
79
#[inline]
810
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
9-
abi::malloc(layout.size(), layout.align())
11+
// SAFETY: The safety contract for `malloc` must be upheld by the caller.
12+
unsafe { abi::malloc(layout.size(), layout.align()) }
1013
}
1114

1215
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
13-
let addr = abi::malloc(layout.size(), layout.align());
16+
// SAFETY: The safety contract for `malloc` must be upheld by the caller.
17+
// Also, `addr` must be valid for writes of `layout.size() * size_of::<u8>()` bytes.
18+
unsafe {
19+
let addr = abi::malloc(layout.size(), layout.align());
1420

15-
if !addr.is_null() {
16-
ptr::write_bytes(addr, 0x00, layout.size());
17-
}
21+
if !addr.is_null() {
22+
ptr::write_bytes(addr, 0x00, layout.size());
23+
}
1824

19-
addr
25+
addr
26+
}
2027
}
2128

2229
#[inline]
2330
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
24-
abi::free(ptr, layout.size(), layout.align())
31+
// SAFETY: The safety contract for `free` must be upheld by the caller.
32+
unsafe { abi::free(ptr, layout.size(), layout.align()) }
2533
}
2634

2735
#[inline]
2836
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
29-
abi::realloc(ptr, layout.size(), layout.align(), new_size)
37+
// SAFETY: The safety contract for `realloc` must be upheld by the caller.
38+
unsafe { abi::realloc(ptr, layout.size(), layout.align(), new_size) }
3039
}
3140
}

library/std/src/sys/hermit/args.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::OsString;
24
use crate::marker::PhantomData;
35
use crate::vec;
46

57
/// One-time global initialization.
68
pub unsafe fn init(argc: isize, argv: *const *const u8) {
7-
imp::init(argc, argv)
9+
unsafe { imp::init(argc, argv) }
810
}
911

1012
/// One-time global cleanup.
1113
pub unsafe fn cleanup() {
12-
imp::cleanup()
14+
unsafe { imp::cleanup() }
1315
}
1416

1517
/// Returns the command line arguments
@@ -65,14 +67,18 @@ mod imp {
6567

6668
pub unsafe fn init(argc: isize, argv: *const *const u8) {
6769
let _guard = LOCK.lock();
68-
ARGC = argc;
69-
ARGV = argv;
70+
unsafe {
71+
ARGC = argc;
72+
ARGV = argv;
73+
}
7074
}
7175

7276
pub unsafe fn cleanup() {
7377
let _guard = LOCK.lock();
7478
ARGC = 0;
75-
ARGV = ptr::null();
79+
unsafe {
80+
ARGV = ptr::null();
81+
}
7682
}
7783

7884
pub fn args() -> Args {

library/std/src/sys/hermit/condvar.rs

+24-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::c_void;
24
use crate::ptr;
35
use crate::sync::atomic::{AtomicUsize, Ordering::SeqCst};
@@ -23,33 +25,43 @@ impl Condvar {
2325
}
2426

2527
pub unsafe fn init(&mut self) {
26-
let _ = abi::sem_init(&mut self.sem1 as *mut *const c_void, 0);
27-
let _ = abi::sem_init(&mut self.sem2 as *mut *const c_void, 0);
28+
unsafe {
29+
let _ = abi::sem_init(&mut self.sem1 as *mut *const c_void, 0);
30+
let _ = abi::sem_init(&mut self.sem2 as *mut *const c_void, 0);
31+
}
2832
}
2933

3034
pub unsafe fn notify_one(&self) {
3135
if self.counter.load(SeqCst) > 0 {
3236
self.counter.fetch_sub(1, SeqCst);
33-
abi::sem_post(self.sem1);
34-
abi::sem_timedwait(self.sem2, 0);
37+
unsafe {
38+
abi::sem_post(self.sem1);
39+
abi::sem_timedwait(self.sem2, 0);
40+
}
3541
}
3642
}
3743

3844
pub unsafe fn notify_all(&self) {
3945
let counter = self.counter.swap(0, SeqCst);
4046
for _ in 0..counter {
41-
abi::sem_post(self.sem1);
47+
unsafe {
48+
abi::sem_post(self.sem1);
49+
}
4250
}
4351
for _ in 0..counter {
44-
abi::sem_timedwait(self.sem2, 0);
52+
unsafe {
53+
abi::sem_timedwait(self.sem2, 0);
54+
}
4555
}
4656
}
4757

4858
pub unsafe fn wait(&self, mutex: &Mutex) {
4959
self.counter.fetch_add(1, SeqCst);
5060
mutex.unlock();
51-
abi::sem_timedwait(self.sem1, 0);
52-
abi::sem_post(self.sem2);
61+
unsafe {
62+
abi::sem_timedwait(self.sem1, 0);
63+
abi::sem_post(self.sem2);
64+
}
5365
mutex.lock();
5466
}
5567

@@ -58,7 +70,9 @@ impl Condvar {
5870
}
5971

6072
pub unsafe fn destroy(&self) {
61-
let _ = abi::sem_destroy(self.sem1);
62-
let _ = abi::sem_destroy(self.sem2);
73+
unsafe {
74+
let _ = abi::sem_destroy(self.sem1);
75+
let _ = abi::sem_destroy(self.sem2);
76+
}
6377
}
6478
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
12
#![unstable(reason = "not public", issue = "none", feature = "fd")]
23

34
use crate::io::{self, ErrorKind, Read};

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

+15-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +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+
#![deny(unsafe_op_in_unsafe_fn)]
17+
1618
use crate::intrinsics;
1719
use crate::os::raw::c_char;
1820

@@ -62,8 +64,12 @@ pub enum Void {}
6264
pub unsafe fn strlen(start: *const c_char) -> usize {
6365
let mut str = start;
6466

65-
while *str != 0 {
66-
str = str.offset(1);
67+
// SAFETY: The safety contract for `*str != 0` must be upheld by the caller.
68+
// `start` must not be null.
69+
unsafe {
70+
while *str != 0 {
71+
str = str.offset(1);
72+
}
6773
}
6874

6975
(str as usize) - (start as usize)
@@ -111,13 +117,15 @@ pub unsafe extern "C" fn runtime_entry(
111117
fn main(argc: isize, argv: *const *const c_char) -> i32;
112118
}
113119

114-
// initialize environment
115-
os::init_environment(env as *const *const i8);
120+
unsafe {
121+
// initialize environment
122+
os::init_environment(env as *const *const i8);
116123

117-
let result = main(argc as isize, argv);
124+
let result = main(argc as isize, argv);
118125

119-
run_dtors();
120-
abi::exit(result);
126+
run_dtors();
127+
abi::exit(result);
128+
}
121129
}
122130

123131
pub fn decode_error_kind(errno: i32) -> ErrorKind {

library/std/src/sys/hermit/mutex.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::ffi::c_void;
24
use crate::ptr;
35
use crate::sys::hermit::abi;
@@ -16,28 +18,34 @@ impl Mutex {
1618

1719
#[inline]
1820
pub unsafe fn init(&mut self) {
19-
let _ = abi::sem_init(&mut self.inner as *mut *const c_void, 1);
21+
unsafe {
22+
let _ = abi::sem_init(&mut self.inner as *mut *const c_void, 1);
23+
}
2024
}
2125

2226
#[inline]
2327
pub unsafe fn lock(&self) {
24-
let _ = abi::sem_timedwait(self.inner, 0);
28+
unsafe {
29+
let _ = abi::sem_timedwait(self.inner, 0);
30+
}
2531
}
2632

2733
#[inline]
2834
pub unsafe fn unlock(&self) {
29-
let _ = abi::sem_post(self.inner);
35+
unsafe {
36+
let _ = abi::sem_post(self.inner);
37+
}
3038
}
3139

3240
#[inline]
3341
pub unsafe fn try_lock(&self) -> bool {
34-
let result = abi::sem_trywait(self.inner);
42+
let result = unsafe { abi::sem_trywait(self.inner) };
3543
result == 0
3644
}
3745

3846
#[inline]
3947
pub unsafe fn destroy(&self) {
40-
let _ = abi::sem_destroy(self.inner);
48+
let _ = unsafe { abi::sem_destroy(self.inner) };
4149
}
4250
}
4351

@@ -52,12 +60,12 @@ impl ReentrantMutex {
5260

5361
#[inline]
5462
pub unsafe fn init(&self) {
55-
let _ = abi::recmutex_init(&self.inner as *const *const c_void as *mut _);
63+
let _ = unsafe { abi::recmutex_init(&self.inner as *const *const c_void as *mut _) };
5664
}
5765

5866
#[inline]
5967
pub unsafe fn lock(&self) {
60-
let _ = abi::recmutex_lock(self.inner);
68+
let _ = unsafe { abi::recmutex_lock(self.inner) };
6169
}
6270

6371
#[inline]
@@ -67,11 +75,11 @@ impl ReentrantMutex {
6775

6876
#[inline]
6977
pub unsafe fn unlock(&self) {
70-
let _ = abi::recmutex_unlock(self.inner);
78+
let _ = unsafe { abi::recmutex_unlock(self.inner) };
7179
}
7280

7381
#[inline]
7482
pub unsafe fn destroy(&self) {
75-
let _ = abi::recmutex_destroy(self.inner);
83+
let _ = unsafe { abi::recmutex_destroy(self.inner) };
7684
}
7785
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::collections::HashMap;
24
use crate::error::Error as StdError;
35
use crate::ffi::{CStr, OsStr, OsString};

library/std/src/sys/hermit/rwlock.rs

+42-26
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::cell::UnsafeCell;
24
use crate::sys::condvar::Condvar;
35
use crate::sys::mutex::Mutex;
@@ -32,62 +34,76 @@ impl RWLock {
3234

3335
#[inline]
3436
pub unsafe fn read(&self) {
35-
self.lock.lock();
36-
while !(*self.state.get()).inc_readers() {
37-
self.cond.wait(&self.lock);
37+
unsafe {
38+
self.lock.lock();
39+
while !(*self.state.get()).inc_readers() {
40+
self.cond.wait(&self.lock);
41+
}
42+
self.lock.unlock();
3843
}
39-
self.lock.unlock();
4044
}
4145

4246
#[inline]
4347
pub unsafe fn try_read(&self) -> bool {
44-
self.lock.lock();
45-
let ok = (*self.state.get()).inc_readers();
46-
self.lock.unlock();
48+
unsafe {
49+
self.lock.lock();
50+
let ok = (*self.state.get()).inc_readers();
51+
self.lock.unlock();
52+
}
4753
return ok;
4854
}
4955

5056
#[inline]
5157
pub unsafe fn write(&self) {
52-
self.lock.lock();
53-
while !(*self.state.get()).inc_writers() {
54-
self.cond.wait(&self.lock);
58+
unsafe {
59+
self.lock.lock();
60+
while !(*self.state.get()).inc_writers() {
61+
self.cond.wait(&self.lock);
62+
}
5563
}
5664
self.lock.unlock();
5765
}
5866

5967
#[inline]
6068
pub unsafe fn try_write(&self) -> bool {
61-
self.lock.lock();
62-
let ok = (*self.state.get()).inc_writers();
63-
self.lock.unlock();
69+
unsafe {
70+
self.lock.lock();
71+
let ok = (*self.state.get()).inc_writers();
72+
self.lock.unlock();
73+
}
6474
return ok;
6575
}
6676

6777
#[inline]
6878
pub unsafe fn read_unlock(&self) {
69-
self.lock.lock();
70-
let notify = (*self.state.get()).dec_readers();
71-
self.lock.unlock();
72-
if notify {
73-
// FIXME: should only wake up one of these some of the time
74-
self.cond.notify_all();
79+
unsafe {
80+
self.lock.lock();
81+
let notify = (*self.state.get()).dec_readers();
82+
self.lock.unlock();
83+
if notify {
84+
// FIXME: should only wake up one of these some of the time
85+
self.cond.notify_all();
86+
}
7587
}
7688
}
7789

7890
#[inline]
7991
pub unsafe fn write_unlock(&self) {
80-
self.lock.lock();
81-
(*self.state.get()).dec_writers();
82-
self.lock.unlock();
83-
// FIXME: should only wake up one of these some of the time
84-
self.cond.notify_all();
92+
unsafe {
93+
self.lock.lock();
94+
(*self.state.get()).dec_writers();
95+
self.lock.unlock();
96+
// FIXME: should only wake up one of these some of the time
97+
self.cond.notify_all();
98+
}
8599
}
86100

87101
#[inline]
88102
pub unsafe fn destroy(&self) {
89-
self.lock.destroy();
90-
self.cond.destroy();
103+
unsafe {
104+
self.lock.destroy();
105+
self.cond.destroy();
106+
}
91107
}
92108
}
93109

0 commit comments

Comments
 (0)