2 #include "sanitizer_common/sanitizer_common.h"
4 #include "xray_interface_internal.h"
17 static std::pair<ssize_t, bool>
18 retryingReadSome(int Fd, char *Begin, char *End) XRAY_NEVER_INSTRUMENT {
19 auto BytesToRead = std::distance(Begin, End);
21 ssize_t TotalBytesRead = 0;
22 while (BytesToRead && (BytesRead = read(Fd, Begin, BytesToRead))) {
23 if (BytesRead == -1) {
26 Report("Read error; errno = %d\n", errno);
27 return std::make_pair(TotalBytesRead, false);
30 TotalBytesRead += BytesRead;
31 BytesToRead -= BytesRead;
34 return std::make_pair(TotalBytesRead, true);
37 static bool readValueFromFile(const char *Filename,
38 long long *Value) XRAY_NEVER_INSTRUMENT {
39 int Fd = open(Filename, O_RDONLY | O_CLOEXEC);
42 static constexpr size_t BufSize = 256;
43 char Line[BufSize] = {};
46 std::tie(BytesRead, Success) = retryingReadSome(Fd, Line, Line + BufSize);
51 long long Tmp = internal_simple_strtoll(Line, &End, 10);
53 if (Line[0] != '\0' && (*End == '\n' || *End == '\0')) {
60 uint64_t getTSCFrequency() XRAY_NEVER_INSTRUMENT {
61 long long TSCFrequency = -1;
62 if (readValueFromFile("/sys/devices/system/cpu/cpu0/tsc_freq_khz",
65 } else if (readValueFromFile(
66 "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq",
70 Report("Unable to determine CPU frequency for TSC accounting.\n");
72 return TSCFrequency == -1 ? 0 : static_cast<uint64_t>(TSCFrequency);
75 static constexpr uint8_t CallOpCode = 0xe8;
76 static constexpr uint16_t MovR10Seq = 0xba41;
77 static constexpr uint16_t Jmp9Seq = 0x09eb;
78 static constexpr uint8_t JmpOpCode = 0xe9;
79 static constexpr uint8_t RetOpCode = 0xc3;
81 static constexpr int64_t MinOffset{std::numeric_limits<int32_t>::min()};
82 static constexpr int64_t MaxOffset{std::numeric_limits<int32_t>::max()};
84 bool patchFunctionEntry(const bool Enable, const uint32_t FuncId,
85 const XRaySledEntry &Sled,
86 void (*Trampoline)()) XRAY_NEVER_INSTRUMENT {
87 // Here we do the dance of replacing the following sled:
93 // With the following:
95 // mov r10d, <function id>
96 // call <relative 32bit offset to entry trampoline>
98 // We need to do this in the following order:
100 // 1. Put the function id first, 2 bytes from the start of the sled (just
101 // after the 2-byte jmp instruction).
102 // 2. Put the call opcode 6 bytes from the start of the sled.
103 // 3. Put the relative offset 7 bytes from the start of the sled.
104 // 4. Do an atomic write over the jmp instruction for the "mov r10d"
105 // opcode and first operand.
107 // Prerequisite is to compute the relative offset to the trampoline's address.
108 int64_t TrampolineOffset = reinterpret_cast<int64_t>(Trampoline) -
109 (static_cast<int64_t>(Sled.Address) + 11);
110 if (TrampolineOffset < MinOffset || TrampolineOffset > MaxOffset) {
111 Report("XRay Entry trampoline (%p) too far from sled (%p)\n",
112 Trampoline, reinterpret_cast<void *>(Sled.Address));
116 *reinterpret_cast<uint32_t *>(Sled.Address + 2) = FuncId;
117 *reinterpret_cast<uint8_t *>(Sled.Address + 6) = CallOpCode;
118 *reinterpret_cast<uint32_t *>(Sled.Address + 7) = TrampolineOffset;
119 std::atomic_store_explicit(
120 reinterpret_cast<std::atomic<uint16_t> *>(Sled.Address), MovR10Seq,
121 std::memory_order_release);
123 std::atomic_store_explicit(
124 reinterpret_cast<std::atomic<uint16_t> *>(Sled.Address), Jmp9Seq,
125 std::memory_order_release);
126 // FIXME: Write out the nops still?
131 bool patchFunctionExit(const bool Enable, const uint32_t FuncId,
132 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
133 // Here we do the dance of replacing the following sled:
139 // With the following:
141 // mov r10d, <function id>
142 // jmp <relative 32bit offset to exit trampoline>
144 // 1. Put the function id first, 2 bytes from the start of the sled (just
145 // after the 1-byte ret instruction).
146 // 2. Put the jmp opcode 6 bytes from the start of the sled.
147 // 3. Put the relative offset 7 bytes from the start of the sled.
148 // 4. Do an atomic write over the jmp instruction for the "mov r10d"
149 // opcode and first operand.
151 // Prerequisite is to compute the relative offset fo the
152 // __xray_FunctionExit function's address.
153 int64_t TrampolineOffset = reinterpret_cast<int64_t>(__xray_FunctionExit) -
154 (static_cast<int64_t>(Sled.Address) + 11);
155 if (TrampolineOffset < MinOffset || TrampolineOffset > MaxOffset) {
156 Report("XRay Exit trampoline (%p) too far from sled (%p)\n",
157 __xray_FunctionExit, reinterpret_cast<void *>(Sled.Address));
161 *reinterpret_cast<uint32_t *>(Sled.Address + 2) = FuncId;
162 *reinterpret_cast<uint8_t *>(Sled.Address + 6) = JmpOpCode;
163 *reinterpret_cast<uint32_t *>(Sled.Address + 7) = TrampolineOffset;
164 std::atomic_store_explicit(
165 reinterpret_cast<std::atomic<uint16_t> *>(Sled.Address), MovR10Seq,
166 std::memory_order_release);
168 std::atomic_store_explicit(
169 reinterpret_cast<std::atomic<uint8_t> *>(Sled.Address), RetOpCode,
170 std::memory_order_release);
171 // FIXME: Write out the nops still?
176 bool patchFunctionTailExit(const bool Enable, const uint32_t FuncId,
177 const XRaySledEntry &Sled) XRAY_NEVER_INSTRUMENT {
178 // Here we do the dance of replacing the tail call sled with a similar
179 // sequence as the entry sled, but calls the tail exit sled instead.
180 int64_t TrampolineOffset =
181 reinterpret_cast<int64_t>(__xray_FunctionTailExit) -
182 (static_cast<int64_t>(Sled.Address) + 11);
183 if (TrampolineOffset < MinOffset || TrampolineOffset > MaxOffset) {
184 Report("XRay Exit trampoline (%p) too far from sled (%p)\n",
185 __xray_FunctionExit, reinterpret_cast<void *>(Sled.Address));
189 *reinterpret_cast<uint32_t *>(Sled.Address + 2) = FuncId;
190 *reinterpret_cast<uint8_t *>(Sled.Address + 6) = CallOpCode;
191 *reinterpret_cast<uint32_t *>(Sled.Address + 7) = TrampolineOffset;
192 std::atomic_store_explicit(
193 reinterpret_cast<std::atomic<uint16_t> *>(Sled.Address), MovR10Seq,
194 std::memory_order_release);
196 std::atomic_store_explicit(
197 reinterpret_cast<std::atomic<uint16_t> *>(Sled.Address), Jmp9Seq,
198 std::memory_order_release);
199 // FIXME: Write out the nops still?
204 // We determine whether the CPU we're running on has the correct features we
205 // need. In x86_64 this will be rdtscp support.
206 bool probeRequiredCPUFeatures() XRAY_NEVER_INSTRUMENT {
207 unsigned int EAX, EBX, ECX, EDX;
209 // We check whether rdtscp support is enabled. According to the x86_64 manual,
210 // level should be set at 0x80000001, and we should have a look at bit 27 in
211 // EDX. That's 0x8000000 (or 1u << 26).
212 __get_cpuid(0x80000001, &EAX, &EBX, &ECX, &EDX);
213 if (!(EDX & (1u << 26))) {
214 Report("Missing rdtscp support.\n");
217 // Also check whether we can determine the CPU frequency, since if we cannot,
218 // we should use the emulated TSC instead.
219 if (!getTSCFrequency()) {
220 Report("Unable to determine CPU frequency.\n");
226 } // namespace __xray