From aa806595308f856304278b0b0b3631a8e39d9a95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 1 Jan 2023 19:22:41 +0100 Subject: [PATCH 1/3] Memory exception: Add facility to track size Might theoretically help in tracking some things down. Not fully utilized yet, the fault handler needs to extract the information from the faulting instruction. But we can use it for GetPointerRange etc. --- Core/Core.cpp | 16 +++++++++------- Core/Core.h | 5 +++-- Core/MIPS/IR/IRInterpreter.cpp | 2 +- Core/MemFault.cpp | 4 +++- Core/MemMapFunctions.cpp | 15 +++++++++------ UI/EmuScreen.cpp | 3 ++- 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Core/Core.cpp b/Core/Core.cpp index 0993de16c5ae..b67a7edf93a0 100644 --- a/Core/Core.cpp +++ b/Core/Core.cpp @@ -434,13 +434,13 @@ const char *ExecExceptionTypeAsString(ExecExceptionType type) { } } -void Core_MemoryException(u32 address, u32 pc, MemoryExceptionType type) { +void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type) { const char *desc = MemoryExceptionTypeAsString(type); // In jit, we only flush PC when bIgnoreBadMemAccess is off. if (g_Config.iCpuCore == (int)CPUCore::JIT && g_Config.bIgnoreBadMemAccess) { - WARN_LOG(MEMMAP, "%s: Invalid address %08x", desc, address); + WARN_LOG(MEMMAP, "%s: Invalid access at %08x (size %08x)", desc, address, accessSize); } else { - WARN_LOG(MEMMAP, "%s: Invalid address %08x PC %08x LR %08x", desc, address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); + WARN_LOG(MEMMAP, "%s: Invalid access at %08x (size %08x) PC %08x LR %08x", desc, address, accessSize, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); } if (!g_Config.bIgnoreBadMemAccess) { @@ -450,18 +450,19 @@ void Core_MemoryException(u32 address, u32 pc, MemoryExceptionType type) { e.info.clear(); e.memory_type = type; e.address = address; + e.accessSize = accessSize; e.pc = pc; Core_EnableStepping(true, "memory.exception", address); } } -void Core_MemoryExceptionInfo(u32 address, u32 pc, MemoryExceptionType type, std::string additionalInfo, bool forceReport) { +void Core_MemoryExceptionInfo(u32 address, u32 pc, u32 accessSize, MemoryExceptionType type, std::string additionalInfo, bool forceReport) { const char *desc = MemoryExceptionTypeAsString(type); // In jit, we only flush PC when bIgnoreBadMemAccess is off. if (g_Config.iCpuCore == (int)CPUCore::JIT && g_Config.bIgnoreBadMemAccess) { - WARN_LOG(MEMMAP, "%s: Invalid address %08x. %s", desc, address, additionalInfo.c_str()); + WARN_LOG(MEMMAP, "%s: Invalid access at %08x (size %08x). %s", desc, address, accessSize, additionalInfo.c_str()); } else { - WARN_LOG(MEMMAP, "%s: Invalid address %08x PC %08x LR %08x %s", desc, address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA], additionalInfo.c_str()); + WARN_LOG(MEMMAP, "%s: Invalid access at %08x (size %08x) PC %08x LR %08x %s", desc, address, accessSize, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA], additionalInfo.c_str()); } if (!g_Config.bIgnoreBadMemAccess || forceReport) { @@ -479,7 +480,7 @@ void Core_MemoryExceptionInfo(u32 address, u32 pc, MemoryExceptionType type, std // Can't be ignored void Core_ExecException(u32 address, u32 pc, ExecExceptionType type) { const char *desc = ExecExceptionTypeAsString(type); - WARN_LOG(MEMMAP, "%s: Invalid destination %08x PC %08x LR %08x", desc, address, pc, currentMIPS->r[MIPS_REG_RA]); + WARN_LOG(MEMMAP, "%s: Invalid exec address %08x PC %08x LR %08x", desc, address, pc, currentMIPS->r[MIPS_REG_RA]); ExceptionInfo &e = g_exceptionInfo; e = {}; @@ -487,6 +488,7 @@ void Core_ExecException(u32 address, u32 pc, ExecExceptionType type) { e.info.clear(); e.exec_type = type; e.address = address; + e.accessSize = 4; // size of an instruction e.pc = pc; // This just records the closest value that could be useful as reference. e.ra = currentMIPS->r[MIPS_REG_RA]; diff --git a/Core/Core.h b/Core/Core.h index a807f61b9ab4..6fb372311c49 100644 --- a/Core/Core.h +++ b/Core/Core.h @@ -101,9 +101,9 @@ enum class ExecExceptionType { }; // Separate one for without info, to avoid having to allocate a string -void Core_MemoryException(u32 address, u32 pc, MemoryExceptionType type); +void Core_MemoryException(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type); -void Core_MemoryExceptionInfo(u32 address, u32 pc, MemoryExceptionType type, std::string additionalInfo, bool forceReport); +void Core_MemoryExceptionInfo(u32 address, u32 accessSize, u32 pc, MemoryExceptionType type, std::string additionalInfo, bool forceReport); void Core_ExecException(u32 address, u32 pc, ExecExceptionType type); void Core_Break(u32 pc); @@ -125,6 +125,7 @@ struct ExceptionInfo { MemoryExceptionType memory_type; uint32_t pc; uint32_t address; + uint32_t accessSize; uint32_t ra = 0; // Reuses pc and address from memory type, where address is the failed destination. diff --git a/Core/MIPS/IR/IRInterpreter.cpp b/Core/MIPS/IR/IRInterpreter.cpp index da1e643888ab..984bbdf29488 100644 --- a/Core/MIPS/IR/IRInterpreter.cpp +++ b/Core/MIPS/IR/IRInterpreter.cpp @@ -82,7 +82,7 @@ u32 RunMemCheck(u32 pc, u32 addr) { template u32 RunValidateAddress(u32 pc, u32 addr, u32 isWrite) { const auto toss = [&](MemoryExceptionType t) { - Core_MemoryException(addr, pc, t); + Core_MemoryException(addr, 4, pc, t); return coreState != CORE_RUNNING ? 1 : 0; }; diff --git a/Core/MemFault.cpp b/Core/MemFault.cpp index 317cb875a835..d9670529ae05 100644 --- a/Core/MemFault.cpp +++ b/Core/MemFault.cpp @@ -282,7 +282,9 @@ bool HandleFault(uintptr_t hostAddress, void *ctx) { // Either bIgnoreBadMemAccess is off, or we failed recovery analysis. // We can't ignore this memory access. uint32_t approximatePC = currentMIPS->pc; - Core_MemoryExceptionInfo(guestAddress, approximatePC, type, infoString, true); + // TODO: Determine access size from the disassembled native instruction. We have some partial info already, + // just need to clean it up. + Core_MemoryExceptionInfo(guestAddress, 0, approximatePC, type, infoString, true); // There's a small chance we can resume from this type of crash. g_lastCrashAddress = codePtr; diff --git a/Core/MemMapFunctions.cpp b/Core/MemMapFunctions.cpp index af824750166e..ecab020418a2 100644 --- a/Core/MemMapFunctions.cpp +++ b/Core/MemMapFunctions.cpp @@ -40,7 +40,9 @@ u8 *GetPointerWrite(const u32 address) { Reporting::ReportMessage("Unknown GetPointerWrite %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); reported = true; } - Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK); + + // Size is not known, we pass 0 to signal that. + Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK); return nullptr; } } @@ -57,7 +59,8 @@ const u8 *GetPointer(const u32 address) { Reporting::ReportMessage("Unknown GetPointer %08x PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); reported = true; } - Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::READ_BLOCK); + // Size is not known, we pass 0 to signal that. + Core_MemoryException(address, 0, currentMIPS->pc, MemoryExceptionType::READ_BLOCK); return nullptr; } } @@ -67,7 +70,7 @@ u8 *GetPointerWriteRange(const u32 address, const u32 size) { if (ptr) { if (ValidSize(address, size) != size) { // That's a memory exception! TODO: Adjust reported address to the end of the range? - Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK); + Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::WRITE_BLOCK); return nullptr; } else { return ptr; @@ -83,7 +86,7 @@ const u8 *GetPointerRange(const u32 address, const u32 size) { if (ptr) { if (ValidSize(address, size) != size) { // That's a memory exception! TODO: Adjust reported address to the end of the range? - Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::READ_BLOCK); + Core_MemoryException(address, size, currentMIPS->pc, MemoryExceptionType::READ_BLOCK); return nullptr; } else { return ptr; @@ -115,7 +118,7 @@ inline void ReadFromHardware(T &var, const u32 address) { Reporting::ReportMessage("ReadFromHardware: Invalid address %08x near PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); reported = true; } - Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::READ_WORD); + Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::READ_WORD); var = 0; } } @@ -140,7 +143,7 @@ inline void WriteToHardware(u32 address, const T data) { Reporting::ReportMessage("WriteToHardware: Invalid address %08x near PC %08x LR %08x", address, currentMIPS->pc, currentMIPS->r[MIPS_REG_RA]); reported = true; } - Core_MemoryException(address, currentMIPS->pc, MemoryExceptionType::WRITE_WORD); + Core_MemoryException(address, sizeof(T), currentMIPS->pc, MemoryExceptionType::WRITE_WORD); } } diff --git a/UI/EmuScreen.cpp b/UI/EmuScreen.cpp index e44cca44b661..60fd0062a8f0 100644 --- a/UI/EmuScreen.cpp +++ b/UI/EmuScreen.cpp @@ -1219,11 +1219,12 @@ static void DrawCrashDump(UIContext *ctx) { if (info.type == ExceptionType::MEMORY) { snprintf(statbuf, sizeof(statbuf), R"( -Access: %s at %08x +Access: %s at %08x (sz: %d) PC: %08x %s)", MemoryExceptionTypeAsString(info.memory_type), info.address, + info.accessSize, info.pc, info.info.c_str()); ctx->Draw()->DrawTextShadow(ubuntu24, statbuf, x, y, 0xFFFFFFFF); From 91b7bf986ed2a4d8a5fb894a0d9808aeb84d9dbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 1 Jan 2023 20:48:03 +0100 Subject: [PATCH 2/3] Can do an unchecked GetPointer here --- Core/MemMap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/MemMap.h b/Core/MemMap.h index 79949f06ed5a..d9ef31c8ae34 100644 --- a/Core/MemMap.h +++ b/Core/MemMap.h @@ -289,7 +289,7 @@ inline void MemcpyUnchecked(const u32 to_address, const void *from_data, const u } inline void MemcpyUnchecked(const u32 to_address, const u32 from_address, const u32 len) { - MemcpyUnchecked(GetPointerWrite(to_address), from_address, len); + MemcpyUnchecked(GetPointerWriteUnchecked(to_address), from_address, len); } inline bool IsValidAddress(const u32 address) { From 700a018ef017af138436b832fd436d59368a3ded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 1 Jan 2023 20:48:16 +0100 Subject: [PATCH 3/3] IRInterpreter: Use alignment as access size in exceptions --- Core/MIPS/IR/IRInterpreter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Core/MIPS/IR/IRInterpreter.cpp b/Core/MIPS/IR/IRInterpreter.cpp index 984bbdf29488..c003b4f4dffa 100644 --- a/Core/MIPS/IR/IRInterpreter.cpp +++ b/Core/MIPS/IR/IRInterpreter.cpp @@ -82,7 +82,7 @@ u32 RunMemCheck(u32 pc, u32 addr) { template u32 RunValidateAddress(u32 pc, u32 addr, u32 isWrite) { const auto toss = [&](MemoryExceptionType t) { - Core_MemoryException(addr, 4, pc, t); + Core_MemoryException(addr, alignment, pc, t); return coreState != CORE_RUNNING ? 1 : 0; };