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

Tracking allocator: mark Spinlock::unlock() as unsafe and provide a safety contract #2156

Merged
merged 2 commits into from
Nov 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions polkadot/node/tracking-allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ impl<T> Spinlock<T> {
}
}

// SAFETY: It should be only called from the guard's destructor. Calling it explicitly while
// the guard is alive is undefined behavior, as it breaks the security contract of `Deref` and
// `DerefMut`, which implies that lock is held at the moment of dereferencing.
#[inline]
fn unlock(&self) {
unsafe fn unlock(&self) {
self.lock.store(false, Ordering::Release);
}
}
Expand All @@ -97,7 +100,9 @@ impl<T> DerefMut for SpinlockGuard<'_, T> {

impl<T> Drop for SpinlockGuard<'_, T> {
fn drop(&mut self) {
self.lock.unlock();
// SAFETY: Calling `unlock` is only safe when it's guaranteed no guard outlives the
// unlocking point; here, the guard is dropped, so it is safe.
unsafe { self.lock.unlock() }
}
}

Expand Down
Loading