Add debug asserts for invalid MEMPTR

Also fixed some corruptions this uncovered
This commit is contained in:
Exzap 2023-06-15 22:18:53 +02:00
parent 7886b594a2
commit 808d1bb424
13 changed files with 193 additions and 156 deletions

1
.gitignore vendored
View file

@ -38,6 +38,7 @@ bin/debugger/*
bin/sdcard/*
bin/screenshots/*
bin/dump/*
bin/cafeLibs/*
!bin/shaderCache/info.txt
bin/shaderCache/*

View file

@ -264,3 +264,5 @@ namespace MMU
uint16 ReadMMIO_16(PAddr address);
}
#define MMU_IsInPPCMemorySpace(__ptr) ((const uint8*)(__ptr) >= memory_base && (const uint8*)(__ptr) < (memory_base + 0x100000000))

View file

@ -211,6 +211,7 @@ namespace coreinit
void __OSInitiateAlarm(OSAlarm_t* alarm, uint64 startTime, uint64 period, MPTR handlerFunc, bool isPeriodic)
{
cemu_assert_debug(MMU_IsInPPCMemorySpace(alarm));
cemu_assert_debug(__OSHasSchedulerLock());
uint64 nextTime = startTime;

View file

@ -50,7 +50,7 @@ namespace coreinit
MEMList g_list3;
std::array<uint32, 3> gHeapFillValues{ 0xC3C3C3C3, 0xF3F3F3F3, 0xD3D3D3D3 };
OSSpinLock gHeapGlobalLock;
SysAllocator<OSSpinLock> gHeapGlobalLock;
MEMHeapBase* gDefaultHeap;
bool MEMHeapTable_Add(MEMHeapBase* heap)

View file

@ -263,9 +263,7 @@ namespace coreinit
thread = (OSThread_t*)memory_getPointerFromVirtualOffset(coreinit_allocFromSysArea(sizeof(OSThread_t), 32));
memset(thread, 0x00, sizeof(OSThread_t));
// init signatures
thread->context.magic0 = OS_CONTEXT_MAGIC_0;
thread->context.magic1 = OS_CONTEXT_MAGIC_1;
thread->magic = 'tHrD';
thread->SetMagic();
thread->type = threadType;
thread->state = (entryPoint != MPTR_NULL) ? OSThread_t::THREAD_STATE::STATE_READY : OSThread_t::THREAD_STATE::STATE_NONE;
thread->entrypoint = _swapEndianU32(entryPoint);
@ -563,7 +561,10 @@ namespace coreinit
// adds the thread to each core's run queue if in runable state
void __OSAddReadyThreadToRunQueue(OSThread_t* thread)
{
cemu_assert_debug(MMU_IsInPPCMemorySpace(thread));
cemu_assert_debug(thread->IsValidMagic());
cemu_assert_debug(__OSHasSchedulerLock());
if (thread->state != OSThread_t::THREAD_STATE::STATE_READY)
return;
if (thread->suspendCounter != 0)
@ -702,12 +703,20 @@ namespace coreinit
// should this reschedule the thread?
}
else if (prevAffinityMask != affinityMask)
{
if(thread->state != OSThread_t::THREAD_STATE::STATE_NONE)
{
__OSRemoveThreadFromRunQueues(thread);
thread->attr = (thread->attr & ~7) | (affinityMask & 7);
thread->context.setAffinity(affinityMask);
__OSAddReadyThreadToRunQueue(thread);
}
else
{
thread->attr = (thread->attr & ~7) | (affinityMask & 7);
thread->context.setAffinity(affinityMask);
}
}
__OSUnlockScheduler();
return true;
}

View file

@ -404,6 +404,18 @@ struct OSThread_t
return 0;
}
void SetMagic()
{
context.magic0 = OS_CONTEXT_MAGIC_0;
context.magic1 = OS_CONTEXT_MAGIC_1;
magic = 'tHrD';
}
bool IsValidMagic() const
{
return magic == 'tHrD' && context.magic0 == OS_CONTEXT_MAGIC_0 && context.magic1 == OS_CONTEXT_MAGIC_1;
}
/* +0x000 */ OSContext_t context;
/* +0x320 */ uint32be magic; // 'tHrD'
/* +0x324 */ betype<THREAD_STATE> state;

View file

