Avoid casting void* to uint64_t.

- Add new wrapper that accepts const void *.

Change-Id: I1cea088b563cf704b6673844b533ce44f388e5fc
Signed-off-by: Michal Mrozek <michal.mrozek@intel.com>
This commit is contained in:
Michal Mrozek
2019-12-03 10:21:33 +01:00
committed by sys_ocldev
parent ee544e2515
commit 83c0f80431
5 changed files with 31 additions and 2 deletions

View File

@ -79,3 +79,7 @@ inline void patchIncrement(void *memoryToBePatched, uint32_t patchSize, uint64_t
inline uint64_t castToUint64(void *address) {
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(address));
}
inline uint64_t castToUint64(const void *address) {
return castToUint64(const_cast<void *>(address));
}

View File

@ -362,7 +362,7 @@ bool Wddm::makeResident(const D3DKMT_HANDLE *handles, uint32_t count, bool cantT
bool Wddm::mapGpuVirtualAddress(AllocationStorageData *allocationStorageData) {
return mapGpuVirtualAddress(allocationStorageData->osHandleStorage->gmm,
allocationStorageData->osHandleStorage->handle,
0u, MemoryConstants::maxSvmAddress, reinterpret_cast<D3DGPU_VIRTUAL_ADDRESS>(allocationStorageData->cpuPtr),
0u, MemoryConstants::maxSvmAddress, castToUint64(allocationStorageData->cpuPtr),
allocationStorageData->osHandleStorage->gpuPtr);
}

View File

@ -499,7 +499,7 @@ bool WddmMemoryManager::createWddmAllocation(WddmAllocation *allocation, void *r
}
bool WddmMemoryManager::mapGpuVaForOneHandleAllocation(WddmAllocation *allocation, const void *preferredGpuVirtualAddress) {
D3DGPU_VIRTUAL_ADDRESS addressToMap = castToUint64(const_cast<void *>(preferredGpuVirtualAddress));
D3DGPU_VIRTUAL_ADDRESS addressToMap = castToUint64(preferredGpuVirtualAddress);
auto heapIndex = selectHeap(allocation, preferredGpuVirtualAddress != nullptr, is32bit || executionEnvironment.isFullRangeSvm());
if (!executionEnvironment.isFullRangeSvm()) {
addressToMap = 0u;

View File

@ -45,6 +45,13 @@ TEST(PtrMath, givenCastToUint64FunctionWhenItIsCalledThenProperValueIsReturned)
auto uintAddress = castToUint64(addressWithTrailingBitSet);
EXPECT_EQ(uintAddress, expectedUint64Address);
}
TEST(PtrMath, givenCastToUint64FunctionWhenConstPointerIsPassedItIsCalledThenProperValueIsReturned) {
uintptr_t address = 0xf0000000;
const void *addressWithTrailingBitSet = reinterpret_cast<const void *>(address);
uint64_t expectedUint64Address = 0xf0000000;
auto uintAddress = castToUint64(addressWithTrailingBitSet);
EXPECT_EQ(uintAddress, expectedUint64Address);
}
TEST(ptrOffset, preserve64Bit) {
uint64_t ptrBefore = 0x800000000;

View File

@ -615,6 +615,24 @@ TEST_F(WddmCommandStreamTest, givenHostPtrAllocationWhenMapFailsThenFragmentsAre
EXPECT_EQ(nullptr, gfxAllocation);
}
TEST_F(WddmCommandStreamTest, givenAddressWithHighestBitSetWhenItIsMappedThenProperAddressIsPassed) {
uintptr_t address = 0xffff0000;
void *faultyAddress = reinterpret_cast<void *>(address);
wddm->mapGpuVirtualAddressResult.called = 0u;
auto gfxAllocation = memoryManager->allocateGraphicsMemoryWithProperties(MockAllocationProperties{false, MemoryConstants::pageSize}, faultyAddress);
EXPECT_EQ(1u, wddm->mapGpuVirtualAddressResult.called);
ASSERT_NE(nullptr, gfxAllocation);
auto expectedAddress = castToUint64(faultyAddress);
EXPECT_EQ(gfxAllocation->getGpuAddress(), expectedAddress);
ASSERT_EQ(gfxAllocation->fragmentsStorage.fragmentCount, 1u);
EXPECT_EQ(expectedAddress, gfxAllocation->fragmentsStorage.fragmentStorageData[0].osHandleStorage->gpuPtr);
memoryManager->freeGraphicsMemory(gfxAllocation);
}
TEST_F(WddmCommandStreamTest, givenHostPtrWhenPtrBelowRestrictionThenCreateAllocationAndMakeResident) {
void *hostPtr = reinterpret_cast<void *>(memoryManager->getAlignedMallocRestrictions()->minAddress - 0x1000);
auto size = 0x2000u;