]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_interface.cc
MFV r321673:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_interface.cc
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
18 #include <cstdint>
19 #include <cstdio>
20 #include <errno.h>
21 #include <limits>
22 #include <sys/mman.h>
23
24 #include "sanitizer_common/sanitizer_common.h"
25 #include "xray_defs.h"
26
27 namespace __xray {
28
29 #if defined(__x86_64__)
30 // FIXME: The actual length is 11 bytes. Why was length 12 passed to mprotect()
31 // ?
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;
43 #else
44 #error "Unsupported CPU Architecture"
45 #endif /* CPU architecture */
46
47 // This is the function to call when we encounter the entry or exit sleds.
48 __sanitizer::atomic_uintptr_t XRayPatchedFunction{0};
49
50 // This is the function to call from the arg1-enabled sleds/trampolines.
51 __sanitizer::atomic_uintptr_t XRayArgLogger{0};
52
53 // This is the function to call when we encounter a custom event log call.
54 __sanitizer::atomic_uintptr_t XRayPatchedCustomEvent{0};
55
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.
60 //
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;
67   bool MustCleanup;
68
69 public:
70   explicit MProtectHelper(void *PageAlignedAddr,
71                           std::size_t MProtectLen) XRAY_NEVER_INSTRUMENT
72       : PageAlignedAddr(PageAlignedAddr),
73         MProtectLen(MProtectLen),
74         MustCleanup(false) {}
75
76   int MakeWriteable() XRAY_NEVER_INSTRUMENT {
77     auto R = mprotect(PageAlignedAddr, MProtectLen,
78                       PROT_READ | PROT_WRITE | PROT_EXEC);
79     if (R != -1)
80       MustCleanup = true;
81     return R;
82   }
83
84   ~MProtectHelper() XRAY_NEVER_INSTRUMENT {
85     if (MustCleanup) {
86       mprotect(PageAlignedAddr, MProtectLen, PROT_READ | PROT_EXEC);
87     }
88   }
89 };
90
91 } // namespace __xray
92
93 extern __sanitizer::SpinMutex XRayInstrMapMutex;
94 extern __sanitizer::atomic_uint8_t XRayInitialized;
95 extern __xray::XRaySledMap XRayInstrMap;
96
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)) {
101
102     __sanitizer::atomic_store(&__xray::XRayPatchedFunction,
103                               reinterpret_cast<uintptr_t>(entry),
104                               __sanitizer::memory_order_release);
105     return 1;
106   }
107   return 0;
108 }
109
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);
117     return 1;
118   }
119   return 0;
120 }
121
122
123 int __xray_remove_handler() XRAY_NEVER_INSTRUMENT {
124   return __xray_set_handler(nullptr);
125 }
126
127 int __xray_remove_customevent_handler() XRAY_NEVER_INSTRUMENT {
128   return __xray_set_customevent_handler(nullptr);
129 }
130
131 __sanitizer::atomic_uint8_t XRayPatching{0};
132
133 using namespace __xray;
134
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 {
138   Function Fn;
139
140 public:
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;
144   CleanupInvoker &
145   operator=(const CleanupInvoker &) XRAY_NEVER_INSTRUMENT = delete;
146   CleanupInvoker &operator=(CleanupInvoker &&) XRAY_NEVER_INSTRUMENT = delete;
147   ~CleanupInvoker() XRAY_NEVER_INSTRUMENT { Fn(); }
148 };
149
150 template <class Function>
151 CleanupInvoker<Function> scopeCleanup(Function Fn) XRAY_NEVER_INSTRUMENT {
152   return CleanupInvoker<Function>{Fn};
153 }
154
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;
168   }
169
170   bool Success = false;
171   switch (Sled.Kind) {
172   case XRayEntryType::ENTRY:
173     Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_FunctionEntry);
174     break;
175   case XRayEntryType::EXIT:
176     Success = patchFunctionExit(Enable, FuncId, Sled);
177     break;
178   case XRayEntryType::TAIL:
179     Success = patchFunctionTailExit(Enable, FuncId, Sled);
180     break;
181   case XRayEntryType::LOG_ARGS_ENTRY:
182     Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_ArgLoggerEntry);
183     break;
184   case XRayEntryType::CUSTOM_EVENT:
185     Success = patchCustomEvent(Enable, FuncId, Sled);
186     break;
187   default:
188     Report("Unsupported sled kind '%d' @%04x\n", Sled.Address, int(Sled.Kind));
189     return false;
190   }
191   return Success;
192 }
193
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.
201
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.
206
207   uint8_t PatchingSuccess = false;
208   auto XRayPatchingStatusResetter = scopeCleanup([&PatchingSuccess] {
209     if (!PatchingSuccess)
210       __sanitizer::atomic_store(&XRayPatching, false,
211                                 __sanitizer::memory_order_release);
212   });
213
214   // Step 1: Compute the function id, as a unique identifier per function in the
215   // instrumentation map.
216   XRaySledMap InstrMap;
217   {
218     __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
219     InstrMap = XRayInstrMap;
220   }
221   if (InstrMap.Entries == 0)
222     return XRayPatchingStatus::NOT_INITIALIZED;
223
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;
228   }
229
230   uint32_t FuncId = 1;
231   uint64_t CurFun = 0;
232   for (std::size_t I = 0; I < InstrMap.Entries; I++) {
233     auto Sled = InstrMap.Sleds[I];
234     auto F = Sled.Function;
235     if (CurFun == 0)
236       CurFun = F;
237     if (F != CurFun) {
238       ++FuncId;
239       CurFun = F;
240     }
241     patchSled(Sled, Enable, FuncId);
242   }
243   __sanitizer::atomic_store(&XRayPatching, false,
244                             __sanitizer::memory_order_release);
245   PatchingSuccess = true;
246   return XRayPatchingStatus::SUCCESS;
247 }
248
249 XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT {
250   return controlPatching(true);
251 }
252
253 XRayPatchingStatus __xray_unpatch() XRAY_NEVER_INSTRUMENT {
254   return controlPatching(false);
255 }
256
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.
262
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.
267
268   // Next, we look for the function index.
269   XRaySledMap InstrMap;
270   {
271     __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
272     InstrMap = XRayInstrMap;
273   }
274
275   // If we don't have an index, we can't patch individual functions.
276   if (InstrMap.Functions == 0)
277     return XRayPatchingStatus::NOT_INITIALIZED;
278
279   // FuncId must be a positive number, less than the number of functions
280   // instrumented.
281   if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions) {
282     Report("Invalid function id provided: %d\n", FuncId);
283     return XRayPatchingStatus::FAILED;
284   }
285
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;
290
291   bool SucceedOnce = false;
292   while (f != e)
293     SucceedOnce |= patchSled(*f++, Enable, FuncId);
294
295   __sanitizer::atomic_store(&XRayPatching, false,
296                             __sanitizer::memory_order_release);
297
298   if (!SucceedOnce) {
299     Report("Failed patching any sled for function '%d'.", FuncId);
300     return XRayPatchingStatus::FAILED;
301   }
302
303   return XRayPatchingStatus::SUCCESS;
304 }
305
306 XRayPatchingStatus __xray_patch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
307   return patchFunction(FuncId, true);
308 }
309
310 XRayPatchingStatus
311 __xray_unpatch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
312   return patchFunction(FuncId, false);
313 }
314
315 int __xray_set_handler_arg1(void (*entry)(int32_t, XRayEntryType, uint64_t)) {
316   if (!__sanitizer::atomic_load(&XRayInitialized,
317                                 __sanitizer::memory_order_acquire))
318     return 0;
319
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);
325   return 1;
326 }
327
328 int __xray_remove_handler_arg1() { return __xray_set_handler_arg1(nullptr); }
329
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)
333     return 0;
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.
338 #ifdef __PPC__
339          & ~0xf
340 #endif
341       ;
342 }
343
344 size_t __xray_max_function_id() XRAY_NEVER_INSTRUMENT {
345   __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
346   return XRayInstrMap.Functions;
347 }