]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_interface.cc
Merge ^/head r311692 through r311807.
[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 <atomic>
19 #include <cstdint>
20 #include <cstdio>
21 #include <errno.h>
22 #include <limits>
23 #include <sys/mman.h>
24
25 #include "sanitizer_common/sanitizer_common.h"
26 #include "xray_defs.h"
27
28 namespace __xray {
29
30 #if defined(__x86_64__)
31 // FIXME: The actual length is 11 bytes. Why was length 12 passed to mprotect()
32 // ?
33 static const int16_t cSledLength = 12;
34 #elif defined(__aarch64__)
35 static const int16_t cSledLength = 32;
36 #elif defined(__arm__)
37 static const int16_t cSledLength = 28;
38 #else
39 #error "Unsupported CPU Architecture"
40 #endif /* CPU architecture */
41
42 // This is the function to call when we encounter the entry or exit sleds.
43 std::atomic<void (*)(int32_t, XRayEntryType)> XRayPatchedFunction{nullptr};
44
45 // MProtectHelper is an RAII wrapper for calls to mprotect(...) that will undo
46 // any successful mprotect(...) changes. This is used to make a page writeable
47 // and executable, and upon destruction if it was successful in doing so returns
48 // the page into a read-only and executable page.
49 //
50 // This is only used specifically for runtime-patching of the XRay
51 // instrumentation points. This assumes that the executable pages are originally
52 // read-and-execute only.
53 class MProtectHelper {
54   void *PageAlignedAddr;
55   std::size_t MProtectLen;
56   bool MustCleanup;
57
58 public:
59   explicit MProtectHelper(void *PageAlignedAddr,
60                           std::size_t MProtectLen) XRAY_NEVER_INSTRUMENT
61       : PageAlignedAddr(PageAlignedAddr),
62         MProtectLen(MProtectLen),
63         MustCleanup(false) {}
64
65   int MakeWriteable() XRAY_NEVER_INSTRUMENT {
66     auto R = mprotect(PageAlignedAddr, MProtectLen,
67                       PROT_READ | PROT_WRITE | PROT_EXEC);
68     if (R != -1)
69       MustCleanup = true;
70     return R;
71   }
72
73   ~MProtectHelper() XRAY_NEVER_INSTRUMENT {
74     if (MustCleanup) {
75       mprotect(PageAlignedAddr, MProtectLen, PROT_READ | PROT_EXEC);
76     }
77   }
78 };
79
80 } // namespace __xray
81
82 extern std::atomic<bool> XRayInitialized;
83 extern std::atomic<__xray::XRaySledMap> XRayInstrMap;
84
85 int __xray_set_handler(void (*entry)(int32_t,
86                                      XRayEntryType)) XRAY_NEVER_INSTRUMENT {
87   if (XRayInitialized.load(std::memory_order_acquire)) {
88     __xray::XRayPatchedFunction.store(entry, std::memory_order_release);
89     return 1;
90   }
91   return 0;
92 }
93
94 int __xray_remove_handler() XRAY_NEVER_INSTRUMENT {
95   return __xray_set_handler(nullptr);
96 }
97
98 std::atomic<bool> XRayPatching{false};
99
100 using namespace __xray;
101
102 // FIXME: Figure out whether we can move this class to sanitizer_common instead
103 // as a generic "scope guard".
104 template <class Function> class CleanupInvoker {
105   Function Fn;
106
107 public:
108   explicit CleanupInvoker(Function Fn) XRAY_NEVER_INSTRUMENT : Fn(Fn) {}
109   CleanupInvoker(const CleanupInvoker &) XRAY_NEVER_INSTRUMENT = default;
110   CleanupInvoker(CleanupInvoker &&) XRAY_NEVER_INSTRUMENT = default;
111   CleanupInvoker &
112   operator=(const CleanupInvoker &) XRAY_NEVER_INSTRUMENT = delete;
113   CleanupInvoker &operator=(CleanupInvoker &&) XRAY_NEVER_INSTRUMENT = delete;
114   ~CleanupInvoker() XRAY_NEVER_INSTRUMENT { Fn(); }
115 };
116
117 template <class Function>
118 CleanupInvoker<Function> ScopeCleanup(Function Fn) XRAY_NEVER_INSTRUMENT {
119   return CleanupInvoker<Function>{Fn};
120 }
121
122 // ControlPatching implements the common internals of the patching/unpatching
123 // implementation. |Enable| defines whether we're enabling or disabling the
124 // runtime XRay instrumentation.
125 XRayPatchingStatus ControlPatching(bool Enable) XRAY_NEVER_INSTRUMENT {
126   if (!XRayInitialized.load(std::memory_order_acquire))
127     return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized.
128
129   static bool NotPatching = false;
130   if (!XRayPatching.compare_exchange_strong(NotPatching, true,
131                                             std::memory_order_acq_rel,
132                                             std::memory_order_acquire)) {
133     return XRayPatchingStatus::ONGOING; // Already patching.
134   }
135
136   bool PatchingSuccess = false;
137   auto XRayPatchingStatusResetter = ScopeCleanup([&PatchingSuccess] {
138     if (!PatchingSuccess) {
139       XRayPatching.store(false, std::memory_order_release);
140     }
141   });
142
143   // Step 1: Compute the function id, as a unique identifier per function in the
144   // instrumentation map.
145   XRaySledMap InstrMap = XRayInstrMap.load(std::memory_order_acquire);
146   if (InstrMap.Entries == 0)
147     return XRayPatchingStatus::NOT_INITIALIZED;
148
149   const uint64_t PageSize = GetPageSizeCached();
150   if ((PageSize == 0) || ((PageSize & (PageSize - 1)) != 0)) {
151     Report("System page size is not a power of two: %lld\n", PageSize);
152     return XRayPatchingStatus::FAILED;
153   }
154
155   uint32_t FuncId = 1;
156   uint64_t CurFun = 0;
157   for (std::size_t I = 0; I < InstrMap.Entries; I++) {
158     auto Sled = InstrMap.Sleds[I];
159     auto F = Sled.Function;
160     if (CurFun == 0)
161       CurFun = F;
162     if (F != CurFun) {
163       ++FuncId;
164       CurFun = F;
165     }
166
167     // While we're here, we should patch the nop sled. To do that we mprotect
168     // the page containing the function to be writeable.
169     void *PageAlignedAddr =
170         reinterpret_cast<void *>(Sled.Address & ~(PageSize - 1));
171     std::size_t MProtectLen = (Sled.Address + cSledLength) -
172                               reinterpret_cast<uint64_t>(PageAlignedAddr);
173     MProtectHelper Protector(PageAlignedAddr, MProtectLen);
174     if (Protector.MakeWriteable() == -1) {
175       printf("Failed mprotect: %d\n", errno);
176       return XRayPatchingStatus::FAILED;
177     }
178
179     bool Success = false;
180     switch (Sled.Kind) {
181     case XRayEntryType::ENTRY:
182       Success = patchFunctionEntry(Enable, FuncId, Sled);
183       break;
184     case XRayEntryType::EXIT:
185       Success = patchFunctionExit(Enable, FuncId, Sled);
186       break;
187     case XRayEntryType::TAIL:
188       Success = patchFunctionTailExit(Enable, FuncId, Sled);
189       break;
190     default:
191       Report("Unsupported sled kind: %d\n", int(Sled.Kind));
192       continue;
193     }
194     (void)Success;
195   }
196   XRayPatching.store(false, std::memory_order_release);
197   PatchingSuccess = true;
198   return XRayPatchingStatus::SUCCESS;
199 }
200
201 XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT {
202   return ControlPatching(true);
203 }
204
205 XRayPatchingStatus __xray_unpatch() XRAY_NEVER_INSTRUMENT {
206   return ControlPatching(false);
207 }