[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
This commit is contained in:
Joseph Huber 2021-10-18 11:41:43 -04:00
parent 74f91741b6
commit 6dd791bca8
2 changed files with 14 additions and 4 deletions

View File

@ -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

View File

@ -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;
}