2121#include " Utils/ELF.h"
2222#include " omptarget.h"
2323#include " llvm/Support/ErrorHandling.h"
24+ #include " llvm/Support/Signals.h"
25+ #include " llvm/Support/raw_ostream.h"
2426#include < cstdio>
2527#include < string>
2628
@@ -1380,6 +1382,22 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13801382 if (auto Err = PinnedAllocs.registerHostBuffer (Alloc, Alloc, Size))
13811383 return std::move (Err);
13821384
1385+ std::string StackTrace;
1386+ llvm::raw_string_ostream OS (StackTrace);
1387+ llvm::sys::PrintStackTrace (OS);
1388+
1389+ AllocationInfoTy *AllocationInfo = new AllocationInfoTy ();
1390+ AllocationInfo->AllocationTrace = std::move (StackTrace);
1391+ AllocationInfo->HostPtr = HostPtr;
1392+ AllocationInfo->Size = Size;
1393+ AllocationInfo->Kind = Kind;
1394+
1395+ auto AllocationTraceMap = AllocationTraces.getExclusiveAccessor ();
1396+ auto *&AI = (*AllocationTraceMap)[Alloc];
1397+ if (AI)
1398+ delete AI;
1399+ AI = AllocationInfo;
1400+
13831401 return Alloc;
13841402}
13851403
@@ -1388,6 +1406,32 @@ Error GenericDeviceTy::dataDelete(void *TgtPtr, TargetAllocTy Kind) {
13881406 if (Plugin.getRecordReplay ().isRecordingOrReplaying ())
13891407 return Plugin::success ();
13901408
1409+ AllocationInfoTy *AllocationInfo = nullptr ;
1410+ {
1411+ auto AllocationTraceMap = AllocationTraces.getExclusiveAccessor ();
1412+ AllocationInfo = (*AllocationTraceMap)[TgtPtr];
1413+ }
1414+
1415+ std::string StackTrace;
1416+ llvm::raw_string_ostream OS (StackTrace);
1417+ llvm::sys::PrintStackTrace (OS);
1418+ if (!AllocationInfo) {
1419+ fprintf (stderr, " %s" , StackTrace.c_str ());
1420+ report_fatal_error (" Free of non-allocated memory" );
1421+ }
1422+
1423+ if (!AllocationInfo->DeallocationTrace .empty ()) {
1424+ fprintf (stderr, " %s" , StackTrace.c_str ());
1425+ report_fatal_error (" double-free" );
1426+ }
1427+
1428+ if (AllocationInfo->Kind != Kind) {
1429+ fprintf (stderr, " %s" , StackTrace.c_str ());
1430+ report_fatal_error (" free of wrong kind of memory" );
1431+ }
1432+
1433+ AllocationInfo->DeallocationTrace = StackTrace;
1434+
13911435 int Res;
13921436 switch (Kind) {
13931437 case TARGET_ALLOC_DEFAULT:
@@ -2294,6 +2338,22 @@ void GPUSanTy::checkAndReportError() {
22942338 " Allocation[slot:%d,tag:%u,kind:%i]\n %s" ,
22952339 Green (), STI->PtrSlot , STI->PtrTag , STI->PtrKind , STI->AllocationId ,
22962340 STI->AllocationTag , STI->AllocationKind , Default ());
2341+
2342+ AllocationInfoTy *AllocationInfo = nullptr ;
2343+ if (STI->AllocationStart ) {
2344+ auto AllocationTraceMap = Device.AllocationTraces .getExclusiveAccessor ();
2345+ AllocationInfo = (*AllocationTraceMap)[STI->AllocationStart ];
2346+ }
2347+ if (AllocationInfo) {
2348+ fprintf (stderr, " \n Allocated at\n " );
2349+ fprintf (stderr, " %s" , AllocationInfo->AllocationTrace .c_str ());
2350+
2351+ if (!AllocationInfo->DeallocationTrace .empty ()) {
2352+ fprintf (stderr, " \n Deallocated at\n " );
2353+ fprintf (stderr, " %s" , AllocationInfo->DeallocationTrace .c_str ());
2354+ }
2355+ }
2356+
22972357 };
22982358
22992359 switch (STI->ErrorCode ) {
0 commit comments