@ -82,7 +82,7 @@ namespace erreula
struct ErrEula_t
{
coreinit::OSMutex mutex;
SysAllocator<coreinit::OSMutex> mutex;
uint32 regionType;
uint32 langType;
MEMPTR<coreinit::FSClient_t> fsClient;

View file

@ -23,7 +23,7 @@ memset(bossRequest, 0, sizeof(iosuBossCemuRequest_t)); \
memset(bossBufferVector, 0, sizeof(ioBufferVector_t)); \
bossBufferVector->buffer = (uint8*)bossRequest;
coreinit::OSMutex g_mutex;
SysAllocator<coreinit::OSMutex> g_mutex;
sint32 g_initCounter = 0;
bool g_isInitialized = false;

View file

@ -54,7 +54,7 @@ namespace padscore
WPADState_t g_wpad_state = kWPADStateMaster;
struct {
coreinit::OSAlarm_t alarm;
SysAllocator<coreinit::OSAlarm_t> alarm;
bool kpad_initialized = false;
struct WPADData

View file

@ -163,7 +163,7 @@ namespace vpad
struct
{
coreinit::OSAlarm_t alarm;
SysAllocator<coreinit::OSAlarm_t> alarm;
struct
{

View file

@ -37,8 +37,11 @@ public:
if (ptr == nullptr)
m_value = 0;
else
{
cemu_assert_debug((uint8*)ptr >= memory_base && (uint8*)ptr <= memory_base + 0x100000000);
m_value = (uint32)((uintptr_t)ptr - (uintptr_t)memory_base);
}
}
constexpr MEMPTR(const MEMPTR& memptr)
: m_value(memptr.m_value) { }
@ -66,7 +69,10 @@ public:
if (ptr == nullptr)
m_value = 0;
else
{
cemu_assert_debug((uint8*)ptr >= memory_base && (uint8*)ptr <= memory_base + 0x100000000);
m_value = (uint32)((uintptr_t)ptr - (uintptr_t)memory_base);
}
return *this;
}

View file

@ -296,6 +296,61 @@ inline unsigned char _addcarry_u64(unsigned char carry, unsigned long long a, un
#endif
// asserts
inline void cemu_assert(bool _condition)
{
if ((_condition) == false)
{
DEBUG_BREAK;
}
}
#ifndef CEMU_DEBUG_ASSERT
//#define cemu_assert_debug(__cond) -> Forcing __cond not to be evaluated currently has unexpected side-effects
inline void cemu_assert_debug(bool _condition)
{
}
inline void cemu_assert_unimplemented()
{
}
inline void cemu_assert_suspicious()
{
}
inline void cemu_assert_error()
{
DEBUG_BREAK;
}
#else
inline void cemu_assert_debug(bool _condition)
{
if ((_condition) == false)
DEBUG_BREAK;
}
inline void cemu_assert_unimplemented()
{
DEBUG_BREAK;
}
inline void cemu_assert_suspicious()
{
DEBUG_BREAK;
}
inline void cemu_assert_error()
{
DEBUG_BREAK;
}
#endif
#define assert_dbg() DEBUG_BREAK // old style unconditional generic assert
// MEMPTR
#include "Common/MemPtr.h"
@ -380,58 +435,6 @@ bool match_any_of(T1 value, T2 compareTo, Types&&... others)
#endif
}
inline void cemu_assert(bool _condition)
{
if ((_condition) == false)
{
DEBUG_BREAK;
}
}
#ifndef CEMU_DEBUG_ASSERT
//#define cemu_assert_debug(__cond) -> Forcing __cond not to be evaluated currently has unexpected side-effects
inline void cemu_assert_debug(bool _condition)
{
}
inline void cemu_assert_unimplemented()
{
}
inline void cemu_assert_suspicious()
{
}
inline void cemu_assert_error()
{
DEBUG_BREAK;
}
#else
inline void cemu_assert_debug(bool _condition)
{
if ((_condition) == false)
DEBUG_BREAK;
}
inline void cemu_assert_unimplemented()
{
DEBUG_BREAK;
}
inline void cemu_assert_suspicious()
{
DEBUG_BREAK;
}
inline void cemu_assert_error()
{
DEBUG_BREAK;
}
#endif
#define assert_dbg() DEBUG_BREAK // old style unconditional generic assert
// Some string conversion helpers because C++20 std::u8string is too cumbersome to use in practice
// mixing string types generally causes loads of issues and many of the libraries we use dont expose interfaces for u8string

View file

@ -173,6 +173,8 @@ void DebugPPCThreadsWindow::RefreshThreadList()
const int scrollPos = m_thread_list->GetScrollPos(0);
m_thread_list->DeleteAllItems();
if(activeThreadCount > 0)
{
__OSLockScheduler();
srwlock_activeThreadList.LockWrite();
for (sint32 i = 0; i < activeThreadCount; i++)
@ -267,6 +269,7 @@ void DebugPPCThreadsWindow::RefreshThreadList()
}
srwlock_activeThreadList.UnlockWrite();
__OSUnlockScheduler();
}
m_thread_list->SetScrollPos(0, scrollPos, true);
}