Skip to content

arc::get_mut/Arc::make_unique are racy #24880

Closed

Description

@talchas

Both of these essentially check that inner().strong == 1 && inner().weak == 1 to check that &mut self is the only reference to the Arc's data. However in between the checks a Weak pointer could be promoted and the Weak pointer dropped, resulting in both checks succeeding. Adding an additional strong check afterwards won't help as the second strong pointer could then be demoted again.

I don't see an obvious way without a two-word atomic read or a generation counter or something of that sort. On 64-bit splitting an atomic64 into two 32-bit sections /might/ be ok, but on 32-bit only allowing 16 bits of refcount seems bad. (There might be something with using a high bit as a lock bit or such?)

Activity

  1. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    I think you're right.

    I'm tempted to say this could be fixed by switching from a strong/weak pair to a num_ptrs/num_weak pair, where every pointer (whether strong or weak) is counted in num_ptrs and then weak pointers are also counted in num_weak. This does lower the number of total pointers (strong + weak) that we can have outstanding, but it's not as restrictive as actually dividing a usize into two halves.

    Another alternative might be to use the high bit of the strong pointer as a "there are weak references" marker. I'm not sure how well that would actually work.

    And of course we could always just use a spinlock instead.

  2. added
    A-concurrency
    on Apr 28, 2015
  3. sfackler commented on Apr 28, 2015

    @sfackler
    Member

    Using num_ptrs + num_weak shouldn't actually lower the total number of pointers since each Arc or Weak is larger than 1 byte so you'd exhaust the address space before overflowing num_ptrs.

  4. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    Upon reflection I suspect that num_ptrs + num_weak fails for the same reason that get_mut fails, because the destructor of Arc needs to know how many strong references there are, and the only way to get num_strong is with two loads.

  5. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    Although it's possible that using num_ptrs + num_strong instead might possibly work.

  6. talchas commented on Apr 28, 2015

    @talchas
    Author

    If that works (which sounds plausible) it has the disadvantage of requiring two atomics on clone() (though they're probably the same cacheline, so it's not as horrible as it might be).

  7. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    I still haven't thought it entirely all the way through, but I think that num_ptrs + num_strong will work (as long as num_strong is always modified before num_ptrs). As you say, this will require two atomics on clone().

    The other alternative that I think would work is using the low bit of the strong counter as a "has weak" flag that, when set, is never un-set. Once it's set, a spinlock is then used around all relevant accesses to the strong and weak counters. This is more complicated than num_ptrs + num_strong, but has the benefit of meaning that Arcs that don't use Weak won't have to pay any cost (and in fact they'll be slightly cheaper than today, because no atomic load of weak is necessary if the flag is 0).

    Incidentally, it appears we do not have a spinlock construct in our stdlib. That's rather unfortunate, as ad-hoc simple spinlocks are not generally ideal. Preferably we'd have a construct that spins some number of times (it looks like pthread on OS X defines that number as 1000) on multi-processor machines before yielding the thread, and yields immediately on single-processor machines.

  8. bluss commented on Apr 28, 2015

    @bluss
    Contributor

    Split the Arc struct in two: Give Arc another marker type parameter that decides statically if the type should support weak pointers or not. Use default type parameters to make Arc<T> equivalent Arc<T, NoWeak>

    Only the NoWeak Arc will permit get_mut and make_unique. Since the Arc source references boost a lot, I tried to find any indication of how this is solved in the boost sphere, and I can't find any solution when weak pointers are involved.

    Default type parameters make this seamless for users that don't need weak pointers.

    Note: boost shared_ptr has a member function bool unique() but it only considers stong references.

  9. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    @bluss That seems like a very heavy-handed approach. The creator of the Arc instance may not know ahead of time whether any code is going to want to create a Weak instance. And at such time as that decision is made, all code that touches the Arc would have to be updated even though that code doesn't really care whether there's a Weak reference.

    As for Boost, I glanced at it yesterday and it didn't appear to me that it supported weak pointers.

  10. bluss commented on Apr 28, 2015

    @bluss
    Contributor

    It is heavy-handed, but it is also flexible, and allows users to only pay for what they use. Didn't we already want to make Weak pointers optional? (What other reason are they unstable for?)

    See boost Smart Pointers doc, it has weak_ptr

  11. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    @bluss I'm not really sure why they're unstable, the message just says "Weak pointers may not belong in this module".

    And you're right, Boost does have weak_ptr, the link I followed before (from the comments of Arc) didn't show it. Looking through Boost's weak_ptr and shared_ptr it appears that shared_ptr::unique() ignores weak pointers entirely and returns true even if there are existing weak pointers (it appears that shared_ptr works the same way our Arc does, with a shared counter (i.e. our strong) and a weak counter (that includes an extra +1 count as long as shared is non-zero).

    So the difference here is our Arc's unique-testing needs to care about weak pointers (in order to meet the memory safety guarantees of Rust) but Boost doesn't enforce that.

  12. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    I think what we should do today is remove Arc::make_unique and alloc::arc::get_mut. They're both marked as #[unstable] so this is acceptable. Without those two functions, our existing reference-counting implementation works fine. Then we can explore ways to re-add that functionality in the future. It appears that there are zero clients of Arc::make_unique in rust-lang/rust, although checking for get_mut is not so trivial as that name is not unique.

  13. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    @alexcrichton You added Arc::make_unique in the first place, and @aturon you updated it to check weak refs. Any thoughts?

  14. kvark commented on Apr 28, 2015

    @kvark
    Contributor

    @kballard I added arc::get_mut() in order to use it for parallel ECS proof of concept. I don't know if any real projects use it at the moment, so it should be safe to kill, but I'm going to miss it anyways.

  15. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    @kvark I would really like to have Arc::get_mut(), I think it's potentially useful, but as long as there's no clients in rust-lang/rust that depend on it right now, I think we should just kill it for 1.0 and investigate ways to safely re-add it in the future.

  16. bluss commented on Apr 28, 2015

    @bluss
    Contributor

    Since weak_count and strong_count are public, get_mut can be implemented as it is today, outside of the crate too. At least I think you can, you would be effectively converting &T to &mut T, but isn't that legal if you go through raw pointers? Of course you'll only use this unsafe-marked get_mut if you know no Weak pointers are used in your application.

    Removing unsound unstable API sounds fine.

  17. lilyball commented on Apr 28, 2015

    @lilyball
    Contributor

    @bluss True, you could reimplement the current logic of get_mut, but since that logic is broken I don't think it's something we should be telling people to do.

    At least I think you can, you would be effectively converting &T to &mut T, but isn't that legal if you go through raw pointers?.

    Well no, that doesn't make it legal. If you construct a &mut T, you need to guarantee it's not aliased by anything else. This means you need to guarantee that there are no &T references to the same value (and that you can't construct one). That said, I'm not 100% positive about the actual hard requirements here; I think it's just the aliasing requirement, which means if you can guarantee that there are no (accessible) &T references extant, then your constructed &mut T is valid.

    Based on that, I believe that it would be legal to construct this third-party fn arc_get_mut<T>(ptr: &mut Arc<T>) -> Option<&mut T> even though the implementation would have to transmute a &T into a &mut T. The implementation of the function can ensure that it doesn't construct any other &T reference after creating the &mut T (or more generally, that it doesn't touch the ptr again), and the API of the function means that the resulting &mut T borrows the Arc.


    All that said, given that the current scheme of separate atomic strong and weak counts does not allow you to do this unique-test safely, none of this really matters. get_mut() can't be implemented safely without changing the implementation details of Arc.

  18. huonw commented on Apr 28, 2015

    @huonw
    Contributor
  19. bluss commented on Apr 29, 2015

    @bluss
    Contributor

    since that logic is broken I don't think it's something we should be telling people to do.

    It's a systems language, we don't tell them what to do. We tell them what's possible and where the boundaries lie. We give them unsafe blocks for when they feel they can take over the reins themselves.

    Well no, that doesn't make it legal. If you construct a &mut T, you need to guarantee it's not aliased by anything else.

    Obviously. I didn't say it was a sufficient criterium. Anyway, let's not do this kind of thing where we out-nitpick each other.. All I suggested was if wouldn't it be possible to implement outside of crate.

  20. bluss commented on May 10, 2015

    @bluss
    Contributor

    The racy functions still exist. We should document that or remove them.

  21. added a commit that references this issue on May 31, 2015
  22. aturon commented on Jun 26, 2015

    @aturon
    Contributor

    For reference, these functions, together with weak pointers, are used heavily in Servo. I've made a PR to fix this problem more directly: #26610

  23. added a commit that references this issue on Jul 3, 2015
    4a21775
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-concurrency

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions