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

#[deny(unsafe_op_in_unsafe_fn)] in sys/wasm #74477

Merged
merged 5 commits into from
Oct 26, 2020

Conversation

chansuke
Copy link
Contributor

This is part of #73904.

This encloses unsafe operations in unsafe fn in libstd/sys/wasm.

@rustbot modify labels: F-unsafe-block-in-unsafe-fn

@rust-highfive
Copy link
Collaborator

r? @joshtriplett

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 18, 2020
// SAFETY: The caller must gurantee that the argument of `fetch_add()` is valid
// to describe memory ordering.
unsafe {
self.cnt.fetch_add(1, SeqCst);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an unsafe operation.

self.cnt.fetch_add(1, SeqCst);
}
unsafe {
wasm32::atomic_notify(self.ptr(), 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My guess is that the unsafety here is basically just from the raw pointer, so we can say something like "ptr() is always valid"

@@ -60,7 +72,7 @@ impl Condvar {
// thread. Incrementing the counter after we unlock the mutex will
// prevent us from sleeping and otherwise the call to `wake` will
// wake us up once we're asleep.
let ticket = self.cnt.load(SeqCst) as i32;
let ticket = unsafe { self.cnt.load(SeqCst) as i32 };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also not an unsafe operation.

1, // we expect our mutex is locked
-1, // wait infinitely
);
// SAFETY: the caller must uphold the safety contract for `i32_atomic_wait`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems... odd. @alexcrichton, can you recall why lock() here is unsafe? It seems like it could be safe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intrinsic has a *mut i32 in the signature which it modifies, so the intrinsic is unsafe.

// 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);
}
}

