#include "sanitizer_common/sanitizer_common.h"
#include "xray_defs.h"
#include "xray_interface_internal.h"
#include <atomic>
namespace __xray {
enum PatchOpcodes : uint32_t {
PO_DADDIU = 0x64000000,
PO_SD = 0xFC000000,
PO_LUI = 0x3C000000,
PO_ORI = 0x34000000,
PO_DSLL = 0x00000038,
PO_JALR = 0x00000009,
PO_LD = 0xDC000000,
PO_B60 = 0x1000000f,
PO_NOP = 0x0,
};
enum RegNum : uint32_t {
RN_T0 = 0xC,
RN_T9 = 0x19,
RN_RA = 0x1F,
RN_SP = 0x1D,
};
inline static uint32_t encodeInstruction(uint32_t Opcode, uint32_t Rs,
uint32_t Rt,
uint32_t Imm) XRAY_NEVER_INSTRUMENT {
return (Opcode | Rs << 21 | Rt << 16 | Imm);
}
inline static uint32_t
encodeSpecialInstruction(uint32_t Opcode, uint32_t Rs, uint32_t Rt, uint32_t Rd,
uint32_t Imm) XRAY_NEVER_INSTRUMENT {
return (Rs << 21 | Rt << 16 | Rd << 11 | Imm << 6 | Opcode);
}
inline static bool patchSled(const bool Enable, const uint32_t FuncId,
const XRaySledEntry &Sled,
void (*TracingHook)()) XRAY_NEVER_INSTRUMENT {
uint32_t *Address = reinterpret_cast<uint32_t *>(Sled.address());
if (Enable) {
uint32_t LoTracingHookAddr =
reinterpret_cast<int64_t>(TracingHook) & 0xffff;
uint32_t HiTracingHookAddr =
(reinterpret_cast<int64_t>(TracingHook) >> 16) & 0xffff;
uint32_t HigherTracingHookAddr =
(reinterpret_cast<int64_t>(TracingHook) >> 32) & 0xffff;
uint32_t HighestTracingHookAddr =
(reinterpret_cast<int64_t>(TracingHook) >> 48) & 0xffff;
uint32_t LoFunctionID = FuncId & 0xffff;
uint32_t HiFunctionID = (FuncId >> 16) & 0xffff;
Address[2] = encodeInstruction(PatchOpcodes::PO_SD, RegNum::RN_SP,
RegNum::RN_RA, 0x8);
Address[3] = encodeInstruction(PatchOpcodes::PO_SD, RegNum::RN_SP,
RegNum::RN_T9, 0x0);
Address[4] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T9,
HighestTracingHookAddr);
Address[5] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
RegNum::RN_T9, HigherTracingHookAddr);
Address[6] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,
RegNum::RN_T9, RegNum::RN_T9, 0x10);
Address[7] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
RegNum::RN_T9, HiTracingHookAddr);
Address[8] = encodeSpecialInstruction(PatchOpcodes::PO_DSLL, 0x0,
RegNum::RN_T9, RegNum::RN_T9, 0x10);
Address[9] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T9,
RegNum::RN_T9, LoTracingHookAddr);
Address[10] = encodeInstruction(PatchOpcodes::PO_LUI, 0x0, RegNum::RN_T0,
HiFunctionID);
Address[11] = encodeSpecialInstruction(PatchOpcodes::PO_JALR, RegNum::RN_T9,
0x0, RegNum::RN_RA, 0X0);
Address[12] = encodeInstruction(PatchOpcodes::PO_ORI, RegNum::RN_T0,
RegNum::RN_T0, LoFunctionID);
Address[13] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,
RegNum::RN_T9, 0x0);
Address[14] = encodeInstruction(PatchOpcodes::PO_LD, RegNum::RN_SP,
RegNum::RN_RA, 0x8);
Address[15] = encodeInstruction(PatchOpcodes::PO_DADDIU, RegNum::RN_SP,
RegNum::RN_SP, 0x10);
uint32_t CreateStackSpace = encodeInstruction(
PatchOpcodes::PO_DADDIU, RegNum::RN_SP, RegNum::RN_SP, 0xfff0);
std::atomic_store_explicit(
reinterpret_cast<std::atomic<uint32_t> *>(Address), CreateStackSpace,
std::memory_order_release);
} else {
std::atomic_store_explicit(
reinterpret_cast<std::atomic<uint32_t> *>(Address),
uint32_t(PatchOpcodes::PO_B60), std::memory_order_release);
}
return true;
}
bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
const XRaySledEntry &Sled,
void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
return patchSled(Enable, FuncId, Sled, Trampoline);
}
bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
}
bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
return patchSled(Enable, FuncId, Sled, __xray_FunctionExit);
}
bool patchCustomEvent(const bool Enable, const uint32_t FuncId,
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
return false;
}
bool patchTypedEvent(const bool Enable, const uint32_t FuncId,
const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
return false;
}
}
extern "C" void __xray_ArgLoggerEntry() XRAY_NEVER_INSTRUMENT {
}