Skip to content

Fix race condition in Arc's get_mut and make_unqiue - #26610

Merged
bors merged 1 commit into
rust-lang:masterfrom
aturon:fix_make_unique
Jul 3, 2015
Merged

bors merged 1 commit into
rust-lang:masterfrom
aturon:fix_make_unique

Conversation

@aturon

@aturon aturon commented Jun 26, 2015

Copy link
Copy Markdown
Contributor

This commit resolves the race condition in the get_mut and
make_unique functions, which arose through interaction with weak
pointers. The basic strategy is to "lock" the weak pointer count when
trying to establish uniqueness, by reusing the field as a simple
spinlock. The overhead for normal use of Arc is expected to be minimal
-- it will be none when only strong pointers are used, and only
requires a move from atomic increment to CAS for usage of weak pointers.

The commit also removes the unsafe and deprecated status of these functions.

Closes #24880

r? @alexcrichton

cc @metajack @SimonSapin @Ms2ger

@alexcrichton

Copy link
Copy Markdown
Member

Could this also move the methods to Arc::{get_mut, make_unique}? Other than that I'd like to get another pair of eyes or two to check this out, but I believe that this is safe.

Comment thread src/liballoc/arc.rs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We should only clone if there are other live strong references; the correct implementation is something like the following (untested, possibly buggy):

if self.inner().strong.compare_and_swap(1, 0, Release) != 1 {
    // Another strong pointer exists; clone.
    *self = Arc::new((**self).clone());
} else if self.inner().weak.load(SeqCst) != 1 {
    // There are no strong pointers left, but there are remaining weak
    // pointers; steal the contents.
    // FIXME: Is there a better way to write this?
    let weak = Weak { _ptr: self._ptr };
    self._ptr = 0;
    let stolen_data = ptr::read(&(*weak._ptr).data);
    *self = Arc::new(stolen_data);
} else {
    // No other strong or weak pointers; just revive this Arc.
    self.inner().strong = 1;
}

No spinlock necessary.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This would not work for get_mut though, so the spinlock would still need to exist, just not for make_unique?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes. Granted, we could change the semantics of get_mut so it kills weak pointers rather than failing, which would allow removing the spinlock, but that's probably not a decision we should make based on implementation details.

@talchas

talchas commented Jun 27, 2015

Copy link
Copy Markdown

Clone/Drop for Weak and Arc::weak_count also need to be CAS loops. I would also suggest using weak.CAS(acquire); strong.load(Relaxed); weak.store(Release);, as I believe that is sufficient and will remove an unnecessary fence on x86 (and maybe pull it out into a private is_unique() to reduce code duplication?)

@eefriedman

Copy link
Copy Markdown
Contributor

An argument could be made that get_mut() on an Arc with no other strong references should kill all weak pointers and memcpy the data if necessary instead of failing. Not sure how get_mut() is used in practice. Either way, it needs documentation which explicitly states what happens in the presence of weak pointers.

IIRC, we need to move from atomic increment to CAS anyway to avoid unsafety from overflowing the counter, so any overhead involved in that is irrelevant.

Comment thread src/liballoc/arc.rs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Since references take up memory, I don't think usize can be overflowed by them... except we now have safe mem::forget which makes it trivial. RefCell is also affected.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's a loong discussion about it (internals)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually, scratch that, RefCell overflows into a "deadlock" state - mutably locked by too many immutable borrows, which denies any further borrows.

@aturon
aturon force-pushed the fix_make_unique branch 2 times, most recently from 0d56386 to d264b0e Compare June 30, 2015 17:43
@aturon

aturon commented Jun 30, 2015

Copy link
Copy Markdown
Contributor Author

Thanks everyone for the feedback, especially @eefriedman for the insight about the semantics of make_unique with respect to weak refs. I've updated the impl and am testing now.

Comment thread src/liballoc/arc.rs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why not &mut self?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

That could interfere with any get_mut() on T. (The doc comment still thinks it's a free function, though.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don’t see how that’s a problem given we have UFCS.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It follows the new convention for smart pointer methods. I guess it can be written as this: &mut Self though.

@aturon
aturon force-pushed the fix_make_unique branch 4 times, most recently from 9726a81 to d733e2b Compare June 30, 2015 23:28
@aturon

aturon commented Jun 30, 2015

Copy link
Copy Markdown
Contributor Author

Additional nits addressed. I switched to a swap + mem::forget to avoid manual zeroing.

Comment thread src/liballoc/arc.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can you add some comments here to the effect of the memory orderings? It's not immediately clear to me why this is Release and the one below is SeqCst. Also, can't the one at the bottom be relaxed b/c this is the only pointer in existence? (e.g. single threaded)

@alexcrichton

Copy link
Copy Markdown
Member

A few minor nits here and there, but otherwise looks good to me. Tricky stuff!

r=me

@aturon
aturon force-pushed the fix_make_unique branch 2 times, most recently from 8566f31 to 71b496c Compare July 2, 2015 00:45
@cuzbog

cuzbog commented Jul 2, 2015

Copy link
Copy Markdown

Any news on this? It would be awesome to get rid of those Servo warnings :)

@aturon
aturon force-pushed the fix_make_unique branch from 71b496c to 1c8e43c Compare July 2, 2015 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

arc::get_mut/Arc::make_unique are racy

10 participants