Skip to content

Could there be a use-after-free bug on the atomic shared_ptr implementation? #4

Description

@QuangTung97

I think the implementation here (and possibly many other implementation) has this bug:

For example, assume we have 2 threads:

  • Thread 1 does the get() of SharedAtomicPtr ptr
  • Thread 2 does the store() of ptr.

With following interleaving actions:

  1. Thread 1 increases the local refcount and load the pointer to the control block from ptr
  2. Thread 1 increases the global refcount from the control block. (global refcount now = 2)
  3. Thread 2 comes along and update the 48 bits pointer to the new control block
  4. BUT before thread 2 can increase the global refcount, it's temporary paused by system scheduler.
  5. Thread 1 then tries to decrease the local refcount, finds out that the 48 bits pointer has changed
  6. Thread 1 decrease the global refcount. (global refcount now = 1)
  7. Thread 1 then starts using the object
  8. After finish using the object, Thread 1 then decrease the global refcount. (global refcount now = 0)
  9. Thread 1 does the deletion on the control block.
  10. Finally thread 2 is resumed, and then it tries to increase the global refcount. But the control block has already been freed => use-after-free bug

Did I get something wrong somewhere.
Or the algorithm itself is incorrect?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions