[Openmp-commits] [openmp] 6dd791b - [OpenMP] Check output of malloc in the device for debug

Joseph Huber via Openmp-commits openmp-commits at lists.llvm.org
Fri Oct 29 11:57:24 PDT 2021


Author: Joseph Huber
Date: 2021-10-29T14:57:12-04:00
New Revision: 6dd791bca858c0d5241689c9eefd9313fe823ee3

URL: https://github.com/llvm/llvm-project/commit/6dd791bca858c0d5241689c9eefd9313fe823ee3
DIFF: https://github.com/llvm/llvm-project/commit/6dd791bca858c0d5241689c9eefd9313fe823ee3.diff

LOG: [OpenMP] Check output of malloc in the device for debug

A common problem is the device running out of global heap memory and
crashing due to a nullptr dereference when using the data sharing stack.
This explicitly checks that a nullptr was not returned by malloc when
debugging field 1 is enabled.

Reviewed By: jdoerfert

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

Added: 
    

Modified: 
    openmp/libomptarget/DeviceRTL/include/Configuration.h
    openmp/libomptarget/DeviceRTL/src/State.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/DeviceRTL/include/Configuration.h b/openmp/libomptarget/DeviceRTL/include/Configuration.h
index 7e826940cf01..5727f1f2bfbf 100644
--- a/openmp/libomptarget/DeviceRTL/include/Configuration.h
+++ b/openmp/libomptarget/DeviceRTL/include/Configuration.h
@@ -21,6 +21,7 @@ namespace config {
 enum DebugKind : uint32_t {
   Assertion = 1U << 0,
   FunctionTracing = 1U << 1,
+  CommonIssues = 1U << 2,
 };
 
 /// Return the number of devices in the system, same number as returned on the

diff  --git a/openmp/libomptarget/DeviceRTL/src/State.cpp b/openmp/libomptarget/DeviceRTL/src/State.cpp
index 54a191ce01fa..40d10a65817e 100644
--- a/openmp/libomptarget/DeviceRTL/src/State.cpp
+++ b/openmp/libomptarget/DeviceRTL/src/State.cpp
@@ -134,9 +134,12 @@ void *SharedMemorySmartStackTy::push(uint64_t Bytes) {
     return Ptr;
   }
 
-  return memory::allocGlobal(AlignedBytes,
-                             "Slow path shared memory allocation, insufficient "
-                             "shared memory stack memory!");
+  void *GlobalMemory = memory::allocGlobal(
+      AlignedBytes, "Slow path shared memory allocation, insufficient "
+                    "shared memory stack memory!");
+  ASSERT(GlobalMemory != nullptr && "nullptr returned by malloc!");
+
+  return GlobalMemory;
 }
 
 void SharedMemorySmartStackTy::pop(void *Ptr, uint32_t Bytes) {
@@ -162,7 +165,10 @@ void memory::freeShared(void *Ptr, uint64_t Bytes, const char *Reason) {
 }
 
 void *memory::allocGlobal(uint64_t Bytes, const char *Reason) {
-  return malloc(Bytes);
+  void *Ptr = malloc(Bytes);
+  if (config::isDebugMode(config::DebugKind::CommonIssues) && Ptr == nullptr)
+    PRINT("nullptr returned by malloc!\n");
+  return Ptr;
 }
 
 void memory::freeGlobal(void *Ptr, const char *Reason) { free(Ptr); }
@@ -280,6 +286,7 @@ uint32_t &lookupForModify32Impl(uint32_t ICVStateTy::*Var) {
   if (!ThreadStates[TId]) {
     ThreadStates[TId] = reinterpret_cast<ThreadStateTy *>(memory::allocGlobal(
         sizeof(ThreadStateTy), "ICV modification outside data environment"));
+    ASSERT(ThreadStates[TId] != nullptr && "Nullptr returned by malloc!");
     ThreadStates[TId]->init();
   }
   return ThreadStates[TId]->ICVState.*Var;
@@ -531,6 +538,8 @@ void __kmpc_begin_sharing_variables(void ***GlobalArgs, uint64_t nArgs) {
   } else {
     SharedMemVariableSharingSpacePtr = (void **)memory::allocGlobal(
         nArgs * sizeof(void *), "new extended args");
+    ASSERT(SharedMemVariableSharingSpacePtr != nullptr &&
+           "Nullptr returned by malloc!");
   }
   *GlobalArgs = SharedMemVariableSharingSpacePtr;
 }


        


More information about the Openmp-commits mailing list