1717#include " ErrorReporting.h"
1818#include " GlobalHandler.h"
1919#include " JIT.h"
20+ #include " Shared/EnvironmentVar.h"
2021#include " Shared/Sanitizer.h"
2122#include " Shared/Utils.h"
2223#include " Utils/ELF.h"
@@ -939,24 +940,33 @@ GenericDeviceTy::loadBinary(GenericPluginTy &Plugin,
939940 if (auto Err = callGlobalConstructors (Plugin, *Image))
940941 return std::move (Err);
941942
942- auto GetKernel = [&](StringRef Name) -> GenericKernelTy * {
943- auto KernelOrErr = constructKernel (Name.data ());
944- if (Error Err = KernelOrErr.takeError ()) {
945- REPORT (" Failure to look up kernel: %s\n " ,
946- toString (std::move (Err)).data ());
947- return nullptr ;
943+ auto *&SanitizerTrapInfo = SanitizerTrapInfos[Image];
944+ GlobalTy TrapId (" __sanitizer_trap_info_ptr" , sizeof (SanitizerTrapInfo),
945+ &SanitizerTrapInfo);
946+ GenericGlobalHandlerTy &GHandler = Plugin.getGlobalHandler ();
947+ if (GHandler.isSymbolInImage (*this , *Image, TrapId.getName ())) {
948+
949+ SanitizerTrapInfo = reinterpret_cast <SanitizerTrapInfoTy *>(allocate (
950+ sizeof (*SanitizerTrapInfo), &SanitizerTrapInfo, TARGET_ALLOC_HOST));
951+ memset (SanitizerTrapInfo, ' \0 ' , sizeof (SanitizerTrapInfoTy));
952+
953+ if (auto Err = GHandler.writeGlobalToDevice (*this , *Image, TrapId))
954+ return Err;
955+ {
956+ auto KernelOrErr = getKernel (" __sanitizer_register" , Image);
957+ if (auto Err = KernelOrErr.takeError ())
958+ consumeError (std::move (Err));
959+ else
960+ addGPUSanNewFn (*KernelOrErr);
948961 }
949- GenericKernelTy &Kernel = *KernelOrErr;
950- if (auto Err = Kernel.init (*this , *Image)) {
951- REPORT (" Failure to init kernel: %s\n " , toString (std::move (Err)).data ());
952- return nullptr ;
962+ {
963+ auto KernelOrErr = getKernel (" __sanitizer_unregister" , Image);
964+ if (auto Err = KernelOrErr.takeError ())
965+ consumeError (std::move (Err));
966+ else
967+ addGPUSanFreeFn (*KernelOrErr);
953968 }
954- return &Kernel;
955- };
956- if (GenericKernelTy *Kernel = GetKernel (" __sanitizer_register" ))
957- addGPUSanNewFn (*Kernel);
958- if (GenericKernelTy *Kernel = GetKernel (" __sanitizer_unregister" ))
959- addGPUSanFreeFn (*Kernel);
969+ }
960970
961971 // Return the pointer to the table of entries.
962972 return Image;
@@ -1036,16 +1046,6 @@ Error GenericDeviceTy::setupDeviceMemoryPool(GenericPluginTy &Plugin,
10361046 if (auto Err = GHandler.writeGlobalToDevice (*this , Image, TrackerGlobal))
10371047 return Err;
10381048
1039- auto *&SanitizerTrapInfo = SanitizerTrapInfos[&Image];
1040- SanitizerTrapInfo = reinterpret_cast <SanitizerTrapInfoTy *>(allocate (
1041- sizeof (*SanitizerTrapInfo), &SanitizerTrapInfo, TARGET_ALLOC_HOST));
1042- memset (SanitizerTrapInfo, ' \0 ' , sizeof (SanitizerTrapInfoTy));
1043-
1044- GlobalTy TrapId (" __sanitizer_trap_info_ptr" , sizeof (SanitizerTrapInfo),
1045- &SanitizerTrapInfo);
1046- if (auto Err = GHandler.writeGlobalToDevice (*this , Image, TrapId))
1047- return Err;
1048-
10491049 // Create the metainfo of the device environment global.
10501050 GlobalTy DevEnvGlobal (" __omp_rtl_device_memory_pool" ,
10511051 sizeof (DeviceMemoryPoolTy), &DeviceMemoryPool);
@@ -2404,32 +2404,17 @@ void GPUSanTy::checkAndReportError() {
24042404 Green (), STI->PtrSlot , STI->PtrTag , STI->PtrKind , STI->AllocationId ,
24052405 STI->AllocationTag , STI->AllocationKind , Default ());
24062406
2407- AllocationInfoTy *AllocationInfo = nullptr ;
2408- if (STI->AllocationStart ) {
2409- auto AllocationTraceMap = Device.AllocationTraces .getExclusiveAccessor ();
2410- AllocationInfo = (*AllocationTraceMap)[STI->AllocationStart ];
2411- }
2412- if (AllocationInfo) {
2413- fprintf (stderr, " \n Allocated at\n " );
2414- fprintf (stderr, " %s" , AllocationInfo->AllocationTrace .c_str ());
2415-
2416- if (!AllocationInfo->DeallocationTrace .empty ()) {
2417- fprintf (stderr, " \n Deallocated at\n " );
2418- fprintf (stderr, " %s" , AllocationInfo->DeallocationTrace .c_str ());
2419- }
2420- }
2421-
24222407 };
24232408
24242409 switch (STI->ErrorCode ) {
24252410 case SanitizerTrapInfoTy::None:
24262411 llvm_unreachable (" Unexpected exception" );
24272412 case SanitizerTrapInfoTy::ExceedsLength:
2428- fprintf (stderr, " %sERROR: OffloadSanitizer %s %d \n %s" , Red (),
2413+ fprintf (stderr, " %sERROR: OffloadSanitizer %s %lu \n %s" , Red (),
24292414 " exceeds length" , STI->PtrSlot , Default ());
24302415 break ;
24312416 case SanitizerTrapInfoTy::ExceedsSlots:
2432- fprintf (stderr, " %sERROR: OffloadSanitizer %s %d \n %s" , Red (),
2417+ fprintf (stderr, " %sERROR: OffloadSanitizer %s %lu \n %s" , Red (),
24332418 " exceeds slots" , STI->PtrSlot , Default ());
24342419 break ;
24352420 case SanitizerTrapInfoTy::PointerOutsideAllocation:
@@ -2449,7 +2434,7 @@ void GPUSanTy::checkAndReportError() {
24492434 DiagnoseAccess (" use-after-free" );
24502435 break ;
24512436 case SanitizerTrapInfoTy::MemoryLeak:
2452- fprintf (stderr, " %sERROR: OffloadSanitizer memory leak at slot %d \n %s" ,
2437+ fprintf (stderr, " %sERROR: OffloadSanitizer memory leak at slot %lu \n %s" ,
24532438 Red (), STI->PtrSlot , Default ());
24542439 break ;
24552440 case SanitizerTrapInfoTy::GarbagePointer:
0 commit comments