From dd79da89fc4ddf020bb6f8d8c8a733429249bab3 Mon Sep 17 00:00:00 2001 From: gameblabla Date: Sun, 22 Aug 2021 21:39:05 +0000 Subject: [PATCH] Properly protect the HLE instructions against corrupted memory. (#189) Fix is from PCSX-redux : https://github.com/grumpycoders/pcsx-redux/commit/99c9508f2a9dc1444b88f37eb100cdfb17862b52 This should hopefully fix HDHOSHY's experimental patch properly. Co-authored-by: Nicolas Noble --- libpcsxcore/new_dynarec/emu_if.h | 2 -- libpcsxcore/new_dynarec/new_dynarec.c | 7 ++++++- libpcsxcore/psxhle.c | 2 +- libpcsxcore/psxhle.h | 2 +- libpcsxcore/psxinterpreter.c | 8 +++++++- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libpcsxcore/new_dynarec/emu_if.h b/libpcsxcore/new_dynarec/emu_if.h index 3980490a..17abab0b 100644 --- a/libpcsxcore/new_dynarec/emu_if.h +++ b/libpcsxcore/new_dynarec/emu_if.h @@ -97,8 +97,6 @@ void pcsx_mtc0(u32 reg, u32 val); void pcsx_mtc0_ds(u32 reg, u32 val); /* misc */ -extern void (*psxHLEt[])(); - extern void SysPrintf(const char *fmt, ...); #ifdef RAM_FIXED diff --git a/libpcsxcore/new_dynarec/new_dynarec.c b/libpcsxcore/new_dynarec/new_dynarec.c index cd63d2bf..c3c470dd 100644 --- a/libpcsxcore/new_dynarec/new_dynarec.c +++ b/libpcsxcore/new_dynarec/new_dynarec.c @@ -35,6 +35,7 @@ static int sceBlock; #endif #include "new_dynarec_config.h" +#include "../psxhle.h" //emulator interface #include "emu_if.h" //emulator interface //#define DISASM @@ -3445,7 +3446,11 @@ void hlecall_assemble(int i,struct regstat *i_regs) assert(!is_delayslot); (void)ccreg; emit_movimm(start+i*4+4,0); // Get PC - emit_movimm((int)psxHLEt[source[i]&7],1); + uint32_t hleCode = source[i] & 0x03ffffff; + if (hleCode >= (sizeof(psxHLEt) / sizeof(psxHLEt[0]))) + emit_movimm((int)psxNULL,1); + else + emit_movimm((int)psxHLEt[hleCode],1); emit_addimm(HOST_CCREG,CLOCK_ADJUST(ccadj[i]),HOST_CCREG); // XXX emit_jmp((int)jump_hlecall); } diff --git a/libpcsxcore/psxhle.c b/libpcsxcore/psxhle.c index 83362ecd..52227a40 100644 --- a/libpcsxcore/psxhle.c +++ b/libpcsxcore/psxhle.c @@ -89,7 +89,7 @@ static void hleExecRet() { psxRegs.pc = psxRegs.GPR.n.ra; } -void (*psxHLEt[256])() = { +const void (*psxHLEt[8])() = { hleDummy, hleA0, hleB0, hleC0, hleBootstrap, hleExecRet, hleDummy, hleDummy diff --git a/libpcsxcore/psxhle.h b/libpcsxcore/psxhle.h index eef78e80..0529c389 100644 --- a/libpcsxcore/psxhle.h +++ b/libpcsxcore/psxhle.h @@ -28,7 +28,7 @@ extern "C" { #include "r3000a.h" #include "plugins.h" -extern void (*psxHLEt[256])(); +extern const void (*psxHLEt[8])(); #ifdef __cplusplus } diff --git a/libpcsxcore/psxinterpreter.c b/libpcsxcore/psxinterpreter.c index cf3de798..fa454e1a 100644 --- a/libpcsxcore/psxinterpreter.c +++ b/libpcsxcore/psxinterpreter.c @@ -842,7 +842,13 @@ void psxBASIC(struct psxCP2Regs *regs) { void psxHLE() { // psxHLEt[psxRegs.code & 0xffff](); - psxHLEt[psxRegs.code & 0x07](); // HDHOSHY experimental patch +// psxHLEt[psxRegs.code & 0x07](); // HDHOSHY experimental patch + uint32_t hleCode = psxRegs.code & 0x03ffffff; + if (hleCode >= (sizeof(psxHLEt) / sizeof(psxHLEt[0]))) { + psxNULL(); + } else { + psxHLEt[hleCode](); + } } void (*psxBSC[64])() = { -- 2.39.5