JitBackpatch: fix NOP padding

The new NOP emitter breaks when called with a negative count. As it
turns out, it did happen when deoptimizing 8 bit MOVs because they are
only 4 bytes long and need no BSWAP.
This commit is contained in:
Tillmann Karras 2014-04-30 10:12:15 +02:00
parent a40ea4e26a
commit e659f5ac58
4 changed files with 18 additions and 6 deletions

View File

@ -6,6 +6,7 @@
#include "Common/Common.h" #include "Common/Common.h"
#include "Common/CPUDetect.h" #include "Common/CPUDetect.h"
#include "Common/Log.h"
#include "Common/x64Emitter.h" #include "Common/x64Emitter.h"
namespace Gen namespace Gen
@ -516,8 +517,9 @@ void XEmitter::RET() {Write8(0xC3);}
void XEmitter::RET_FAST() {Write8(0xF3); Write8(0xC3);} //two-byte return (rep ret) - recommended by AMD optimization manual for the case of jumping to a ret void XEmitter::RET_FAST() {Write8(0xF3); Write8(0xC3);} //two-byte return (rep ret) - recommended by AMD optimization manual for the case of jumping to a ret
// The first sign of decadence: optimized NOPs. // The first sign of decadence: optimized NOPs.
void XEmitter::NOP(int size) void XEmitter::NOP(size_t size)
{ {
_dbg_assert_(DYNA_REC, (int)size > 0);
while (true) while (true)
{ {
switch (size) switch (size)

View File

@ -290,7 +290,7 @@ public:
void INT3(); void INT3();
// Do nothing // Do nothing
void NOP(int count = 1); void NOP(size_t count = 1);
// Save energy in wait-loops on P4 only. Probably not too useful. // Save energy in wait-loops on P4 only. Probably not too useful.
void PAUSE(); void PAUSE();

View File

@ -187,7 +187,7 @@ const u8 *Jitx86Base::BackPatch(u8 *codePtr, u32 emAddress, void *ctx_void)
return nullptr; return nullptr;
} }
if (info.byteSwap && info.instructionSize < 5) if (info.byteSwap && info.instructionSize < BACKPATCH_SIZE)
{ {
PanicAlert("BackPatch: MOVBE is too small"); PanicAlert("BackPatch: MOVBE is too small");
return nullptr; return nullptr;
@ -217,7 +217,11 @@ const u8 *Jitx86Base::BackPatch(u8 *codePtr, u32 emAddress, void *ctx_void)
const u8 *trampoline = trampolines.GetReadTrampoline(info, registersInUse); const u8 *trampoline = trampolines.GetReadTrampoline(info, registersInUse);
emitter.CALL((void *)trampoline); emitter.CALL((void *)trampoline);
emitter.NOP((int)info.instructionSize + bswapNopCount - 5); int padding = info.instructionSize + bswapNopCount - BACKPATCH_SIZE;
if (padding > 0)
{
emitter.NOP(padding);
}
return codePtr; return codePtr;
} }
else else
@ -258,11 +262,14 @@ const u8 *Jitx86Base::BackPatch(u8 *codePtr, u32 emAddress, void *ctx_void)
XEmitter emitter(start); XEmitter emitter(start);
const u8 *trampoline = trampolines.GetWriteTrampoline(info, registersInUse); const u8 *trampoline = trampolines.GetWriteTrampoline(info, registersInUse);
emitter.CALL((void *)trampoline); emitter.CALL((void *)trampoline);
emitter.NOP((int)(codePtr + info.instructionSize - emitter.GetCodePtr())); int padding = codePtr + info.instructionSize - emitter.GetCodePtr();
if (padding > 0)
{
emitter.NOP(padding);
}
return start; return start;
} }
#else #else
return 0; return 0;
#endif #endif
} }

View File

@ -8,6 +8,9 @@
#include "Common/x64Analyzer.h" #include "Common/x64Analyzer.h"
#include "Common/x64Emitter.h" #include "Common/x64Emitter.h"
// We need at least this many bytes for backpatching.
const int BACKPATCH_SIZE = 5;
// meh. // meh.
#if defined(_WIN32) #if defined(_WIN32)
#include <windows.h> #include <windows.h>