Skip to content

Commit

Permalink
Merge pull request #915 from SChernykh/dev
Browse files Browse the repository at this point in the history
Make JIT memory read-only after patching is done
  • Loading branch information
xmrig committed Jan 19, 2019
2 parents 897ff83 + 31a571d commit 5f9ebdf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Mem
static void release(cryptonight_ctx **ctx, size_t count, MemInfo &info);

static void *allocateExecutableMemory(size_t size);
static void protectExecutableMemory(void *p, size_t size);
static void flushInstructionCache(void *p, size_t size);

static inline bool isHugepagesAvailable() { return (m_flags & HugepagesAvailable) != 0; }
Expand Down
6 changes: 6 additions & 0 deletions src/Mem_unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,12 @@ void *Mem::allocateExecutableMemory(size_t size)
}


void Mem::protectExecutableMemory(void *p, size_t size)
{
mprotect(p, size, PROT_READ | PROT_EXEC);
}


void Mem::flushInstructionCache(void *p, size_t size)
{
# ifndef __FreeBSD__
Expand Down
7 changes: 7 additions & 0 deletions src/Mem_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ void *Mem::allocateExecutableMemory(size_t size)
}


void Mem::protectExecutableMemory(void *p, size_t size)
{
DWORD oldProtect;
VirtualProtect(p, size, PAGE_EXECUTE_READ, &oldProtect);
}


void Mem::flushInstructionCache(void *p, size_t size)
{
::FlushInstructionCache(GetCurrentProcess(), p, size);
Expand Down
1 change: 1 addition & 0 deletions src/workers/CpuThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void xmrig::CpuThread::patchAsmVariants()
patchCode(cn_half_mainloop_bulldozer_asm, cnv2_mainloop_bulldozer_asm, xmrig::CRYPTONIGHT_HALF_ITER, xmrig::CRYPTONIGHT_MASK);
patchCode(cn_half_double_mainloop_sandybridge_asm, cnv2_double_mainloop_sandybridge_asm, xmrig::CRYPTONIGHT_HALF_ITER, xmrig::CRYPTONIGHT_MASK);

Mem::protectExecutableMemory(base, allocation_size);
Mem::flushInstructionCache(base, allocation_size);
}
#endif
Expand Down

0 comments on commit 5f9ebdf

Please sign in to comment.