Skip to content

Commit

Permalink
MemoryUtil: add CheckRIPRelative()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tilka committed Jun 6, 2015
1 parent 49602a9 commit f4eb5d5
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 1 deletion.
33 changes: 33 additions & 0 deletions Source/Core/Common/MemoryUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "Common/CommonFuncs.h"
#include "Common/CommonTypes.h"
#include "Common/FileUtil.h"
#include "Common/MemoryUtil.h"
#include "Common/MsgHandler.h"
#include "Common/Logging/Log.h"
Expand Down Expand Up @@ -185,3 +186,35 @@ size_t MemPhysical()
return (size_t)memInfo.totalram * memInfo.mem_unit;
#endif
}

void CheckRIPRelative(const void* addr, size_t size)
{
#if defined(_M_X86_64) && defined(__linux__)
static u8* low = nullptr;
static u8* high = nullptr;
if (!low)
{
char* exe_name = realpath("/proc/self/exe", nullptr);
std::ifstream maps("/proc/self/maps");
std::string line;
while (std::getline(maps, line))
{
if (line.rfind(exe_name) != std::string::npos)
{
uintptr_t start, end;
sscanf(line.c_str(), "%16lx-%16lx", &start, &end);
if (!low)
low = (u8*)start;
else
high = (u8*)end;
}
}
free(exe_name);
_assert_(low && high);
}
if ((u8*)addr + size - 0x80000000ll > low || (u8*)addr + 0x80000000ll < high)
PanicAlert("%p can't be used for RIP-relative addressing. "
"For GDB: \"set disable-randomization off\" "
"(you can add this to ~/.gdbinit).", addr);
#endif
}
1 change: 1 addition & 0 deletions Source/Core/Common/MemoryUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void WriteProtectMemory(void* ptr, size_t size, bool executable = false);
void UnWriteProtectMemory(void* ptr, size_t size, bool allowExecute = false);
std::string MemUsage();
size_t MemPhysical();
void CheckRIPRelative(const void* addr, size_t size);

void GuardMemoryMake(void* ptr, size_t size);
void GuardMemoryUnmake(void* ptr, size_t size);
Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Core/PowerPC/Jit64IL/JitIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "Common/Common.h"
#include "Common/FileUtil.h"
#include "Common/Intrinsics.h"
#include "Common/MemoryUtil.h"
#include "Common/StdMakeUnique.h"
#include "Common/StringUtil.h"
#include "Core/PatchEngine.h"
Expand Down Expand Up @@ -251,6 +252,7 @@ void JitIL::Init()

trampolines.Init(jo.memcheck ? TRAMPOLINE_CODE_SIZE_MMU : TRAMPOLINE_CODE_SIZE);
AllocCodeSpace(CODE_SIZE, PPCSTATE_BASE);
CheckRIPRelative(region, CODE_SIZE);
blocks.Init();
asm_routines.Init(nullptr);

Expand Down
3 changes: 2 additions & 1 deletion Source/Core/Core/PowerPC/JitCommon/Jit_Util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "Common/BitSet.h"
#include "Common/CPUDetect.h"
#include "Common/MemoryUtil.h"
#include "Common/x64Emitter.h"
#include "Core/PowerPC/PowerPC.h"

Expand All @@ -31,7 +32,7 @@ class FarCodeCache : public Gen::X64CodeBlock
bool m_enabled = false;
public:
bool Enabled() { return m_enabled; }
void Init(int size) { AllocCodeSpace(size, PPCSTATE_BASE); m_enabled = true; }
void Init(int size) { AllocCodeSpace(size, PPCSTATE_BASE); CheckRIPRelative(region, size); m_enabled = true; }
void Shutdown() { FreeCodeSpace(); m_enabled = false; }
};

Expand Down
2 changes: 2 additions & 0 deletions Source/Core/Core/PowerPC/JitCommon/TrampolineCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "Common/CommonTypes.h"
#include "Common/JitRegister.h"
#include "Common/MemoryUtil.h"
#include "Common/StringUtil.h"
#include "Common/x64ABI.h"
#include "Core/HW/Memmap.h"
Expand All @@ -23,6 +24,7 @@ using namespace Gen;
void TrampolineCache::Init(int size)
{
AllocCodeSpace(size, PPCSTATE_BASE);
CheckRIPRelative(region, size);
}

void TrampolineCache::ClearCodeSpace()
Expand Down
1 change: 1 addition & 0 deletions Source/UnitTests/Common/x64EmitterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class x64EmitterTest : public testing::Test
emitter.reset(new X64CodeBlock());
emitter->AllocCodeSpace(4096, PPCSTATE_BASE);
code_buffer = emitter->GetWritableCodePtr();
CheckRIPRelative(code_buffer, 4096);

disasm.reset(new disassembler);
disasm->set_syntax_intel();
Expand Down

0 comments on commit f4eb5d5

Please sign in to comment.