[libcxx-commits] [PATCH] D122864: [libc++] Avoid lifetime UB in __thread_local_data()

Louis Dionne via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 11 09:00:54 PDT 2022


ldionne added a comment.

Thanks for the patch! I'm not sure I understand how we'd catch this though, since D122869 <https://reviews.llvm.org/D122869> doesn't touch any libc++ tests. Is there something we can do to fix our test setup in libc++ so as to catch this issue in our own tests?



================
Comment at: libcxx/src/thread.cpp:118-120
+  alignas(__thread_specific_ptr<__thread_struct>) static char __b[sizeof(__thread_specific_ptr<__thread_struct>)];
+  static __thread_specific_ptr<__thread_struct>* __p = new (__b) __thread_specific_ptr<__thread_struct>();
+  return *__p;
----------------
I think we should add a comment like:

```
// Even though __thread_specific_ptr's destructor doesn't actually destroy anything
// (see comments there), we can't call it at all because threads may outlive the static
// variable and calling its destructor means accessing an object outside of its lifetime,
// which is UB.
```


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D122864/new/

https://reviews.llvm.org/D122864



More information about the libcxx-commits mailing list