Use size_t for methods returning memory sizes
By returning size_t instead of int we ensure that on 64-bit platforms
any arithmetic that uses these methods is done in 64-bit, which makes it
easy to detect whether resources would exceed the 4 GiB (or lower) limit
imposed by using 32-bit indices to perform memory accesses.
Places where we intentionally cast to 32-bit are now explicit and use
unsigned integers instead. In a subsequent change these casts will
include an assert that no information was lost.
Bug: b/200806413
Change-Id: I1cfea566444357614d1b2c2b878926e99f61b312
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/60008
Tested-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Sean Risser <srisser@google.com>
diff --git a/src/Device/Blitter.cpp b/src/Device/Blitter.cpp
index 4eab6e8..0537db8 100644
--- a/src/Device/Blitter.cpp
+++ b/src/Device/Blitter.cpp
@@ -116,10 +116,10 @@
BlitData data = {
pixel, nullptr, // source, dest
- format.bytes(), // sPitchB
- dest->rowPitchBytes(aspect, subres.mipLevel), // dPitchB
- 0, // sSliceB (unused in clear operations)
- dest->slicePitchBytes(aspect, subres.mipLevel), // dSliceB
+ static_cast<uint32_t>(format.bytes()), // sPitchB
+ static_cast<uint32_t>(dest->rowPitchBytes(aspect, subres.mipLevel)), // dPitchB
+ 0, // sSliceB (unused in clear operations)
+ static_cast<uint32_t>(dest->slicePitchBytes(aspect, subres.mipLevel)), // dSliceB
0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, // x0, y0, z0, w, h, d
@@ -1901,12 +1901,12 @@
}
BlitData data = {
- nullptr, // source
- nullptr, // dest
- src->rowPitchBytes(srcAspect, region.srcSubresource.mipLevel), // sPitchB
- dst->rowPitchBytes(dstAspect, region.dstSubresource.mipLevel), // dPitchB
- src->slicePitchBytes(srcAspect, region.srcSubresource.mipLevel), // sSliceB
- dst->slicePitchBytes(dstAspect, region.dstSubresource.mipLevel), // dSliceB
+ nullptr, // source
+ nullptr, // dest
+ static_cast<uint32_t>(src->rowPitchBytes(srcAspect, region.srcSubresource.mipLevel)), // sPitchB
+ static_cast<uint32_t>(dst->rowPitchBytes(dstAspect, region.dstSubresource.mipLevel)), // dPitchB
+ static_cast<uint32_t>(src->slicePitchBytes(srcAspect, region.srcSubresource.mipLevel)), // sSliceB
+ static_cast<uint32_t>(dst->slicePitchBytes(dstAspect, region.dstSubresource.mipLevel)), // dSliceB
x0,
y0,
@@ -2354,7 +2354,7 @@
VkExtent3D extent = image->getMipLevelExtent(aspect, subresource.mipLevel);
CubeBorderData data = {
image->getTexelPointer({ 0, 0, 0 }, posX),
- image->rowPitchBytes(aspect, subresource.mipLevel),
+ static_cast<uint32_t>(image->rowPitchBytes(aspect, subresource.mipLevel)),
static_cast<uint32_t>(image->getLayerSize(aspect)),
extent.width
};
diff --git a/src/Device/Blitter.hpp b/src/Device/Blitter.hpp
index 0a7ed00..0d91e50 100644
--- a/src/Device/Blitter.hpp
+++ b/src/Device/Blitter.hpp
@@ -104,10 +104,10 @@
{
const void *source;
void *dest;
- int sPitchB;
- int dPitchB;
- int sSliceB;
- int dSliceB;
+ uint32_t sPitchB;
+ uint32_t dPitchB;
+ uint32_t sSliceB;
+ uint32_t dSliceB;
float x0;
float y0;
@@ -133,7 +133,7 @@
struct CubeBorderData
{
void *layers;
- int pitchB;
+ uint32_t pitchB;
uint32_t layerSize;
uint32_t dim;
};
diff --git a/src/Vulkan/VkFormat.cpp b/src/Vulkan/VkFormat.cpp
index 56b331e..65ed94a 100644
--- a/src/Vulkan/VkFormat.cpp
+++ b/src/Vulkan/VkFormat.cpp
@@ -1529,7 +1529,7 @@
return false;
}
-int Format::bytes() const
+size_t Format::bytes() const
{
switch(format)
{
@@ -1742,7 +1742,7 @@
return 0;
}
-int Format::pitchB(int width, int border) const
+size_t Format::pitchB(int width, int border) const
{
// Render targets require 2x2 quads
width = sw::align<2>(width + 2 * border);
@@ -1818,7 +1818,7 @@
}
}
-int Format::sliceBUnpadded(int width, int height, int border) const
+size_t Format::sliceBUnpadded(int width, int height, int border) const
{
// Render targets require 2x2 quads
height = sw::align<2>(height + 2 * border);
@@ -1890,7 +1890,7 @@
}
}
-int Format::sliceB(int width, int height, int border) const
+size_t Format::sliceB(int width, int height, int border) const
{
return sw::align<16>(sliceBUnpadded(width, height, border) + 15);
}
diff --git a/src/Vulkan/VkFormat.hpp b/src/Vulkan/VkFormat.hpp
index 8a69d5b..dcfe40e 100644
--- a/src/Vulkan/VkFormat.hpp
+++ b/src/Vulkan/VkFormat.hpp
@@ -56,9 +56,9 @@
int componentCount() const;
bool isUnsignedComponent(int component) const;
- int bytes() const;
- int pitchB(int width, int border) const;
- int sliceB(int width, int height, int border) const;
+ size_t bytes() const;
+ size_t pitchB(int width, int border) const;
+ size_t sliceB(int width, int height, int border) const;
sw::float4 getScale() const;
@@ -78,7 +78,7 @@
private:
VkFormat compatibleFormat() const;
- int sliceBUnpadded(int width, int height, int border) const;
+ size_t sliceBUnpadded(int width, int height, int border) const;
VkFormat format = VK_FORMAT_UNDEFINED;
};
diff --git a/src/Vulkan/VkImage.cpp b/src/Vulkan/VkImage.cpp
index 1fd7d37..3f77be6 100644
--- a/src/Vulkan/VkImage.cpp
+++ b/src/Vulkan/VkImage.cpp
@@ -817,7 +817,7 @@
return mipLevelExtent;
}
-int Image::rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
+size_t Image::rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
{
if(deviceMemory && deviceMemory->hasExternalImageProperties())
{
@@ -839,7 +839,7 @@
return usedFormat.pitchB(mipLevelExtent.width, borderSize());
}
-int Image::slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
+size_t Image::slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
{
// Depth and Stencil slice should be computed separately
ASSERT((aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT)) !=
@@ -937,8 +937,7 @@
VkDeviceSize Image::getMipLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
{
- return static_cast<VkDeviceSize>(slicePitchBytes(aspect, mipLevel)) *
- getMipLevelExtent(aspect, mipLevel).depth;
+ return slicePitchBytes(aspect, mipLevel) * getMipLevelExtent(aspect, mipLevel).depth;
}
VkDeviceSize Image::getMultiSampledLevelSize(VkImageAspectFlagBits aspect, uint32_t mipLevel) const
diff --git a/src/Vulkan/VkImage.hpp b/src/Vulkan/VkImage.hpp
index 65813ab..0f059e9 100644
--- a/src/Vulkan/VkImage.hpp
+++ b/src/Vulkan/VkImage.hpp
@@ -87,8 +87,8 @@
VkSampleCountFlagBits getSampleCountFlagBits() const { return samples; }
const VkExtent3D &getExtent() const { return extent; }
VkExtent3D getMipLevelExtent(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
- int rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
- int slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
+ size_t rowPitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
+ size_t slicePitchBytes(VkImageAspectFlagBits aspect, uint32_t mipLevel) const;
void *getTexelPointer(const VkOffset3D &offset, const VkImageSubresource &subresource) const;
bool isCubeCompatible() const;
bool is3DSlice() const;