From 4c59443ed21e1236e3441d4901d6129b43b3a9cd Mon Sep 17 00:00:00 2001 From: Steveice10 <1269164+Steveice10@users.noreply.github.com> Date: Sun, 15 Oct 2023 14:08:29 -0700 Subject: [PATCH] common: Add more robust ZSTD handling. (#7071) --- src/common/zstd_compression.cpp | 36 +++++++++++++++---- .../renderer_opengl/gl_shader_disk_cache.cpp | 4 +++ src/video_core/shader/generator/shader_gen.h | 3 +- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/common/zstd_compression.cpp b/src/common/zstd_compression.cpp index d77863fbf..43199e52f 100644 --- a/src/common/zstd_compression.cpp +++ b/src/common/zstd_compression.cpp @@ -11,20 +11,25 @@ namespace Common::Compression { std::vector CompressDataZSTD(std::span source, s32 compression_level) { compression_level = std::clamp(compression_level, ZSTD_minCLevel(), ZSTD_maxCLevel()); - const std::size_t max_compressed_size = ZSTD_compressBound(source.size()); - std::vector compressed(max_compressed_size); + if (ZSTD_isError(max_compressed_size)) { + LOG_ERROR(Common, "Error determining ZSTD maximum compressed size: {} ({})", + ZSTD_getErrorName(max_compressed_size), max_compressed_size); + return {}; + } + + std::vector compressed(max_compressed_size); const std::size_t compressed_size = ZSTD_compress( compressed.data(), compressed.size(), source.data(), source.size(), compression_level); if (ZSTD_isError(compressed_size)) { - // Compression failed + LOG_ERROR(Common, "Error compressing ZSTD data: {} ({})", + ZSTD_getErrorName(compressed_size), compressed_size); return {}; } compressed.resize(compressed_size); - return compressed; } @@ -35,15 +40,32 @@ std::vector CompressDataZSTDDefault(std::span source) { std::vector DecompressDataZSTD(std::span compressed) { const std::size_t decompressed_size = ZSTD_getFrameContentSize(compressed.data(), compressed.size()); - std::vector decompressed(decompressed_size); + if (decompressed_size == ZSTD_CONTENTSIZE_UNKNOWN) { + LOG_ERROR(Common, "ZSTD decompressed size could not be determined."); + return {}; + } + if (decompressed_size == ZSTD_CONTENTSIZE_ERROR || ZSTD_isError(decompressed_size)) { + LOG_ERROR(Common, "Error determining ZSTD decompressed size: {} ({})", + ZSTD_getErrorName(decompressed_size), decompressed_size); + return {}; + } + + std::vector decompressed(decompressed_size); const std::size_t uncompressed_result_size = ZSTD_decompress( decompressed.data(), decompressed.size(), compressed.data(), compressed.size()); - if (decompressed_size != uncompressed_result_size || ZSTD_isError(uncompressed_result_size)) { - // Decompression failed + if (decompressed_size != uncompressed_result_size) { + LOG_ERROR(Common, "ZSTD decompression expected {} bytes, got {}", decompressed_size, + uncompressed_result_size); return {}; } + if (ZSTD_isError(uncompressed_result_size)) { + LOG_ERROR(Common, "Error decompressing ZSTD data: {} ({})", + ZSTD_getErrorName(uncompressed_result_size), uncompressed_result_size); + return {}; + } + return decompressed; } diff --git a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp index bbc9cf578..9f2ed2bab 100644 --- a/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp +++ b/src/video_core/renderer_opengl/gl_shader_disk_cache.cpp @@ -206,6 +206,10 @@ ShaderDiskCache::LoadPrecompiledFile(FileUtil::IOFile& file, bool compressed) { if (compressed) { const std::vector decompressed = Common::Compression::DecompressDataZSTD(precompiled_file); + if (decompressed.empty()) { + LOG_ERROR(Render_OpenGL, "Could not decompress precompiled shader cache."); + return std::nullopt; + } SaveArrayToPrecompiled(decompressed.data(), decompressed.size()); } else { SaveArrayToPrecompiled(precompiled_file.data(), precompiled_file.size()); diff --git a/src/video_core/shader/generator/shader_gen.h b/src/video_core/shader/generator/shader_gen.h index 3dfbe09e2..4a8ffc656 100644 --- a/src/video_core/shader/generator/shader_gen.h +++ b/src/video_core/shader/generator/shader_gen.h @@ -10,10 +10,11 @@ namespace Pica::Shader::Generator { +// NOTE: Changing the order impacts shader transferable and precompiled cache loading. enum ProgramType : u32 { VS = 0, - GS = 2, FS = 1, + GS = 2, }; enum Attributes {