|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2024 Intel Corporation |
| 4 | + * |
| 5 | + * Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions. |
| 6 | + * See LICENSE.TXT |
| 7 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 8 | + * |
| 9 | + * @file asan_shadow.cpp |
| 10 | + * |
| 11 | + */ |
| 12 | + |
| 13 | +#include "asan_shadow.hpp" |
| 14 | +#include "asan_interceptor.hpp" |
| 15 | +#include "asan_libdevice.hpp" |
| 16 | +#include "ur_sanitizer_layer.hpp" |
| 17 | +#include "ur_sanitizer_utils.hpp" |
| 18 | + |
| 19 | +namespace ur_sanitizer_layer { |
| 20 | + |
| 21 | +std::shared_ptr<ShadowMemory> GetShadowMemory(ur_context_handle_t Context, |
| 22 | + ur_device_handle_t Device, |
| 23 | + DeviceType Type) { |
| 24 | + if (Type == DeviceType::CPU) { |
| 25 | + static std::shared_ptr<ShadowMemory> ShadowCPU = |
| 26 | + std::make_shared<ShadowMemoryCPU>(Context, Device); |
| 27 | + return ShadowCPU; |
| 28 | + } else if (Type == DeviceType::GPU_PVC) { |
| 29 | + static std::shared_ptr<ShadowMemory> ShadowPVC = |
| 30 | + std::make_shared<ShadowMemoryPVC>(Context, Device); |
| 31 | + return ShadowPVC; |
| 32 | + } else if (Type == DeviceType::GPU_DG2) { |
| 33 | + static std::shared_ptr<ShadowMemory> ShadowDG2 = |
| 34 | + std::make_shared<ShadowMemoryDG2>(Context, Device); |
| 35 | + return ShadowDG2; |
| 36 | + } else { |
| 37 | + getContext()->logger.error("Unsupport device type"); |
| 38 | + return nullptr; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +ur_result_t ShadowMemoryCPU::Setup() { |
| 43 | + static ur_result_t Result = [this]() { |
| 44 | + size_t ShadowSize = GetShadowSize(); |
| 45 | + ShadowBegin = MmapNoReserve(0, ShadowSize); |
| 46 | + if (ShadowBegin == 0) { |
| 47 | + return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 48 | + } |
| 49 | + DontCoredumpRange(ShadowBegin, ShadowSize); |
| 50 | + ShadowEnd = ShadowBegin + ShadowSize; |
| 51 | + |
| 52 | + // Set shadow memory for null pointer |
| 53 | + auto URes = EnqueuePoisonShadow({}, 0, 1, kNullPointerRedzoneMagic); |
| 54 | + if (URes != UR_RESULT_SUCCESS) { |
| 55 | + getContext()->logger.error("EnqueuePoisonShadow(NullPointerRZ): {}", |
| 56 | + URes); |
| 57 | + return URes; |
| 58 | + } |
| 59 | + return URes; |
| 60 | + }(); |
| 61 | + return Result; |
| 62 | +} |
| 63 | + |
| 64 | +ur_result_t ShadowMemoryCPU::Destory() { |
| 65 | + if (ShadowBegin == 0) { |
| 66 | + return UR_RESULT_SUCCESS; |
| 67 | + } |
| 68 | + static ur_result_t Result = [this]() { |
| 69 | + if (!Munmap(ShadowBegin, GetShadowSize())) { |
| 70 | + return UR_RESULT_ERROR_UNKNOWN; |
| 71 | + } |
| 72 | + return UR_RESULT_SUCCESS; |
| 73 | + }(); |
| 74 | + return Result; |
| 75 | +} |
| 76 | + |
| 77 | +uptr ShadowMemoryCPU::MemToShadow(uptr Ptr) { |
| 78 | + return ShadowBegin + (Ptr >> ASAN_SHADOW_SCALE); |
| 79 | +} |
| 80 | + |
| 81 | +ur_result_t ShadowMemoryCPU::EnqueuePoisonShadow(ur_queue_handle_t, uptr Ptr, |
| 82 | + uptr Size, u8 Value) { |
| 83 | + if (Size == 0) { |
| 84 | + return UR_RESULT_SUCCESS; |
| 85 | + } |
| 86 | + |
| 87 | + uptr ShadowBegin = MemToShadow(Ptr); |
| 88 | + uptr ShadowEnd = MemToShadow(Ptr + Size - 1); |
| 89 | + assert(ShadowBegin <= ShadowEnd); |
| 90 | + getContext()->logger.debug( |
| 91 | + "EnqueuePoisonShadow(addr={}, count={}, value={})", (void *)ShadowBegin, |
| 92 | + ShadowEnd - ShadowBegin + 1, (void *)(size_t)Value); |
| 93 | + memset((void *)ShadowBegin, Value, ShadowEnd - ShadowBegin + 1); |
| 94 | + |
| 95 | + return UR_RESULT_SUCCESS; |
| 96 | +} |
| 97 | + |
| 98 | +ur_result_t ShadowMemoryGPU::Setup() { |
| 99 | + // Currently, Level-Zero doesn't create independent VAs for each contexts, if we reserve |
| 100 | + // shadow memory for each contexts, this will cause out-of-resource error when user uses |
| 101 | + // multiple contexts. Therefore, we just create one shadow memory here. |
| 102 | + static ur_result_t Result = [this]() { |
| 103 | + size_t ShadowSize = GetShadowSize(); |
| 104 | + // TODO: Protect Bad Zone |
| 105 | + auto Result = getContext()->urDdiTable.VirtualMem.pfnReserve( |
| 106 | + Context, nullptr, ShadowSize, (void **)&ShadowBegin); |
| 107 | + if (Result == UR_RESULT_SUCCESS) { |
| 108 | + ShadowEnd = ShadowBegin + ShadowSize; |
| 109 | + // Retain the context which reserves shadow memory |
| 110 | + getContext()->urDdiTable.Context.pfnRetain(Context); |
| 111 | + } |
| 112 | + |
| 113 | + // Set shadow memory for null pointer |
| 114 | + ManagedQueue Queue(Context, Device); |
| 115 | + |
| 116 | + Result = EnqueuePoisonShadow(Queue, 0, 1, kNullPointerRedzoneMagic); |
| 117 | + if (Result != UR_RESULT_SUCCESS) { |
| 118 | + getContext()->logger.error("EnqueuePoisonShadow(NullPointerRZ): {}", |
| 119 | + Result); |
| 120 | + return Result; |
| 121 | + } |
| 122 | + return Result; |
| 123 | + }(); |
| 124 | + return Result; |
| 125 | +} |
| 126 | + |
| 127 | +ur_result_t ShadowMemoryGPU::Destory() { |
| 128 | + if (ShadowBegin == 0) { |
| 129 | + return UR_RESULT_SUCCESS; |
| 130 | + } |
| 131 | + static ur_result_t Result = [this]() { |
| 132 | + auto Result = getContext()->urDdiTable.VirtualMem.pfnFree( |
| 133 | + Context, (const void *)ShadowBegin, GetShadowSize()); |
| 134 | + getContext()->urDdiTable.Context.pfnRelease(Context); |
| 135 | + return Result; |
| 136 | + }(); |
| 137 | + return Result; |
| 138 | +} |
| 139 | + |
| 140 | +ur_result_t ShadowMemoryGPU::EnqueuePoisonShadow(ur_queue_handle_t Queue, |
| 141 | + uptr Ptr, uptr Size, |
| 142 | + u8 Value) { |
| 143 | + if (Size == 0) { |
| 144 | + return UR_RESULT_SUCCESS; |
| 145 | + } |
| 146 | + |
| 147 | + uptr ShadowBegin = MemToShadow(Ptr); |
| 148 | + uptr ShadowEnd = MemToShadow(Ptr + Size - 1); |
| 149 | + assert(ShadowBegin <= ShadowEnd); |
| 150 | + { |
| 151 | + static const size_t PageSize = |
| 152 | + GetVirtualMemGranularity(Context, Device); |
| 153 | + |
| 154 | + ur_physical_mem_properties_t Desc{ |
| 155 | + UR_STRUCTURE_TYPE_PHYSICAL_MEM_PROPERTIES, nullptr, 0}; |
| 156 | + |
| 157 | + // Make sure [Ptr, Ptr + Size] is mapped to physical memory |
| 158 | + for (auto MappedPtr = RoundDownTo(ShadowBegin, PageSize); |
| 159 | + MappedPtr <= ShadowEnd; MappedPtr += PageSize) { |
| 160 | + std::scoped_lock<ur_mutex> Guard(VirtualMemMapsMutex); |
| 161 | + if (VirtualMemMaps.find(MappedPtr) == VirtualMemMaps.end()) { |
| 162 | + ur_physical_mem_handle_t PhysicalMem{}; |
| 163 | + auto URes = getContext()->urDdiTable.PhysicalMem.pfnCreate( |
| 164 | + Context, Device, PageSize, &Desc, &PhysicalMem); |
| 165 | + if (URes != UR_RESULT_SUCCESS) { |
| 166 | + getContext()->logger.error("urPhysicalMemCreate(): {}", |
| 167 | + URes); |
| 168 | + return URes; |
| 169 | + } |
| 170 | + |
| 171 | + URes = getContext()->urDdiTable.VirtualMem.pfnMap( |
| 172 | + Context, (void *)MappedPtr, PageSize, PhysicalMem, 0, |
| 173 | + UR_VIRTUAL_MEM_ACCESS_FLAG_READ_WRITE); |
| 174 | + if (URes != UR_RESULT_SUCCESS) { |
| 175 | + getContext()->logger.error("urVirtualMemMap({}, {}): {}", |
| 176 | + (void *)MappedPtr, PageSize, |
| 177 | + URes); |
| 178 | + return URes; |
| 179 | + } |
| 180 | + |
| 181 | + getContext()->logger.debug("urVirtualMemMap: {} ~ {}", |
| 182 | + (void *)MappedPtr, |
| 183 | + (void *)(MappedPtr + PageSize - 1)); |
| 184 | + |
| 185 | + // Initialize to zero |
| 186 | + URes = EnqueueUSMBlockingSet(Queue, (void *)MappedPtr, 0, |
| 187 | + PageSize); |
| 188 | + if (URes != UR_RESULT_SUCCESS) { |
| 189 | + getContext()->logger.error("EnqueueUSMBlockingSet(): {}", |
| 190 | + URes); |
| 191 | + return URes; |
| 192 | + } |
| 193 | + |
| 194 | + VirtualMemMaps[MappedPtr].first = PhysicalMem; |
| 195 | + } |
| 196 | + |
| 197 | + // We don't need to record virtual memory map for null pointer, |
| 198 | + // since it doesn't have an alloc info. |
| 199 | + if (Ptr == 0) { |
| 200 | + continue; |
| 201 | + } |
| 202 | + |
| 203 | + auto AllocInfoIt = |
| 204 | + getContext()->interceptor->findAllocInfoByAddress(Ptr); |
| 205 | + assert(AllocInfoIt); |
| 206 | + VirtualMemMaps[MappedPtr].second.insert((*AllocInfoIt)->second); |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + auto URes = EnqueueUSMBlockingSet(Queue, (void *)ShadowBegin, Value, |
| 211 | + ShadowEnd - ShadowBegin + 1); |
| 212 | + getContext()->logger.debug( |
| 213 | + "EnqueuePoisonShadow (addr={}, count={}, value={}): {}", |
| 214 | + (void *)ShadowBegin, ShadowEnd - ShadowBegin + 1, (void *)(size_t)Value, |
| 215 | + URes); |
| 216 | + if (URes != UR_RESULT_SUCCESS) { |
| 217 | + getContext()->logger.error("EnqueueUSMBlockingSet(): {}", URes); |
| 218 | + return URes; |
| 219 | + } |
| 220 | + |
| 221 | + return UR_RESULT_SUCCESS; |
| 222 | +} |
| 223 | + |
| 224 | +ur_result_t ShadowMemoryGPU::ReleaseShadow(std::shared_ptr<AllocInfo> AI) { |
| 225 | + uptr ShadowBegin = MemToShadow(AI->AllocBegin); |
| 226 | + uptr ShadowEnd = MemToShadow(AI->AllocBegin + AI->AllocSize); |
| 227 | + assert(ShadowBegin <= ShadowEnd); |
| 228 | + |
| 229 | + static const size_t PageSize = GetVirtualMemGranularity(Context, Device); |
| 230 | + |
| 231 | + for (auto MappedPtr = RoundDownTo(ShadowBegin, PageSize); |
| 232 | + MappedPtr <= ShadowEnd; MappedPtr += PageSize) { |
| 233 | + std::scoped_lock<ur_mutex> Guard(VirtualMemMapsMutex); |
| 234 | + if (VirtualMemMaps.find(MappedPtr) == VirtualMemMaps.end()) { |
| 235 | + continue; |
| 236 | + } |
| 237 | + VirtualMemMaps[MappedPtr].second.erase(AI); |
| 238 | + if (VirtualMemMaps[MappedPtr].second.empty()) { |
| 239 | + UR_CALL(getContext()->urDdiTable.VirtualMem.pfnUnmap( |
| 240 | + Context, (void *)MappedPtr, PageSize)); |
| 241 | + UR_CALL(getContext()->urDdiTable.PhysicalMem.pfnRelease( |
| 242 | + VirtualMemMaps[MappedPtr].first)); |
| 243 | + getContext()->logger.debug("urVirtualMemUnmap: {} ~ {}", |
| 244 | + (void *)MappedPtr, |
| 245 | + (void *)(MappedPtr + PageSize - 1)); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + return UR_RESULT_SUCCESS; |
| 250 | +} |
| 251 | + |
| 252 | +uptr ShadowMemoryPVC::MemToShadow(uptr Ptr) { |
| 253 | + if (Ptr & 0xFF00000000000000ULL) { // Device USM |
| 254 | + return ShadowBegin + 0x80000000000ULL + |
| 255 | + ((Ptr & 0xFFFFFFFFFFFFULL) >> ASAN_SHADOW_SCALE); |
| 256 | + } else { // Only consider 47bit VA |
| 257 | + return ShadowBegin + ((Ptr & 0x7FFFFFFFFFFFULL) >> ASAN_SHADOW_SCALE); |
| 258 | + } |
| 259 | +} |
| 260 | + |
| 261 | +uptr ShadowMemoryDG2::MemToShadow(uptr Ptr) { |
| 262 | + if (Ptr & 0xFFFF000000000000ULL) { // Device USM |
| 263 | + return ShadowBegin + 0x80000000000ULL + |
| 264 | + ((Ptr & 0x7FFFFFFFFFFFULL) >> ASAN_SHADOW_SCALE); |
| 265 | + } else { // Host/Shared USM |
| 266 | + return ShadowBegin + (Ptr >> ASAN_SHADOW_SCALE); |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +} // namespace ur_sanitizer_layer |
0 commit comments