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
@@ -1377,6 +1379,22 @@ Expected<void *> GenericDeviceTy::dataAlloc(int64_t Size, void *HostPtr,
13771379 if (auto Err = PinnedAllocs.registerHostBuffer (Alloc, Alloc, Size))
13781380 return std::move (Err);
13791381
1382+ std::string StackTrace;
1383+ llvm::raw_string_ostream OS (StackTrace);
1384+ llvm::sys::PrintStackTrace (OS);
1385+
1386+ AllocationInfoTy *AllocationInfo = new AllocationInfoTy ();
1387+ AllocationInfo->AllocationTrace = std::move (StackTrace);
1388+ AllocationInfo->HostPtr = HostPtr;
1389+ AllocationInfo->Size = Size;
1390+ AllocationInfo->Kind = Kind;
1391+
1392+ auto AllocationTraceMap = AllocationTraces.getExclusiveAccessor ();
1393+ auto *&AI = (*AllocationTraceMap)[Alloc];
1394+ if (AI)
1395+ delete AI;
1396+ AI = AllocationInfo;
1397+
13801398 return Alloc;
13811399}
13821400
@@ -1385,6 +1403,32 @@ Error GenericDeviceTy::dataDelete(void *TgtPtr, TargetAllocTy Kind) {
13851403 if (Plugin.getRecordReplay ().isRecordingOrReplaying ())
13861404 return Plugin::success ();
13871405
1406+ AllocationInfoTy *AllocationInfo = nullptr ;
1407+ {
1408+ auto AllocationTraceMap = AllocationTraces.getExclusiveAccessor ();
1409+ AllocationInfo = (*AllocationTraceMap)[TgtPtr];
1410+ }
1411+
1412+ std::string StackTrace;
1413+ llvm::raw_string_ostream OS (StackTrace);
1414+ llvm::sys::PrintStackTrace (OS);
1415+ if (!AllocationInfo) {
1416+ fprintf (stderr, " %s" , StackTrace.c_str ());
1417+ report_fatal_error (" Free of non-allocated memory" );
1418+ }
1419+
1420+ if (!AllocationInfo->DeallocationTrace .empty ()) {
1421+ fprintf (stderr, " %s" , StackTrace.c_str ());
1422+ report_fatal_error (" double-free" );
1423+ }
1424+
1425+ if (AllocationInfo->Kind != Kind) {
1426+ fprintf (stderr, " %s" , StackTrace.c_str ());
1427+ report_fatal_error (" free of wrong kind of memory" );
1428+ }
1429+
1430+ AllocationInfo->DeallocationTrace = StackTrace;
1431+
13881432 int Res;
13891433 switch (Kind) {
13901434 case TARGET_ALLOC_DEFAULT:
@@ -2291,6 +2335,22 @@ void GPUSanTy::checkAndReportError() {
22912335 " Allocation[slot:%d,tag:%u,kind:%i]\n %s" ,
22922336 Green (), STI->PtrSlot , STI->PtrTag , STI->PtrKind , STI->AllocationId ,
22932337 STI->AllocationTag , STI->AllocationKind , Default ());
2338+
2339+ AllocationInfoTy *AllocationInfo = nullptr ;
2340+ if (STI->AllocationStart ) {
2341+ auto AllocationTraceMap = Device.AllocationTraces .getExclusiveAccessor ();
2342+ AllocationInfo = (*AllocationTraceMap)[STI->AllocationStart ];
2343+ }
2344+ if (AllocationInfo) {
2345+ fprintf (stderr, " \n Allocated at\n " );
2346+ fprintf (stderr, " %s" , AllocationInfo->AllocationTrace .c_str ());
2347+
2348+ if (!AllocationInfo->DeallocationTrace .empty ()) {
2349+ fprintf (stderr, " \n Deallocated at\n " );
2350+ fprintf (stderr, " %s" , AllocationInfo->DeallocationTrace .c_str ());
2351+ }
2352+ }
2353+
22942354 };
22952355
22962356 switch (STI->ErrorCode ) {
0 commit comments