1 //===-- xray_interface.cpp --------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a part of XRay, a dynamic runtime instrumentation system.
12 // Implementation of the API functions.
14 //===----------------------------------------------------------------------===//
16 #include "xray_interface_internal.h"
24 #include "sanitizer_common/sanitizer_common.h"
25 #include "xray_defs.h"
29 #if defined(__x86_64__)
30 // FIXME: The actual length is 11 bytes. Why was length 12 passed to mprotect()
32 static const int16_t cSledLength = 12;
33 #elif defined(__aarch64__)
34 static const int16_t cSledLength = 32;
35 #elif defined(__arm__)
36 static const int16_t cSledLength = 28;
37 #elif SANITIZER_MIPS32
38 static const int16_t cSledLength = 48;
39 #elif SANITIZER_MIPS64
40 static const int16_t cSledLength = 64;
41 #elif defined(__powerpc64__)
42 static const int16_t cSledLength = 8;
44 #error "Unsupported CPU Architecture"
45 #endif /* CPU architecture */
47 // This is the function to call when we encounter the entry or exit sleds.
48 __sanitizer::atomic_uintptr_t XRayPatchedFunction{0};
50 // This is the function to call from the arg1-enabled sleds/trampolines.
51 __sanitizer::atomic_uintptr_t XRayArgLogger{0};
53 // This is the function to call when we encounter a custom event log call.
54 __sanitizer::atomic_uintptr_t XRayPatchedCustomEvent{0};
56 // MProtectHelper is an RAII wrapper for calls to mprotect(...) that will undo
57 // any successful mprotect(...) changes. This is used to make a page writeable
58 // and executable, and upon destruction if it was successful in doing so returns
59 // the page into a read-only and executable page.
61 // This is only used specifically for runtime-patching of the XRay
62 // instrumentation points. This assumes that the executable pages are originally
63 // read-and-execute only.
64 class MProtectHelper {
65 void *PageAlignedAddr;
66 std::size_t MProtectLen;
70 explicit MProtectHelper(void *PageAlignedAddr,
71 std::size_t MProtectLen) XRAY_NEVER_INSTRUMENT
72 : PageAlignedAddr(PageAlignedAddr),
73 MProtectLen(MProtectLen),
76 int MakeWriteable() XRAY_NEVER_INSTRUMENT {
77 auto R = mprotect(PageAlignedAddr, MProtectLen,
78 PROT_READ | PROT_WRITE | PROT_EXEC);
84 ~MProtectHelper() XRAY_NEVER_INSTRUMENT {
86 mprotect(PageAlignedAddr, MProtectLen, PROT_READ | PROT_EXEC);
93 extern __sanitizer::SpinMutex XRayInstrMapMutex;
94 extern __sanitizer::atomic_uint8_t XRayInitialized;
95 extern __xray::XRaySledMap XRayInstrMap;
97 int __xray_set_handler(void (*entry)(int32_t,
98 XRayEntryType)) XRAY_NEVER_INSTRUMENT {
99 if (__sanitizer::atomic_load(&XRayInitialized,
100 __sanitizer::memory_order_acquire)) {
102 __sanitizer::atomic_store(&__xray::XRayPatchedFunction,
103 reinterpret_cast<uintptr_t>(entry),
104 __sanitizer::memory_order_release);
110 int __xray_set_customevent_handler(void (*entry)(void *, size_t))
111 XRAY_NEVER_INSTRUMENT {
112 if (__sanitizer::atomic_load(&XRayInitialized,
113 __sanitizer::memory_order_acquire)) {
114 __sanitizer::atomic_store(&__xray::XRayPatchedCustomEvent,
115 reinterpret_cast<uintptr_t>(entry),
116 __sanitizer::memory_order_release);
123 int __xray_remove_handler() XRAY_NEVER_INSTRUMENT {
124 return __xray_set_handler(nullptr);
127 int __xray_remove_customevent_handler() XRAY_NEVER_INSTRUMENT {
128 return __xray_set_customevent_handler(nullptr);
131 __sanitizer::atomic_uint8_t XRayPatching{0};
133 using namespace __xray;
135 // FIXME: Figure out whether we can move this class to sanitizer_common instead
136 // as a generic "scope guard".
137 template <class Function> class CleanupInvoker {
141 explicit CleanupInvoker(Function Fn) XRAY_NEVER_INSTRUMENT : Fn(Fn) {}
142 CleanupInvoker(const CleanupInvoker &) XRAY_NEVER_INSTRUMENT = default;
143 CleanupInvoker(CleanupInvoker &&) XRAY_NEVER_INSTRUMENT = default;
145 operator=(const CleanupInvoker &) XRAY_NEVER_INSTRUMENT = delete;
146 CleanupInvoker &operator=(CleanupInvoker &&) XRAY_NEVER_INSTRUMENT = delete;
147 ~CleanupInvoker() XRAY_NEVER_INSTRUMENT { Fn(); }
150 template <class Function>
151 CleanupInvoker<Function> scopeCleanup(Function Fn) XRAY_NEVER_INSTRUMENT {
152 return CleanupInvoker<Function>{Fn};
155 inline bool patchSled(const XRaySledEntry &Sled, bool Enable,
156 int32_t FuncId) XRAY_NEVER_INSTRUMENT {
157 // While we're here, we should patch the nop sled. To do that we mprotect
158 // the page containing the function to be writeable.
159 const uint64_t PageSize = GetPageSizeCached();
160 void *PageAlignedAddr =
161 reinterpret_cast<void *>(Sled.Address & ~(PageSize - 1));
162 std::size_t MProtectLen = (Sled.Address + cSledLength) -
163 reinterpret_cast<uint64_t>(PageAlignedAddr);
164 MProtectHelper Protector(PageAlignedAddr, MProtectLen);
165 if (Protector.MakeWriteable() == -1) {
166 printf("Failed mprotect: %d\n", errno);
167 return XRayPatchingStatus::FAILED;
170 bool Success = false;
172 case XRayEntryType::ENTRY:
173 Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_FunctionEntry);
175 case XRayEntryType::EXIT:
176 Success = patchFunctionExit(Enable, FuncId, Sled);
178 case XRayEntryType::TAIL:
179 Success = patchFunctionTailExit(Enable, FuncId, Sled);
181 case XRayEntryType::LOG_ARGS_ENTRY:
182 Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_ArgLoggerEntry);
184 case XRayEntryType::CUSTOM_EVENT:
185 Success = patchCustomEvent(Enable, FuncId, Sled);
188 Report("Unsupported sled kind '%d' @%04x\n", Sled.Address, int(Sled.Kind));
194 // controlPatching implements the common internals of the patching/unpatching
195 // implementation. |Enable| defines whether we're enabling or disabling the
196 // runtime XRay instrumentation.
197 XRayPatchingStatus controlPatching(bool Enable) XRAY_NEVER_INSTRUMENT {
198 if (!__sanitizer::atomic_load(&XRayInitialized,
199 __sanitizer::memory_order_acquire))
200 return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized.
202 uint8_t NotPatching = false;
203 if (!__sanitizer::atomic_compare_exchange_strong(
204 &XRayPatching, &NotPatching, true, __sanitizer::memory_order_acq_rel))
205 return XRayPatchingStatus::ONGOING; // Already patching.
207 uint8_t PatchingSuccess = false;
208 auto XRayPatchingStatusResetter = scopeCleanup([&PatchingSuccess] {
209 if (!PatchingSuccess)
210 __sanitizer::atomic_store(&XRayPatching, false,
211 __sanitizer::memory_order_release);
214 // Step 1: Compute the function id, as a unique identifier per function in the
215 // instrumentation map.
216 XRaySledMap InstrMap;
218 __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
219 InstrMap = XRayInstrMap;
221 if (InstrMap.Entries == 0)
222 return XRayPatchingStatus::NOT_INITIALIZED;
224 const uint64_t PageSize = GetPageSizeCached();
225 if ((PageSize == 0) || ((PageSize & (PageSize - 1)) != 0)) {
226 Report("System page size is not a power of two: %lld\n", PageSize);
227 return XRayPatchingStatus::FAILED;
232 for (std::size_t I = 0; I < InstrMap.Entries; I++) {
233 auto Sled = InstrMap.Sleds[I];
234 auto F = Sled.Function;
241 patchSled(Sled, Enable, FuncId);
243 __sanitizer::atomic_store(&XRayPatching, false,
244 __sanitizer::memory_order_release);
245 PatchingSuccess = true;
246 return XRayPatchingStatus::SUCCESS;
249 XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT {
250 return controlPatching(true);
253 XRayPatchingStatus __xray_unpatch() XRAY_NEVER_INSTRUMENT {
254 return controlPatching(false);
257 XRayPatchingStatus patchFunction(int32_t FuncId,
258 bool Enable) XRAY_NEVER_INSTRUMENT {
259 if (!__sanitizer::atomic_load(&XRayInitialized,
260 __sanitizer::memory_order_acquire))
261 return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized.
263 uint8_t NotPatching = false;
264 if (!__sanitizer::atomic_compare_exchange_strong(
265 &XRayPatching, &NotPatching, true, __sanitizer::memory_order_acq_rel))
266 return XRayPatchingStatus::ONGOING; // Already patching.
268 // Next, we look for the function index.
269 XRaySledMap InstrMap;
271 __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
272 InstrMap = XRayInstrMap;
275 // If we don't have an index, we can't patch individual functions.
276 if (InstrMap.Functions == 0)
277 return XRayPatchingStatus::NOT_INITIALIZED;
279 // FuncId must be a positive number, less than the number of functions
281 if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions) {
282 Report("Invalid function id provided: %d\n", FuncId);
283 return XRayPatchingStatus::FAILED;
286 // Now we patch ths sleds for this specific function.
287 auto SledRange = InstrMap.SledsIndex[FuncId - 1];
288 auto *f = SledRange.Begin;
289 auto *e = SledRange.End;
291 bool SucceedOnce = false;
293 SucceedOnce |= patchSled(*f++, Enable, FuncId);
295 __sanitizer::atomic_store(&XRayPatching, false,
296 __sanitizer::memory_order_release);
299 Report("Failed patching any sled for function '%d'.", FuncId);
300 return XRayPatchingStatus::FAILED;
303 return XRayPatchingStatus::SUCCESS;
306 XRayPatchingStatus __xray_patch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
307 return patchFunction(FuncId, true);
311 __xray_unpatch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
312 return patchFunction(FuncId, false);
315 int __xray_set_handler_arg1(void (*entry)(int32_t, XRayEntryType, uint64_t)) {
316 if (!__sanitizer::atomic_load(&XRayInitialized,
317 __sanitizer::memory_order_acquire))
320 // A relaxed write might not be visible even if the current thread gets
321 // scheduled on a different CPU/NUMA node. We need to wait for everyone to
322 // have this handler installed for consistency of collected data across CPUs.
323 __sanitizer::atomic_store(&XRayArgLogger, reinterpret_cast<uint64_t>(entry),
324 __sanitizer::memory_order_release);
328 int __xray_remove_handler_arg1() { return __xray_set_handler_arg1(nullptr); }
330 uintptr_t __xray_function_address(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
331 __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
332 if (FuncId <= 0 || static_cast<size_t>(FuncId) > XRayInstrMap.Functions)
334 return XRayInstrMap.SledsIndex[FuncId - 1].Begin->Address
335 // On PPC, function entries are always aligned to 16 bytes. The beginning of a
336 // sled might be a local entry, which is always +8 based on the global entry.
337 // Always return the global entry.
344 size_t __xray_max_function_id() XRAY_NEVER_INSTRUMENT {
345 __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
346 return XRayInstrMap.Functions;