Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

ABA-problem with pointer provenance in lockless queues #121950

Open

Description

@joboet

Both lockless queue implementations inside std for Once and RwLock suffer from the ABA problem, resulting in unsoundness.

In both cases, the enqueue operation roughly looks like the following:

struct Node {
    next: *mut Node,
}

struct Queue {
    state: AtomicPtr<Node>,
}

let mut state = queue.state.load();
loop {
    node.next = to_node(state); // Some pointer masking
    let next = to_state(node); // Some bittagging
    match state.compare_exchange(state, next) {
        Ok(_) => break,
        Err(new) => state = next,
    }
}

This code is problematic, because it allows the following to occur:

  1. Thread 1: Append node 1 to the queue.
  2. Thread 2: Load the state (a pointer to node 1) and store it as the next pointer of node 2.
  3. Thread 1: Remove node 1 from the queue and deallocate it/modify it.
  4. Thread 1: Create node 3 at the same address as node 1 and append it to the queue.
  5. Thread 2: Perform the CAS. Because the state has the same bits, this succeeds.

Now, any traversal operation on the queue that attempts to dereference the next pointer of node 2 exhibits UB, as the pointer, while having the right address, only has provenance to node 1, which is no longer valid, but may not access node 3.

This is of little practical concern, as it is extremely unlikely that LLVM will perform a problematic optimization; but nevertheless the code is unsound, both under stacked-borrows and LLVM's semantics.

This is the cause of #121626, where this exact situation led to a (correct) UB report from miri.

@rustbot label +T-libs +I-unsound

Possible solutions

The only solution I see at the moment is to use fuzzy provenance and use from_exposed_provenance inside the traversal operation to make miri/LLVM guess the right provenance.

Activity

  1. added
    needs-triage
    I-unsound
    T-libs
    I-prioritize
    on Mar 3, 2024
  2. the8472 commented on Mar 3, 2024

    @the8472
    Member

    Since this is mac-specific why not use a DWCAS? Afaik they don't have any hardware that doesn't have those.

  3. joboet commented on Mar 3, 2024

    @joboet
    MemberAuthor

    It's not mac-specific, though. Once gets used on all platforms except the futex ones, I believe even Windows. RwLock gets used on NetBSD and other UNIXes as well, and I'd like to use it to get rid of the unsound SGX lock implementations. So we need to find something portable here.

    Also, this is a provenance-only problem, so I'd like to avoid anything that makes the machine code less efficient, because in terms of what is actually generated, the current implementation is perfectly bug-free. It "just" violates the memory model.

  4. the8472 commented on Mar 3, 2024

    @the8472
    Member

    And using the return value from the CAS (the Ok(_)) and working on that instead of state doesn't work either?

  5. joboet commented on Mar 3, 2024

    @joboet
    MemberAuthor

    No, because the old pointer is stored inside the node, and we can't retroactively update the pointer, because a traversal operation could access the old one immediately after the compare_exchange.

  6. saethlin commented on Mar 3, 2024

    @saethlin
    Member

    to make miri/LLVM guess the right provenance.

    I don't think LLVM implements expose semantics in any sense, so I suspect doing the expose dance here wouldn't change LLVM's view of the code. (Happy to be corrected on this)

  7. removed
    needs-triage
    on Mar 3, 2024
  8. joboet commented on Mar 3, 2024

    @joboet
    MemberAuthor

    LLVM's pointer aliasing rules mention that the pointer resulting from inttoptr is based on "all pointer values that contribute (directly or indirectly) to the computation of the pointer’s value". IIUC, that means that because the traversing thread reads the current pointer from state and uses it to load the integer that is then passed to inttoptr, the resulting pointer will also be based on the current pointer, making the code sound.

  9. added
    A-atomic
    on Mar 3, 2024
  10. RalfJung commented on Mar 3, 2024

    @RalfJung
    Member

    Also, this is a provenance-only problem, so I'd like to avoid anything that makes the machine code less efficient, because in terms of what is actually generated, the current implementation is perfectly bug-free. It "just" violates the memory model.

    That is assuming that we don't have optimizations that make full use of the provenance model. Or have you inspected the generated assembly to be sure?

    This looks extremely related to rust-lang/unsafe-code-guidelines#480 to me. I wonder if the CAS semantics I proposed here would help.

  11. joboet commented on Mar 3, 2024

    @joboet
    MemberAuthor

    I inspected the assembly of a simplified version (I removed the Thread handle and park operations). Also, the Once implementation has existed for ~9 years, with no issue encountered.

  12. saethlin commented on Mar 3, 2024

    @saethlin
    Member

    The age of an implementation is not an indicator of its soundness with respect to provenance rules. If anything, perhaps the correlation runs the other direction.

    I'm willing to review codegen tests for suspicious things like this, if someone can find a way to write them that's both effective and doesn't make them break often. I don't think we usually check in codegen tests for structures as complex as this.

  13. 2 remaining items

  14. removed
    I-prioritize
    on Mar 5, 2024
  15. RalfJung commented on Mar 5, 2024

    @RalfJung
    Member

    The only solution I see at the moment is to use fuzzy provenance and use from_exposed_provenance inside the traversal operation to make miri/LLVM guess the right provenance.

    Just recording here that another possible solution has been sketched, also spelled out here.

  16. added 2 commits that reference this issue on Aug 8, 2024
  17. added a commit that references this issue on Aug 9, 2024
  18. added a commit that references this issue on Nov 19, 2024
  19. added a commit that references this issue on Nov 19, 2024
  20. added a commit that references this issue on Nov 25, 2024
  21. added a commit that references this issue on Nov 26, 2024
  22. added a commit that references this issue on Nov 26, 2024
  23. added a commit that references this issue on Nov 27, 2024
  24. added a commit that references this issue on Mar 11, 2025
  25. RalfJung commented on Jun 3, 2025

    @RalfJung
    Member

    The offending compare_exchange here is this, right?

    if let Err(new) = self.state.compare_exchange_weak(state, next, AcqRel, Relaxed) {

    And the place where the provenance needs to be atomically updated is node.next? After all, if the insert succeeds, our new node's next field must be exactly the previous head of the queue, I think.

  26. joboet commented on Jun 3, 2025

    @joboet
    MemberAuthor

    The offending compare_exchange here is this, right?

    if let Err(new) = self.state.compare_exchange_weak(state, next, AcqRel, Relaxed) {

    And the place where the provenance needs to be atomically updated is node.next? After all, if the insert succeeds, our new node's next field must be exactly the previous head of the queue, I think.

    Yes, exactly.

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-atomicI-unsoundP-mediumT-libs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions