diff --git a/Core/Debugger/DebugInterface.h b/Core/Debugger/DebugInterface.h index 1f62aeea15..522e329a49 100644 --- a/Core/Debugger/DebugInterface.h +++ b/Core/Debugger/DebugInterface.h @@ -26,7 +26,6 @@ struct MemMap; class DebugInterface { public: - virtual const char *disasm(unsigned int address, unsigned int align) {return "NODEBUGGER";} virtual int getInstructionSize(int instruction) {return 1;} virtual bool isAlive() {return true;} @@ -55,8 +54,8 @@ public: virtual u32 GetPC() = 0; virtual void SetPC(u32 _pc) = 0; virtual u32 GetLR() {return GetPC();} - virtual void DisAsm(u32 op, u32 pc, int align, char *out, size_t outSize) { - snprintf(out, outSize, "[%08x] UNKNOWN", op); + virtual void DisAsm(u32 pc, char *out, size_t outSize) { + snprintf(out, outSize, "[%08x] UNKNOWN", pc); } // More stuff for debugger virtual int GetNumCategories() {return 0;} diff --git a/Core/Debugger/DisassemblyManager.cpp b/Core/Debugger/DisassemblyManager.cpp index f9bd9c1f0e..882aca2d5e 100644 --- a/Core/Debugger/DisassemblyManager.cpp +++ b/Core/Debugger/DisassemblyManager.cpp @@ -766,7 +766,8 @@ bool DisassemblyOpcode::disassemble(u32 address, DisassemblyLineInfo &dest, bool cpuDebug = DisassemblyManager::getCpu(); char opcode[64],arguments[256]; - const char *dizz = cpuDebug->disasm(address, 4); + char dizz[512]; + cpuDebug->DisAsm(address, dizz, sizeof(dizz)); parseDisasm(dizz,opcode,arguments,insertSymbols); dest.type = DISTYPE_OPCODE; dest.name = opcode; diff --git a/Core/ELF/ElfReader.cpp b/Core/ELF/ElfReader.cpp index b842f6798e..636c6f741e 100644 --- a/Core/ELF/ElfReader.cpp +++ b/Core/ELF/ElfReader.cpp @@ -204,7 +204,7 @@ bool ElfReader::LoadRelocations(const Elf32_Rel *rels, int numRelocs) { default: { char temp[256]; - MIPSDisAsm(MIPSOpcode(op), 0, temp); + MIPSDisAsm(MIPSOpcode(op), 0, temp, sizeof(temp)); ERROR_LOG_REPORT(LOADER, "ARGH IT'S AN UNKNOWN RELOCATION!!!!!!!! %08x, type=%d : %s", addr, type, temp); } break; diff --git a/Core/MIPS/ARM/ArmJit.cpp b/Core/MIPS/ARM/ArmJit.cpp index 9873130e21..81fd07f136 100644 --- a/Core/MIPS/ARM/ArmJit.cpp +++ b/Core/MIPS/ARM/ArmJit.cpp @@ -383,7 +383,7 @@ const u8 *ArmJit::DoJit(u32 em_address, JitBlock *b) if (logBlocks > 0 && dontLogBlocks == 0) { INFO_LOG(JIT, "=============== mips ==============="); for (u32 cpc = em_address; cpc != GetCompilerPC() + 4; cpc += 4) { - MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp, true); + MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp, sizeof(temp), true); INFO_LOG(JIT, "M: %08x %s", cpc, temp); } } diff --git a/Core/MIPS/ARM64/Arm64Jit.cpp b/Core/MIPS/ARM64/Arm64Jit.cpp index 1186928dc4..6034f2ad08 100644 --- a/Core/MIPS/ARM64/Arm64Jit.cpp +++ b/Core/MIPS/ARM64/Arm64Jit.cpp @@ -372,7 +372,7 @@ const u8 *Arm64Jit::DoJit(u32 em_address, JitBlock *b) { if (logBlocks > 0 && dontLogBlocks == 0) { INFO_LOG(JIT, "=============== mips %d ===============", blocks.GetNumBlocks()); for (u32 cpc = em_address; cpc != GetCompilerPC() + 4; cpc += 4) { - MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp, true); + MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp, sizeof(temp), true); INFO_LOG(JIT, "M: %08x %s", cpc, temp); } } diff --git a/Core/MIPS/IR/IRFrontend.cpp b/Core/MIPS/IR/IRFrontend.cpp index 66a3efbafe..77e787ba74 100644 --- a/Core/MIPS/IR/IRFrontend.cpp +++ b/Core/MIPS/IR/IRFrontend.cpp @@ -283,7 +283,7 @@ void IRFrontend::DoJit(u32 em_address, std::vector &instructions, u32 &m NOTICE_LOG(JIT, "=============== mips %08x ===============", em_address); for (u32 cpc = em_address; cpc != GetCompilerPC(); cpc += 4) { temp2[0] = 0; - MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp2, true); + MIPSDisAsm(Memory::Read_Opcode_JIT(cpc), cpc, temp2, sizeof(temp2), true); NOTICE_LOG(JIT, "M: %08x %s", cpc, temp2); } } diff --git a/Core/MIPS/IR/IRJit.cpp b/Core/MIPS/IR/IRJit.cpp index 66608fc86f..37860f1564 100644 --- a/Core/MIPS/IR/IRJit.cpp +++ b/Core/MIPS/IR/IRJit.cpp @@ -371,7 +371,7 @@ JitBlockDebugInfo IRBlockCache::GetBlockDebugInfo(int blockNum) const { for (u32 addr = start; addr < start + size; addr += 4) { char temp[256]; - MIPSDisAsm(Memory::Read_Instruction(addr), addr, temp, true); + MIPSDisAsm(Memory::Read_Instruction(addr), addr, temp, sizeof(temp), true); std::string mipsDis = temp; debugInfo.origDisasm.push_back(mipsDis); } diff --git a/Core/MIPS/JitCommon/JitBlockCache.cpp b/Core/MIPS/JitCommon/JitBlockCache.cpp index a08f430ed4..16e5b6cc4d 100644 --- a/Core/MIPS/JitCommon/JitBlockCache.cpp +++ b/Core/MIPS/JitCommon/JitBlockCache.cpp @@ -687,7 +687,7 @@ JitBlockDebugInfo JitBlockCache::GetBlockDebugInfo(int blockNum) const { debugInfo.originalAddress = block->originalAddress; for (u32 addr = block->originalAddress; addr <= block->originalAddress + block->originalSize * 4; addr += 4) { char temp[256]; - MIPSDisAsm(Memory::Read_Instruction(addr), addr, temp, true); + MIPSDisAsm(Memory::Read_Instruction(addr), addr, temp, sizeof(temp), true); std::string mipsDis = temp; debugInfo.origDisasm.push_back(mipsDis); } diff --git a/Core/MIPS/MIPSDebugInterface.cpp b/Core/MIPS/MIPSDebugInterface.cpp index 69b3c8e970..9087fd80cd 100644 --- a/Core/MIPS/MIPSDebugInterface.cpp +++ b/Core/MIPS/MIPSDebugInterface.cpp @@ -22,11 +22,11 @@ #include #endif +#include "Common/StringUtils.h" #include "Core/Debugger/Breakpoints.h" #include "Core/Debugger/SymbolMap.h" #include "Core/Debugger/DebugInterface.h" #include "Core/MIPS/MIPSDebugInterface.h" - #include "Core/HLE/sceKernelThread.h" #include "Core/MemMap.h" #include "Core/MIPS/MIPSTables.h" @@ -189,14 +189,11 @@ private: -const char *MIPSDebugInterface::disasm(unsigned int address, unsigned int align) -{ - static char mojs[256]; - if (Memory::IsValidAddress(address)) - MIPSDisAsm(Memory::Read_Opcode_JIT(address), address, mojs); +void MIPSDebugInterface::DisAsm(u32 pc, char *out, size_t outSize) { + if (Memory::IsValidAddress(pc)) + MIPSDisAsm(Memory::Read_Opcode_JIT(pc), pc, out, outSize); else - strcpy(mojs, "-"); - return mojs; + truncate_cpy(out, outSize, "-"); } unsigned int MIPSDebugInterface::readMemory(unsigned int address) { diff --git a/Core/MIPS/MIPSDebugInterface.h b/Core/MIPS/MIPSDebugInterface.h index 36651c2f16..cb6781455f 100644 --- a/Core/MIPS/MIPSDebugInterface.h +++ b/Core/MIPS/MIPSDebugInterface.h @@ -28,7 +28,6 @@ class MIPSDebugInterface : public DebugInterface MIPSState *cpu; public: MIPSDebugInterface(MIPSState *_cpu) { cpu = _cpu; } - const char *disasm(unsigned int address, unsigned int align) override; int getInstructionSize(int instruction) override { return 4; } bool isAlive() override; bool isBreakpoint(unsigned int address) override; @@ -51,6 +50,7 @@ public: u32 GetGPR32Value(int reg) override { return cpu->r[reg]; } u32 GetPC() override { return cpu->pc; } u32 GetLR() override { return cpu->r[MIPS_REG_RA]; } + void DisAsm(u32 pc, char *out, size_t outSize) override; void SetPC(u32 _pc) override { cpu->pc = _pc; } const char *GetCategoryName(int cat) override { diff --git a/Core/MIPS/MIPSDis.cpp b/Core/MIPS/MIPSDis.cpp index 81eac961fe..14ea4ff774 100644 --- a/Core/MIPS/MIPSDis.cpp +++ b/Core/MIPS/MIPSDis.cpp @@ -16,6 +16,7 @@ // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/. #include +#include "Common/StringUtils.h" #include "Core/HLE/HLE.h" #include "Core/MemMap.h" #include "Core/MIPS/MIPS.h" @@ -37,8 +38,6 @@ #define FN(i) currentDebugMIPS->GetRegName(1,i) //#define VN(i) currentMIPS->GetRegName(2,i) -u32 disPC; - namespace MIPSDis { // One shot, not re-entrant. @@ -53,108 +52,96 @@ namespace MIPSDis i = -i; } - sprintf(&temp[offset], "0x%X", i); + snprintf(&temp[offset], sizeof(temp) - offset, "0x%X", i); return temp; } - void Dis_Generic(MIPSOpcode op, char *out) - { - sprintf(out, "%s\t --- unknown ---", MIPSGetName(op)); + void Dis_Generic(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { + snprintf(out, outSize, "%s\t --- unknown ---", MIPSGetName(op)); } - void Dis_Cache(MIPSOpcode op, char *out) - { + void Dis_Cache(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int imm = SignExtend16ToS32(op & 0xFFFF); int rs = _RS; int func = (op >> 16) & 0x1F; - sprintf(out, "%s\tfunc=%i, %s(%s)", MIPSGetName(op), func, RN(rs), SignedHex(imm)); + snprintf(out, outSize, "%s\tfunc=%i, %s(%s)", MIPSGetName(op), func, RN(rs), SignedHex(imm)); } - void Dis_mxc1(MIPSOpcode op, char *out) - { + void Dis_mxc1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int fs = _FS; int rt = _RT; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s",name,RN(rt),FN(fs)); + snprintf(out, outSize, "%s\t%s, %s", name, RN(rt), FN(fs)); } - void Dis_FPU3op(MIPSOpcode op, char *out) - { + void Dis_FPU3op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int ft = _FT; int fs = _FS; int fd = _FD; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s, %s",name,FN(fd),FN(fs),FN(ft)); + snprintf(out, outSize, "%s\t%s, %s, %s", name, FN(fd), FN(fs), FN(ft)); } - void Dis_FPU2op(MIPSOpcode op, char *out) - { + void Dis_FPU2op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int fs = _FS; int fd = _FD; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s",name,FN(fd),FN(fs)); + snprintf(out, outSize, "%s\t%s, %s", name, FN(fd), FN(fs)); } - void Dis_FPULS(MIPSOpcode op, char *out) - { + void Dis_FPULS(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int offset = SignExtend16ToS32(op & 0xFFFF); int ft = _FT; int rs = _RS; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s(%s)",name,FN(ft),SignedHex(offset),RN(rs)); + snprintf(out, outSize, "%s\t%s, %s(%s)", name, FN(ft), SignedHex(offset), RN(rs)); } - void Dis_FPUComp(MIPSOpcode op, char *out) - { + + void Dis_FPUComp(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int fs = _FS; int ft = _FT; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s",name,FN(fs),FN(ft)); + snprintf(out, outSize, "%s\t%s, %s", name, FN(fs), FN(ft)); } - void Dis_FPUBranch(MIPSOpcode op, char *out) - { - u32 off = disPC; + void Dis_FPUBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { + u32 off = pc; int imm = SignExtend16ToS32(op & 0xFFFF) << 2; off += imm + 4; const char *name = MIPSGetName(op); - sprintf(out, "%s\t->$%08x",name,off); + snprintf(out, outSize, "%s\t->$%08x", name, off); } - void Dis_RelBranch(MIPSOpcode op, char *out) - { - u32 off = disPC; + void Dis_RelBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { + u32 off = pc; int imm = SignExtend16ToS32(op & 0xFFFF) << 2; int rs = _RS; off += imm + 4; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, ->$%08x", name, RN(rs), off); + snprintf(out, outSize, "%s\t%s, ->$%08x", name, RN(rs), off); } - void Dis_Syscall(MIPSOpcode op, char *out) - { + void Dis_Syscall(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { u32 callno = (op>>6) & 0xFFFFF; //20 bits int funcnum = callno & 0xFFF; int modulenum = (callno & 0xFF000) >> 12; - sprintf(out, "syscall\t %s",/*PSPHLE::GetModuleName(modulenum),*/GetFuncName(modulenum, funcnum)); + snprintf(out, outSize, "syscall\t %s", GetFuncName(modulenum, funcnum)); } - void Dis_ToHiloTransfer(MIPSOpcode op, char *out) - { + void Dis_ToHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rs = _RS; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s",name,RN(rs)); + snprintf(out, outSize, "%s\t%s", name, RN(rs)); } - void Dis_FromHiloTransfer(MIPSOpcode op, char *out) - { + void Dis_FromHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rd = _RD; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s",name,RN(rd)); + snprintf(out, outSize, "%s\t%s", name, RN(rd)); } - void Dis_RelBranch2(MIPSOpcode op, char *out) - { - u32 off = disPC; + void Dis_RelBranch2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { + u32 off = pc; int imm = SignExtend16ToS32(op & 0xFFFF) << 2; int rt = _RT; int rs = _RS; @@ -163,15 +150,14 @@ namespace MIPSDis const char *name = MIPSGetName(op); int o = op>>26; if (o==4 && rs == rt)//beq - sprintf(out, "b\t->$%08x", off); + snprintf(out, outSize, "b\t->$%08x", off); else if (o==20 && rs == rt)//beql - sprintf(out, "bl\t->$%08x", off); + snprintf(out, outSize, "bl\t->$%08x", off); else - sprintf(out, "%s\t%s, %s, ->$%08x", name, RN(rs), RN(rt), off); + snprintf(out, outSize, "%s\t%s, %s, ->$%08x", name, RN(rs), RN(rt), off); } - void Dis_IType(MIPSOpcode op, char *out) - { + void Dis_IType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { u32 uimm = op & 0xFFFF; u32 suimm = SignExtend16ToU32(op); s32 simm = SignExtend16ToS32(op); @@ -184,127 +170,114 @@ namespace MIPSDis case 8: //addi case 9: //addiu case 10: //slti - sprintf(out, "%s\t%s, %s, %s",name,RN(rt),RN(rs),SignedHex(simm)); + snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rt), RN(rs), SignedHex(simm)); break; case 11: //sltiu - sprintf(out, "%s\t%s, %s, 0x%X",name,RN(rt),RN(rs),suimm); + snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), suimm); break; default: - sprintf(out, "%s\t%s, %s, 0x%X",name,RN(rt),RN(rs),uimm); + snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), uimm); break; } } - void Dis_ori(MIPSOpcode op, char *out) - { + void Dis_ori(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { u32 uimm = op & 0xFFFF; int rt = _RT; int rs = _RS; const char *name = MIPSGetName(op); if (rs == 0) - sprintf(out, "li\t%s, 0x%X",RN(rt),uimm); + snprintf(out, outSize, "li\t%s, 0x%X", RN(rt), uimm); else - sprintf(out, "%s\t%s, %s, 0x%X",name,RN(rt),RN(rs),uimm); + snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rt), RN(rs), uimm); } - void Dis_IType1(MIPSOpcode op, char *out) - { + void Dis_IType1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { u32 uimm = op & 0xFFFF; int rt = _RT; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, 0x%X",name,RN(rt),uimm); + snprintf(out, outSize, "%s\t%s, 0x%X", name, RN(rt), uimm); } - void Dis_addi(MIPSOpcode op, char *out) - { + void Dis_addi(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int imm = SignExtend16ToS32(op & 0xFFFF); int rt = _RT; int rs = _RS; if (rs == 0) - sprintf(out, "li\t%s, %s",RN(rt),SignedHex(imm)); + snprintf(out, outSize, "li\t%s, %s", RN(rt), SignedHex(imm)); else - Dis_IType(op,out); + Dis_IType(op, pc, out, outSize); } - void Dis_ITypeMem(MIPSOpcode op, char *out) - { + void Dis_ITypeMem(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int imm = SignExtend16ToS32(op & 0xFFFF); int rt = _RT; int rs = _RS; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s(%s)",name,RN(rt),SignedHex(imm),RN(rs)); + snprintf(out, outSize, "%s\t%s, %s(%s)", name, RN(rt), SignedHex(imm), RN(rs)); } - void Dis_RType2(MIPSOpcode op, char *out) - { + void Dis_RType2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rs = _RS; int rd = _RD; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s",name,RN(rd),RN(rs)); + snprintf(out, outSize, "%s\t%s, %s", name, RN(rd), RN(rs)); } - void Dis_RType3(MIPSOpcode op, char *out) - { + void Dis_RType3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rt = _RT; int rs = _RS; int rd = _RD; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s, %s",name,RN(rd),RN(rs),RN(rt)); + snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rs), RN(rt)); } - void Dis_addu(MIPSOpcode op, char *out) - { + void Dis_addu(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rt = _RT; int rs = _RS; int rd = _RD; const char *name = MIPSGetName(op); if (rs==0 && rt==0) - sprintf(out,"li\t%s, 0",RN(rd)); + snprintf(out, outSize, "li\t%s, 0", RN(rd)); else if (rs == 0) - sprintf(out,"move\t%s, %s",RN(rd),RN(rt)); + snprintf(out, outSize, "move\t%s, %s", RN(rd), RN(rt)); else if (rt == 0) - sprintf(out,"move\t%s, %s",RN(rd),RN(rs)); + snprintf(out, outSize, "move\t%s, %s", RN(rd), RN(rs)); else - sprintf(out, "%s\t%s, %s, %s",name,RN(rd),RN(rs),RN(rt)); + snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rs), RN(rt)); } - void Dis_ShiftType(MIPSOpcode op, char *out) - { + void Dis_ShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rt = _RT; int rs = _RS; int rd = _RD; - int sa = (op>>6) & 0x1F; + int sa = (op>>6) & 0x1F; const char *name = MIPSGetName(op); if (((op & 0x3f) == 2) && rs == 1) name = "rotr"; if (((op & 0x3f) == 6) && sa == 1) name = "rotrv"; - sprintf(out, "%s\t%s, %s, 0x%X",name,RN(rd),RN(rt),sa); + snprintf(out, outSize, "%s\t%s, %s, 0x%X", name, RN(rd), RN(rt), sa); } - void Dis_VarShiftType(MIPSOpcode op, char *out) - { + void Dis_VarShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rt = _RT; int rs = _RS; int rd = _RD; - int sa = (op>>6) & 0x1F; + int sa = (op>>6) & 0x1F; const char *name = MIPSGetName(op); if (((op & 0x3f) == 6) && sa == 1) name = "rotrv"; - sprintf(out, "%s\t%s, %s, %s",name,RN(rd),RN(rt),RN(rs)); + snprintf(out, outSize, "%s\t%s, %s, %s", name, RN(rd), RN(rt), RN(rs)); } - - void Dis_MulDivType(MIPSOpcode op, char *out) - { + void Dis_MulDivType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rt = _RT; int rs = _RS; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s",name,RN(rs),RN(rt)); + snprintf(out, outSize, "%s\t%s, %s", name, RN(rs), RN(rt)); } - - void Dis_Special3(MIPSOpcode op, char *out) - { + void Dis_Special3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rs = _RS; int Rt = _RT; int pos = _POS; @@ -315,77 +288,67 @@ namespace MIPSDis case 0x0: //ext { int size = _SIZE + 1; - sprintf(out,"%s\t%s, %s, 0x%X, 0x%X",name,RN(Rt),RN(rs),pos,size); + snprintf(out, outSize, "%s\t%s, %s, 0x%X, 0x%X", name, RN(Rt), RN(rs), pos, size); } break; case 0x4: // ins { int size = (_SIZE + 1) - pos; - sprintf(out,"%s\t%s, %s, 0x%X, 0x%X",name,RN(Rt),RN(rs),pos,size); + snprintf(out, outSize, "%s\t%s, %s, 0x%X, 0x%X", name, RN(Rt), RN(rs), pos, size); } break; } } - void Dis_JumpType(MIPSOpcode op, char *out) - { + void Dis_JumpType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { u32 off = ((op & 0x03FFFFFF) << 2); - u32 addr = (disPC & 0xF0000000) | off; + u32 addr = (pc & 0xF0000000) | off; const char *name = MIPSGetName(op); - sprintf(out, "%s\t->$%08x",name,addr); + snprintf(out, outSize, "%s\t->$%08x", name, addr); } - void Dis_JumpRegType(MIPSOpcode op, char *out) - { + void Dis_JumpRegType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rs = _RS; int rd = _RD; const char *name = MIPSGetName(op); if ((op & 0x3f) == 9 && rd != MIPS_REG_RA) - sprintf(out, "%s\t%s,->%s", name, RN(rd), RN(rs)); + snprintf(out, outSize, "%s\t%s,->%s", name, RN(rd), RN(rs)); else - sprintf(out, "%s\t->%s", name, RN(rs)); + snprintf(out, outSize, "%s\t->%s", name, RN(rs)); } - void Dis_Allegrex(MIPSOpcode op, char *out) - { + void Dis_Allegrex(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rt = _RT; int rd = _RD; const char *name = MIPSGetName(op); - sprintf(out,"%s\t%s,%s",name,RN(rd),RN(rt)); + snprintf(out, outSize, "%s\t%s,%s", name, RN(rd), RN(rt)); } - void Dis_Allegrex2(MIPSOpcode op, char *out) - { + void Dis_Allegrex2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int rt = _RT; int rd = _RD; const char *name = MIPSGetName(op); - sprintf(out,"%s\t%s,%s",name,RN(rd),RN(rt)); + snprintf(out, outSize,"%s\t%s,%s", name, RN(rd), RN(rt)); } - void Dis_Emuhack(MIPSOpcode op, char *out) - { - auto resolved = Memory::Read_Instruction(disPC, true); - union { - char disasm[256]; - char truncated[241]; - }; + void Dis_Emuhack(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { + auto resolved = Memory::Read_Instruction(pc, true); + char disasm[256]; if (MIPS_IS_EMUHACK(resolved)) { - strcpy(disasm, "(invalid emuhack)"); + truncate_cpy(disasm, sizeof(disasm), "(invalid emuhack)"); } else { - MIPSDisAsm(resolved, disPC, disasm, true); + MIPSDisAsm(resolved, pc, disasm, sizeof(disasm), true); } - // Truncate in case it was too long, just to avoid warnings. - truncated[240] = '\0'; switch (op.encoding >> 24) { case 0x68: - snprintf(out, 256, "* jitblock: %s", truncated); + snprintf(out, outSize, "* jitblock: %s", disasm); break; case 0x6a: - snprintf(out, 256, "* replacement: %s", truncated); + snprintf(out, outSize, "* replacement: %s", disasm); break; default: - snprintf(out, 256, "* (invalid): %s", truncated); + snprintf(out, outSize, "* (invalid): %s", disasm); break; } } diff --git a/Core/MIPS/MIPSDis.h b/Core/MIPS/MIPSDis.h index 8ac177fcbd..464ec6d87e 100644 --- a/Core/MIPS/MIPSDis.h +++ b/Core/MIPS/MIPSDis.h @@ -20,44 +20,42 @@ #include "Common/CommonTypes.h" #include "Core/MIPS/MIPS.h" -extern u32 disPC; - namespace MIPSDis { - void Dis_Unknown(MIPSOpcode op, char *out); - void Dis_Unimpl(MIPSOpcode op, char *out); - void Dis_Syscall(MIPSOpcode op, char *out); + void Dis_Unknown(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Unimpl(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Syscall(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_mxc1(MIPSOpcode op, char *out); - void Dis_addi(MIPSOpcode op, char *out); - void Dis_addu(MIPSOpcode op, char *out); - void Dis_RelBranch2(MIPSOpcode op, char *out); - void Dis_RelBranch(MIPSOpcode op, char *out); - void Dis_Generic(MIPSOpcode op, char *out); - void Dis_Cache(MIPSOpcode op, char *out); - void Dis_IType(MIPSOpcode op, char *out); - void Dis_IType1(MIPSOpcode op, char *out); - void Dis_ITypeMem(MIPSOpcode op, char *out); - void Dis_RType2(MIPSOpcode op, char *out); - void Dis_RType3(MIPSOpcode op, char *out); - void Dis_MulDivType(MIPSOpcode op, char *out); - void Dis_ShiftType(MIPSOpcode op, char *out); - void Dis_VarShiftType(MIPSOpcode op, char *out); - void Dis_FPU3op(MIPSOpcode op, char *out); - void Dis_FPU2op(MIPSOpcode op, char *out); - void Dis_FPULS(MIPSOpcode op, char *out); - void Dis_FPUComp(MIPSOpcode op, char *out); - void Dis_FPUBranch(MIPSOpcode op, char *out); - void Dis_ori(MIPSOpcode op, char *out); - void Dis_Special3(MIPSOpcode op, char *out); + void Dis_mxc1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_addi(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_addu(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_RelBranch2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_RelBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Generic(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Cache(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_IType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_IType1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_ITypeMem(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_RType2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_RType3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_MulDivType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_ShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VarShiftType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_FPU3op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_FPU2op(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_FPULS(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_FPUComp(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_FPUBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_ori(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Special3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_ToHiloTransfer(MIPSOpcode op, char *out); - void Dis_FromHiloTransfer(MIPSOpcode op, char *out); - void Dis_JumpType(MIPSOpcode op, char *out); - void Dis_JumpRegType(MIPSOpcode op, char *out); + void Dis_ToHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_FromHiloTransfer(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_JumpType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_JumpRegType(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_Allegrex(MIPSOpcode op, char *out); - void Dis_Allegrex2(MIPSOpcode op, char *out); + void Dis_Allegrex(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Allegrex2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_Emuhack(MIPSOpcode op, char *out); + void Dis_Emuhack(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); } diff --git a/Core/MIPS/MIPSDisVFPU.cpp b/Core/MIPS/MIPSDisVFPU.cpp index a39bd4e209..fdc4aece46 100644 --- a/Core/MIPS/MIPSDisVFPU.cpp +++ b/Core/MIPS/MIPSDisVFPU.cpp @@ -17,6 +17,7 @@ #include #include "Common/Data/Convert/SmallDataConvert.h" +#include "Common/StringUtils.h" #include "Core/MIPS/MIPS.h" #include "Core/MIPS/MIPSDis.h" #include "Core/MIPS/MIPSTables.h" @@ -100,65 +101,60 @@ namespace MIPSDis { const char *SignedHex(int i); - void Dis_SV(MIPSOpcode op, char *out) - { + void Dis_SV(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int offset = SignExtend16ToS32(op & 0xFFFC); int vt = ((op>>16)&0x1f)|((op&3)<<5); int rs = (op>>21) & 0x1f; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s(%s)", name, VN(vt, V_Single), SignedHex(offset), RN(rs)); + snprintf(out, outSize, "%s\t%s, %s(%s)", name, VN(vt, V_Single), SignedHex(offset), RN(rs)); } - void Dis_SVQ(MIPSOpcode op, char *out) - { + void Dis_SVQ(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int offset = SignExtend16ToS32(op & 0xFFFC); int vt = (((op>>16)&0x1f))|((op&1)<<5); int rs = (op>>21) & 0x1f; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s(%s)", name, VN(vt, V_Quad), SignedHex(offset), RN(rs)); - if (op & 2) - strcat(out, ", wb"); + size_t outpos = 0; + outpos += snprintf(out, outSize, "%s\t%s, %s(%s)", name, VN(vt, V_Quad), SignedHex(offset), RN(rs)); + if ((op & 2) && outpos < outSize) + truncate_cpy(out + outpos, outSize - outpos, ", wb"); } - void Dis_SVLRQ(MIPSOpcode op, char *out) - { + void Dis_SVLRQ(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int offset = SignExtend16ToS32(op & 0xFFFC); int vt = (((op>>16)&0x1f))|((op&1)<<5); int rs = (op>>21) & 0x1f; int lr = (op>>1)&1; const char *name = MIPSGetName(op); - sprintf(out, "%s%s.q\t%s, %s(%s)", name, lr ? "r" : "l", VN(vt, V_Quad), SignedHex(offset), RN(rs)); + snprintf(out, outSize, "%s%s.q\t%s, %s(%s)", name, lr ? "r" : "l", VN(vt, V_Quad), SignedHex(offset), RN(rs)); } - void Dis_Mftv(MIPSOpcode op, char *out) - { + void Dis_Mftv(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int vr = op & 0xFF; int rt = _RT; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s",name,vr>127?"c":"", RN(rt), VN(vr, V_Single)); + snprintf(out, outSize, "%s%s\t%s, %s", name, vr > 127 ? "c" : "", RN(rt), VN(vr, V_Single)); } - void Dis_Vmfvc(MIPSOpcode op, char *out) - { + void Dis_Vmfvc(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int vd = _VD; int vr = (op >> 8) & 0x7F; const char* name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s", name, VN(vd, V_Single), VN(vr + 128, V_Single)); + snprintf(out, outSize, "%s\t%s, %s", name, VN(vd, V_Single), VN(vr + 128, V_Single)); } - void Dis_Vmtvc(MIPSOpcode op, char *out) - { + void Dis_Vmtvc(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int vr = op & 0x7F; int vs = _VS; const char *name = MIPSGetName(op); - sprintf(out, "%s\t%s, %s", name, VN(vs, V_Single), VN(vr + 128, V_Single)); + snprintf(out, outSize, "%s\t%s, %s", name, VN(vs, V_Single), VN(vr + 128, V_Single)); } - void Dis_VPFXST(MIPSOpcode op, char *out) - { + void Dis_VPFXST(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int data = op & 0xFFFFF; const char *name = MIPSGetName(op); - sprintf(out, "%s\t[",name); + size_t outpos = snprintf(out, outSize, "%s\t[", name); + static const char *regnam[4] = {"X","Y","Z","W"}; static const char *constan[8] = {"0","1","2","1/2","3","1/3","1/4","1/6"}; for (int i=0; i<4; i++) @@ -167,68 +163,67 @@ namespace MIPSDis int abs = (data>>(8+i)) & 1; int negate = (data>>(16+i)) & 1; int constants = (data>>(12+i)) & 1; - if (negate) - strcat(out, "-"); - if (abs && !constants) - strcat(out, "|"); - if (!constants) - { - strcat(out, regnam[regnum]); - } - else - { + if (negate && outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, "-"); + if (abs && !constants && outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, "|"); + if (!constants) { + if (outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, regnam[regnum]); + } else { if (abs) regnum+=4; - strcat(out, constan[regnum]); + if (outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, constan[regnum]); } - if (abs && !constants) - strcat(out, "|"); - if (i != 3) - strcat(out, ","); + if (abs && !constants && outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, "|"); + if (i != 3 && outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, ","); } - strcat(out, "]"); + if (outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, "]"); } - void Dis_VPFXD(MIPSOpcode op, char *out) - { + void Dis_VPFXD(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int data = op & 0xFFFFF; const char *name = MIPSGetName(op); - sprintf(out, "%s\t[", name); + size_t outpos = snprintf(out, outSize, "%s\t[", name); + static const char *satNames[4] = {"", "0:1", "X", "-1:1"}; for (int i=0; i<4; i++) { int sat = (data>>i*2)&3; int mask = (data>>(8+i))&1; - if (sat) - strcat(out, satNames[sat]); - if (mask) - strcat(out, "M"); - if (i < 4 - 1) - strcat(out, ","); + if (sat && outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, satNames[sat]); + if (mask && outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, "M"); + if (i < 4 - 1 && outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, ","); } - strcat(out, "]"); + if (outpos < outSize) + outpos += truncate_cpy(out + outpos, outSize - outpos, "]"); } - void Dis_Viim(MIPSOpcode op, char *out) - { + void Dis_Viim(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int vt = _VT; int imm = SignExtend16ToS32(op & 0xFFFF); const char *name = MIPSGetName(op); int type = (op >> 23) & 7; if (type == 6) - sprintf(out, "%s\t%s, %i", name, VN(vt, V_Single), imm); + snprintf(out, outSize, "%s\t%s, %i", name, VN(vt, V_Single), imm); else if (type == 7) - sprintf(out, "%s\t%s, %f", name, VN(vt, V_Single), Float16ToFloat32((u16)imm)); + snprintf(out, outSize, "%s\t%s, %f", name, VN(vt, V_Single), Float16ToFloat32((u16)imm)); else - sprintf(out, "%s\tARGH", name); + snprintf(out, outSize, "%s\tARGH", name); } - void Dis_Vcst(MIPSOpcode op, char *out) - { + void Dis_Vcst(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int conNum = (op>>16) & 0x1f; int vd = _VD; VectorSize sz = GetVecSizeSafe(op); @@ -258,68 +253,61 @@ namespace MIPSDis const char *name = MIPSGetName(op); const char *c = constants[conNum]; if (c==0) c = constants[0]; - sprintf(out,"%s%s\t%s, %s",name,VSuff(op),VN(vd,sz), c); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd,sz), c); } - void Dis_MatrixSet1(MIPSOpcode op, char *out) - { + void Dis_MatrixSet1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; MatrixSize sz = GetMtxSizeSafe(op); - sprintf(out, "%s%s\t%s",name,VSuff(op),MN(vd, sz)); + snprintf(out, outSize, "%s%s\t%s", name, VSuff(op), MN(vd, sz)); } - void Dis_MatrixSet2(MIPSOpcode op, char *out) - { + void Dis_MatrixSet2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; MatrixSize sz = GetMtxSizeSafe(op); - sprintf(out, "%s%s\t%s, %s",name,VSuff(op),MN(vd, sz),MN(vs,sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), MN(vd, sz), MN(vs,sz)); } - void Dis_MatrixSet3(MIPSOpcode op, char *out) - { + void Dis_MatrixSet3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; int vt = _VT; MatrixSize sz = GetMtxSizeSafe(op); - sprintf(out, "%s%s\t%s, %s, %s",name,VSuff(op),MN(vd, sz),MN(vs,sz),MN(vt,sz)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), MN(vd, sz), MN(vs,sz), MN(vt,sz)); } - void Dis_MatrixMult(MIPSOpcode op, char *out) - { + void Dis_MatrixMult(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; int vt = _VT; MatrixSize sz = GetMtxSizeSafe(op); // TODO: Xpose? - sprintf(out, "%s%s\t%s, %s, %s",name,VSuff(op),MN(vd, sz),MN(Xpose(vs),sz),MN(vt,sz)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), MN(vd, sz), MN(Xpose(vs),sz), MN(vt,sz)); } - void Dis_Vmscl(MIPSOpcode op, char *out) - { + void Dis_Vmscl(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; int vt = _VT; MatrixSize sz = GetMtxSizeSafe(op); - sprintf(out, "%s%s\t%s, %s, %s", name, VSuff(op), MN(vd, sz), MN(vs, sz), VN(vt, V_Single)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), MN(vd, sz), MN(vs, sz), VN(vt, V_Single)); } - void Dis_VectorDot(MIPSOpcode op, char *out) - { + void Dis_VectorDot(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; int vt = _VT; VectorSize sz = GetVecSizeSafe(op); - sprintf(out, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, V_Single), VN(vs,sz), VN(vt, sz)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, V_Single), VN(vs,sz), VN(vt, sz)); } - void Dis_Vtfm(MIPSOpcode op, char *out) - { + void Dis_Vtfm(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int vd = _VD; int vs = _VS; int vt = _VT; @@ -331,25 +319,23 @@ namespace MIPSDis if (n == ins) { //homogenous - sprintf(out, "vhtfm%i%s\t%s, %s, %s", n, VSuff(op), VN(vd, sz), MN(vs, msz), VN(vt, sz)); + snprintf(out, outSize, "vhtfm%i%s\t%s, %s, %s", n, VSuff(op), VN(vd, sz), MN(vs, msz), VN(vt, sz)); } else if (n == ins+1) { - sprintf(out, "vtfm%i%s\t%s, %s, %s", n, VSuff(op), VN(vd, sz), MN(vs, msz), VN(vt, sz)); + snprintf(out, outSize, "vtfm%i%s\t%s, %s, %s", n, VSuff(op), VN(vd, sz), MN(vs, msz), VN(vt, sz)); } else { - sprintf(out,"BADVTFM"); + truncate_cpy(out, outSize, "BADVTFM"); } } - void Dis_Vflush(MIPSOpcode op, char *out) - { - sprintf(out,"vflush"); + void Dis_Vflush(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { + truncate_cpy(out, outSize, "vflush"); } - void Dis_Vcrs(MIPSOpcode op, char *out) - { + void Dis_Vcrs(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vt = _VT; int vs = _VS; @@ -357,26 +343,24 @@ namespace MIPSDis VectorSize sz = GetVecSizeSafe(op); if (sz != V_Triple) { - sprintf(out, "vcrs\tERROR"); + truncate_cpy(out, outSize, "vcrs\tERROR"); } else - sprintf(out, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs, sz), VN(vt,sz)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs, sz), VN(vt,sz)); } - void Dis_Vcmp(MIPSOpcode op, char *out) - { + void Dis_Vcmp(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vt = _VT; int vs = _VS; int cond = op&15; VectorSize sz = GetVecSizeSafe(op); const char *condNames[16] = {"FL","EQ","LT","LE","TR","NE","GE","GT","EZ","EN","EI","ES","NZ","NN","NI","NS"}; - sprintf(out, "%s%s\t%s, %s, %s", name, VSuff(op), condNames[cond], VN(vs, sz), VN(vt,sz)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), condNames[cond], VN(vs, sz), VN(vt,sz)); } - void Dis_Vcmov(MIPSOpcode op, char *out) - { + void Dis_Vcmov(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); VectorSize sz = GetVecSizeSafe(op); int vd = _VD; @@ -385,61 +369,55 @@ namespace MIPSDis int imm3 = (op>>16)&7; if (tf > 1) { - sprintf(out, "%s\tARGH%i", name, tf); + snprintf(out, outSize, "%s\tARGH%i", name, tf); return; } if (imm3<6) - sprintf(out, "%s%s%s\t%s, %s, CC[%i]", name, tf==0?"t":"f", VSuff(op), VN(vd, sz), VN(vs,sz), imm3); + snprintf(out, outSize, "%s%s%s\t%s, %s, CC[%i]", name, tf==0?"t":"f", VSuff(op), VN(vd, sz), VN(vs,sz), imm3); else if (imm3 == 6) - sprintf(out, "%s%s%s\t%s, %s, CC[...]", name, tf==0?"t":"f", VSuff(op), VN(vd, sz), VN(vs,sz)); + snprintf(out, outSize, "%s%s%s\t%s, %s, CC[...]", name, tf==0?"t":"f", VSuff(op), VN(vd, sz), VN(vs,sz)); } - void Dis_Vfad(MIPSOpcode op, char *out) - { + void Dis_Vfad(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; VectorSize sz = GetVecSizeSafe(op); - sprintf(out, "%s%s\t%s, %s", name, VSuff(op), VN(vd, V_Single), VN(vs,sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, V_Single), VN(vs,sz)); } - void Dis_VScl(MIPSOpcode op, char *out) - { + void Dis_VScl(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; int vt = _VT; VectorSize sz = GetVecSizeSafe(op); - sprintf(out, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs,sz), VN(vt, V_Single)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs,sz), VN(vt, V_Single)); } - void Dis_VectorSet1(MIPSOpcode op, char *out) - { + void Dis_VectorSet1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; VectorSize sz = GetVecSizeSafe(op); - sprintf(out, "%s%s\t%s",name,VSuff(op),VN(vd, sz)); + snprintf(out, outSize, "%s%s\t%s", name, VSuff(op), VN(vd, sz)); } - void Dis_VectorSet2(MIPSOpcode op, char *out) - { + void Dis_VectorSet2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; VectorSize sz = GetVecSizeSafe(op); - sprintf(out, "%s%s\t%s, %s",name,VSuff(op),VN(vd, sz),VN(vs, sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, sz), VN(vs, sz)); } - void Dis_VectorSet3(MIPSOpcode op, char *out) - { + void Dis_VectorSet3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { const char *name = MIPSGetName(op); int vd = _VD; int vs = _VS; int vt = _VT; VectorSize sz = GetVecSizeSafe(op); - sprintf(out, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs,sz), VN(vt, sz)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs,sz), VN(vt, sz)); } - void Dis_VRot(MIPSOpcode op, char *out) - { + void Dis_VRot(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int vd = _VD; int vs = _VS; int imm = (op>>16) & 0x1f; @@ -468,11 +446,10 @@ namespace MIPSDis temp[pos++] = ']'; temp[pos]=0; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s, %s",name,VSuff(op),VN(vd, sz),VN(vs, V_Single),temp); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs, V_Single),temp); } - void Dis_CrossQuat(MIPSOpcode op, char *out) - { + void Dis_CrossQuat(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); const char *name; switch (sz) @@ -493,39 +470,35 @@ namespace MIPSDis int vd = _VD; int vs = _VS; int vt = _VT; - sprintf(out, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs,sz), VN(vt, sz)); + snprintf(out, outSize, "%s%s\t%s, %s, %s", name, VSuff(op), VN(vd, sz), VN(vs,sz), VN(vt, sz)); } - void Dis_Vbfy(MIPSOpcode op, char *out) - { + void Dis_Vbfy(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); int vd = _VD; int vs = _VS; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s",name,VSuff(op),VN(vd, sz),VN(vs, sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, sz), VN(vs, sz)); } - void Dis_Vf2i(MIPSOpcode op, char *out) - { + void Dis_Vf2i(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); int vd = _VD; int vs = _VS; int imm = (op>>16)&0x1f; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s, %i",name,VSuff(op),VN(vd, sz),VN(vs, sz),imm); + snprintf(out, outSize, "%s%s\t%s, %s, %i", name, VSuff(op), VN(vd, sz), VN(vs, sz), imm); } - void Dis_Vs2i(MIPSOpcode op, char *out) - { + void Dis_Vs2i(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); int vd = _VD; int vs = _VS; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s",name,VSuff(op),VN(vd, sz),VN(vs, sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, sz), VN(vs, sz)); } - void Dis_Vi2x(MIPSOpcode op, char *out) - { + void Dis_Vi2x(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); VectorSize dsz = GetHalfVectorSizeSafe(sz); if (((op>>16)&3)==0) @@ -534,22 +507,20 @@ namespace MIPSDis int vd = _VD; int vs = _VS; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s",name,VSuff(op),VN(vd, dsz),VN(vs, sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, dsz), VN(vs, sz)); } - void Dis_Vwbn(MIPSOpcode op, char *out) - { + void Dis_Vwbn(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); int vd = _VD; int vs = _VS; int imm = (int)((op >> 16) & 0xFF); const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s, %d", name, VSuff(op), VN(vd, sz), VN(vs, sz), imm); + snprintf(out, outSize, "%s%s\t%s, %s, %d", name, VSuff(op), VN(vd, sz), VN(vs, sz), imm); } - void Dis_Vf2h(MIPSOpcode op, char *out) - { + void Dis_Vf2h(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); VectorSize dsz = GetHalfVectorSizeSafe(sz); if (((op>>16)&3)==0) @@ -558,55 +529,50 @@ namespace MIPSDis int vd = _VD; int vs = _VS; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s", name, VSuff(op), VN(vd, dsz), VN(vs, sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, dsz), VN(vs, sz)); } - void Dis_Vh2f(MIPSOpcode op, char *out) - { + void Dis_Vh2f(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); VectorSize dsz = GetDoubleVectorSizeSafe(sz); int vd = _VD; int vs = _VS; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s", name, VSuff(op), VN(vd, dsz), VN(vs, sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, dsz), VN(vs, sz)); } - void Dis_ColorConv(MIPSOpcode op, char *out) - { + void Dis_ColorConv(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); VectorSize dsz = GetHalfVectorSizeSafe(sz); int vd = _VD; int vs = _VS; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s, %s", name, VSuff(op), VN(vd, dsz), VN(vs, sz)); + snprintf(out, outSize, "%s%s\t%s, %s", name, VSuff(op), VN(vd, dsz), VN(vs, sz)); } - void Dis_Vrnds(MIPSOpcode op, char *out) - { + void Dis_Vrnds(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { int vd = _VD; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s", name, VSuff(op), VN(vd, V_Single)); + snprintf(out, outSize, "%s%s\t%s", name, VSuff(op), VN(vd, V_Single)); } - void Dis_VrndX(MIPSOpcode op, char *out) - { + void Dis_VrndX(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { VectorSize sz = GetVecSizeSafe(op); int vd = _VD; const char *name = MIPSGetName(op); - sprintf(out, "%s%s\t%s", name, VSuff(op), VN(vd, sz)); + snprintf(out, outSize, "%s%s\t%s", name, VSuff(op), VN(vd, sz)); } - void Dis_VBranch(MIPSOpcode op, char *out) - { - u32 off = disPC; + void Dis_VBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize) { + u32 off = pc; int imm = SignExtend16ToS32(op&0xFFFF) << 2; int imm3 = (op>>18)&7; off += imm + 4; const char *name = MIPSGetName(op); - sprintf(out, "%s\t->$%08x (CC[%i])",name,off,imm3); + snprintf(out, outSize, "%s\t->$%08x (CC[%i])", name, off, imm3); } } diff --git a/Core/MIPS/MIPSDisVFPU.h b/Core/MIPS/MIPSDisVFPU.h index 8890ed29b1..d03f2ff995 100644 --- a/Core/MIPS/MIPSDisVFPU.h +++ b/Core/MIPS/MIPSDisVFPU.h @@ -19,52 +19,50 @@ #include "Common/CommonTypes.h" -extern u32 disPC; - namespace MIPSDis { - void Dis_Mftv(MIPSOpcode op, char *out); - void Dis_Vmfvc(MIPSOpcode op, char *out); - void Dis_Vmtvc(MIPSOpcode op, char *out); + void Dis_Mftv(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vmfvc(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vmtvc(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_SV(MIPSOpcode op, char *out); - void Dis_SVQ(MIPSOpcode op, char *out); - void Dis_SVLRQ(MIPSOpcode op, char *out); + void Dis_SV(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_SVQ(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_SVLRQ(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_MatrixSet1(MIPSOpcode op, char *out); - void Dis_MatrixSet2(MIPSOpcode op, char *out); - void Dis_MatrixSet3(MIPSOpcode op, char *out); - void Dis_MatrixMult(MIPSOpcode op, char *out); - void Dis_Vmscl(MIPSOpcode op, char *out); + void Dis_MatrixSet1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_MatrixSet2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_MatrixSet3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_MatrixMult(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vmscl(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_VectorDot(MIPSOpcode op, char *out); - void Dis_Vfad(MIPSOpcode op, char *out); - void Dis_VectorSet1(MIPSOpcode op, char *out); - void Dis_VectorSet2(MIPSOpcode op, char *out); - void Dis_VectorSet3(MIPSOpcode op, char *out); - void Dis_VRot(MIPSOpcode op, char *out); - void Dis_VScl(MIPSOpcode op, char *out); + void Dis_VectorDot(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vfad(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VectorSet1(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VectorSet2(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VectorSet3(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VRot(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VScl(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_VPFXST(MIPSOpcode op, char *out); - void Dis_VPFXD(MIPSOpcode op, char *out); - void Dis_Vcrs(MIPSOpcode op, char *out); - void Dis_Viim(MIPSOpcode op, char *out); - void Dis_Vcst(MIPSOpcode op, char *out); - void Dis_CrossQuat(MIPSOpcode op, char *out); - void Dis_Vtfm(MIPSOpcode op, char *out); - void Dis_Vcmp(MIPSOpcode op, char *out); - void Dis_Vcmov(MIPSOpcode op, char *out); - void Dis_Vflush(MIPSOpcode op, char *out); - void Dis_Vbfy(MIPSOpcode op, char *out); - void Dis_Vf2i(MIPSOpcode op, char *out); - void Dis_Vi2x(MIPSOpcode op, char *out); - void Dis_Vs2i(MIPSOpcode op, char *out); - void Dis_Vwbn(MIPSOpcode op, char *out); - void Dis_Vf2h(MIPSOpcode op, char *out); - void Dis_Vh2f(MIPSOpcode op, char *out); - void Dis_Vrnds(MIPSOpcode op, char *out); - void Dis_VrndX(MIPSOpcode op, char *out); - void Dis_ColorConv(MIPSOpcode op, char *out); + void Dis_VPFXST(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VPFXD(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vcrs(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Viim(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vcst(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_CrossQuat(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vtfm(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vcmp(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vcmov(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vflush(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vbfy(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vf2i(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vi2x(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vs2i(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vwbn(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vf2h(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vh2f(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_Vrnds(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_VrndX(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); + void Dis_ColorConv(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); - void Dis_VBranch(MIPSOpcode op, char *out); + void Dis_VBranch(MIPSOpcode op, uint32_t pc, char *out, size_t outSize); } diff --git a/Core/MIPS/MIPSTables.cpp b/Core/MIPS/MIPSTables.cpp index f5b4af34c7..0e8e29de0d 100644 --- a/Core/MIPS/MIPSTables.cpp +++ b/Core/MIPS/MIPSTables.cpp @@ -918,14 +918,13 @@ void MIPSCompileOp(MIPSOpcode op, MIPSComp::MIPSFrontendInterface *jit) { } } -void MIPSDisAsm(MIPSOpcode op, u32 pc, char *out, bool tabsToSpaces) { +void MIPSDisAsm(MIPSOpcode op, u32 pc, char *out, size_t outSize, bool tabsToSpaces) { if (op == 0) { - strcpy(out, "nop"); + truncate_cpy(out, outSize, "nop"); } else { - disPC = pc; const MIPSInstruction *instr = MIPSGetInstruction(op); if (instr && instr->disasm) { - instr->disasm(op, out); + instr->disasm(op, pc, out, outSize); if (tabsToSpaces) { while (*out) { if (*out == '\t') @@ -934,7 +933,7 @@ void MIPSDisAsm(MIPSOpcode op, u32 pc, char *out, bool tabsToSpaces) { } } } else { - strcpy(out, "no instruction :("); + truncate_cpy(out, outSize, "no instruction :("); } } } @@ -946,7 +945,7 @@ static inline void Interpret(const MIPSInstruction *instr, MIPSOpcode op) { ERROR_LOG_REPORT(CPU, "Unknown instruction %08x at %08x", op.encoding, currentMIPS->pc); // Try to disassemble it char disasm[256]; - MIPSDisAsm(op, currentMIPS->pc, disasm); + MIPSDisAsm(op, currentMIPS->pc, disasm, sizeof(disasm)); _dbg_assert_msg_(0, "%s", disasm); currentMIPS->pc += 4; } @@ -1122,6 +1121,6 @@ int MIPSGetMemoryAccessSize(MIPSOpcode op) { const char *MIPSDisasmAt(u32 compilerPC) { static char temp[256]; - MIPSDisAsm(Memory::Read_Instruction(compilerPC), 0, temp); + MIPSDisAsm(Memory::Read_Instruction(compilerPC), 0, temp, sizeof(temp)); return temp; } diff --git a/Core/MIPS/MIPSTables.h b/Core/MIPS/MIPSTables.h index 484eab35ea..e550b35212 100644 --- a/Core/MIPS/MIPSTables.h +++ b/Core/MIPS/MIPSTables.h @@ -115,7 +115,7 @@ struct MIPSInfo { u64 cycles : 16; }; -typedef void (CDECL *MIPSDisFunc)(MIPSOpcode opcode, char *out); +typedef void (CDECL *MIPSDisFunc)(MIPSOpcode opcode, uint32_t pc, char *out, size_t outSize); typedef void (CDECL *MIPSInterpretFunc)(MIPSOpcode opcode); namespace MIPSComp { @@ -123,7 +123,7 @@ namespace MIPSComp { } void MIPSCompileOp(MIPSOpcode op, MIPSComp::MIPSFrontendInterface *jit); -void MIPSDisAsm(MIPSOpcode op, u32 pc, char *out, bool tabsToSpaces = false); +void MIPSDisAsm(MIPSOpcode op, u32 pc, char *out, size_t outSize, bool tabsToSpaces = false); MIPSInfo MIPSGetInfo(MIPSOpcode op); void MIPSInterpret(MIPSOpcode op); //only for those rare ones int MIPSInterpret_RunUntil(u64 globalTicks); diff --git a/Core/MIPS/x86/CompBranch.cpp b/Core/MIPS/x86/CompBranch.cpp index 474936cde3..52655319c1 100644 --- a/Core/MIPS/x86/CompBranch.cpp +++ b/Core/MIPS/x86/CompBranch.cpp @@ -107,7 +107,7 @@ static void JitBranchLog(MIPSOpcode op, u32 pc) { static void JitBranchLogMismatch(MIPSOpcode op, u32 pc) { char temp[256]; - MIPSDisAsm(op, pc, temp, true); + MIPSDisAsm(op, pc, temp, sizeof(temp), true); ERROR_LOG(JIT, "Bad jump: %s - int:%08x jit:%08x", temp, currentMIPS->intBranchExit, currentMIPS->jitBranchExit); Core_EnableStepping(true, "jit.branchdebug", pc); } diff --git a/SDL/SDLMain.cpp b/SDL/SDLMain.cpp index d81157d83d..4262d2c571 100644 --- a/SDL/SDLMain.cpp +++ b/SDL/SDLMain.cpp @@ -1186,7 +1186,6 @@ int main(int argc, char *argv[]) { break; } #endif - // TODO: Should we even keep the "non-precise" events? if (event.wheel.y > 0) { key.keyCode = NKCODE_EXT_MOUSEWHEEL_UP; mouseWheelMovedUpFrames = 5; diff --git a/UI/DevScreens.cpp b/UI/DevScreens.cpp index 59feb097de..6b9a9e9311 100644 --- a/UI/DevScreens.cpp +++ b/UI/DevScreens.cpp @@ -1141,7 +1141,7 @@ void JitCompareScreen::OnRandomBlock(int flag) { MIPSOpcode opcode = Memory::Read_Instruction(addr); if (MIPSGetInfo(opcode) & flag) { char temp[256]; - MIPSDisAsm(opcode, addr, temp); + MIPSDisAsm(opcode, addr, temp, sizeof(temp)); // INFO_LOG(HLE, "Stopping at random instruction: %08x %s", addr, temp); anyWanted = true; break; diff --git a/unittest/JitHarness.cpp b/unittest/JitHarness.cpp index f9dd036ac2..44f0cdf751 100644 --- a/unittest/JitHarness.cpp +++ b/unittest/JitHarness.cpp @@ -164,7 +164,7 @@ bool TestJit() { addr = currentMIPS->pc; for (size_t j = 0; j < ARRAY_SIZE(lines); ++j) { char line[512]; - MIPSDisAsm(Memory::Read_Instruction(addr), addr, line, true); + MIPSDisAsm(Memory::Read_Instruction(addr), addr, line, sizeof(line), true); addr += 4; printf("%s\n", line); }