Restore zero-initialization of device memory allocations for GN builds
Current MemorySanitizer errors in Chromium prevent use from omitting
zero-initialization of Vulkan 'device' mememory allocations:
https://chromium-review.googlesource.com/c/chromium/src/+/3215252/
Bug: b/140991626
Change-Id: I9dbdf33f9014a1563d69c72c2a3eac7977195e12
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/58068
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Commit-Queue: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Vulkan/BUILD.gn b/src/Vulkan/BUILD.gn
index e20282c..f8db101 100644
--- a/src/Vulkan/BUILD.gn
+++ b/src/Vulkan/BUILD.gn
@@ -53,6 +53,7 @@
defines += [
"SWIFTSHADER_ENABLE_ASTC", # TODO(b/150130101)
+ "SWIFTSHADER_ZERO_INITIALIZE_DEVICE_MEMORY", # TODO(b/140991626)
]
}
diff --git a/src/Vulkan/VkMemory.cpp b/src/Vulkan/VkMemory.cpp
index c71e209..2789bdf 100644
--- a/src/Vulkan/VkMemory.cpp
+++ b/src/Vulkan/VkMemory.cpp
@@ -21,8 +21,13 @@
void *allocateDeviceMemory(size_t bytes, size_t alignment)
{
+ // TODO(b/140991626): Use allocateZeroOrPoison() instead of allocateZero() to detect MemorySanitizer errors.
+#if defined(SWIFTSHADER_ZERO_INITIALIZE_DEVICE_MEMORY)
+ return sw::allocateZero(bytes, alignment);
+#else
// TODO(b/140991626): Use allocateUninitialized() instead of allocateZeroOrPoison() to improve startup peformance.
return sw::allocateZeroOrPoison(bytes, alignment);
+#endif
}
void freeDeviceMemory(void *ptr)
@@ -32,14 +37,27 @@
void *allocateHostMemory(size_t bytes, size_t alignment, const VkAllocationCallbacks *pAllocator, VkSystemAllocationScope allocationScope)
{
- // TODO(b/140991626): Use allocateUninitialized() instead of allocateZeroOrPoison() to improve startup peformance.
- return pAllocator ? pAllocator->pfnAllocation(pAllocator->pUserData, bytes, alignment, allocationScope)
- : sw::allocateZeroOrPoison(bytes, alignment);
+ if(pAllocator)
+ {
+ return pAllocator->pfnAllocation(pAllocator->pUserData, bytes, alignment, allocationScope);
+ }
+ else
+ {
+ // TODO(b/140991626): Use allocateUninitialized() instead of allocateZeroOrPoison() to improve startup peformance.
+ return sw::allocateZeroOrPoison(bytes, alignment);
+ }
}
void freeHostMemory(void *ptr, const VkAllocationCallbacks *pAllocator)
{
- pAllocator ? pAllocator->pfnFree(pAllocator->pUserData, ptr) : sw::freeMemory(ptr);
+ if(pAllocator)
+ {
+ pAllocator->pfnFree(pAllocator->pUserData, ptr);
+ }
+ else
+ {
+ sw::freeMemory(ptr);
+ }
}
} // namespace vk