Fix crash on unaligned SSE instructions

movaps requires the memory address to be 16-byte aligned. Make sure our buffer is properly aligned for the xmm0-7 registers.
This commit is contained in:
Peace-Maker 2018-10-05 14:13:12 +02:00
parent ef60b120cc
commit 274b4276c0
2 changed files with 32 additions and 14 deletions

View File

@ -151,14 +151,15 @@ CRegisters::CRegisters(ke::Vector<Register_t> registers)
// ======================================================================== // ========================================================================
// >> 128-bit XMM registers // >> 128-bit XMM registers
// ======================================================================== // ========================================================================
m_xmm0 = CreateRegister(registers, XMM0, 16); // Copying data from xmm0-7 requires the memory address to be 16-byte aligned.
m_xmm1 = CreateRegister(registers, XMM1, 16); m_xmm0 = CreateRegister(registers, XMM0, 16, 16);
m_xmm2 = CreateRegister(registers, XMM2, 16); m_xmm1 = CreateRegister(registers, XMM1, 16, 16);
m_xmm3 = CreateRegister(registers, XMM3, 16); m_xmm2 = CreateRegister(registers, XMM2, 16, 16);
m_xmm4 = CreateRegister(registers, XMM4, 16); m_xmm3 = CreateRegister(registers, XMM3, 16, 16);
m_xmm5 = CreateRegister(registers, XMM5, 16); m_xmm4 = CreateRegister(registers, XMM4, 16, 16);
m_xmm6 = CreateRegister(registers, XMM6, 16); m_xmm5 = CreateRegister(registers, XMM5, 16, 16);
m_xmm7 = CreateRegister(registers, XMM7, 16); m_xmm6 = CreateRegister(registers, XMM6, 16, 16);
m_xmm7 = CreateRegister(registers, XMM7, 16, 16);
// 64-bit mode only // 64-bit mode only
/* /*
@ -360,13 +361,13 @@ CRegisters::~CRegisters()
DeleteRegister(m_st7); DeleteRegister(m_st7);
} }
CRegister* CRegisters::CreateRegister(ke::Vector<Register_t>& registers, Register_t reg, int iSize) CRegister* CRegisters::CreateRegister(ke::Vector<Register_t>& registers, Register_t reg, uint16_t iSize, uint16_t iAlignment)
{ {
for(size_t i = 0; i < registers.length(); i++) for(size_t i = 0; i < registers.length(); i++)
{ {
if (registers[i] == reg) if (registers[i] == reg)
{ {
return new CRegister(iSize); return new CRegister(iSize, iAlignment);
} }
} }
return NULL; return NULL;

View File

@ -216,15 +216,31 @@ enum Register_t
class CRegister class CRegister
{ {
public: public:
CRegister(int iSize) CRegister(uint16_t iSize, uint16_t iAlignment = 0)
{ {
m_iSize = iSize; m_iSize = iSize;
m_iAlignment = iAlignment;
if (iAlignment > 0)
#ifdef KE_WINDOWS
m_pAddress = _aligned_malloc(iSize, iAlignment);
#else
m_pAddress = aligned_alloc(iAlignment, iSize);
#endif
else
m_pAddress = malloc(iSize); m_pAddress = malloc(iSize);
} }
~CRegister() ~CRegister()
{ {
#ifdef KE_WINDOWS
if (m_iAlignment > 0)
_aligned_free(m_pAddress);
else
free(m_pAddress); free(m_pAddress);
#else
free(m_pAddress);
#endif
} }
template<class T> template<class T>
@ -252,7 +268,8 @@ public:
} }
public: public:
int m_iSize; uint16_t m_iSize;
uint16_t m_iAlignment;
void* m_pAddress; void* m_pAddress;
}; };
@ -269,7 +286,7 @@ public:
CRegister* GetRegister(Register_t reg); CRegister* GetRegister(Register_t reg);
private: private:
CRegister* CreateRegister(ke::Vector<Register_t>& registers, Register_t reg, int iSize); CRegister* CreateRegister(ke::Vector<Register_t>& registers, Register_t reg, uint16_t iSize, uint16_t iAlignment = 0);
void DeleteRegister(CRegister* pRegister); void DeleteRegister(CRegister* pRegister);
public: public: