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"
25 #include "sanitizer_common/sanitizer_common.h"
26 #include "xray_defs.h"
30 #if defined(__x86_64__)
31 // FIXME: The actual length is 11 bytes. Why was length 12 passed to mprotect()
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;
39 #error "Unsupported CPU Architecture"
40 #endif /* CPU architecture */
42 // This is the function to call when we encounter the entry or exit sleds.
43 std::atomic<void (*)(int32_t, XRayEntryType)> XRayPatchedFunction{nullptr};
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.
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;
59 explicit MProtectHelper(void *PageAlignedAddr,
60 std::size_t MProtectLen) XRAY_NEVER_INSTRUMENT
61 : PageAlignedAddr(PageAlignedAddr),
62 MProtectLen(MProtectLen),
65 int MakeWriteable() XRAY_NEVER_INSTRUMENT {
66 auto R = mprotect(PageAlignedAddr, MProtectLen,
67 PROT_READ | PROT_WRITE | PROT_EXEC);
73 ~MProtectHelper() XRAY_NEVER_INSTRUMENT {
75 mprotect(PageAlignedAddr, MProtectLen, PROT_READ | PROT_EXEC);
82 extern std::atomic<bool> XRayInitialized;
83 extern std::atomic<__xray::XRaySledMap> XRayInstrMap;
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);
94 int __xray_remove_handler() XRAY_NEVER_INSTRUMENT {
95 return __xray_set_handler(nullptr);
98 std::atomic<bool> XRayPatching{false};
100 using namespace __xray;
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 {
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;
112 operator=(const CleanupInvoker &) XRAY_NEVER_INSTRUMENT = delete;
113 CleanupInvoker &operator=(CleanupInvoker &&) XRAY_NEVER_INSTRUMENT = delete;
114 ~CleanupInvoker() XRAY_NEVER_INSTRUMENT { Fn(); }
117 template <class Function>
118 CleanupInvoker<Function> ScopeCleanup(Function Fn) XRAY_NEVER_INSTRUMENT {
119 return CleanupInvoker<Function>{Fn};
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.
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.
136 bool PatchingSuccess = false;
137 auto XRayPatchingStatusResetter = ScopeCleanup([&PatchingSuccess] {
138 if (!PatchingSuccess) {
139 XRayPatching.store(false, std::memory_order_release);
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;
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;
157 for (std::size_t I = 0; I < InstrMap.Entries; I++) {
158 auto Sled = InstrMap.Sleds[I];
159 auto F = Sled.Function;
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;
179 bool Success = false;
181 case XRayEntryType::ENTRY:
182 Success = patchFunctionEntry(Enable, FuncId, Sled);
184 case XRayEntryType::EXIT:
185 Success = patchFunctionExit(Enable, FuncId, Sled);
187 case XRayEntryType::TAIL:
188 Success = patchFunctionTailExit(Enable, FuncId, Sled);
191 Report("Unsupported sled kind: %d\n", int(Sled.Kind));
196 XRayPatching.store(false, std::memory_order_release);
197 PatchingSuccess = true;
198 return XRayPatchingStatus::SUCCESS;
201 XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT {
202 return ControlPatching(true);
205 XRayPatchingStatus __xray_unpatch() XRAY_NEVER_INSTRUMENT {
206 return ControlPatching(false);