From e4d4a356744f29a3f9d650bb4e915435dc0d411a Mon Sep 17 00:00:00 2001 From: wheremyfoodat <44909372+wheremyfoodat@users.noreply.github.com> Date: Tue, 23 Jul 2024 04:11:12 +0300 Subject: [PATCH] Renderer GL: Add UB checks --- include/PICA/gpu.hpp | 3 ++- src/core/renderer_gl/renderer_gl.cpp | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/include/PICA/gpu.hpp b/include/PICA/gpu.hpp index c4c8db5c1..ac2a49e6f 100644 --- a/include/PICA/gpu.hpp +++ b/include/PICA/gpu.hpp @@ -167,7 +167,8 @@ class GPU { u32 index = paddr - PhysicalAddrs::VRAM; return (T*)&vram[index]; } else [[unlikely]] { - Helpers::panic("[GPU] Tried to access unknown physical address: %08X", paddr); + Helpers::warn("[GPU] Tried to access unknown physical address: %08X", paddr); + return nullptr; } } diff --git a/src/core/renderer_gl/renderer_gl.cpp b/src/core/renderer_gl/renderer_gl.cpp index f26158aed..8b614d2db 100644 --- a/src/core/renderer_gl/renderer_gl.cpp +++ b/src/core/renderer_gl/renderer_gl.cpp @@ -659,7 +659,15 @@ OpenGL::Texture RendererGL::getTexture(Texture& tex) { if (buffer.has_value()) { return buffer.value().get().texture; } else { - const auto textureData = std::span{gpu.getPointerPhys(tex.location), tex.sizeInBytes()}; // Get pointer to the texture data in 3DS memory + const u8* startPointer = gpu.getPointerPhys(tex.location); + const usize sizeInBytes = tex.sizeInBytes(); + + if (startPointer == nullptr || (sizeInBytes > 0 && gpu.getPointerPhys(tex.location + sizeInBytes - 1) == nullptr)) [[unlikely]] { + Helpers::warn("Out-of-bounds texture fetch"); + return blankTexture; + } + + const auto textureData = std::span{startPointer, tex.sizeInBytes()}; // Get pointer to the texture data in 3DS memory Texture& newTex = textureCache.add(tex); newTex.decodeTexture(textureData); @@ -770,7 +778,8 @@ void RendererGL::textureCopy(u32 inputAddr, u32 outputAddr, u32 totalBytes, u32 if (inputWidth != 0) [[likely]] { copyHeight = (copySize / inputWidth) * 8; } else { - copyHeight = 0; + Helpers::warn("Zero-width texture copy"); + return; } // Find the source surface.