Skip to content

Commit 0aec4ce

Browse files
committed
wip
1 parent da04d13 commit 0aec4ce

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

source/loader/layers/sanitizer/msan/msan_interceptor.cpp

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,12 @@ ur_result_t MsanInterceptor::registerProgram(ur_program_handle_t Program) {
144144
return Result;
145145
}
146146

147+
getContext()->logger.info("registerDeviceGlobals");
148+
Result = registerDeviceGlobals(Program);
149+
if (Result != UR_RESULT_SUCCESS) {
150+
return Result;
151+
}
152+
147153
return Result;
148154
}
149155

@@ -212,6 +218,53 @@ ur_result_t MsanInterceptor::registerSpirKernels(ur_program_handle_t Program) {
212218
return UR_RESULT_SUCCESS;
213219
}
214220

221+
ur_result_t
222+
MsanInterceptor::registerDeviceGlobals(ur_program_handle_t Program) {
223+
std::vector<ur_device_handle_t> Devices = GetDevices(Program);
224+
assert(Devices.size() != 0 && "No devices in registerDeviceGlobals");
225+
auto Context = GetContext(Program);
226+
auto ContextInfo = getContextInfo(Context);
227+
auto ProgramInfo = getProgramInfo(Program);
228+
assert(ProgramInfo != nullptr && "unregistered program!");
229+
230+
for (auto Device : Devices) {
231+
ManagedQueue Queue(Context, Device);
232+
233+
size_t MetadataSize;
234+
void *MetadataPtr;
235+
auto Result =
236+
getContext()->urDdiTable.Program.pfnGetGlobalVariablePointer(
237+
Device, Program, kSPIR_MsanDeviceGlobalMetadata, &MetadataSize,
238+
&MetadataPtr);
239+
if (Result != UR_RESULT_SUCCESS) {
240+
getContext()->logger.info("No device globals");
241+
continue;
242+
}
243+
244+
const uint64_t NumOfDeviceGlobal =
245+
MetadataSize / sizeof(DeviceGlobalInfo);
246+
assert((MetadataSize % sizeof(DeviceGlobalInfo) == 0) &&
247+
"DeviceGlobal metadata size is not correct");
248+
std::vector<DeviceGlobalInfo> GVInfos(NumOfDeviceGlobal);
249+
Result = getContext()->urDdiTable.Enqueue.pfnUSMMemcpy(
250+
Queue, true, &GVInfos[0], MetadataPtr,
251+
sizeof(DeviceGlobalInfo) * NumOfDeviceGlobal, 0, nullptr, nullptr);
252+
if (Result != UR_RESULT_SUCCESS) {
253+
getContext()->logger.error("Device Global[{}] Read Failed: {}",
254+
kSPIR_MsanDeviceGlobalMetadata, Result);
255+
return Result;
256+
}
257+
258+
auto DeviceInfo = getMsanInterceptor()->getDeviceInfo(Device);
259+
for (size_t i = 0; i < NumOfDeviceGlobal; i++) {
260+
const auto &GVInfo = GVInfos[i];
261+
UR_CALL(DeviceInfo->Shadow->EnqueuePoisonShadow(Queue, GVInfo.Addr, GVInfo.Size, 0));
262+
}
263+
}
264+
265+
return UR_RESULT_SUCCESS;
266+
}
267+
215268
ur_result_t MsanInterceptor::insertContext(ur_context_handle_t Context,
216269
std::shared_ptr<ContextInfo> &CI) {
217270
std::scoped_lock<ur_shared_mutex> Guard(m_ContextMapMutex);

source/loader/layers/sanitizer/msan/msan_interceptor.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ struct USMLaunchInfo {
159159
ur_result_t initialize();
160160
};
161161

162+
struct DeviceGlobalInfo {
163+
uptr Size;
164+
uptr Addr;
165+
};
166+
162167
struct SpirKernelInfo {
163168
uptr KernelName;
164169
uptr Size;
@@ -261,6 +266,7 @@ class MsanInterceptor {
261266
std::shared_ptr<msan::DeviceInfo> &DeviceInfo);
262267

263268
ur_result_t registerSpirKernels(ur_program_handle_t Program);
269+
ur_result_t registerDeviceGlobals(ur_program_handle_t Program);
264270

265271
private:
266272
std::unordered_map<ur_context_handle_t, std::shared_ptr<msan::ContextInfo>>

source/loader/layers/sanitizer/msan/msan_shadow.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,15 +227,10 @@ ur_result_t MsanShadowMemoryGPU::EnqueueMapShadow(
227227
VirtualMemMaps[MappedPtr].first = PhysicalMem;
228228
}
229229

230-
// We don't need to record virtual memory map for null pointer,
231-
// since it doesn't have an alloc info.
232-
if (Ptr == 0) {
233-
continue;
230+
auto AllocInfoItOp = getMsanInterceptor()->findAllocInfoByAddress(Ptr);
231+
if (AllocInfoItOp) {
232+
VirtualMemMaps[MappedPtr].second.insert((*AllocInfoItOp)->second);
234233
}
235-
236-
auto AllocInfoIt = getMsanInterceptor()->findAllocInfoByAddress(Ptr);
237-
assert(AllocInfoIt);
238-
VirtualMemMaps[MappedPtr].second.insert((*AllocInfoIt)->second);
239234
}
240235

241236
return UR_RESULT_SUCCESS;

0 commit comments

Comments
 (0)