1 //===-- xray_interface.cpp --------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file is a part of XRay, a dynamic runtime instrumentation system.
10 //
11 // Implementation of the API functions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "xray_interface_internal.h"
16 
17 #include <cstdint>
18 #include <cstdio>
19 #include <errno.h>
20 #include <limits>
21 #include <string.h>
22 #include <sys/mman.h>
23 
24 #if SANITIZER_FUCHSIA
25 #include <zircon/process.h>
26 #include <zircon/sanitizer.h>
27 #include <zircon/status.h>
28 #include <zircon/syscalls.h>
29 #endif
30 
31 #include "sanitizer_common/sanitizer_addrhashmap.h"
32 #include "sanitizer_common/sanitizer_common.h"
33 
34 #include "xray_defs.h"
35 #include "xray_flags.h"
36 
37 extern __sanitizer::SpinMutex XRayInstrMapMutex;
38 extern __sanitizer::atomic_uint8_t XRayInitialized;
39 extern __xray::XRaySledMap XRayInstrMap;
40 
41 namespace __xray {
42 
43 #if defined(__x86_64__)
44 static const int16_t cSledLength = 12;
45 #elif defined(__aarch64__)
46 static const int16_t cSledLength = 32;
47 #elif defined(__arm__)
48 static const int16_t cSledLength = 28;
49 #elif SANITIZER_MIPS32
50 static const int16_t cSledLength = 48;
51 #elif SANITIZER_MIPS64
52 static const int16_t cSledLength = 64;
53 #elif defined(__powerpc64__)
54 static const int16_t cSledLength = 8;
55 #else
56 #error "Unsupported CPU Architecture"
57 #endif /* CPU architecture */
58 
59 // This is the function to call when we encounter the entry or exit sleds.
60 atomic_uintptr_t XRayPatchedFunction{0};
61 
62 // This is the function to call from the arg1-enabled sleds/trampolines.
63 atomic_uintptr_t XRayArgLogger{0};
64 
65 // This is the function to call when we encounter a custom event log call.
66 atomic_uintptr_t XRayPatchedCustomEvent{0};
67 
68 // This is the function to call when we encounter a typed event log call.
69 atomic_uintptr_t XRayPatchedTypedEvent{0};
70 
71 // This is the global status to determine whether we are currently
72 // patching/unpatching.
73 atomic_uint8_t XRayPatching{0};
74 
75 struct TypeDescription {
76   uint32_t type_id;
77   std::size_t description_string_length;
78 };
79 
80 using TypeDescriptorMapType = AddrHashMap<TypeDescription, 11>;
81 // An address map from immutable descriptors to type ids.
82 TypeDescriptorMapType TypeDescriptorAddressMap{};
83 
84 atomic_uint32_t TypeEventDescriptorCounter{0};
85 
86 // MProtectHelper is an RAII wrapper for calls to mprotect(...) that will
87 // undo any successful mprotect(...) changes. This is used to make a page
88 // writeable and executable, and upon destruction if it was successful in
89 // doing so returns the page into a read-only and executable page.
90 //
91 // This is only used specifically for runtime-patching of the XRay
92 // instrumentation points. This assumes that the executable pages are
93 // originally read-and-execute only.
94 class MProtectHelper {
95   void *PageAlignedAddr;
96   std::size_t MProtectLen;
97   bool MustCleanup;
98 
99 public:
100   explicit MProtectHelper(void *PageAlignedAddr,
101                           std::size_t MProtectLen,
102                           std::size_t PageSize) XRAY_NEVER_INSTRUMENT
103       : PageAlignedAddr(PageAlignedAddr),
104         MProtectLen(MProtectLen),
105         MustCleanup(false) {
106 #if SANITIZER_FUCHSIA
107     MProtectLen = RoundUpTo(MProtectLen, PageSize);
108 #endif
109   }
110 
111   int MakeWriteable() XRAY_NEVER_INSTRUMENT {
112 #if SANITIZER_FUCHSIA
113     auto R = __sanitizer_change_code_protection(
114         reinterpret_cast<uintptr_t>(PageAlignedAddr), MProtectLen, true);
115     if (R != ZX_OK) {
116       Report("XRay: cannot change code protection: %s\n",
117              _zx_status_get_string(R));
118       return -1;
119     }
120     MustCleanup = true;
121     return 0;
122 #else
123     auto R = mprotect(PageAlignedAddr, MProtectLen,
124                       PROT_READ | PROT_WRITE | PROT_EXEC);
125     if (R != -1)
126       MustCleanup = true;
127     return R;
128 #endif
129   }
130 
131   ~MProtectHelper() XRAY_NEVER_INSTRUMENT {
132     if (MustCleanup) {
133 #if SANITIZER_FUCHSIA
134       auto R = __sanitizer_change_code_protection(
135           reinterpret_cast<uintptr_t>(PageAlignedAddr), MProtectLen, false);
136       if (R != ZX_OK) {
137         Report("XRay: cannot change code protection: %s\n",
138                _zx_status_get_string(R));
139       }
140 #else
141       mprotect(PageAlignedAddr, MProtectLen, PROT_READ | PROT_EXEC);
142 #endif
143     }
144   }
145 };
146 
147 namespace {
148 
149 bool patchSled(const XRaySledEntry &Sled, bool Enable,
150                int32_t FuncId) XRAY_NEVER_INSTRUMENT {
151   bool Success = false;
152   switch (Sled.Kind) {
153   case XRayEntryType::ENTRY:
154     Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_FunctionEntry);
155     break;
156   case XRayEntryType::EXIT:
157     Success = patchFunctionExit(Enable, FuncId, Sled);
158     break;
159   case XRayEntryType::TAIL:
160     Success = patchFunctionTailExit(Enable, FuncId, Sled);
161     break;
162   case XRayEntryType::LOG_ARGS_ENTRY:
163     Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_ArgLoggerEntry);
164     break;
165   case XRayEntryType::CUSTOM_EVENT:
166     Success = patchCustomEvent(Enable, FuncId, Sled);
167     break;
168   case XRayEntryType::TYPED_EVENT:
169     Success = patchTypedEvent(Enable, FuncId, Sled);
170     break;
171   default:
172     Report("Unsupported sled kind '%d' @%04x\n", Sled.Address, int(Sled.Kind));
173     return false;
174   }
175   return Success;
176 }
177 
178 XRayPatchingStatus patchFunction(int32_t FuncId,
179                                  bool Enable) XRAY_NEVER_INSTRUMENT {
180   if (!atomic_load(&XRayInitialized,
181                                 memory_order_acquire))
182     return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized.
183 
184   uint8_t NotPatching = false;
185   if (!atomic_compare_exchange_strong(
186           &XRayPatching, &NotPatching, true, memory_order_acq_rel))
187     return XRayPatchingStatus::ONGOING; // Already patching.
188 
189   // Next, we look for the function index.
190   XRaySledMap InstrMap;
191   {
192     SpinMutexLock Guard(&XRayInstrMapMutex);
193     InstrMap = XRayInstrMap;
194   }
195 
196   // If we don't have an index, we can't patch individual functions.
197   if (InstrMap.Functions == 0)
198     return XRayPatchingStatus::NOT_INITIALIZED;
199 
200   // FuncId must be a positive number, less than the number of functions
201   // instrumented.
202   if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions) {
203     Report("Invalid function id provided: %d\n", FuncId);
204     return XRayPatchingStatus::FAILED;
205   }
206 
207   // Now we patch ths sleds for this specific function.
208   auto SledRange = InstrMap.SledsIndex[FuncId - 1];
209   auto *f = SledRange.Begin;
210   auto *e = SledRange.End;
211 
212   bool SucceedOnce = false;
213   while (f != e)
214     SucceedOnce |= patchSled(*f++, Enable, FuncId);
215 
216   atomic_store(&XRayPatching, false,
217                             memory_order_release);
218 
219   if (!SucceedOnce) {
220     Report("Failed patching any sled for function '%d'.", FuncId);
221     return XRayPatchingStatus::FAILED;
222   }
223 
224   return XRayPatchingStatus::SUCCESS;
225 }
226 
227 // controlPatching implements the common internals of the patching/unpatching
228 // implementation. |Enable| defines whether we're enabling or disabling the
229 // runtime XRay instrumentation.
230 XRayPatchingStatus controlPatching(bool Enable) XRAY_NEVER_INSTRUMENT {
231   if (!atomic_load(&XRayInitialized,
232                                 memory_order_acquire))
233     return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized.
234 
235   uint8_t NotPatching = false;
236   if (!atomic_compare_exchange_strong(
237           &XRayPatching, &NotPatching, true, memory_order_acq_rel))
238     return XRayPatchingStatus::ONGOING; // Already patching.
239 
240   uint8_t PatchingSuccess = false;
241   auto XRayPatchingStatusResetter =
242       at_scope_exit([&PatchingSuccess] {
243         if (!PatchingSuccess)
244           atomic_store(&XRayPatching, false,
245                                     memory_order_release);
246       });
247 
248   XRaySledMap InstrMap;
249   {
250     SpinMutexLock Guard(&XRayInstrMapMutex);
251     InstrMap = XRayInstrMap;
252   }
253   if (InstrMap.Entries == 0)
254     return XRayPatchingStatus::NOT_INITIALIZED;
255 
256   uint32_t FuncId = 1;
257   uint64_t CurFun = 0;
258 
259   // First we want to find the bounds for which we have instrumentation points,
260   // and try to get as few calls to mprotect(...) as possible. We're assuming
261   // that all the sleds for the instrumentation map are contiguous as a single
262   // set of pages. When we do support dynamic shared object instrumentation,
263   // we'll need to do this for each set of page load offsets per DSO loaded. For
264   // now we're assuming we can mprotect the whole section of text between the
265   // minimum sled address and the maximum sled address (+ the largest sled
266   // size).
267   auto *MinSled = &InstrMap.Sleds[0];
268   auto *MaxSled = &InstrMap.Sleds[InstrMap.Entries - 1];
269   for (std::size_t I = 0; I < InstrMap.Entries; I++) {
270     const auto &Sled = InstrMap.Sleds[I];
271     if (Sled.address() < MinSled->address())
272       MinSled = &Sled;
273     if (Sled.address() > MaxSled->address())
274       MaxSled = &Sled;
275   }
276 
277   const size_t PageSize = flags()->xray_page_size_override > 0
278                               ? flags()->xray_page_size_override
279                               : GetPageSizeCached();
280   if ((PageSize == 0) || ((PageSize & (PageSize - 1)) != 0)) {
281     Report("System page size is not a power of two: %lld\n", PageSize);
282     return XRayPatchingStatus::FAILED;
283   }
284 
285   void *PageAlignedAddr =
286       reinterpret_cast<void *>(MinSled->address() & ~(PageSize - 1));
287   size_t MProtectLen =
288       (MaxSled->address() - reinterpret_cast<uptr>(PageAlignedAddr)) +
289       cSledLength;
290   MProtectHelper Protector(PageAlignedAddr, MProtectLen, PageSize);
291   if (Protector.MakeWriteable() == -1) {
292     Report("Failed mprotect: %d\n", errno);
293     return XRayPatchingStatus::FAILED;
294   }
295 
296   for (std::size_t I = 0; I < InstrMap.Entries; ++I) {
297     auto &Sled = InstrMap.Sleds[I];
298     auto F = Sled.function();
299     if (CurFun == 0)
300       CurFun = F;
301     if (F != CurFun) {
302       ++FuncId;
303       CurFun = F;
304     }
305     patchSled(Sled, Enable, FuncId);
306   }
307   atomic_store(&XRayPatching, false,
308                             memory_order_release);
309   PatchingSuccess = true;
310   return XRayPatchingStatus::SUCCESS;
311 }
312 
313 XRayPatchingStatus mprotectAndPatchFunction(int32_t FuncId,
314                                             bool Enable) XRAY_NEVER_INSTRUMENT {
315   XRaySledMap InstrMap;
316   {
317     SpinMutexLock Guard(&XRayInstrMapMutex);
318     InstrMap = XRayInstrMap;
319   }
320 
321   // FuncId must be a positive number, less than the number of functions
322   // instrumented.
323   if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions) {
324     Report("Invalid function id provided: %d\n", FuncId);
325     return XRayPatchingStatus::FAILED;
326   }
327 
328   const size_t PageSize = flags()->xray_page_size_override > 0
329                               ? flags()->xray_page_size_override
330                               : GetPageSizeCached();
331   if ((PageSize == 0) || ((PageSize & (PageSize - 1)) != 0)) {
332     Report("Provided page size is not a power of two: %lld\n", PageSize);
333     return XRayPatchingStatus::FAILED;
334   }
335 
336   // Here we compute the minumum sled and maximum sled associated with a
337   // particular function ID.
338   auto SledRange = InstrMap.SledsIndex[FuncId - 1];
339   auto *f = SledRange.Begin;
340   auto *e = SledRange.End;
341   auto *MinSled = f;
342   auto *MaxSled = (SledRange.End - 1);
343   while (f != e) {
344     if (f->address() < MinSled->address())
345       MinSled = f;
346     if (f->address() > MaxSled->address())
347       MaxSled = f;
348     ++f;
349   }
350 
351   void *PageAlignedAddr =
352       reinterpret_cast<void *>(MinSled->address() & ~(PageSize - 1));
353   size_t MProtectLen =
354       (MaxSled->address() - reinterpret_cast<uptr>(PageAlignedAddr)) +
355       cSledLength;
356   MProtectHelper Protector(PageAlignedAddr, MProtectLen, PageSize);
357   if (Protector.MakeWriteable() == -1) {
358     Report("Failed mprotect: %d\n", errno);
359     return XRayPatchingStatus::FAILED;
360   }
361   return patchFunction(FuncId, Enable);
362 }
363 
364 } // namespace
365 
366 } // namespace __xray
367 
368 using namespace __xray;
369 
370 // The following functions are declared `extern "C" {...}` in the header, hence
371 // they're defined in the global namespace.
372 
373 int __xray_set_handler(void (*entry)(int32_t,
374                                      XRayEntryType)) XRAY_NEVER_INSTRUMENT {
375   if (atomic_load(&XRayInitialized,
376                                memory_order_acquire)) {
377 
378     atomic_store(&__xray::XRayPatchedFunction,
379                               reinterpret_cast<uintptr_t>(entry),
380                               memory_order_release);
381     return 1;
382   }
383   return 0;
384 }
385 
386 int __xray_set_customevent_handler(void (*entry)(void *, size_t))
387     XRAY_NEVER_INSTRUMENT {
388   if (atomic_load(&XRayInitialized,
389                                memory_order_acquire)) {
390     atomic_store(&__xray::XRayPatchedCustomEvent,
391                               reinterpret_cast<uintptr_t>(entry),
392                               memory_order_release);
393     return 1;
394   }
395   return 0;
396 }
397 
398 int __xray_set_typedevent_handler(void (*entry)(
399     uint16_t, const void *, size_t)) XRAY_NEVER_INSTRUMENT {
400   if (atomic_load(&XRayInitialized,
401                                memory_order_acquire)) {
402     atomic_store(&__xray::XRayPatchedTypedEvent,
403                               reinterpret_cast<uintptr_t>(entry),
404                               memory_order_release);
405     return 1;
406   }
407   return 0;
408 }
409 
410 int __xray_remove_handler() XRAY_NEVER_INSTRUMENT {
411   return __xray_set_handler(nullptr);
412 }
413 
414 int __xray_remove_customevent_handler() XRAY_NEVER_INSTRUMENT {
415   return __xray_set_customevent_handler(nullptr);
416 }
417 
418 int __xray_remove_typedevent_handler() XRAY_NEVER_INSTRUMENT {
419   return __xray_set_typedevent_handler(nullptr);
420 }
421 
422 uint16_t __xray_register_event_type(
423     const char *const event_type) XRAY_NEVER_INSTRUMENT {
424   TypeDescriptorMapType::Handle h(&TypeDescriptorAddressMap, (uptr)event_type);
425   if (h.created()) {
426     h->type_id = atomic_fetch_add(
427         &TypeEventDescriptorCounter, 1, memory_order_acq_rel);
428     h->description_string_length = strnlen(event_type, 1024);
429   }
430   return h->type_id;
431 }
432 
433 XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT {
434   return controlPatching(true);
435 }
436 
437 XRayPatchingStatus __xray_unpatch() XRAY_NEVER_INSTRUMENT {
438   return controlPatching(false);
439 }
440 
441 XRayPatchingStatus __xray_patch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
442   return mprotectAndPatchFunction(FuncId, true);
443 }
444 
445 XRayPatchingStatus
446 __xray_unpatch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
447   return mprotectAndPatchFunction(FuncId, false);
448 }
449 
450 int __xray_set_handler_arg1(void (*entry)(int32_t, XRayEntryType, uint64_t)) {
451   if (!atomic_load(&XRayInitialized,
452                                 memory_order_acquire))
453     return 0;
454 
455   // A relaxed write might not be visible even if the current thread gets
456   // scheduled on a different CPU/NUMA node.  We need to wait for everyone to
457   // have this handler installed for consistency of collected data across CPUs.
458   atomic_store(&XRayArgLogger, reinterpret_cast<uint64_t>(entry),
459                             memory_order_release);
460   return 1;
461 }
462 
463 int __xray_remove_handler_arg1() { return __xray_set_handler_arg1(nullptr); }
464 
465 uintptr_t __xray_function_address(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
466   SpinMutexLock Guard(&XRayInstrMapMutex);
467   if (FuncId <= 0 || static_cast<size_t>(FuncId) > XRayInstrMap.Functions)
468     return 0;
469   return XRayInstrMap.SledsIndex[FuncId - 1].Begin->function()
470 // On PPC, function entries are always aligned to 16 bytes. The beginning of a
471 // sled might be a local entry, which is always +8 based on the global entry.
472 // Always return the global entry.
473 #ifdef __PPC__
474          & ~0xf
475 #endif
476       ;
477 }
478 
479 size_t __xray_max_function_id() XRAY_NEVER_INSTRUMENT {
480   SpinMutexLock Guard(&XRayInstrMapMutex);
481   return XRayInstrMap.Functions;
482 }
483