Skip to content

Commit 5bae27a

Browse files
committed
Fix a bug in Graphics Allocations constructors.
- There was a wrong cast in Graphics Allocation constructor resulting in wrong GPU address generation in some sporadic scenarios. - Problem appears in 32 bit applications where void* address is cast to uint64_t value, if c style cast is used it makes trailing bit to be populated to higher bits constructing wrong value 0xf000000 is being casted to 0xfffffffff0000000 while it should be casted to 0x00000000f0000000 - added special cast function for further use. Change-Id: I56d53a8e13e17cbacd127566442eea3f6a089977
1 parent d900bdf commit 5bae27a

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

runtime/helpers/ptr_math.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -61,3 +61,7 @@ inline void patchWithRequiredSize(void *memoryToBePatched, uint32_t patchSize, u
6161
*curbeAddress = (uint32_t)patchValue;
6262
}
6363
}
64+
65+
inline uint64_t castToUint64(void *address) {
66+
return static_cast<uint64_t>(reinterpret_cast<uintptr_t>(address));
67+
}

runtime/memory_manager/graphics_allocation.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <vector>
2727

2828
#include "runtime/helpers/debug_helpers.h"
29+
#include "runtime/helpers/ptr_math.h"
2930
#include "runtime/memory_manager/host_ptr_defines.h"
3031
#include "runtime/utilities/idlist.h"
3132

@@ -69,7 +70,7 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
6970
GraphicsAllocation(const GraphicsAllocation &) = delete;
7071
GraphicsAllocation(void *cpuPtrIn, size_t sizeIn) : size(sizeIn),
7172
cpuPtr(cpuPtrIn),
72-
gpuAddress((uint64_t)cpuPtrIn),
73+
gpuAddress(castToUint64(cpuPtrIn)),
7374
sharedHandle(Sharing::nonSharedResource),
7475

7576
allocationType(ALLOCATION_TYPE_UNKNOWN) {}
@@ -83,7 +84,7 @@ class GraphicsAllocation : public IDNode<GraphicsAllocation> {
8384

8485
GraphicsAllocation(void *cpuPtrIn, size_t sizeIn, osHandle sharedHandleIn) : size(sizeIn),
8586
cpuPtr(cpuPtrIn),
86-
gpuAddress((uint64_t)cpuPtrIn),
87+
gpuAddress(castToUint64(cpuPtrIn)),
8788
sharedHandle(sharedHandleIn),
8889

8990
allocationType(ALLOCATION_TYPE_UNKNOWN) {}

unit_tests/helpers/ptr_math_tests.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, Intel Corporation
2+
* Copyright (c) 2017 - 2018, Intel Corporation
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a
55
* copy of this software and associated documentation files (the "Software"),
@@ -51,3 +51,11 @@ TEST(PtrMath, addrToPtr) {
5151
EXPECT_EQ(ptr32BitAddr, addrToPtr(addr32Bit));
5252
EXPECT_EQ(ptr64BitAddr, addrToPtr(addr64Bit));
5353
}
54+
55+
TEST(PtrMath, givenCastToUint64FunctionWhenItIsCalledThenProperValueIsReturned) {
56+
uintptr_t address = 0xf0000000;
57+
void *addressWithTrailingBitSet = reinterpret_cast<void *>(address);
58+
uint64_t expectedUint64Address = 0xf0000000;
59+
auto uintAddress = castToUint64(addressWithTrailingBitSet);
60+
EXPECT_EQ(uintAddress, expectedUint64Address);
61+
}

unit_tests/memory_manager/memory_manager_tests.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,3 +1656,20 @@ TEST_F(MockAlignMallocMemoryManagerTest, givenMemoryManagerWitNonZeroAlignRestri
16561656
uintptr_t memVal = reinterpret_cast<uintptr_t>(alignedMemoryManager->allocateSystemMemory(0x1000, 0x1000));
16571657
EXPECT_EQ(expectedVal, memVal);
16581658
}
1659+
1660+
TEST(GraphicsAllocation, givenCpuPointerBasedConstructorWhenGraphicsAllocationIsCreatedThenGpuAddressHasCorrectValue) {
1661+
uintptr_t address = 0xf0000000;
1662+
void *addressWithTrailingBitSet = reinterpret_cast<void *>(address);
1663+
uint64_t expectedGpuAddress = 0xf0000000;
1664+
GraphicsAllocation graphicsAllocation(addressWithTrailingBitSet, 1u);
1665+
EXPECT_EQ(expectedGpuAddress, graphicsAllocation.getGpuAddress());
1666+
}
1667+
1668+
TEST(GraphicsAllocation, givenSharedHandleBasedConstructorWhenGraphicsAllocationIsCreatedThenGpuAddressHasCorrectValue) {
1669+
uintptr_t address = 0xf0000000;
1670+
void *addressWithTrailingBitSet = reinterpret_cast<void *>(address);
1671+
uint64_t expectedGpuAddress = 0xf0000000;
1672+
osHandle sharedHandle{};
1673+
GraphicsAllocation graphicsAllocation(addressWithTrailingBitSet, 1u, sharedHandle);
1674+
EXPECT_EQ(expectedGpuAddress, graphicsAllocation.getGpuAddress());
1675+
}

0 commit comments

Comments
 (0)