|
| 1 | +//===-- xray_interface.cpp --------------------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file is a part of XRay, a dynamic runtime instrumentation system. |
| 11 | +// |
| 12 | +// Implementation of the API functions. |
| 13 | +// |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "xray_interface_internal.h" |
| 17 | +#include <atomic> |
| 18 | +#include <cstdint> |
| 19 | +#include <cstdio> |
| 20 | +#include <errno.h> |
| 21 | +#include <limits> |
| 22 | +#include <sys/mman.h> |
| 23 | + |
| 24 | +namespace __xray { |
| 25 | + |
| 26 | +// This is the function to call when we encounter the entry or exit sleds. |
| 27 | +std::atomic<void (*)(int32_t, XRayEntryType)> XRayPatchedFunction{nullptr}; |
| 28 | + |
| 29 | +} // namespace __xray |
| 30 | + |
| 31 | +extern "C" { |
| 32 | +// The following functions have to be defined in assembler, on a per-platform |
| 33 | +// basis. See xray_trampoline_*.s files for implementations. |
| 34 | +extern void __xray_FunctionEntry(); |
| 35 | +extern void __xray_FunctionExit(); |
| 36 | +} |
| 37 | + |
| 38 | +extern std::atomic<bool> XRayInitialized; |
| 39 | +extern std::atomic<__xray::XRaySledMap> XRayInstrMap; |
| 40 | + |
| 41 | +int __xray_set_handler(void (*entry)(int32_t, XRayEntryType)) { |
| 42 | + if (XRayInitialized.load(std::memory_order_acquire)) { |
| 43 | + __xray::XRayPatchedFunction.store(entry, std::memory_order_release); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +std::atomic<bool> XRayPatching{false}; |
| 50 | + |
| 51 | +XRayPatchingStatus __xray_patch() { |
| 52 | + // FIXME: Make this happen asynchronously. For now just do this sequentially. |
| 53 | + if (!XRayInitialized.load(std::memory_order_acquire)) |
| 54 | + return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized. |
| 55 | + |
| 56 | + static bool NotPatching = false; |
| 57 | + if (!XRayPatching.compare_exchange_strong(NotPatching, true, |
| 58 | + std::memory_order_acq_rel, |
| 59 | + std::memory_order_acquire)) { |
| 60 | + return XRayPatchingStatus::ONGOING; // Already patching. |
| 61 | + } |
| 62 | + |
| 63 | + // Step 1: Compute the function id, as a unique identifier per function in the |
| 64 | + // instrumentation map. |
| 65 | + __xray::XRaySledMap InstrMap = XRayInstrMap.load(std::memory_order_acquire); |
| 66 | + if (InstrMap.Entries == 0) |
| 67 | + return XRayPatchingStatus::NOT_INITIALIZED; |
| 68 | + |
| 69 | + int32_t FuncId = 1; |
| 70 | + static constexpr uint8_t CallOpCode = 0xe8; |
| 71 | + static constexpr uint16_t MovR10Seq = 0xba41; |
| 72 | + static constexpr uint8_t JmpOpCode = 0xe9; |
| 73 | + uint64_t CurFun = 0; |
| 74 | + for (std::size_t I = 0; I < InstrMap.Entries; I++) { |
| 75 | + auto Sled = InstrMap.Sleds[I]; |
| 76 | + auto F = Sled.Function; |
| 77 | + if (CurFun == 0) |
| 78 | + CurFun = F; |
| 79 | + if (F != CurFun) { |
| 80 | + ++FuncId; |
| 81 | + CurFun = F; |
| 82 | + } |
| 83 | + |
| 84 | + // While we're here, we should patch the nop sled. To do that we mprotect |
| 85 | + // the page containing the function to be writeable. |
| 86 | + void *PageAlignedAddr = |
| 87 | + reinterpret_cast<void *>(Sled.Address & ~((2 << 16) - 1)); |
| 88 | + std::size_t MProtectLen = |
| 89 | + (Sled.Address + 12) - reinterpret_cast<uint64_t>(PageAlignedAddr); |
| 90 | + if (mprotect(PageAlignedAddr, MProtectLen, |
| 91 | + PROT_READ | PROT_WRITE | PROT_EXEC) == -1) { |
| 92 | + printf("Failed mprotect: %d\n", errno); |
| 93 | + return XRayPatchingStatus::FAILED; |
| 94 | + } |
| 95 | + |
| 96 | + static constexpr int64_t MinOffset{std::numeric_limits<int32_t>::min()}; |
| 97 | + static constexpr int64_t MaxOffset{std::numeric_limits<int32_t>::max()}; |
| 98 | + if (Sled.Kind == XRayEntryType::ENTRY) { |
| 99 | + // Here we do the dance of replacing the following sled: |
| 100 | + // |
| 101 | + // xray_sled_n: |
| 102 | + // jmp +9 |
| 103 | + // <9 byte nop> |
| 104 | + // |
| 105 | + // With the following: |
| 106 | + // |
| 107 | + // mov r10d, <function id> |
| 108 | + // call <relative 32bit offset to entry trampoline> |
| 109 | + // |
| 110 | + // We need to do this in the following order: |
| 111 | + // |
| 112 | + // 1. Put the function id first, 2 bytes from the start of the sled (just |
| 113 | + // after the 2-byte jmp instruction). |
| 114 | + // 2. Put the call opcode 6 bytes from the start of the sled. |
| 115 | + // 3. Put the relative offset 7 bytes from the start of the sled. |
| 116 | + // 4. Do an atomic write over the jmp instruction for the "mov r10d" |
| 117 | + // opcode and first operand. |
| 118 | + // |
| 119 | + // Prerequisite is to compute the relative offset to the |
| 120 | + // __xray_FunctionEntry function's address. |
| 121 | + int64_t TrampolineOffset = |
| 122 | + reinterpret_cast<int64_t>(__xray_FunctionEntry) - |
| 123 | + (static_cast<int64_t>(Sled.Address) + 11); |
| 124 | + if (TrampolineOffset < MinOffset || TrampolineOffset > MaxOffset) { |
| 125 | + // FIXME: Print out an error here. |
| 126 | + continue; |
| 127 | + } |
| 128 | + *reinterpret_cast<uint32_t *>(Sled.Address + 2) = FuncId; |
| 129 | + *reinterpret_cast<uint8_t *>(Sled.Address + 6) = CallOpCode; |
| 130 | + *reinterpret_cast<uint32_t *>(Sled.Address + 7) = TrampolineOffset; |
| 131 | + std::atomic_store_explicit( |
| 132 | + reinterpret_cast<std::atomic<uint16_t> *>(Sled.Address), MovR10Seq, |
| 133 | + std::memory_order_release); |
| 134 | + } |
| 135 | + |
| 136 | + if (Sled.Kind == XRayEntryType::EXIT) { |
| 137 | + // Here we do the dance of replacing the following sled: |
| 138 | + // |
| 139 | + // xray_sled_n: |
| 140 | + // ret |
| 141 | + // <10 byte nop> |
| 142 | + // |
| 143 | + // With the following: |
| 144 | + // |
| 145 | + // mov r10d, <function id> |
| 146 | + // jmp <relative 32bit offset to exit trampoline> |
| 147 | + // |
| 148 | + // 1. Put the function id first, 2 bytes from the start of the sled (just |
| 149 | + // after the 1-byte ret instruction). |
| 150 | + // 2. Put the jmp opcode 6 bytes from the start of the sled. |
| 151 | + // 3. Put the relative offset 7 bytes from the start of the sled. |
| 152 | + // 4. Do an atomic write over the jmp instruction for the "mov r10d" |
| 153 | + // opcode and first operand. |
| 154 | + // |
| 155 | + // Prerequisite is to compute the relative offset fo the |
| 156 | + // __xray_FunctionExit function's address. |
| 157 | + int64_t TrampolineOffset = |
| 158 | + reinterpret_cast<int64_t>(__xray_FunctionExit) - |
| 159 | + (static_cast<int64_t>(Sled.Address) + 11); |
| 160 | + if (TrampolineOffset < MinOffset || TrampolineOffset > MaxOffset) { |
| 161 | + // FIXME: Print out an error here. |
| 162 | + continue; |
| 163 | + } |
| 164 | + *reinterpret_cast<uint32_t *>(Sled.Address + 2) = FuncId; |
| 165 | + *reinterpret_cast<uint8_t *>(Sled.Address + 6) = JmpOpCode; |
| 166 | + *reinterpret_cast<uint32_t *>(Sled.Address + 7) = TrampolineOffset; |
| 167 | + std::atomic_store_explicit( |
| 168 | + reinterpret_cast<std::atomic<uint16_t> *>(Sled.Address), MovR10Seq, |
| 169 | + std::memory_order_release); |
| 170 | + } |
| 171 | + |
| 172 | + if (mprotect(PageAlignedAddr, MProtectLen, PROT_READ | PROT_EXEC) == -1) { |
| 173 | + printf("Failed mprotect: %d\n", errno); |
| 174 | + return XRayPatchingStatus::FAILED; |
| 175 | + } |
| 176 | + } |
| 177 | + XRayPatching.store(false, std::memory_order_release); |
| 178 | + return XRayPatchingStatus::NOTIFIED; |
| 179 | +} |
0 commit comments