Skip to content

Commit d413bb6

Browse files
author
chansuke
committed
#[deny(unsafe_op_in_unsafe_fn)] in sys/wasm
1 parent 7bade6e commit d413bb6

File tree

4 files changed

+32
-18
lines changed

4 files changed

+32
-18
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,25 @@ unsafe impl GlobalAlloc for System {
2525
#[inline]
2626
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
2727
let _lock = lock::lock();
28-
DLMALLOC.malloc(layout.size(), layout.align())
28+
unsafe { DLMALLOC.malloc(layout.size(), layout.align()) }
2929
}
3030

3131
#[inline]
3232
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
3333
let _lock = lock::lock();
34-
DLMALLOC.calloc(layout.size(), layout.align())
34+
unsafe { DLMALLOC.calloc(layout.size(), layout.align()) }
3535
}
3636

3737
#[inline]
3838
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
3939
let _lock = lock::lock();
40-
DLMALLOC.free(ptr, layout.size(), layout.align())
40+
unsafe { DLMALLOC.free(ptr, layout.size(), layout.align()) }
4141
}
4242

4343
#[inline]
4444
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
4545
let _lock = lock::lock();
46-
DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size)
46+
unsafe { DLMALLOC.realloc(ptr, layout.size(), layout.align(), new_size) }
4747
}
4848
}
4949

library/std/src/sys/wasm/condvar_atomics.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,18 @@ impl Condvar {
4444

4545
pub unsafe fn notify_one(&self) {
4646
self.cnt.fetch_add(1, SeqCst);
47-
wasm32::memory_atomic_notify(self.ptr(), 1);
47+
// SAFETY: ptr() is always valid
48+
unsafe {
49+
wasm32::memory_atomic_notify(self.ptr(), 1);
50+
}
4851
}
4952

5053
#[inline]
5154
pub unsafe fn notify_all(&self) {
52-
self.cnt.fetch_add(1, SeqCst);
53-
wasm32::memory_atomic_notify(self.ptr(), u32::MAX); // -1 == "wake everyone"
55+
unsafe {
56+
self.cnt.fetch_add(1, SeqCst);
57+
wasm32::memory_atomic_notify(self.ptr(), u32::MAX); // -1 == "wake everyone"
58+
}
5459
}
5560

5661
pub unsafe fn wait(&self, mutex: &Mutex) {

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

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
//! compiling for wasm. That way it's a compile time error for something that's
1515
//! guaranteed to be a runtime error!
1616
17+
#![deny(unsafe_op_in_unsafe_fn)]
18+
1719
pub mod alloc;
1820
pub mod args;
1921
#[path = "../unsupported/cmath.rs"]

library/std/src/sys/wasm/mutex_atomics.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ impl Mutex {
2828

2929
pub unsafe fn lock(&self) {
3030
while !self.try_lock() {
31-
let val = wasm32::memory_atomic_wait32(
32-
self.ptr(),
33-
1, // we expect our mutex is locked
34-
-1, // wait infinitely
35-
);
31+
// SAFETY: the caller must uphold the safety contract for `memory_atomic_wait32`.
32+
let val = unsafe {
33+
wasm32::memory_atomic_wait32(
34+
self.ptr(),
35+
1, // we expect our mutex is locked
36+
-1, // wait infinitely
37+
)
38+
};
3639
// we should have either woke up (0) or got a not-equal due to a
3740
// race (1). We should never time out (2)
3841
debug_assert!(val == 0 || val == 1);
@@ -47,7 +50,7 @@ impl Mutex {
4750

4851
#[inline]
4952
pub unsafe fn try_lock(&self) -> bool {
50-
self.locked.compare_exchange(0, 1, SeqCst, SeqCst).is_ok()
53+
unsafe { self.locked.compare_exchange(0, 1, SeqCst, SeqCst).is_ok() }
5154
}
5255

5356
#[inline]
@@ -83,7 +86,7 @@ unsafe impl Sync for ReentrantMutex {}
8386

8487
impl ReentrantMutex {
8588
pub const unsafe fn uninitialized() -> ReentrantMutex {
86-
ReentrantMutex { owner: AtomicU32::new(0), recursions: UnsafeCell::new(0) }
89+
unsafe { ReentrantMutex { owner: AtomicU32::new(0), recursions: UnsafeCell::new(0) } }
8790
}
8891

8992
pub unsafe fn init(&self) {
@@ -93,19 +96,20 @@ impl ReentrantMutex {
9396
pub unsafe fn lock(&self) {
9497
let me = thread::my_id();
9598
while let Err(owner) = self._try_lock(me) {
96-
let val = wasm32::memory_atomic_wait32(self.ptr(), owner as i32, -1);
99+
// SAFETY: the caller must gurantee that `self.ptr()` and `owner` are valid i32.
100+
let val = unsafe { wasm32::memory_atomic_wait32(self.ptr(), owner as i32, -1) };
97101
debug_assert!(val == 0 || val == 1);
98102
}
99103
}
100104

101105
#[inline]
102106
pub unsafe fn try_lock(&self) -> bool {
103-
self._try_lock(thread::my_id()).is_ok()
107+
unsafe { self._try_lock(thread::my_id()).is_ok() }
104108
}
105109

106110
#[inline]
107111
unsafe fn _try_lock(&self, id: u32) -> Result<(), u32> {
108-
let id = id.checked_add(1).unwrap(); // make sure `id` isn't 0
112+
let id = id.checked_add(1).unwrap();
109113
match self.owner.compare_exchange(0, id, SeqCst, SeqCst) {
110114
// we transitioned from unlocked to locked
111115
Ok(_) => {
@@ -132,7 +136,10 @@ impl ReentrantMutex {
132136
match *self.recursions.get() {
133137
0 => {
134138
self.owner.swap(0, SeqCst);
135-
wasm32::memory_atomic_notify(self.ptr() as *mut i32, 1); // wake up one waiter, if any
139+
// SAFETY: the caller must gurantee that `self.ptr()` is valid i32.
140+
unsafe {
141+
wasm32::atomic_notify(self.ptr() as *mut i32, 1);
142+
} // wake up one waiter, if any
136143
}
137144
ref mut n => *n -= 1,
138145
}

0 commit comments

Comments
 (0)