<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Insufficient locking in std::atomic_compare_exchange_strong for std::shared_ptr"
   href="https://bugs.llvm.org/show_bug.cgi?id=43813">43813</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Insufficient locking in std::atomic_compare_exchange_strong for std::shared_ptr
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>9.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>lahav.sch.llvm@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Hi,

The implementation of 'std::atomic_compare_exchange_strong(shared_ptr* p,
shared_ptr* v, shared_ptr w)' only locks the mutex related to 'p', but not the
one for 'v' as well.

This means the following code (Which should have no race conditions):


#include <memory>
#include <atomic>
#include <unistd.h>
#include <pthread.h>

std::shared_ptr<int> g_p;

static void* thread_run(void*) {
    auto w = std::make_shared<int>();

    while (true) {
        auto p = std::make_shared<int>();

        std::atomic_compare_exchange_strong(&p, &g_p, w);
    }

    return nullptr;
}

int main() {
    pthread_t tid;
    pthread_create(&tid, nullptr, thread_run, nullptr);

    auto w = std::make_shared<int>();

    while(true) {        
        std::atomic_store(&g_p, w);
    }
}


may attempt to access the same 'g_p' instance from multiple threads - The main
thread (Via std::atomic_store()) will properly lock the matching mutex, but the
2nd thread will not, causing a race condition.

I believe the mutex for 'v' should be locked as well, with special care as to
not introduce a deadlock now that 2 mutexes are being locked.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>