[Openmp-commits] [openmp] Memory leaks in OpenMP when affinity is disabled (PR #225441)

Thomas Köppe via Openmp-commits openmp-commits at lists.llvm.org
Tue Sep 22 08:54:40 PDT 2026


https://github.com/tkoeppe created https://github.com/llvm/llvm-project/pull/225441

When running with `KMP_AFFINITY=disabled`, there are two places in the OpenMP codebase that currently leak memory.

Found by the ASAN leak sanitizer.

>From db8810ccbe88483251cb7d923adc1932adf48852 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= <tkoeppe at google.com>
Date: Tue, 22 Sep 2026 16:48:44 +0100
Subject: [PATCH 1/2] [Clang][OpenMP] Don't call __kmp_allocate with zero
 argument.

Found by the ASAN leak sanitizer.
---
 openmp/runtime/src/kmp_affinity.h | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/openmp/runtime/src/kmp_affinity.h b/openmp/runtime/src/kmp_affinity.h
index fa69585f7e2d5..f30428229c903 100644
--- a/openmp/runtime/src/kmp_affinity.h
+++ b/openmp/runtime/src/kmp_affinity.h
@@ -350,7 +350,11 @@ class KMPNativeAffinity : public KMPAffinity {
 
   public:
     mask_t *mask;
-    Mask() { mask = (mask_t *)__kmp_allocate(__kmp_affin_mask_size); }
+    Mask()
+      : mask(__kmp_affin_mask_size == 0
+             ? nullptr
+             : (mask_t *)__kmp_allocate(__kmp_affin_mask_size)) {
+    }
     ~Mask() {
       if (mask)
         __kmp_free(mask);

>From e0b51f6850ed338e616a66d59134a2da0d7b8d5a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thomas=20K=C3=B6ppe?= <tkoeppe at google.com>
Date: Tue, 22 Sep 2026 16:53:40 +0100
Subject: [PATCH 2/2] [Clang][OpenMP] Missing cleanup before early return in
 __kmp_affinity_create_cpuinfo_map

Found by the ASAN leak sanitizer.
---
 openmp/runtime/src/kmp_affinity.cpp | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/openmp/runtime/src/kmp_affinity.cpp b/openmp/runtime/src/kmp_affinity.cpp
index 6a0e2910db8e0..31fe46be82cc2 100644
--- a/openmp/runtime/src/kmp_affinity.cpp
+++ b/openmp/runtime/src/kmp_affinity.cpp
@@ -3798,6 +3798,11 @@ static bool __kmp_affinity_create_cpuinfo_map(int *line,
   __kmp_ncores = totals[coreIdIndex];
   if (!KMP_AFFINITY_CAPABLE()) {
     KMP_ASSERT(__kmp_affinity.type == affinity_none);
+    __kmp_free(lastId);
+    __kmp_free(totals);
+    __kmp_free(maxCt);
+    __kmp_free(counts);
+    CLEANUP_THREAD_INFO;
     return true;
   }
 



More information about the Openmp-commits mailing list