Core: Add utility function to properly report memory exceptions from HLE functions (when they would actually crash the PSP)

This commit is contained in:
Henrik Rydgård committed 2026-08-10 10:41:32 +02:00
1 parent 66c8bfbcb2
commit b42157aa49
6 files changed
+95 -18

No files matched your search

+60 -4
View File
@@ -602,9 +602,11 @@ const char *MemoryExceptionTypeAsString(MemoryExceptionType type) {
switch (type) {
case MemoryExceptionType::UNKNOWN: return "Unknown";
case MemoryExceptionType::READ_WORD: return "Read Word";
case MemoryExceptionType::WRITE_WORD: return "Write Word";
case MemoryExceptionType::READ_BLOCK: return "Read Block";
case MemoryExceptionType::WRITE_WORD: return "Write Word";
case MemoryExceptionType::WRITE_BLOCK: return "Read/Write Block";
case MemoryExceptionType::HLE_READ: return "HLE Read";
case MemoryExceptionType::HLE_WRITE: return "HLE Write";
case MemoryExceptionType::ALIGNMENT: return "Alignment";
default:
return "N/A";
@@ -638,14 +640,13 @@ static std::string ModuleAddressSuffix(u32 address) {
}
}
void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type, std::string_view additionalInfo, bool forceReport) {
const char *desc = MemoryExceptionTypeAsString(type);
void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type, std::string_view additionalInfo) {
// In jit, we only flush PC when bIgnoreBadMemAccess is off.
char pcDetails[128];
pcDetails[0] = 0;
if ((CPUCore)g_Config.iCpuCore == CPUCore::INTERPRETER) {
snprintf(pcDetails, sizeof(pcDetails), " PC %08x%s LR %08x%s", currentMIPS->pc, ModuleAddressSuffix(currentMIPS->pc).c_str(), currentMIPS->r[MIPS_REG_RA], ModuleAddressSuffix(currentMIPS->r[MIPS_REG_RA]).c_str());
snprintf(pcDetails, sizeof(pcDetails), " PC %08x%s RA %08x%s", currentMIPS->pc, ModuleAddressSuffix(currentMIPS->pc).c_str(), currentMIPS->r[MIPS_REG_RA], ModuleAddressSuffix(currentMIPS->r[MIPS_REG_RA]).c_str());
}
const std::string addressSuffix = ModuleAddressSuffix(address);
@@ -663,6 +664,7 @@ void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionTy
break;
}
const char *desc = MemoryExceptionTypeAsString(type);
if (action == ExceptionAction::Ignore) {
// Simplest logging and continue.
WARN_LOG(Log::MemMap, "%s: Invalid access at %08x%s (size %08x) %s%.*s", desc, address, addressSuffix.c_str(), accessSize, pcDetails, (int)additionalInfo.length(), additionalInfo.data());
@@ -686,6 +688,60 @@ void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionTy
}
}
void Core_MemoryExceptionHLE(MIPSState *mips, u32 address, u32 accessSize, MemoryExceptionType type) {
ExceptionAction action;
switch (type) {
case MemoryExceptionType::HLE_WRITE:
action = ResolveExceptionAction((ExceptionAction)g_Config.iExceptionActionMemWrite);
break;
case MemoryExceptionType::HLE_READ:
action = ResolveExceptionAction((ExceptionAction)g_Config.iExceptionActionMemRead);
break;
default:
_dbg_assert_(false);
action = ExceptionAction::Break;
break;
}
const HLEFunction *func = HLEGetFunctionBeingCalled();
const char *funcName = func ? func->name : "unknown";
char args[512] = "";
if (func) {
HLEFormatLogArgs(mips, args, sizeof(args), func->argmask);
}
const u32 pc = mips->pc;
char msg[512];
snprintf(msg, sizeof(msg), "Invalid access in %s(%s) at %08x%s (size %08x) PC %08x%s RA %08x%s",
funcName, args,
address, ModuleAddressSuffix(address).c_str(), accessSize,
pc, ModuleAddressSuffix(pc).c_str(),
mips->r[MIPS_REG_RA], ModuleAddressSuffix(mips->r[MIPS_REG_RA]).c_str());
const char *desc = MemoryExceptionTypeAsString(type);
if (action == ExceptionAction::Ignore) {
// Simplest logging and continue.
WARN_LOG(Log::MemMap, "HLE %s: %s", MemoryExceptionTypeAsString(type), msg);
return;
}
const std::string stackTrace = FormatStackTrace(WalkCurrentStack(-1));
ERROR_LOG(Log::MemMap, "%s: %s\n%s", desc, msg, stackTrace.c_str());
if (action == ExceptionAction::Break) {
MIPSExceptionInfo &e = g_exceptionInfo;
e = {};
e.type = MIPSExceptionType::MEMORY;
e.info.clear();
e.memory_type = type;
e.address = address;
e.accessSize = accessSize;
e.stackTrace = stackTrace;
e.pc = pc;
Core_Break(BreakReason::MemoryException, address);
}
}
// Can't be ignored, must break. Not sure we can get a meaningful stack trace here (since the PC is invalid).
void Core_ExecException(u32 address, u32 pc, ExecExceptionType type) {
const char *desc = ExecExceptionTypeAsString(type);
+7 -2
View File
@@ -20,7 +20,6 @@
#include <cstdint>
#include <functional>
#include <mutex>
#include <string>
#include <string_view>
#include "Common/CommonTypes.h"
@@ -199,6 +198,8 @@ enum class MemoryExceptionType {
UNKNOWN,
READ_WORD,
WRITE_WORD,
HLE_READ,
HLE_WRITE,
READ_BLOCK,
WRITE_BLOCK,
ALIGNMENT,
@@ -208,12 +209,16 @@ enum class ExecExceptionType {
THREAD,
};
void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type, std::string_view additionalInfo = "", bool forceReport = false);
void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type, std::string_view additionalInfo = "");
void Core_ExecException(u32 address, u32 pc, ExecExceptionType type);
void Core_BreakException(u32 pc);
// Call when loading save states, etc.
void Core_ResetException();
class MIPSState;
// Shortcut, just calls Core_MemoryException with automatically determined parameters (function name, etc).
void Core_MemoryExceptionHLE(MIPSState *mips, u32 address, u32 accessSize, MemoryExceptionType type);
enum class MIPSExceptionType {
NONE,
MEMORY,
+17 -8
View File
@@ -1009,7 +1009,7 @@ void hlePushFuncDesc(std::string_view module, std::string_view funcName) {
}
// TODO: Also add support for argument names.
size_t hleFormatLogArgs(char *message, size_t sz, const char *argmask) {
size_t HLEFormatLogArgs(const MIPSState *mips, char *message, size_t sz, const char *argmask) {
char *p = message;
size_t used = 0;
@@ -1026,9 +1026,9 @@ size_t hleFormatLogArgs(char *message, size_t sz, const char *argmask) {
for (size_t i = 0, n = strlen(argmask); i < n; ++i, ++reg) {
u32 regval;
if (reg < 8) {
regval = PARAM(reg);
regval = PARAM_MIPS(mips, reg);
} else {
u32 sp = currentMIPS->r[MIPS_REG_SP];
u32 sp = mips->r[MIPS_REG_SP];
// Goes upward on stack.
// NOTE: Currently we only support > 8 for 32-bit integer args.
regval = Memory::Read_U32(sp + (reg - 8) * 4);
@@ -1036,16 +1036,16 @@ size_t hleFormatLogArgs(char *message, size_t sz, const char *argmask) {
switch (argmask[i]) {
case 'p':
if (Memory::IsValidAddress(regval)) {
APPEND_FMT("%08x[%08x]", regval, Memory::Read_U32(regval));
if (Memory::IsValidRange(regval, 4)) {
APPEND_FMT("%08x[%08x]", regval, Memory::ReadUnchecked_U32(regval));
} else {
APPEND_FMT("%08x[invalid]", regval);
}
break;
case 'P':
if (Memory::IsValidAddress(regval)) {
APPEND_FMT("%08x[%016llx]", regval, Memory::Read_U64(regval));
if (Memory::IsValidRange(regval, 8)) {
APPEND_FMT("%08x[%016llx]", regval, Memory::ReadUnchecked_U64(regval));
} else {
APPEND_FMT("%08x[invalid]", regval);
}
@@ -1088,6 +1088,7 @@ size_t hleFormatLogArgs(char *message, size_t sz, const char *argmask) {
--reg;
break;
// TODO: Double? Does it ever happen?
default:
@@ -1118,6 +1119,14 @@ void hleLeave() {
} // else warn?
}
const HLEFunction *HLEGetFunctionBeingCalled() {
int stackSize = g_stackSize;
if (stackSize > 0) {
return g_stack[stackSize - 1];
}
return nullptr;
}
void hleDoLogInternal(Log t, LogLevel level, u64 res, const char *file, int line, const char *reportTag, const char *reason, const char *formatted_reason) {
char formatted_args[2048];
const char *funcName = "?";
@@ -1139,7 +1148,7 @@ void hleDoLogInternal(Log t, LogLevel level, u64 res, const char *file, int line
// Need to do something smart in hleCall. But it's better than printing function name and args from the wrong function.
if (stackSize == 1) {
hleFormatLogArgs(formatted_args, sizeof(formatted_args), hleFunc->argmask);
HLEFormatLogArgs(currentMIPS, formatted_args, sizeof(formatted_args), hleFunc->argmask);
} else {
truncate_cpy(formatted_args, "...N/A...");
}
+4
View File
@@ -98,6 +98,8 @@ struct Syscall {
#define RETURN64(n) {u64 RETURN64_tmp = n; currentMIPS->r[MIPS_REG_V0] = RETURN64_tmp & 0xFFFFFFFF; currentMIPS->r[MIPS_REG_V1] = RETURN64_tmp >> 32;}
#define RETURNF(fl) currentMIPS->f[0] = fl
#define PARAM_MIPS(mips, n) mips->r[MIPS_REG_A0 + n]
struct HLEModuleMeta {
// This is the modname (name from the PRX header). Probably, we should really blacklist on the module names of the exported symbol metadata.
const char *modname;
@@ -174,6 +176,8 @@ inline s64 hleDelayResult(s64 result, const char *reason, int usec) {
void HLEInit();
void HLEDoState(PointerWrap &p);
void HLEShutdown();
const HLEFunction *HLEGetFunctionBeingCalled();
size_t HLEFormatLogArgs(const MIPSState *mips, char *message, size_t sz, const char *argmask);
u32 GetSyscallOp(std::string_view module, u32 nib);
bool WriteHLESyscall(std::string_view module, u32 nib, u32 address);
void CallSyscall(MIPSOpcode op);
+2 -3
View File
@@ -524,7 +524,7 @@ void Jit::Comp_Jump(MIPSOpcode op) {
// Might be a stubbed address or something?
if (!Memory::IsValidAddress(targetAddr) || (targetAddr & 3) != 0) {
if (js.nextExit == 0) {
ERROR_LOG(Log::JIT, "Jump to invalid address: %08x PC %08x LR %08x", targetAddr, GetCompilerPC(), currentMIPS->r[MIPS_REG_RA]);
ERROR_LOG(Log::JIT, "Jump to invalid address: %08x PC %08x RA %08x", targetAddr, GetCompilerPC(), currentMIPS->r[MIPS_REG_RA]);
} else {
js.compiling = false;
}
@@ -562,8 +562,7 @@ void Jit::Comp_Jump(MIPSOpcode op) {
js.compiling = false;
}
void Jit::Comp_JumpReg(MIPSOpcode op)
{
void Jit::Comp_JumpReg(MIPSOpcode op) {
CONDITIONAL_LOG;
if (js.inDelaySlot) {
ERROR_LOG_REPORT(Log::JIT, "Branch in JumpReg delay slot at %08x in block starting at %08x", GetCompilerPC(), js.blockStart);
+5 -1
View File
@@ -320,7 +320,7 @@ bool HandleFault(uintptr_t hostAddress, void *ctx) {
uint32_t approximatePC = currentMIPS->pc;
// TODO: Determine access size from the disassembled native instruction. We have some partial info already,
// just need to clean it up.
Core_MemoryException(guestAddress, 0, approximatePC, type, infoString, true);
Core_MemoryException(guestAddress, 0, approximatePC, type, infoString);
// There's a small chance we can resume from this type of crash.
g_lastCrashAddress = codePtr;
@@ -373,6 +373,10 @@ std::vector<MIPSStackWalk::StackFrame> WalkCurrentStack(int threadID) {
std::string FormatStackTrace(const std::vector<MIPSStackWalk::StackFrame> &frames) {
std::stringstream str;
for (const auto &frame : frames) {
if (frame.pc == 0xFFFFFFFF) {
// Bottom of stack, probably.
continue;
}
std::string desc = g_symbolMap->GetDescription(frame.entry);
char moduleDesc[96];
if (DescribeKernelModuleAddress(frame.entry, moduleDesc, sizeof(moduleDesc))) {