[Openmp-commits] [openmp] [OpenMP] avoid segv for a lock that has already been destroyed (PR #145625)

Larry Meadows via Openmp-commits openmp-commits at lists.llvm.org
Wed Jun 25 10:23:00 PDT 2025


https://github.com/lfmeadow updated https://github.com/llvm/llvm-project/pull/145625

>From a6f3c1b25dbd0e876bf92b07a9b1ed47736b768a Mon Sep 17 00:00:00 2001
From: Larry Meadows <Lawrence.Meadows at amd.com>
Date: Tue, 24 Jun 2025 20:34:19 -0500
Subject: [PATCH 1/2]   [OpenMP] avoid segv for a lock that has already been
 destroyed

  This can happen in static destructors when called after the
  runtime is already shutdown (e.g., by ompt_finalize_tool). Even
  though it is technically an error to call omp_destroy_lock after
  shutdown, the application doesn't necessarily know that omp_destroy_lock
  was already called. This is safe becaues all indirect locks are
  destoryed in __kmp_cleanup_indirect_user_locks so the return
  value will always be valid or a nullptr, not garbage.
---
 openmp/runtime/src/kmp_lock.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/openmp/runtime/src/kmp_lock.cpp b/openmp/runtime/src/kmp_lock.cpp
index 0ad14f862bcb9..6b4edecb2f441 100644
--- a/openmp/runtime/src/kmp_lock.cpp
+++ b/openmp/runtime/src/kmp_lock.cpp
@@ -3242,6 +3242,8 @@ static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock) {
   kmp_uint32 gtid = __kmp_entry_gtid();
   kmp_indirect_lock_t *l =
       __kmp_lookup_indirect_lock((void **)lock, "omp_destroy_lock");
+  if (l == nullptr)
+    return;     // avoid segv if lock already destroyed
   KMP_I_LOCK_FUNC(l, destroy)(l->lock);
   kmp_indirect_locktag_t tag = l->type;
 

>From 661955312fe82a0156f192e77f0abafb14f8eb3e Mon Sep 17 00:00:00 2001
From: Larry Meadows <Lawrence.Meadows at amd.com>
Date: Wed, 25 Jun 2025 12:22:26 -0500
Subject: [PATCH 2/2] fix formatting

---
 openmp/runtime/src/kmp_lock.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/openmp/runtime/src/kmp_lock.cpp b/openmp/runtime/src/kmp_lock.cpp
index 6b4edecb2f441..11fa233c4bd27 100644
--- a/openmp/runtime/src/kmp_lock.cpp
+++ b/openmp/runtime/src/kmp_lock.cpp
@@ -3243,7 +3243,7 @@ static void __kmp_destroy_indirect_lock(kmp_dyna_lock_t *lock) {
   kmp_indirect_lock_t *l =
       __kmp_lookup_indirect_lock((void **)lock, "omp_destroy_lock");
   if (l == nullptr)
-    return;     // avoid segv if lock already destroyed
+    return; // avoid segv if lock already destroyed
   KMP_I_LOCK_FUNC(l, destroy)(l->lock);
   kmp_indirect_locktag_t tag = l->type;
 



More information about the Openmp-commits mailing list