-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
#[deny(unsafe_op_in_unsafe_fn)]
in sys/wasm
#74477
Merged
bors
merged 5 commits into
rust-lang:master
from
chansuke:sys-wasm-unsafe-op-in-unsafe-fn
Oct 26, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d413bb6
`#[deny(unsafe_op_in_unsafe_fn)]` in sys/wasm
eed4510
Add some description for (malloc/calloc/free/realloc)
de87ae7
Add documents for DLMALLOC
d147f78
Fix unsafe operation of wasm32::memory_atomic_notify
d37b8cf
Remove unnecessary unsafe block from condvar_atomics & mutex_atomics
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,11 +28,14 @@ impl Mutex { | |
|
||
pub unsafe fn lock(&self) { | ||
while !self.try_lock() { | ||
let val = wasm32::memory_atomic_wait32( | ||
self.ptr(), | ||
1, // we expect our mutex is locked | ||
-1, // wait infinitely | ||
); | ||
// SAFETY: the caller must uphold the safety contract for `memory_atomic_wait32`. | ||
Mark-Simulacrum marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let val = unsafe { | ||
wasm32::memory_atomic_wait32( | ||
self.ptr(), | ||
1, // we expect our mutex is locked | ||
-1, // wait infinitely | ||
) | ||
}; | ||
// we should have either woke up (0) or got a not-equal due to a | ||
// race (1). We should never time out (2) | ||
debug_assert!(val == 0 || val == 1); | ||
|
@@ -93,19 +96,20 @@ impl ReentrantMutex { | |
pub unsafe fn lock(&self) { | ||
let me = thread::my_id(); | ||
while let Err(owner) = self._try_lock(me) { | ||
let val = wasm32::memory_atomic_wait32(self.ptr(), owner as i32, -1); | ||
// SAFETY: the caller must gurantee that `self.ptr()` and `owner` are valid i32. | ||
let val = unsafe { wasm32::memory_atomic_wait32(self.ptr(), owner as i32, -1) }; | ||
debug_assert!(val == 0 || val == 1); | ||
} | ||
} | ||
|
||
#[inline] | ||
pub unsafe fn try_lock(&self) -> bool { | ||
self._try_lock(thread::my_id()).is_ok() | ||
unsafe { self._try_lock(thread::my_id()).is_ok() } | ||
} | ||
|
||
#[inline] | ||
unsafe fn _try_lock(&self, id: u32) -> Result<(), u32> { | ||
let id = id.checked_add(1).unwrap(); // make sure `id` isn't 0 | ||
let id = id.checked_add(1).unwrap(); | ||
match self.owner.compare_exchange(0, id, SeqCst, SeqCst) { | ||
// we transitioned from unlocked to locked | ||
Ok(_) => { | ||
|
@@ -132,7 +136,10 @@ impl ReentrantMutex { | |
match *self.recursions.get() { | ||
0 => { | ||
self.owner.swap(0, SeqCst); | ||
wasm32::memory_atomic_notify(self.ptr() as *mut i32, 1); // wake up one waiter, if any | ||
// SAFETY: the caller must gurantee that `self.ptr()` is valid i32. | ||
unsafe { | ||
wasm32::atomic_notify(self.ptr() as *mut i32, 1); | ||
} // wake up one waiter, if any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you put this comment above the unsafe block instead? |
||
} | ||
ref mut n => *n -= 1, | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are two preconditions that we need to document for these calls:
The comments so far only sort of talk about the first, but should address both. I think basically copy/pasting my two bullet points makes sense.