Skip to content

Commit

Permalink
rsx: Don't accept garbage shader input
Browse files Browse the repository at this point in the history
  • Loading branch information
kd-11 committed Feb 7, 2023
1 parent 0d2714a commit 9a35684
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
33 changes: 27 additions & 6 deletions rpcs3/Emu/RSX/Program/FragmentProgramDecompiler.cpp
Expand Up @@ -270,11 +270,6 @@ std::string FragmentProgramDecompiler::AddTex()
return m_parr.AddParam(PF_PARAM_UNIFORM, sampler, std::string("tex") + std::to_string(dst.tex_num));
}

std::string FragmentProgramDecompiler::AddType3()
{
return m_parr.AddParam(PF_PARAM_NONE, getFloatTypeName(4), "src3", getFloatTypeName(4) + "(1.)");
}

std::string FragmentProgramDecompiler::AddX2d()
{
return m_parr.AddParam(PF_PARAM_NONE, getFloatTypeName(4), "x2d", getFloatTypeName(4) + "(0.)");
Expand Down Expand Up @@ -562,6 +557,12 @@ template<typename T> std::string FragmentProgramDecompiler::GetSRC(T src)
const std::string reg_var = (register_id < std::size(reg_table))? reg_table[register_id] : "unk";
bool insert = true;

if (reg_var == "unk")
{
m_is_valid_ucode = false;
insert = false;
}

if (src2.use_index_reg && m_loop_count)
{
// Dynamically load the input
Expand Down Expand Up @@ -704,7 +705,10 @@ template<typename T> std::string FragmentProgramDecompiler::GetSRC(T src)
rsx_log.error("Src type 3 used, opcode=0x%X, dst=0x%X s0=0x%X s1=0x%X s2=0x%X",
dst.opcode, dst.HEX, src0.HEX, src1.HEX, src2.HEX);

ret += AddType3();
// This is not some special type, it is a bug indicating memory corruption
// Shaders that are even slightly off do not execute on realhw to any meaningful degree
m_is_valid_ucode = false;
ret += "src3";
precision_modifier = RSX_FP_PRECISION_REAL;
break;

Expand Down Expand Up @@ -801,6 +805,22 @@ std::string FragmentProgramDecompiler::BuildCode()
}

std::stringstream OS;

if (!m_is_valid_ucode)
{
// If the code is broken, do not compile. Simply NOP main and write empty outputs
insertHeader(OS);
OS << "\n";
OS << "void main()\n";
OS << "{\n";
OS << "#if 0\n";
OS << main << "\n";
OS << "#endif\n";
OS << " discard;\n";
OS << "}\n";
return OS.str();
}

insertHeader(OS);
OS << "\n";
insertConstants(OS);
Expand Down Expand Up @@ -1206,6 +1226,7 @@ std::string FragmentProgramDecompiler::Decompile()
m_location = 0;
m_loop_count = 0;
m_code_level = 1;
m_is_valid_ucode = true;

enum
{
Expand Down
9 changes: 4 additions & 5 deletions rpcs3/Emu/RSX/Program/FragmentProgramDecompiler.h
Expand Up @@ -155,6 +155,8 @@ class FragmentProgramDecompiler
std::vector<u32> m_end_offsets;
std::vector<u32> m_else_offsets;

bool m_is_valid_ucode = true;

std::array<temp_register, 64> temp_registers;

std::string GetMask() const;
Expand All @@ -169,13 +171,10 @@ class FragmentProgramDecompiler
void AddFlowOp(const std::string& code);
std::string Format(const std::string& code, bool ignore_redirects = false);

//Technically a temporary workaround until we know what type3 is
std::string AddType3();

//Support the transform-2d temp result for use with TEXBEM
// Support the transform-2d temp result for use with TEXBEM
std::string AddX2d();

//Prevents operations from overflowing the desired range (tested with fp_dynamic3 autotest sample, DS2 for src1.input_prec_mod)
// Prevents operations from overflowing the desired range (tested with fp_dynamic3 autotest sample, DS2 for src1.input_prec_mod)
std::string ClampValue(const std::string& code, u32 precision);

/**
Expand Down

0 comments on commit 9a35684

Please sign in to comment.