mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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 <encoding> instruction" first, as DecodeRdnaVop3 and DecodeRdnaVop3p already do. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01R5WJS4ZdQynAJesCZKH5hb
This commit is contained in:
@@ -625,3 +625,16 @@ if(BUILD_TESTING)
|
||||
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libc>")
|
||||
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:$<TARGET_FILE_DIR:libc>")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "RdnaDecoder/RdnaInstructionDecoder.hpp"
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <span>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
using namespace ShaderRecompiler;
|
||||
|
||||
int main() {
|
||||
struct Case {
|
||||
const char* name;
|
||||
std::uint32_t word;
|
||||
};
|
||||
const std::array<Case, 6> 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<std::uint32_t, 2> words{entry.word, 0xaf00e400u};
|
||||
const std::string expected = std::string("truncated ") + entry.name + " instruction";
|
||||
try {
|
||||
static_cast<void>(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;
|
||||
}
|
||||
@@ -823,6 +823,9 @@ void validateVop1Sdwa(const RdnaInstruction& instruction, std::uint32_t destinat
|
||||
}
|
||||
|
||||
void decodeVop1Sdwa(std::uint32_t programCounter, std::span<const std::uint32_t> code, std::uint32_t wordIndex, std::uint32_t vdst, RdnaInstruction& instruction) {
|
||||
if (static_cast<std::size_t>(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<const std::uint32_t> code, std::uint32_t wordIndex, std::uint32_t vdst, RdnaInstruction& instruction) {
|
||||
if (static_cast<std::size_t>(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<const std::uint32_t> code, std::uint32_t
|
||||
}
|
||||
|
||||
void decodeVop2Sdwa(std::uint32_t programCounter, std::span<const std::uint32_t> code, std::uint32_t wordIndex, std::uint32_t opcode, std::uint32_t vdst, std::uint32_t vsrc1, RdnaInstruction& instruction) {
|
||||
if (static_cast<std::size_t>(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<const std::uint32_t>
|
||||
}
|
||||
|
||||
void decodeVop2Dpp(std::uint32_t programCounter, std::span<const std::uint32_t> code, std::uint32_t wordIndex, std::uint32_t opcode, std::uint32_t vdst, std::uint32_t vsrc1, RdnaInstruction& instruction) {
|
||||
if (static_cast<std::size_t>(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<const std::uint32_t> code, std::uint32_t wordIndex, std::uint32_t vsrc1, RdnaInstruction& instruction) {
|
||||
if (static_cast<std::size_t>(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<const std::uint32_t>
|
||||
}
|
||||
|
||||
void decodeVopcDpp(std::uint32_t programCounter, std::span<const std::uint32_t> code, std::uint32_t wordIndex, std::uint32_t opcode, std::uint32_t vsrc1, RdnaInstruction& instruction) {
|
||||
if (static_cast<std::size_t>(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);
|
||||
|
||||
Reference in New Issue
Block a user