[Clang][OpenMP] Reapply "Memory leaks in OpenMP when affinity is disabled" (#226292) (#226294) The previous version was applying a compiler flag to the smoke test that would result in an unrunnable binary on non-ARM platforms. The intention is for ASAN on ARM to run, and we get that by just not specifying any flags and relying on the test system to include this platform. GitOrigin-RevId: 92289bdbced90ccc9f63da0c7fdaa71879c70964
diff --git a/runtime/src/kmp_affinity.cpp b/runtime/src/kmp_affinity.cpp index 6a0e291..31fe46b 100644 --- a/runtime/src/kmp_affinity.cpp +++ b/runtime/src/kmp_affinity.cpp
@@ -3798,6 +3798,11 @@ __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; }
diff --git a/runtime/src/kmp_affinity.h b/runtime/src/kmp_affinity.h index fa69585..97b40fe 100644 --- a/runtime/src/kmp_affinity.h +++ b/runtime/src/kmp_affinity.h
@@ -350,7 +350,10 @@ 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);
diff --git a/runtime/test/affinity/no_leak_with_disabled.c b/runtime/test/affinity/no_leak_with_disabled.c new file mode 100644 index 0000000..43a7f26 --- /dev/null +++ b/runtime/test/affinity/no_leak_with_disabled.c
@@ -0,0 +1,15 @@ +// RUN: %libomp-compile -fopenmp +// RUN: %libomp-run +// RUN: env KMP_AFFINITY=disabled %libomp-run + +// Check that no memory is leaked with KMP_AFFINITY=disabled. +// The detection is done by ASAN/LSAN. + +#include <omp.h> + +int main(void) { +#pragma omp parallel + { + omp_get_thread_num(); + } +}