From fc68a04b0bba520b7d26828835e38b46358eb465 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Thu, 1 Oct 2026 14:30:23 +0200 Subject: [PATCH] fix(shader): reject SDWA and DPP instructions without their modifier word The VOP1, VOP2 and VOPC SDWA and DPP decoders read the modifier word at wordIndex + 1 before SetRdnaRawWords checked that it exists, so a code span ending at such an instruction was read one word past its end. AddressSanitizer reports a heap-buffer-overflow in decodeVop2Sdwa on a synthetic program. The six decoders now throw "truncated instruction" first, as DecodeRdnaVop3 and DecodeRdnaVop3p already do. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01R5WJS4ZdQynAJesCZKH5hb --- core/libs/prx/libSceAgcDriver/CMakeLists.txt | 13 ++++++ .../tests/RdnaTruncatedModifier.cpp | 40 +++++++++++++++++++ .../RdnaDecoder/src/RdnaVectorOpDecoder.cpp | 18 +++++++++ 3 files changed, 71 insertions(+) create mode 100644 core/libs/prx/libSceAgcDriver/tests/RdnaTruncatedModifier.cpp diff --git a/core/libs/prx/libSceAgcDriver/CMakeLists.txt b/core/libs/prx/libSceAgcDriver/CMakeLists.txt index 38e55c2e2..f1903221a 100644 --- a/core/libs/prx/libSceAgcDriver/CMakeLists.txt +++ b/core/libs/prx/libSceAgcDriver/CMakeLists.txt @@ -625,3 +625,16 @@ if(BUILD_TESTING) "PATH=path_list_prepend:$") endif() endif() + +add_test_executable(agc_rdna_truncated_modifier_tests EXCLUDE_FROM_ALL tests/RdnaTruncatedModifier.cpp) +target_link_libraries(agc_rdna_truncated_modifier_tests PRIVATE shader_recompiler_agc_driver libc) +set_target_properties(agc_rdna_truncated_modifier_tests PROPERTIES CXX_EXTENSIONS OFF) +configure_windows_unwind(agc_rdna_truncated_modifier_tests) +if(BUILD_TESTING) + set_target_properties(agc_rdna_truncated_modifier_tests PROPERTIES EXCLUDE_FROM_ALL OFF) + add_test(NAME agc_rdna_truncated_modifier COMMAND agc_rdna_truncated_modifier_tests) + if(WIN32) + set_tests_properties(agc_rdna_truncated_modifier PROPERTIES ENVIRONMENT_MODIFICATION + "PATH=path_list_prepend:$") + endif() +endif() diff --git a/core/libs/prx/libSceAgcDriver/tests/RdnaTruncatedModifier.cpp b/core/libs/prx/libSceAgcDriver/tests/RdnaTruncatedModifier.cpp new file mode 100644 index 000000000..1a3a09a49 --- /dev/null +++ b/core/libs/prx/libSceAgcDriver/tests/RdnaTruncatedModifier.cpp @@ -0,0 +1,40 @@ +#include "RdnaDecoder/RdnaInstructionDecoder.hpp" +#include +#include +#include +#include +#include +#include + +using namespace ShaderRecompiler; + +int main() { + struct Case { + const char* name; + std::uint32_t word; + }; + const std::array cases{{ + {"VOP1 SDWA", 0x7e0202f9u}, + {"VOP1 DPP", 0x7e0202fau}, + {"VOP2 SDWA", 0x4a0202f9u}, + {"VOP2 DPP", 0x4a0202fau}, + {"VOPC SDWA", 0x7c0202f9u}, + {"VOPC DPP", 0x7c0202fau}, + }}; + int failures = 0; + for (const auto& entry : cases) { + const std::array words{entry.word, 0xaf00e400u}; + const std::string expected = std::string("truncated ") + entry.name + " instruction"; + try { + static_cast(RdnaInstructionDecoder{}.Decode(std::span(words).first(1))); + std::fprintf(stderr, "%s: a missing modifier word decodes\n", entry.name); + ++failures; + } catch (const std::out_of_range& error) { + if (expected != error.what()) { + std::fprintf(stderr, "%s: %s\n", entry.name, error.what()); + ++failures; + } + } + } + return failures == 0 ? 0 : 1; +} diff --git a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp index a4d6ec2bc..7f0d47592 100644 --- a/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp +++ b/core/shader/recompiler/RdnaDecoder/src/RdnaVectorOpDecoder.cpp @@ -823,6 +823,9 @@ void validateVop1Sdwa(const RdnaInstruction& instruction, std::uint32_t destinat } void decodeVop1Sdwa(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex, std::uint32_t vdst, RdnaInstruction& instruction) { + if (static_cast(wordIndex) + 1u >= code.size()) { + throw std::out_of_range("truncated VOP1 SDWA instruction"); + } const auto modifier = code[wordIndex + 1u]; const auto src0 = modifier & 0xffu; const auto dstSel = (modifier >> 8u) & 0x7u; @@ -867,6 +870,9 @@ void applyDppModifier(RdnaOperand& operand, std::uint32_t modifier) { } void decodeVop1Dpp(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex, std::uint32_t vdst, RdnaInstruction& instruction) { + if (static_cast(wordIndex) + 1u >= code.size()) { + throw std::out_of_range("truncated VOP1 DPP instruction"); + } const auto modifier = code[wordIndex + 1u]; const auto src0 = modifier & 0xffu; SetRdnaRawWords(instruction, code, wordIndex, 2); @@ -1030,6 +1036,9 @@ void finalizeVop2Instruction(std::span code, std::uint32_t } void decodeVop2Sdwa(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex, std::uint32_t opcode, std::uint32_t vdst, std::uint32_t vsrc1, RdnaInstruction& instruction) { + if (static_cast(wordIndex) + 1u >= code.size()) { + throw std::out_of_range("truncated VOP2 SDWA instruction"); + } const auto modifier = code[wordIndex + 1u]; const auto fields = decodeVop2SdwaFields(modifier); SetRdnaRawWords(instruction, code, wordIndex, 2); @@ -1055,6 +1064,9 @@ void decodeVop2Sdwa(std::uint32_t programCounter, std::span } void decodeVop2Dpp(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex, std::uint32_t opcode, std::uint32_t vdst, std::uint32_t vsrc1, RdnaInstruction& instruction) { + if (static_cast(wordIndex) + 1u >= code.size()) { + throw std::out_of_range("truncated VOP2 DPP instruction"); + } const auto modifier = code[wordIndex + 1u]; const auto src0 = modifier & 0xffu; SetRdnaRawWords(instruction, code, wordIndex, 2); @@ -1166,6 +1178,9 @@ bool isVopcCompareExec(RdnaOpcode opcode) { } void decodeVopcSdwa(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex, std::uint32_t vsrc1, RdnaInstruction& instruction) { + if (static_cast(wordIndex) + 1u >= code.size()) { + throw std::out_of_range("truncated VOPC SDWA instruction"); + } const auto modifier = code[wordIndex + 1u]; const auto fields = decodeVopcSdwaFields(modifier); SetRdnaRawWords(instruction, code, wordIndex, 2); @@ -1195,6 +1210,9 @@ void decodeVopcSdwa(std::uint32_t programCounter, std::span } void decodeVopcDpp(std::uint32_t programCounter, std::span code, std::uint32_t wordIndex, std::uint32_t opcode, std::uint32_t vsrc1, RdnaInstruction& instruction) { + if (static_cast(wordIndex) + 1u >= code.size()) { + throw std::out_of_range("truncated VOPC DPP instruction"); + } const auto modifier = code[wordIndex + 1u]; const auto src0 = modifier & 0xffu; SetRdnaRawWords(instruction, code, wordIndex, 2);