summaryrefslogtreecommitdiff
path: root/Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp
diff options
context:
space:
mode:
authorPokechu22 <Pokechu022@gmail.com>2024-04-01 21:37:11 -0700
committerPokechu22 <Pokechu022@gmail.com>2024-04-04 12:50:34 -0700
commita3951dc2d72ebb6f240d592fdbb348137130ce49 (patch)
tree6d951691062906543487fa1b4400c75e9596e8d0 /Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp
parentdb8b2942aa42b441e6d7f70e27d53fd9c8338c63 (diff)
Fix out of bounds accesses for invalid vertex component formats
On all platforms, this would result in out of bounds accesses when getting the component sizes (which uses stuff from VertexLoader_Position.h/VertexLoader_TextCoord.h/VertexLoader_Normal.h). On platforms other than x64 and ARM64, this would also be out of bounds accesses when getting function pointers for the non-JIT vertex loader (in VertexLoader_Position.cpp etc.). Usually both of these would get data from other entries in the same multi-dimensional array, but the last few entries would be truly out of bounds. This does mean that an out of bounds function pointer can be called on platforms that don't have a JIT vertex loader, but it is limited to invalid component formats with values 5/6/7 due to the size of the bitfield the formats come from, so it seems unlikely that this could be exploited in practice. This issue affects a few games; Def Jam: Fight for New York (https://bugs.dolphin-emu.org/issues/12719) and Fifa Street are known to be affected. I have not done any hardware testing for this PR specifically, though I *think* I previously determined that at least a value of 5 behaves the same as float (4). That's what I implemented in any case. I did previously determine that both Def Jam: Fight for New York and Fifa Street use an invalid normal format, but don't actually have lighting enabled when that normal vector is used, so it doesn't change rendering in practice. The color component format also has two invalid values, but VertexLoader_Color.h/.cpp do check for those invalid ones and return a default value instead of doing an out of bounds access.
Diffstat (limited to 'Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp')
-rw-r--r--Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp64
1 files changed, 39 insertions, 25 deletions
diff --git a/Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp b/Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp
index 65b63b32b9..8fb49f0afd 100644
--- a/Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp
+++ b/Source/Core/VideoBackends/Vulkan/VKVertexFormat.cpp
@@ -19,31 +19,45 @@ static VkFormat VarToVkFormat(ComponentFormat t, uint32_t components, bool integ
using ComponentArray = std::array<VkFormat, 4>;
static constexpr auto f = [](ComponentArray a) { return a; }; // Deduction helper
- static constexpr Common::EnumMap<ComponentArray, ComponentFormat::Float> float_type_lookup = {
- f({VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8B8_UNORM,
- VK_FORMAT_R8G8B8A8_UNORM}), // UByte
- f({VK_FORMAT_R8_SNORM, VK_FORMAT_R8G8_SNORM, VK_FORMAT_R8G8B8_SNORM,
- VK_FORMAT_R8G8B8A8_SNORM}), // Byte
- f({VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM, VK_FORMAT_R16G16B16_UNORM,
- VK_FORMAT_R16G16B16A16_UNORM}), // UShort
- f({VK_FORMAT_R16_SNORM, VK_FORMAT_R16G16_SNORM, VK_FORMAT_R16G16B16_SNORM,
- VK_FORMAT_R16G16B16A16_SNORM}), // Short
- f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
- VK_FORMAT_R32G32B32A32_SFLOAT}), // Float
- };
-
- static constexpr Common::EnumMap<ComponentArray, ComponentFormat::Float> integer_type_lookup = {
- f({VK_FORMAT_R8_UINT, VK_FORMAT_R8G8_UINT, VK_FORMAT_R8G8B8_UINT,
- VK_FORMAT_R8G8B8A8_UINT}), // UByte
- f({VK_FORMAT_R8_SINT, VK_FORMAT_R8G8_SINT, VK_FORMAT_R8G8B8_SINT,
- VK_FORMAT_R8G8B8A8_SINT}), // Byte
- f({VK_FORMAT_R16_UINT, VK_FORMAT_R16G16_UINT, VK_FORMAT_R16G16B16_UINT,
- VK_FORMAT_R16G16B16A16_UINT}), // UShort
- f({VK_FORMAT_R16_SINT, VK_FORMAT_R16G16_SINT, VK_FORMAT_R16G16B16_SINT,
- VK_FORMAT_R16G16B16A16_SINT}), // Short
- f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
- VK_FORMAT_R32G32B32A32_SFLOAT}), // Float
- };
+ static constexpr Common::EnumMap<ComponentArray, ComponentFormat::InvalidFloat7>
+ float_type_lookup = {
+ f({VK_FORMAT_R8_UNORM, VK_FORMAT_R8G8_UNORM, VK_FORMAT_R8G8B8_UNORM,
+ VK_FORMAT_R8G8B8A8_UNORM}), // UByte
+ f({VK_FORMAT_R8_SNORM, VK_FORMAT_R8G8_SNORM, VK_FORMAT_R8G8B8_SNORM,
+ VK_FORMAT_R8G8B8A8_SNORM}), // Byte
+ f({VK_FORMAT_R16_UNORM, VK_FORMAT_R16G16_UNORM, VK_FORMAT_R16G16B16_UNORM,
+ VK_FORMAT_R16G16B16A16_UNORM}), // UShort
+ f({VK_FORMAT_R16_SNORM, VK_FORMAT_R16G16_SNORM, VK_FORMAT_R16G16B16_SNORM,
+ VK_FORMAT_R16G16B16A16_SNORM}), // Short
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Float
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid
+ };
+
+ static constexpr Common::EnumMap<ComponentArray, ComponentFormat::InvalidFloat7>
+ integer_type_lookup = {
+ f({VK_FORMAT_R8_UINT, VK_FORMAT_R8G8_UINT, VK_FORMAT_R8G8B8_UINT,
+ VK_FORMAT_R8G8B8A8_UINT}), // UByte
+ f({VK_FORMAT_R8_SINT, VK_FORMAT_R8G8_SINT, VK_FORMAT_R8G8B8_SINT,
+ VK_FORMAT_R8G8B8A8_SINT}), // Byte
+ f({VK_FORMAT_R16_UINT, VK_FORMAT_R16G16_UINT, VK_FORMAT_R16G16B16_UINT,
+ VK_FORMAT_R16G16B16A16_UINT}), // UShort
+ f({VK_FORMAT_R16_SINT, VK_FORMAT_R16G16_SINT, VK_FORMAT_R16G16B16_SINT,
+ VK_FORMAT_R16G16B16A16_SINT}), // Short
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Float
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid
+ f({VK_FORMAT_R32_SFLOAT, VK_FORMAT_R32G32_SFLOAT, VK_FORMAT_R32G32B32_SFLOAT,
+ VK_FORMAT_R32G32B32A32_SFLOAT}), // Invalid
+ };
ASSERT(components > 0 && components <= 4);
return integer ? integer_type_lookup[t][components - 1] : float_type_lookup[t][components - 1];