[Openmp-commits] [openmp] 35fdf8d - [OpenMP] Fix a segment fault in __kmp_get_global_thread_id

Shilei Tian via Openmp-commits openmp-commits at lists.llvm.org
Thu Aug 31 18:15:33 PDT 2023


Author: Shilei Tian
Date: 2023-08-31T21:15:28-04:00
New Revision: 35fdf8d70329b2b067cb9433a5891f2a57e2f4ed

URL: https://github.com/llvm/llvm-project/commit/35fdf8d70329b2b067cb9433a5891f2a57e2f4ed
DIFF: https://github.com/llvm/llvm-project/commit/35fdf8d70329b2b067cb9433a5891f2a57e2f4ed.diff

LOG: [OpenMP] Fix a segment fault in __kmp_get_global_thread_id

In `__kmp_get_global_thread_id`, if the gtid mode is 1, after getting the gtid
from TLS, it will store the gtid value to the thread stack maintained in the thread
descriptor. However, `__kmp_get_global_thread_id` can be called when the library
is destructed, after the corresponding thread info has been release. This will
cause a segment fault. This can happen on an Intel-based Mac.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D159324

Added: 
    

Modified: 
    openmp/runtime/src/kmp_runtime.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp
index c8a18e81810cb5..a1e36243f750c8 100644
--- a/openmp/runtime/src/kmp_runtime.cpp
+++ b/openmp/runtime/src/kmp_runtime.cpp
@@ -196,6 +196,12 @@ int __kmp_get_global_thread_id() {
   if (i < 0)
     return i;
 
+  // other_threads[i] can be nullptr at this point because the corresponding
+  // thread could have already been destructed. It can happen when this function
+  // is called in end library routine.
+  if (!TCR_SYNC_PTR(other_threads[i]))
+    return i;
+
   /* dynamically updated stack window for uber threads to avoid get_specific
      call */
   if (!TCR_4(other_threads[i]->th.th_info.ds.ds_stackgrow)) {


        


More information about the Openmp-commits mailing list