@@ -416,6 +416,88 @@ ur_result_t DeviceInfo::allocShadowMemory(ur_context_handle_t Context) {
416416 return UR_RESULT_SUCCESS;
417417}
418418
419+ // / Each 8 bytes of application memory are mapped into one byte of shadow memory
420+ // / The meaning of that byte:
421+ // / - Negative: All bytes are not accessible (poisoned)
422+ // / - 0: All bytes are accessible
423+ // / - 1 <= k <= 7: Only the first k bytes is accessible
424+ // /
425+ // / ref: https://github.com/google/sanitizers/wiki/AddressSanitizerAlgorithm#mapping
426+ ur_result_t SanitizerInterceptor::enqueueAllocInfo (
427+ ur_context_handle_t Context, std::shared_ptr<DeviceInfo> &DeviceInfo,
428+ ur_queue_handle_t Queue, std::shared_ptr<AllocInfo> &AI) {
429+ if (AI->IsReleased ) {
430+ int ShadowByte;
431+ switch (AI->Type ) {
432+ case AllocType::HOST_USM:
433+ ShadowByte = kUsmHostDeallocatedMagic ;
434+ break ;
435+ case AllocType::DEVICE_USM:
436+ ShadowByte = kUsmDeviceDeallocatedMagic ;
437+ break ;
438+ case AllocType::SHARED_USM:
439+ ShadowByte = kUsmSharedDeallocatedMagic ;
440+ break ;
441+ case AllocType::MEM_BUFFER:
442+ ShadowByte = kMemBufferDeallocatedMagic ;
443+ break ;
444+ default :
445+ ShadowByte = 0xff ;
446+ assert (false && " Unknow AllocInfo Type" );
447+ }
448+ UR_CALL (enqueueMemSetShadow (Context, DeviceInfo, Queue, AI->AllocBegin ,
449+ AI->AllocSize , ShadowByte));
450+ return UR_RESULT_SUCCESS;
451+ }
452+
453+ // Init zero
454+ UR_CALL (enqueueMemSetShadow (Context, DeviceInfo, Queue, AI->AllocBegin ,
455+ AI->AllocSize , 0 ));
456+
457+ uptr TailBegin = RoundUpTo (AI->UserEnd , ASAN_SHADOW_GRANULARITY);
458+ uptr TailEnd = AI->AllocBegin + AI->AllocSize ;
459+
460+ // User tail
461+ if (TailBegin != AI->UserEnd ) {
462+ auto Value =
463+ AI->UserEnd - RoundDownTo (AI->UserEnd , ASAN_SHADOW_GRANULARITY);
464+ UR_CALL (enqueueMemSetShadow (Context, DeviceInfo, Queue, AI->UserEnd , 1 ,
465+ static_cast <u8 >(Value)));
466+ }
467+
468+ int ShadowByte;
469+ switch (AI->Type ) {
470+ case AllocType::HOST_USM:
471+ ShadowByte = kUsmHostRedzoneMagic ;
472+ break ;
473+ case AllocType::DEVICE_USM:
474+ ShadowByte = kUsmDeviceRedzoneMagic ;
475+ break ;
476+ case AllocType::SHARED_USM:
477+ ShadowByte = kUsmSharedRedzoneMagic ;
478+ break ;
479+ case AllocType::MEM_BUFFER:
480+ ShadowByte = kMemBufferRedzoneMagic ;
481+ break ;
482+ case AllocType::DEVICE_GLOBAL:
483+ ShadowByte = kDeviceGlobalRedzoneMagic ;
484+ break ;
485+ default :
486+ ShadowByte = 0xff ;
487+ assert (false && " Unknow AllocInfo Type" );
488+ }
489+
490+ // Left red zone
491+ UR_CALL (enqueueMemSetShadow (Context, DeviceInfo, Queue, AI->AllocBegin ,
492+ AI->UserBegin - AI->AllocBegin , ShadowByte));
493+
494+ // Right red zone
495+ UR_CALL (enqueueMemSetShadow (Context, DeviceInfo, Queue, TailBegin,
496+ TailEnd - TailBegin, ShadowByte));
497+
498+ return UR_RESULT_SUCCESS;
499+ }
500+
419501ur_result_t SanitizerInterceptor::updateShadowMemory (
420502 std::shared_ptr<ContextInfo> &ContextInfo,
421503 std::shared_ptr<DeviceInfo> &DeviceInfo, ur_queue_handle_t Queue) {
0 commit comments