pub unsafe fn unlock(&self) {
let prev = self.locked.swap(0, SeqCst);
// SAFETY: the caller must uphold the safety contract for `swap`.
let prev = unsafe { self.locked.swap(0, SeqCst) };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Atomic operations are safe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the review

@bors
Copy link
Contributor

bors commented Jul 21, 2020

☔ The latest upstream changes (presumably #74569) made this pull request unmergeable. Please resolve the merge conflicts.

@chansuke chansuke force-pushed the sys-wasm-unsafe-op-in-unsafe-fn branch from 0054eb3 to 90d4625 Compare July 22, 2020 12:03
@bors
Copy link
Contributor

bors commented Jul 28, 2020

☔ The latest upstream changes (presumably #73265) made this pull request unmergeable. Please resolve the merge conflicts.

@chansuke chansuke force-pushed the sys-wasm-unsafe-op-in-unsafe-fn branch 2 times, most recently from 440f06c to c0accca Compare August 3, 2020 14:51
@JohnTitor
Copy link
Member

This hasn't got any response for a while, r? @Mark-Simulacrum as you left some reviews.

// to describe memory ordering.
unsafe {
self.cnt.fetch_add(1, SeqCst);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not an unsafe operation (please also fix the other atomics).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 13, 2020
@chansuke chansuke force-pushed the sys-wasm-unsafe-op-in-unsafe-fn branch from c0accca to fa379b4 Compare August 14, 2020 13:23
@crlf0710
Copy link
Member

crlf0710 commented Sep 4, 2020

@chansuke Ping from triage! What's the current status on this? i assume this is ready for review again?

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 4, 2020
);
// SAFETY: the caller must uphold the safety contract for `i32_atomic_wait`.
let val = unsafe {
wasm32::i32_atomic_wait(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a rebase failure -- this function seems to have been renamed to memory_atomic_wait32 on master. (I think there's one or two more cases where you have that).

}

#[inline]
unsafe fn _try_lock(&self, id: u32) -> Result<(), u32> {
let id = id.checked_add(1).unwrap(); // make sure `id` isn't 0
// SAFETY: the caller must gurantee that `id` isn't 0
let id = unsafe { id.checked_add(1).unwrap() };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also not an unsafe operation.

self.owner.swap(0, SeqCst);
wasm32::memory_atomic_notify(self.ptr() as *mut i32, 1); // wake up one waiter, if any
unsafe {
self.owner.swap(0, SeqCst);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an unsafe operation.

self.cnt.fetch_add(1, SeqCst);
wasm32::memory_atomic_notify(self.ptr(), u32::MAX); // -1 == "wake everyone"
unsafe {
wasm32::atomic_notify(self.ptr(), u32::MAX); // -1 == "wake everyone"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This also lost the right name.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I fixed...

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 9, 2020
@chansuke chansuke force-pushed the sys-wasm-unsafe-op-in-unsafe-fn branch from fa379b4 to 353727b Compare September 9, 2020 13:23
@chansuke
Copy link
Contributor Author

chansuke commented Sep 9, 2020

@crlf0710 Sorry for the late reply. I fixed and now it's ready for the review.

@crlf0710 crlf0710 added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 10, 2020
wasm32::memory_atomic_notify(self.ptr(), 1);
// SAFETY: ptr() is always valid
unsafe {
wasm32::atomic_notify(self.ptr(), 1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is still the wrong name, it should be memory_atomic_notify.

library/std/src/sys/wasm/condvar_atomics.rs Show resolved Hide resolved
wasm32::memory_atomic_notify(self.ptr() as *mut i32, 1); // wake up one waiter, if any
unsafe {
wasm32::atomic_notify(self.ptr() as *mut i32, 1);
} // wake up one waiter, if any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put this comment above the unsafe block instead?

@Mark-Simulacrum Mark-Simulacrum removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 10, 2020
@Mark-Simulacrum Mark-Simulacrum added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 19, 2020
@Mark-Simulacrum
Copy link
Member

I think I likely approved the SGX changes a bit earlier than was warranted (sgx is also less critical in some sense as it's not to my knowledge something readily used by consumers, though I could be wrong).

lock::lock() is defined in the alloc.rs file and is just taking a global lock if std is compiled with atomics enabled.

We need the locks with multithreading because the allocator is called with &self but the malloc/calloc/free/realloc on dlmalloc are defined to take &mut self, and that can only be safe if we ensure safety somehow (in this case with a lock).

@chansuke chansuke force-pushed the sys-wasm-unsafe-op-in-unsafe-fn branch from 4a17d19 to 2ff5965 Compare October 20, 2020 23:28
let _lock = lock::lock();
DLMALLOC.malloc(layout.size(), layout.align())
unsafe { DLMALLOC.malloc(layout.size(), layout.align()) }
Copy link
Member

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:

  • DLMALLOC access is safe: this is true because the lock gives us unique and non-reentrant access.
  • Calling the function itself is safe: Preconditions on these functions match the trait method preconditions, so this is safe.

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.

self.cnt.fetch_add(1, SeqCst);
wasm32::memory_atomic_notify(self.ptr(), u32::MAX); // -1 == "wake everyone"
unsafe {
self.cnt.fetch_add(1, SeqCst);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an unsafe operation and should not be inside the unsafe block. It also looks like are missing a safety comment about the first argument to memory_atomic_notify being a valid pointer.

library/std/src/sys/wasm/mutex_atomics.rs Show resolved Hide resolved
@@ -47,7 +50,7 @@ impl Mutex {

#[inline]
pub unsafe fn try_lock(&self) -> bool {
self.locked.compare_exchange(0, 1, SeqCst, SeqCst).is_ok()
unsafe { self.locked.compare_exchange(0, 1, SeqCst, SeqCst).is_ok() }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an unsafe operation.

@@ -83,7 +86,7 @@ unsafe impl Sync for ReentrantMutex {}

impl ReentrantMutex {
pub const unsafe fn uninitialized() -> ReentrantMutex {
ReentrantMutex { owner: AtomicU32::new(0), recursions: UnsafeCell::new(0) }
unsafe { ReentrantMutex { owner: AtomicU32::new(0), recursions: UnsafeCell::new(0) } }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe there are any unsafe operations here.

@Mark-Simulacrum Mark-Simulacrum added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 22, 2020
@chansuke chansuke force-pushed the sys-wasm-unsafe-op-in-unsafe-fn branch from 2ff5965 to d37b8cf Compare October 24, 2020 09:39
@chansuke
Copy link
Contributor Author

@rustbot modify labels: +S-waiting-on-review -S-waiting-on-author

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 24, 2020
@Mark-Simulacrum
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Oct 25, 2020

📌 Commit d37b8cf has been approved by Mark-Simulacrum

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 25, 2020
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Oct 26, 2020
…fe-fn, r=Mark-Simulacrum

`#[deny(unsafe_op_in_unsafe_fn)]` in sys/wasm

This is part of rust-lang#73904.

This encloses unsafe operations in unsafe fn in `libstd/sys/wasm`.

@rustbot modify labels: F-unsafe-block-in-unsafe-fn
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Oct 26, 2020
…fe-fn, r=Mark-Simulacrum

`#[deny(unsafe_op_in_unsafe_fn)]` in sys/wasm

This is part of rust-lang#73904.

This encloses unsafe operations in unsafe fn in `libstd/sys/wasm`.

@rustbot modify labels: F-unsafe-block-in-unsafe-fn
bors added a commit to rust-lang-ci/rust that referenced this pull request Oct 26, 2020
Rollup of 10 pull requests

Successful merges:

 - rust-lang#74477 (`#[deny(unsafe_op_in_unsafe_fn)]` in sys/wasm)
 - rust-lang#77836 (transmute_copy: explain that alignment is handled correctly)
 - rust-lang#78126 (Properly define va_arg and va_list for aarch64-apple-darwin)
 - rust-lang#78137 (Initialize tracing subscriber in compiletest tool)
 - rust-lang#78161 (Add issue template link to IRLO)
 - rust-lang#78214 (Tweak match arm semicolon removal suggestion to account for futures)
 - rust-lang#78247 (Fix rust-lang#78192)
 - rust-lang#78252 (Add codegen test for rust-lang#45964)
 - rust-lang#78268 (Do not try to report on closures to avoid ICE)
 - rust-lang#78295 (Add some regression tests)

Failed merges:

r? `@ghost`
@bors bors merged commit e0c08ae into rust-lang:master Oct 26, 2020
@rustbot rustbot added this to the 1.49.0 milestone Oct 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F-unsafe-block-in-unsafe-fn RFC #2585 S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.