From 9e1a9ed36a616499ecf89263e3ee353a8c23dc43 Mon Sep 17 00:00:00 2001 From: Jon Haslam Date: Mon, 27 Feb 2023 16:49:42 -0800 Subject: [PATCH] Replace nop instructions with ud2 (#84) --- src/OIDebugger.cpp | 17 +++++------------ src/OIDebugger.h | 1 + src/X86InstDefs.h | 2 ++ 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/OIDebugger.cpp b/src/OIDebugger.cpp index 800ff770..1925c303 100644 --- a/src/OIDebugger.cpp +++ b/src/OIDebugger.cpp @@ -2113,7 +2113,6 @@ bool OIDebugger::writePrologue( const prequest &preq, const OICompiler::RelocResult::SymTable &jitSymbols) { size_t off = 0; uint8_t newInsts[prologueLength]; - memset(newInsts, nopInst /* NOP */, sizeof(newInsts)); /* * Global probes don't have multiple arguments, but calling `getReqForArg(X)` @@ -2136,17 +2135,6 @@ bool OIDebugger::writePrologue( VLOG(1) << "Generating prologue for argument '" << req.arg << "', using probe at " << (void *)jitCodeStart->second; - /* - * With the move to an INT3 to regain control of the target thread I'm - * not convinced that we actually need to do any of this now. We may be - * able to simply tack an INT3 on to the end of the JIT'd code sequence - * (obviously we wouldn't ever execute the 'ret' there but that doesn't - * really matter). - */ - /* - * movabs is really a synthetic for a REX prefixed mov instruction. - * The REX prefix opcode is 0x48 (REX.W == 1). - */ newInsts[off++] = movabsrdi0Inst; newInsts[off++] = movabsrdi1Inst; remoteObjAddrs.emplace(std::move(jitCodeStart->first), @@ -2177,6 +2165,11 @@ bool OIDebugger::writePrologue( newInsts[off++] = int3Inst; + while (off <= prologueLength - sizeofUd2) { + newInsts[off++] = ud2Inst0; + newInsts[off++] = ud2Inst1; + } + assert(off <= prologueLength); return writeTargetMemory(&newInsts, (void *)segConfig.textSegBase, diff --git a/src/OIDebugger.h b/src/OIDebugger.h index 0187eb72..774a154c 100644 --- a/src/OIDebugger.h +++ b/src/OIDebugger.h @@ -160,6 +160,7 @@ class OIDebugger { uint64_t count{}; bool sigIntHandlerActive{false}; const int sizeofInt3 = 1; + const int sizeofUd2 = 2; const int replayInstSize = 512; bool trapsRemoved{false}; std::shared_ptr symbols; diff --git a/src/X86InstDefs.h b/src/X86InstDefs.h index c3ec2f8c..d8932696 100644 --- a/src/X86InstDefs.h +++ b/src/X86InstDefs.h @@ -24,3 +24,5 @@ static constexpr uint8_t movabsrax1Inst = 0xb8; static constexpr uint8_t callRaxInst0Inst = 0xff; static constexpr uint8_t callRaxInst1Inst = 0xd0; static constexpr long syscallInsts = 0x9090909090050fcc; +static constexpr uint8_t ud2Inst0 = 0x0f; +static constexpr uint8_t ud2Inst1 = 0x0b;