diff --git a/Core/ELF/ElfReader.cpp b/Core/ELF/ElfReader.cpp index b8fd5522fb..463eede050 100644 --- a/Core/ELF/ElfReader.cpp +++ b/Core/ELF/ElfReader.cpp @@ -26,15 +26,19 @@ const char *ElfReader::GetSectionName(int section) const { if (sections[section].sh_type == SHT_NULL) - return 0; + return nullptr; int nameOffset = sections[section].sh_name; + if (nameOffset < 0) { // TODO: Where can we get a solid upper limit? + ERROR_LOG(LOADER, "ELF: Bad name offset %d in section %d", nameOffset, section); + return nullptr; + } const char *ptr = (const char *)GetSectionDataPtr(header->e_shstrndx); if (ptr) return ptr + nameOffset; else - return 0; + return nullptr; } void addrToHiLo(u32 addr, u16 &hi, s16 &lo) @@ -49,7 +53,7 @@ void addrToHiLo(u32 addr, u16 &hi, s16 &lo) } } -bool ElfReader::LoadRelocations(Elf32_Rel *rels, int numRelocs) +bool ElfReader::LoadRelocations(const Elf32_Rel *rels, int numRelocs) { int numErrors = 0; DEBUG_LOG(LOADER, "Loading %i relocations...", numRelocs); @@ -196,7 +200,6 @@ bool ElfReader::LoadRelocations(Elf32_Rel *rels, int numRelocs) void ElfReader::LoadRelocations2(int rel_seg) { - Elf32_Phdr *ph; u8 *buf, *end, *flag_table, *type_table; int flag_table_size, type_table_size; int flag_bits, seg_bits, type_bits; @@ -206,8 +209,7 @@ void ElfReader::LoadRelocations2(int rel_seg) u32 op, addr; int rcount = 0; - ph = segments + rel_seg; - + const Elf32_Phdr *ph = segments + rel_seg; buf = (u8*)GetSegmentPtr(rel_seg); end = buf+ph->p_filesz; @@ -376,7 +378,7 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) // Look for the module info - we need to know whether this is kernel or user. const PspModuleInfo *modInfo = 0; for (int i = 0; i < GetNumSections(); i++) { - Elf32_Shdr *s = §ions[i]; + const Elf32_Shdr *s = §ions[i]; const char *name = GetSectionName(i); if (name && !strcmp(name, ".rodata.sceModuleInfo")) { modInfo = (const PspModuleInfo *)GetPtr(s->sh_offset); @@ -398,7 +400,7 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) u32 totalStart = 0xFFFFFFFF; u32 totalEnd = 0; for (int i = 0; i < header->e_phnum; i++) { - Elf32_Phdr *p = &segments[i]; + const Elf32_Phdr *p = &segments[i]; if (p->p_type == PT_LOAD) { if (p->p_vaddr < totalStart) totalStart = p->p_vaddr; @@ -447,7 +449,7 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) for (int i = 0; i < header->e_phnum; i++) { - Elf32_Phdr *p = segments + i; + const Elf32_Phdr *p = segments + i; DEBUG_LOG(LOADER, "Type: %08x Vaddr: %08x Filesz: %08x Memsz: %08x ", (int)p->p_type, (u32)p->p_vaddr, (int)p->p_filesz, (int)p->p_memsz); if (p->p_type == PT_LOAD) @@ -455,7 +457,7 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) segmentVAddr[i] = baseAddress + p->p_vaddr; u32 writeAddr = segmentVAddr[i]; - u8 *src = GetSegmentPtr(i); + const u8 *src = GetSegmentPtr(i); u8 *dst = Memory::GetPointer(writeAddr); u32 srcSize = p->p_filesz; u32 dstSize = p->p_memsz; @@ -476,7 +478,7 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) for (int i = 0; i < GetNumSections(); i++) { - Elf32_Shdr *s = §ions[i]; + const Elf32_Shdr *s = §ions[i]; const char *name = GetSectionName(i); u32 writeAddr = s->sh_addr + baseAddress; @@ -498,7 +500,7 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) // Second pass: Do necessary relocations for (int i = 0; i < GetNumSections(); i++) { - Elf32_Shdr *s = §ions[i]; + const Elf32_Shdr *s = §ions[i]; const char *name = GetSectionName(i); if (s->sh_type == SHT_PSPREL) @@ -559,7 +561,7 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) if (GetNumSections() == 0) { for (int i = 0; i < header->e_phnum; i++) { - Elf32_Phdr *p = &segments[i]; + const Elf32_Phdr *p = &segments[i]; if (p->p_type == PT_PSPREL1) { INFO_LOG(LOADER,"Loading segment relocations"); int numRelocs = p->p_filesz / sizeof(Elf32_Rel); diff --git a/Core/ELF/ElfReader.h b/Core/ELF/ElfReader.h index 407c2afcc5..5895a4a6c3 100644 --- a/Core/ELF/ElfReader.h +++ b/Core/ELF/ElfReader.h @@ -49,17 +49,17 @@ typedef int SectionID; class ElfReader { public: - ElfReader(void *ptr) : + ElfReader(const void *ptr) : sectionOffsets(0), sectionAddrs(0), bRelocate(false), entryPoint(0), vaddr(0) { - base = (char*)ptr; - base32 = (u32 *)ptr; - header = (Elf32_Ehdr*)ptr; - segments = (Elf32_Phdr *)(base + header->e_phoff); - sections = (Elf32_Shdr *)(base + header->e_shoff); + base = (const char*)ptr; + base32 = (const u32 *)ptr; + header = (const Elf32_Ehdr*)ptr; + segments = (const Elf32_Phdr *)(base + header->e_phoff); + sections = (const Elf32_Shdr *)(base + header->e_shoff); } ~ElfReader() { @@ -80,10 +80,10 @@ public: int GetNumSegments() const { return (int)(header->e_phnum); } int GetNumSections() const { return (int)(header->e_shnum); } const char *GetSectionName(int section) const; - u8 *GetPtr(u32 offset) const { - return (u8*)base + offset; + const u8 *GetPtr(u32 offset) const { + return (const u8*)base + offset; } - u8 *GetSectionDataPtr(int section) const { + const u8 *GetSectionDataPtr(int section) const { if (section < 0 || section >= header->e_shnum) return nullptr; if (sections[section].sh_type != SHT_NOBITS) @@ -91,7 +91,7 @@ public: else return nullptr; } - u8 *GetSegmentPtr(int segment) const { + const u8 *GetSegmentPtr(int segment) const { return GetPtr(segments[segment].p_offset); } u32 GetSectionAddr(SectionID section) const { @@ -134,19 +134,17 @@ public: u32 GetTotalDataSize() const; u32 GetTotalSectionSizeByPrefix(const std::string &prefix) const; - // More indepth stuff:) int LoadInto(u32 vaddr, bool fromTop); bool LoadSymbols(); - bool LoadRelocations(Elf32_Rel *rels, int numRelocs); + bool LoadRelocations(const Elf32_Rel *rels, int numRelocs); void LoadRelocations2(int rel_seg); - private: - char *base; - u32 *base32; - Elf32_Ehdr *header; - Elf32_Phdr *segments; - Elf32_Shdr *sections; + const char *base; + const u32 *base32; + const Elf32_Ehdr *header; + const Elf32_Phdr *segments; + const Elf32_Shdr *sections; u32 *sectionOffsets; u32 *sectionAddrs; bool bRelocate; diff --git a/Core/HLE/sceKernelMutex.cpp b/Core/HLE/sceKernelMutex.cpp index 754736e6c0..39e20fd952 100644 --- a/Core/HLE/sceKernelMutex.cpp +++ b/Core/HLE/sceKernelMutex.cpp @@ -932,6 +932,11 @@ int sceKernelTryLockLwMutex(u32 workareaPtr, int count) { DEBUG_LOG(SCEKERNEL, "sceKernelTryLockLwMutex(%08x, %i)", workareaPtr, count); + if (!Memory::IsValidAddress(workareaPtr)) { + ERROR_LOG(SCEKERNEL, "Bad workarea pointer for LwMutex"); + return SCE_KERNEL_ERROR_ACCESS_ERROR; + } + auto workarea = PSPPointer::Create(workareaPtr); u32 error = 0; @@ -948,6 +953,11 @@ int sceKernelTryLockLwMutex_600(u32 workareaPtr, int count) { DEBUG_LOG(SCEKERNEL, "sceKernelTryLockLwMutex_600(%08x, %i)", workareaPtr, count); + if (!Memory::IsValidAddress(workareaPtr)) { + ERROR_LOG(SCEKERNEL, "Bad workarea pointer for LwMutex"); + return SCE_KERNEL_ERROR_ACCESS_ERROR; + } + auto workarea = PSPPointer::Create(workareaPtr); u32 error = 0; @@ -963,6 +973,11 @@ int sceKernelLockLwMutex(u32 workareaPtr, int count, u32 timeoutPtr) { VERBOSE_LOG(SCEKERNEL, "sceKernelLockLwMutex(%08x, %i, %08x)", workareaPtr, count, timeoutPtr); + if (!Memory::IsValidAddress(workareaPtr)) { + ERROR_LOG(SCEKERNEL, "Bad workarea pointer for LwMutex"); + return SCE_KERNEL_ERROR_ACCESS_ERROR; + } + auto workarea = PSPPointer::Create(workareaPtr); u32 error = 0; @@ -994,6 +1009,11 @@ int sceKernelLockLwMutexCB(u32 workareaPtr, int count, u32 timeoutPtr) { VERBOSE_LOG(SCEKERNEL, "sceKernelLockLwMutexCB(%08x, %i, %08x)", workareaPtr, count, timeoutPtr); + if (!Memory::IsValidAddress(workareaPtr)) { + ERROR_LOG(SCEKERNEL, "Bad workarea pointer for LwMutex"); + return SCE_KERNEL_ERROR_ACCESS_ERROR; + } + auto workarea = PSPPointer::Create(workareaPtr); u32 error = 0; @@ -1025,6 +1045,11 @@ int sceKernelUnlockLwMutex(u32 workareaPtr, int count) { VERBOSE_LOG(SCEKERNEL, "sceKernelUnlockLwMutex(%08x, %i)", workareaPtr, count); + if (!Memory::IsValidAddress(workareaPtr)) { + ERROR_LOG(SCEKERNEL, "Bad workarea pointer for LwMutex"); + return SCE_KERNEL_ERROR_ACCESS_ERROR; + } + auto workarea = PSPPointer::Create(workareaPtr); if (workarea->uid == -1) @@ -1093,8 +1118,10 @@ int sceKernelReferLwMutexStatusByID(SceUID uid, u32 infoPtr) int sceKernelReferLwMutexStatus(u32 workareaPtr, u32 infoPtr) { - if (!Memory::IsValidAddress(workareaPtr)) - return -1; + if (!Memory::IsValidAddress(workareaPtr)) { + ERROR_LOG(SCEKERNEL, "Bad workarea pointer for LwMutex"); + return SCE_KERNEL_ERROR_ACCESS_ERROR; + } auto workarea = PSPPointer::Create(workareaPtr);