2016-07-21 15:39:55 +08:00
|
|
|
//===-- xray_interface.cpp --------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-07-21 15:39:55 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file is a part of XRay, a dynamic runtime instrumentation system.
|
|
|
|
//
|
|
|
|
// Implementation of the API functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "xray_interface_internal.h"
|
2016-07-29 15:11:58 +08:00
|
|
|
|
[XRay] fix more -Wformat warnings
Building xray with recent clang on a 64-bit system results in a number
of -Wformat warnings:
compiler-rt/lib/xray/xray_allocator.h:70:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
compiler-rt/lib/xray/xray_allocator.h:119:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
Since `__sanitizer::uptr` has the same size as `size_t`, these can be
fixed by using the printf specifier `%zu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:348:46: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Cleaned up log for TID: %d\n", GetTid());
~~ ^~~~~~~~
%llu
compiler-rt/lib/xray/xray_basic_logging.cpp:353:62: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Skipping buffer for TID: %d; Offset = %llu\n", GetTid(),
~~ ^~~~~~~~
%llu
Since `__sanitizer::tid_t` is effectively declared as `unsigned long
long`, these can be fixed by using the printf specifier `%llu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:354:14: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
TLD.BufferOffset);
^~~~~~~~~~~~~~~~
Since `BufferOffset` is declared as `size_t`, this one can be fixed by
using `%zu` as a printf specifier.
compiler-rt/lib/xray/xray_interface.cpp:172:50: warning: format specifies type 'int' but the argument has type 'uint64_t' (aka 'unsigned long') [-Wformat]
Report("Unsupported sled kind '%d' @%04x\n", Sled.Address, int(Sled.Kind));
~~ ^~~~~~~~~~~~
%lu
Since ``xray::SledEntry::Address` is declared as `uint64_t`, this one
can be fixed by using `PRIu64`, and adding `<cinttypes>`.
compiler-rt/lib/xray/xray_interface.cpp:308:62: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("System page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
compiler-rt/lib/xray/xray_interface.cpp:359:64: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("Provided page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
Since `PageSize` is declared as `size_t`, these can be fixed by using
`%zu` as a printf specifier.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D114469
2021-11-24 04:21:02 +08:00
|
|
|
#include <cinttypes>
|
2016-07-21 15:39:55 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <limits>
|
2018-04-18 05:28:53 +08:00
|
|
|
#include <string.h>
|
2016-07-21 15:39:55 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2018-11-22 10:00:44 +08:00
|
|
|
#if SANITIZER_FUCHSIA
|
|
|
|
#include <zircon/process.h>
|
|
|
|
#include <zircon/sanitizer.h>
|
|
|
|
#include <zircon/status.h>
|
|
|
|
#include <zircon/syscalls.h>
|
|
|
|
#endif
|
|
|
|
|
2018-04-18 05:28:53 +08:00
|
|
|
#include "sanitizer_common/sanitizer_addrhashmap.h"
|
2016-07-29 15:11:58 +08:00
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
2018-04-18 05:28:53 +08:00
|
|
|
|
2016-11-16 09:01:13 +08:00
|
|
|
#include "xray_defs.h"
|
2017-12-14 10:51:20 +08:00
|
|
|
#include "xray_flags.h"
|
|
|
|
|
|
|
|
extern __sanitizer::SpinMutex XRayInstrMapMutex;
|
|
|
|
extern __sanitizer::atomic_uint8_t XRayInitialized;
|
|
|
|
extern __xray::XRaySledMap XRayInstrMap;
|
2016-07-29 15:11:58 +08:00
|
|
|
|
2016-07-21 15:39:55 +08:00
|
|
|
namespace __xray {
|
|
|
|
|
2016-09-20 22:35:57 +08:00
|
|
|
#if defined(__x86_64__)
|
2016-10-06 15:09:40 +08:00
|
|
|
static const int16_t cSledLength = 12;
|
2016-11-21 11:20:43 +08:00
|
|
|
#elif defined(__aarch64__)
|
|
|
|
static const int16_t cSledLength = 32;
|
2016-12-06 07:29:56 +08:00
|
|
|
#elif defined(__arm__)
|
|
|
|
static const int16_t cSledLength = 28;
|
2017-02-15 18:54:09 +08:00
|
|
|
#elif SANITIZER_MIPS32
|
|
|
|
static const int16_t cSledLength = 48;
|
|
|
|
#elif SANITIZER_MIPS64
|
|
|
|
static const int16_t cSledLength = 64;
|
2017-02-16 06:40:29 +08:00
|
|
|
#elif defined(__powerpc64__)
|
|
|
|
static const int16_t cSledLength = 8;
|
2021-12-09 09:57:20 +08:00
|
|
|
#elif defined(__hexagon__)
|
|
|
|
static const int16_t cSledLength = 20;
|
2016-09-20 22:35:57 +08:00
|
|
|
#else
|
2016-10-06 15:09:40 +08:00
|
|
|
#error "Unsupported CPU Architecture"
|
2016-09-20 22:35:57 +08:00
|
|
|
#endif /* CPU architecture */
|
|
|
|
|
2016-07-21 15:39:55 +08:00
|
|
|
// This is the function to call when we encounter the entry or exit sleds.
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_uintptr_t XRayPatchedFunction{0};
|
2016-07-21 15:39:55 +08:00
|
|
|
|
2017-03-06 15:25:41 +08:00
|
|
|
// This is the function to call from the arg1-enabled sleds/trampolines.
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_uintptr_t XRayArgLogger{0};
|
2017-03-06 15:25:41 +08:00
|
|
|
|
2017-05-12 09:07:41 +08:00
|
|
|
// This is the function to call when we encounter a custom event log call.
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_uintptr_t XRayPatchedCustomEvent{0};
|
2017-05-12 09:07:41 +08:00
|
|
|
|
2018-04-18 05:28:53 +08:00
|
|
|
// This is the function to call when we encounter a typed event log call.
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_uintptr_t XRayPatchedTypedEvent{0};
|
2018-04-18 05:28:53 +08:00
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
// This is the global status to determine whether we are currently
|
|
|
|
// patching/unpatching.
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_uint8_t XRayPatching{0};
|
2017-12-14 10:51:20 +08:00
|
|
|
|
2018-04-18 05:28:53 +08:00
|
|
|
struct TypeDescription {
|
|
|
|
uint32_t type_id;
|
|
|
|
std::size_t description_string_length;
|
|
|
|
};
|
|
|
|
|
2018-06-05 14:12:42 +08:00
|
|
|
using TypeDescriptorMapType = AddrHashMap<TypeDescription, 11>;
|
2018-04-18 05:28:53 +08:00
|
|
|
// An address map from immutable descriptors to type ids.
|
|
|
|
TypeDescriptorMapType TypeDescriptorAddressMap{};
|
|
|
|
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_uint32_t TypeEventDescriptorCounter{0};
|
2018-04-18 05:28:53 +08:00
|
|
|
|
|
|
|
// MProtectHelper is an RAII wrapper for calls to mprotect(...) that will
|
|
|
|
// undo any successful mprotect(...) changes. This is used to make a page
|
|
|
|
// writeable and executable, and upon destruction if it was successful in
|
|
|
|
// doing so returns the page into a read-only and executable page.
|
2016-07-27 12:30:25 +08:00
|
|
|
//
|
|
|
|
// This is only used specifically for runtime-patching of the XRay
|
2018-04-18 05:28:53 +08:00
|
|
|
// instrumentation points. This assumes that the executable pages are
|
|
|
|
// originally read-and-execute only.
|
2016-07-27 12:30:25 +08:00
|
|
|
class MProtectHelper {
|
|
|
|
void *PageAlignedAddr;
|
|
|
|
std::size_t MProtectLen;
|
|
|
|
bool MustCleanup;
|
|
|
|
|
|
|
|
public:
|
2016-11-16 09:01:13 +08:00
|
|
|
explicit MProtectHelper(void *PageAlignedAddr,
|
2018-11-22 10:00:44 +08:00
|
|
|
std::size_t MProtectLen,
|
|
|
|
std::size_t PageSize) XRAY_NEVER_INSTRUMENT
|
2016-11-16 09:01:13 +08:00
|
|
|
: PageAlignedAddr(PageAlignedAddr),
|
|
|
|
MProtectLen(MProtectLen),
|
2018-11-22 10:00:44 +08:00
|
|
|
MustCleanup(false) {
|
|
|
|
#if SANITIZER_FUCHSIA
|
|
|
|
MProtectLen = RoundUpTo(MProtectLen, PageSize);
|
|
|
|
#endif
|
|
|
|
}
|
2016-07-27 12:30:25 +08:00
|
|
|
|
2016-11-16 09:01:13 +08:00
|
|
|
int MakeWriteable() XRAY_NEVER_INSTRUMENT {
|
2018-11-22 10:00:44 +08:00
|
|
|
#if SANITIZER_FUCHSIA
|
|
|
|
auto R = __sanitizer_change_code_protection(
|
|
|
|
reinterpret_cast<uintptr_t>(PageAlignedAddr), MProtectLen, true);
|
|
|
|
if (R != ZX_OK) {
|
|
|
|
Report("XRay: cannot change code protection: %s\n",
|
|
|
|
_zx_status_get_string(R));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
MustCleanup = true;
|
|
|
|
return 0;
|
|
|
|
#else
|
2016-07-27 12:30:25 +08:00
|
|
|
auto R = mprotect(PageAlignedAddr, MProtectLen,
|
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC);
|
|
|
|
if (R != -1)
|
|
|
|
MustCleanup = true;
|
|
|
|
return R;
|
2018-11-22 10:00:44 +08:00
|
|
|
#endif
|
2016-07-27 12:30:25 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:01:13 +08:00
|
|
|
~MProtectHelper() XRAY_NEVER_INSTRUMENT {
|
2016-07-27 12:30:25 +08:00
|
|
|
if (MustCleanup) {
|
2018-11-22 10:00:44 +08:00
|
|
|
#if SANITIZER_FUCHSIA
|
|
|
|
auto R = __sanitizer_change_code_protection(
|
|
|
|
reinterpret_cast<uintptr_t>(PageAlignedAddr), MProtectLen, false);
|
|
|
|
if (R != ZX_OK) {
|
|
|
|
Report("XRay: cannot change code protection: %s\n",
|
|
|
|
_zx_status_get_string(R));
|
|
|
|
}
|
|
|
|
#else
|
2016-07-27 12:30:25 +08:00
|
|
|
mprotect(PageAlignedAddr, MProtectLen, PROT_READ | PROT_EXEC);
|
2018-11-22 10:00:44 +08:00
|
|
|
#endif
|
2016-07-27 12:30:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
namespace {
|
2017-05-04 12:59:20 +08:00
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
bool patchSled(const XRaySledEntry &Sled, bool Enable,
|
|
|
|
int32_t FuncId) XRAY_NEVER_INSTRUMENT {
|
2017-05-04 12:59:20 +08:00
|
|
|
bool Success = false;
|
|
|
|
switch (Sled.Kind) {
|
|
|
|
case XRayEntryType::ENTRY:
|
|
|
|
Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_FunctionEntry);
|
|
|
|
break;
|
|
|
|
case XRayEntryType::EXIT:
|
|
|
|
Success = patchFunctionExit(Enable, FuncId, Sled);
|
|
|
|
break;
|
|
|
|
case XRayEntryType::TAIL:
|
|
|
|
Success = patchFunctionTailExit(Enable, FuncId, Sled);
|
|
|
|
break;
|
|
|
|
case XRayEntryType::LOG_ARGS_ENTRY:
|
|
|
|
Success = patchFunctionEntry(Enable, FuncId, Sled, __xray_ArgLoggerEntry);
|
|
|
|
break;
|
2017-05-12 09:07:41 +08:00
|
|
|
case XRayEntryType::CUSTOM_EVENT:
|
|
|
|
Success = patchCustomEvent(Enable, FuncId, Sled);
|
|
|
|
break;
|
2018-04-18 05:28:53 +08:00
|
|
|
case XRayEntryType::TYPED_EVENT:
|
|
|
|
Success = patchTypedEvent(Enable, FuncId, Sled);
|
|
|
|
break;
|
2017-05-04 12:59:20 +08:00
|
|
|
default:
|
[XRay] fix more -Wformat warnings
Building xray with recent clang on a 64-bit system results in a number
of -Wformat warnings:
compiler-rt/lib/xray/xray_allocator.h:70:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
compiler-rt/lib/xray/xray_allocator.h:119:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
Since `__sanitizer::uptr` has the same size as `size_t`, these can be
fixed by using the printf specifier `%zu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:348:46: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Cleaned up log for TID: %d\n", GetTid());
~~ ^~~~~~~~
%llu
compiler-rt/lib/xray/xray_basic_logging.cpp:353:62: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Skipping buffer for TID: %d; Offset = %llu\n", GetTid(),
~~ ^~~~~~~~
%llu
Since `__sanitizer::tid_t` is effectively declared as `unsigned long
long`, these can be fixed by using the printf specifier `%llu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:354:14: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
TLD.BufferOffset);
^~~~~~~~~~~~~~~~
Since `BufferOffset` is declared as `size_t`, this one can be fixed by
using `%zu` as a printf specifier.
compiler-rt/lib/xray/xray_interface.cpp:172:50: warning: format specifies type 'int' but the argument has type 'uint64_t' (aka 'unsigned long') [-Wformat]
Report("Unsupported sled kind '%d' @%04x\n", Sled.Address, int(Sled.Kind));
~~ ^~~~~~~~~~~~
%lu
Since ``xray::SledEntry::Address` is declared as `uint64_t`, this one
can be fixed by using `PRIu64`, and adding `<cinttypes>`.
compiler-rt/lib/xray/xray_interface.cpp:308:62: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("System page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
compiler-rt/lib/xray/xray_interface.cpp:359:64: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("Provided page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
Since `PageSize` is declared as `size_t`, these can be fixed by using
`%zu` as a printf specifier.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D114469
2021-11-24 04:21:02 +08:00
|
|
|
Report("Unsupported sled kind '%" PRIu64 "' @%04x\n", Sled.Address,
|
|
|
|
int(Sled.Kind));
|
2017-05-04 12:59:20 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return Success;
|
|
|
|
}
|
|
|
|
|
2020-06-17 08:36:11 +08:00
|
|
|
const XRayFunctionSledIndex
|
|
|
|
findFunctionSleds(int32_t FuncId,
|
|
|
|
const XRaySledMap &InstrMap) XRAY_NEVER_INSTRUMENT {
|
|
|
|
int32_t CurFn = 0;
|
|
|
|
uint64_t LastFnAddr = 0;
|
|
|
|
XRayFunctionSledIndex Index = {nullptr, nullptr};
|
|
|
|
|
|
|
|
for (std::size_t I = 0; I < InstrMap.Entries && CurFn <= FuncId; I++) {
|
|
|
|
const auto &Sled = InstrMap.Sleds[I];
|
|
|
|
const auto Function = Sled.function();
|
|
|
|
if (Function != LastFnAddr) {
|
|
|
|
CurFn++;
|
|
|
|
LastFnAddr = Function;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurFn == FuncId) {
|
|
|
|
if (Index.Begin == nullptr)
|
|
|
|
Index.Begin = &Sled;
|
|
|
|
Index.End = &Sled;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Index.End += 1;
|
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
XRayPatchingStatus patchFunction(int32_t FuncId,
|
|
|
|
bool Enable) XRAY_NEVER_INSTRUMENT {
|
2018-06-05 14:12:42 +08:00
|
|
|
if (!atomic_load(&XRayInitialized,
|
|
|
|
memory_order_acquire))
|
2017-12-14 10:51:20 +08:00
|
|
|
return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized.
|
|
|
|
|
|
|
|
uint8_t NotPatching = false;
|
2018-06-05 14:12:42 +08:00
|
|
|
if (!atomic_compare_exchange_strong(
|
|
|
|
&XRayPatching, &NotPatching, true, memory_order_acq_rel))
|
2017-12-14 10:51:20 +08:00
|
|
|
return XRayPatchingStatus::ONGOING; // Already patching.
|
|
|
|
|
|
|
|
// Next, we look for the function index.
|
|
|
|
XRaySledMap InstrMap;
|
|
|
|
{
|
2018-06-05 14:12:42 +08:00
|
|
|
SpinMutexLock Guard(&XRayInstrMapMutex);
|
2017-12-14 10:51:20 +08:00
|
|
|
InstrMap = XRayInstrMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we don't have an index, we can't patch individual functions.
|
|
|
|
if (InstrMap.Functions == 0)
|
|
|
|
return XRayPatchingStatus::NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// FuncId must be a positive number, less than the number of functions
|
|
|
|
// instrumented.
|
|
|
|
if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions) {
|
|
|
|
Report("Invalid function id provided: %d\n", FuncId);
|
|
|
|
return XRayPatchingStatus::FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we patch ths sleds for this specific function.
|
2020-06-17 08:36:11 +08:00
|
|
|
auto SledRange = InstrMap.SledsIndex ? InstrMap.SledsIndex[FuncId - 1]
|
|
|
|
: findFunctionSleds(FuncId, InstrMap);
|
2017-12-14 10:51:20 +08:00
|
|
|
auto *f = SledRange.Begin;
|
|
|
|
auto *e = SledRange.End;
|
|
|
|
bool SucceedOnce = false;
|
|
|
|
while (f != e)
|
|
|
|
SucceedOnce |= patchSled(*f++, Enable, FuncId);
|
|
|
|
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_store(&XRayPatching, false,
|
|
|
|
memory_order_release);
|
2017-12-14 10:51:20 +08:00
|
|
|
|
|
|
|
if (!SucceedOnce) {
|
|
|
|
Report("Failed patching any sled for function '%d'.", FuncId);
|
|
|
|
return XRayPatchingStatus::FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return XRayPatchingStatus::SUCCESS;
|
|
|
|
}
|
|
|
|
|
2017-02-08 07:35:34 +08:00
|
|
|
// controlPatching implements the common internals of the patching/unpatching
|
2016-08-08 11:10:22 +08:00
|
|
|
// implementation. |Enable| defines whether we're enabling or disabling the
|
|
|
|
// runtime XRay instrumentation.
|
2017-02-08 07:35:34 +08:00
|
|
|
XRayPatchingStatus controlPatching(bool Enable) XRAY_NEVER_INSTRUMENT {
|
2018-06-05 14:12:42 +08:00
|
|
|
if (!atomic_load(&XRayInitialized,
|
|
|
|
memory_order_acquire))
|
2016-07-21 15:39:55 +08:00
|
|
|
return XRayPatchingStatus::NOT_INITIALIZED; // Not initialized.
|
|
|
|
|
2017-03-27 15:13:35 +08:00
|
|
|
uint8_t NotPatching = false;
|
2018-06-05 14:12:42 +08:00
|
|
|
if (!atomic_compare_exchange_strong(
|
|
|
|
&XRayPatching, &NotPatching, true, memory_order_acq_rel))
|
2016-07-21 15:39:55 +08:00
|
|
|
return XRayPatchingStatus::ONGOING; // Already patching.
|
|
|
|
|
2017-03-27 15:13:35 +08:00
|
|
|
uint8_t PatchingSuccess = false;
|
2017-12-14 10:51:20 +08:00
|
|
|
auto XRayPatchingStatusResetter =
|
2018-06-05 14:12:42 +08:00
|
|
|
at_scope_exit([&PatchingSuccess] {
|
2017-12-14 10:51:20 +08:00
|
|
|
if (!PatchingSuccess)
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_store(&XRayPatching, false,
|
|
|
|
memory_order_release);
|
2017-12-14 10:51:20 +08:00
|
|
|
});
|
|
|
|
|
2017-03-27 15:13:35 +08:00
|
|
|
XRaySledMap InstrMap;
|
|
|
|
{
|
2018-06-05 14:12:42 +08:00
|
|
|
SpinMutexLock Guard(&XRayInstrMapMutex);
|
2017-03-27 15:13:35 +08:00
|
|
|
InstrMap = XRayInstrMap;
|
|
|
|
}
|
2016-07-21 15:39:55 +08:00
|
|
|
if (InstrMap.Entries == 0)
|
|
|
|
return XRayPatchingStatus::NOT_INITIALIZED;
|
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
uint32_t FuncId = 1;
|
|
|
|
uint64_t CurFun = 0;
|
|
|
|
|
|
|
|
// First we want to find the bounds for which we have instrumentation points,
|
|
|
|
// and try to get as few calls to mprotect(...) as possible. We're assuming
|
|
|
|
// that all the sleds for the instrumentation map are contiguous as a single
|
|
|
|
// set of pages. When we do support dynamic shared object instrumentation,
|
|
|
|
// we'll need to do this for each set of page load offsets per DSO loaded. For
|
|
|
|
// now we're assuming we can mprotect the whole section of text between the
|
|
|
|
// minimum sled address and the maximum sled address (+ the largest sled
|
|
|
|
// size).
|
2020-04-14 13:28:16 +08:00
|
|
|
auto *MinSled = &InstrMap.Sleds[0];
|
|
|
|
auto *MaxSled = &InstrMap.Sleds[InstrMap.Entries - 1];
|
2017-12-14 10:51:20 +08:00
|
|
|
for (std::size_t I = 0; I < InstrMap.Entries; I++) {
|
|
|
|
const auto &Sled = InstrMap.Sleds[I];
|
2020-04-14 13:28:16 +08:00
|
|
|
if (Sled.address() < MinSled->address())
|
|
|
|
MinSled = &Sled;
|
|
|
|
if (Sled.address() > MaxSled->address())
|
|
|
|
MaxSled = &Sled;
|
2017-12-14 10:51:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const size_t PageSize = flags()->xray_page_size_override > 0
|
|
|
|
? flags()->xray_page_size_override
|
|
|
|
: GetPageSizeCached();
|
2016-10-06 15:09:40 +08:00
|
|
|
if ((PageSize == 0) || ((PageSize & (PageSize - 1)) != 0)) {
|
[XRay] fix more -Wformat warnings
Building xray with recent clang on a 64-bit system results in a number
of -Wformat warnings:
compiler-rt/lib/xray/xray_allocator.h:70:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
compiler-rt/lib/xray/xray_allocator.h:119:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
Since `__sanitizer::uptr` has the same size as `size_t`, these can be
fixed by using the printf specifier `%zu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:348:46: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Cleaned up log for TID: %d\n", GetTid());
~~ ^~~~~~~~
%llu
compiler-rt/lib/xray/xray_basic_logging.cpp:353:62: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Skipping buffer for TID: %d; Offset = %llu\n", GetTid(),
~~ ^~~~~~~~
%llu
Since `__sanitizer::tid_t` is effectively declared as `unsigned long
long`, these can be fixed by using the printf specifier `%llu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:354:14: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
TLD.BufferOffset);
^~~~~~~~~~~~~~~~
Since `BufferOffset` is declared as `size_t`, this one can be fixed by
using `%zu` as a printf specifier.
compiler-rt/lib/xray/xray_interface.cpp:172:50: warning: format specifies type 'int' but the argument has type 'uint64_t' (aka 'unsigned long') [-Wformat]
Report("Unsupported sled kind '%d' @%04x\n", Sled.Address, int(Sled.Kind));
~~ ^~~~~~~~~~~~
%lu
Since ``xray::SledEntry::Address` is declared as `uint64_t`, this one
can be fixed by using `PRIu64`, and adding `<cinttypes>`.
compiler-rt/lib/xray/xray_interface.cpp:308:62: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("System page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
compiler-rt/lib/xray/xray_interface.cpp:359:64: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("Provided page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
Since `PageSize` is declared as `size_t`, these can be fixed by using
`%zu` as a printf specifier.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D114469
2021-11-24 04:21:02 +08:00
|
|
|
Report("System page size is not a power of two: %zu\n", PageSize);
|
2016-09-20 22:35:57 +08:00
|
|
|
return XRayPatchingStatus::FAILED;
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
void *PageAlignedAddr =
|
2020-04-14 13:28:16 +08:00
|
|
|
reinterpret_cast<void *>(MinSled->address() & ~(PageSize - 1));
|
2017-12-14 10:51:20 +08:00
|
|
|
size_t MProtectLen =
|
2020-04-14 13:28:16 +08:00
|
|
|
(MaxSled->address() - reinterpret_cast<uptr>(PageAlignedAddr)) +
|
|
|
|
cSledLength;
|
2018-11-22 10:00:44 +08:00
|
|
|
MProtectHelper Protector(PageAlignedAddr, MProtectLen, PageSize);
|
2017-12-14 10:51:20 +08:00
|
|
|
if (Protector.MakeWriteable() == -1) {
|
|
|
|
Report("Failed mprotect: %d\n", errno);
|
|
|
|
return XRayPatchingStatus::FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::size_t I = 0; I < InstrMap.Entries; ++I) {
|
|
|
|
auto &Sled = InstrMap.Sleds[I];
|
2020-04-25 04:39:02 +08:00
|
|
|
auto F = Sled.function();
|
2016-07-21 15:39:55 +08:00
|
|
|
if (CurFun == 0)
|
|
|
|
CurFun = F;
|
|
|
|
if (F != CurFun) {
|
|
|
|
++FuncId;
|
|
|
|
CurFun = F;
|
|
|
|
}
|
2017-05-04 12:59:20 +08:00
|
|
|
patchSled(Sled, Enable, FuncId);
|
2016-07-21 15:39:55 +08:00
|
|
|
}
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_store(&XRayPatching, false,
|
|
|
|
memory_order_release);
|
2016-07-29 15:11:58 +08:00
|
|
|
PatchingSuccess = true;
|
|
|
|
return XRayPatchingStatus::SUCCESS;
|
2016-07-21 15:39:55 +08:00
|
|
|
}
|
2016-08-08 11:10:22 +08:00
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
XRayPatchingStatus mprotectAndPatchFunction(int32_t FuncId,
|
|
|
|
bool Enable) XRAY_NEVER_INSTRUMENT {
|
2017-05-04 12:59:20 +08:00
|
|
|
XRaySledMap InstrMap;
|
|
|
|
{
|
2018-06-05 14:12:42 +08:00
|
|
|
SpinMutexLock Guard(&XRayInstrMapMutex);
|
2017-05-04 12:59:20 +08:00
|
|
|
InstrMap = XRayInstrMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FuncId must be a positive number, less than the number of functions
|
|
|
|
// instrumented.
|
2017-05-05 09:27:11 +08:00
|
|
|
if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions) {
|
2017-05-04 12:59:20 +08:00
|
|
|
Report("Invalid function id provided: %d\n", FuncId);
|
|
|
|
return XRayPatchingStatus::FAILED;
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
const size_t PageSize = flags()->xray_page_size_override > 0
|
|
|
|
? flags()->xray_page_size_override
|
|
|
|
: GetPageSizeCached();
|
|
|
|
if ((PageSize == 0) || ((PageSize & (PageSize - 1)) != 0)) {
|
[XRay] fix more -Wformat warnings
Building xray with recent clang on a 64-bit system results in a number
of -Wformat warnings:
compiler-rt/lib/xray/xray_allocator.h:70:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
compiler-rt/lib/xray/xray_allocator.h:119:11: warning: format specifies type 'int' but the argument has type '__sanitizer::uptr' (aka 'unsigned long') [-Wformat]
RoundedSize, B);
^~~~~~~~~~~
Since `__sanitizer::uptr` has the same size as `size_t`, these can be
fixed by using the printf specifier `%zu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:348:46: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Cleaned up log for TID: %d\n", GetTid());
~~ ^~~~~~~~
%llu
compiler-rt/lib/xray/xray_basic_logging.cpp:353:62: warning: format specifies type 'int' but the argument has type '__sanitizer::tid_t' (aka 'unsigned long long') [-Wformat]
Report("Skipping buffer for TID: %d; Offset = %llu\n", GetTid(),
~~ ^~~~~~~~
%llu
Since `__sanitizer::tid_t` is effectively declared as `unsigned long
long`, these can be fixed by using the printf specifier `%llu`.
compiler-rt/lib/xray/xray_basic_logging.cpp:354:14: warning: format specifies type 'unsigned long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
TLD.BufferOffset);
^~~~~~~~~~~~~~~~
Since `BufferOffset` is declared as `size_t`, this one can be fixed by
using `%zu` as a printf specifier.
compiler-rt/lib/xray/xray_interface.cpp:172:50: warning: format specifies type 'int' but the argument has type 'uint64_t' (aka 'unsigned long') [-Wformat]
Report("Unsupported sled kind '%d' @%04x\n", Sled.Address, int(Sled.Kind));
~~ ^~~~~~~~~~~~
%lu
Since ``xray::SledEntry::Address` is declared as `uint64_t`, this one
can be fixed by using `PRIu64`, and adding `<cinttypes>`.
compiler-rt/lib/xray/xray_interface.cpp:308:62: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("System page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
compiler-rt/lib/xray/xray_interface.cpp:359:64: warning: format specifies type 'long long' but the argument has type 'size_t' (aka 'unsigned long') [-Wformat]
Report("Provided page size is not a power of two: %lld\n", PageSize);
~~~~ ^~~~~~~~
%zu
Since `PageSize` is declared as `size_t`, these can be fixed by using
`%zu` as a printf specifier.
Reviewed By: vitalybuka
Differential Revision: https://reviews.llvm.org/D114469
2021-11-24 04:21:02 +08:00
|
|
|
Report("Provided page size is not a power of two: %zu\n", PageSize);
|
2017-12-14 10:51:20 +08:00
|
|
|
return XRayPatchingStatus::FAILED;
|
|
|
|
}
|
|
|
|
|
2021-09-04 15:31:34 +08:00
|
|
|
// Here we compute the minimum sled and maximum sled associated with a
|
2017-12-14 10:51:20 +08:00
|
|
|
// particular function ID.
|
2020-06-17 08:36:11 +08:00
|
|
|
auto SledRange = InstrMap.SledsIndex ? InstrMap.SledsIndex[FuncId - 1]
|
|
|
|
: findFunctionSleds(FuncId, InstrMap);
|
2017-05-04 12:59:20 +08:00
|
|
|
auto *f = SledRange.Begin;
|
|
|
|
auto *e = SledRange.End;
|
2020-04-14 13:28:16 +08:00
|
|
|
auto *MinSled = f;
|
|
|
|
auto *MaxSled = (SledRange.End - 1);
|
2017-12-14 10:51:20 +08:00
|
|
|
while (f != e) {
|
2020-04-14 13:28:16 +08:00
|
|
|
if (f->address() < MinSled->address())
|
|
|
|
MinSled = f;
|
|
|
|
if (f->address() > MaxSled->address())
|
|
|
|
MaxSled = f;
|
2017-12-14 10:51:20 +08:00
|
|
|
++f;
|
|
|
|
}
|
2017-05-04 12:59:20 +08:00
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
void *PageAlignedAddr =
|
2020-04-14 13:28:16 +08:00
|
|
|
reinterpret_cast<void *>(MinSled->address() & ~(PageSize - 1));
|
2017-12-14 10:51:20 +08:00
|
|
|
size_t MProtectLen =
|
2020-04-14 13:28:16 +08:00
|
|
|
(MaxSled->address() - reinterpret_cast<uptr>(PageAlignedAddr)) +
|
|
|
|
cSledLength;
|
2018-11-22 10:00:44 +08:00
|
|
|
MProtectHelper Protector(PageAlignedAddr, MProtectLen, PageSize);
|
2017-12-14 10:51:20 +08:00
|
|
|
if (Protector.MakeWriteable() == -1) {
|
|
|
|
Report("Failed mprotect: %d\n", errno);
|
|
|
|
return XRayPatchingStatus::FAILED;
|
|
|
|
}
|
|
|
|
return patchFunction(FuncId, Enable);
|
|
|
|
}
|
2017-05-04 12:59:20 +08:00
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
} // namespace
|
2017-05-04 12:59:20 +08:00
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
} // namespace __xray
|
|
|
|
|
|
|
|
using namespace __xray;
|
|
|
|
|
|
|
|
// The following functions are declared `extern "C" {...}` in the header, hence
|
|
|
|
// they're defined in the global namespace.
|
|
|
|
|
|
|
|
int __xray_set_handler(void (*entry)(int32_t,
|
|
|
|
XRayEntryType)) XRAY_NEVER_INSTRUMENT {
|
2018-06-05 14:12:42 +08:00
|
|
|
if (atomic_load(&XRayInitialized,
|
|
|
|
memory_order_acquire)) {
|
2017-12-14 10:51:20 +08:00
|
|
|
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_store(&__xray::XRayPatchedFunction,
|
2017-12-14 10:51:20 +08:00
|
|
|
reinterpret_cast<uintptr_t>(entry),
|
2018-06-05 14:12:42 +08:00
|
|
|
memory_order_release);
|
2017-12-14 10:51:20 +08:00
|
|
|
return 1;
|
2017-05-04 12:59:20 +08:00
|
|
|
}
|
2017-12-14 10:51:20 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2017-05-04 12:59:20 +08:00
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
int __xray_set_customevent_handler(void (*entry)(void *, size_t))
|
|
|
|
XRAY_NEVER_INSTRUMENT {
|
2018-06-05 14:12:42 +08:00
|
|
|
if (atomic_load(&XRayInitialized,
|
|
|
|
memory_order_acquire)) {
|
|
|
|
atomic_store(&__xray::XRayPatchedCustomEvent,
|
2017-12-14 10:51:20 +08:00
|
|
|
reinterpret_cast<uintptr_t>(entry),
|
2018-06-05 14:12:42 +08:00
|
|
|
memory_order_release);
|
2017-12-14 10:51:20 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:28:53 +08:00
|
|
|
int __xray_set_typedevent_handler(void (*entry)(
|
2018-04-25 04:33:37 +08:00
|
|
|
uint16_t, const void *, size_t)) XRAY_NEVER_INSTRUMENT {
|
2018-06-05 14:12:42 +08:00
|
|
|
if (atomic_load(&XRayInitialized,
|
|
|
|
memory_order_acquire)) {
|
|
|
|
atomic_store(&__xray::XRayPatchedTypedEvent,
|
2018-04-18 05:28:53 +08:00
|
|
|
reinterpret_cast<uintptr_t>(entry),
|
2018-06-05 14:12:42 +08:00
|
|
|
memory_order_release);
|
2018-04-18 05:28:53 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
int __xray_remove_handler() XRAY_NEVER_INSTRUMENT {
|
|
|
|
return __xray_set_handler(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int __xray_remove_customevent_handler() XRAY_NEVER_INSTRUMENT {
|
|
|
|
return __xray_set_customevent_handler(nullptr);
|
|
|
|
}
|
|
|
|
|
2018-04-25 04:33:37 +08:00
|
|
|
int __xray_remove_typedevent_handler() XRAY_NEVER_INSTRUMENT {
|
2018-04-18 05:28:53 +08:00
|
|
|
return __xray_set_typedevent_handler(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t __xray_register_event_type(
|
2018-04-25 04:33:37 +08:00
|
|
|
const char *const event_type) XRAY_NEVER_INSTRUMENT {
|
2018-04-18 05:28:53 +08:00
|
|
|
TypeDescriptorMapType::Handle h(&TypeDescriptorAddressMap, (uptr)event_type);
|
|
|
|
if (h.created()) {
|
2018-06-05 14:12:42 +08:00
|
|
|
h->type_id = atomic_fetch_add(
|
|
|
|
&TypeEventDescriptorCounter, 1, memory_order_acq_rel);
|
2018-04-18 05:28:53 +08:00
|
|
|
h->description_string_length = strnlen(event_type, 1024);
|
|
|
|
}
|
|
|
|
return h->type_id;
|
|
|
|
}
|
|
|
|
|
2017-12-14 10:51:20 +08:00
|
|
|
XRayPatchingStatus __xray_patch() XRAY_NEVER_INSTRUMENT {
|
|
|
|
return controlPatching(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
XRayPatchingStatus __xray_unpatch() XRAY_NEVER_INSTRUMENT {
|
|
|
|
return controlPatching(false);
|
2017-05-04 12:59:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
XRayPatchingStatus __xray_patch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
|
2017-12-14 10:51:20 +08:00
|
|
|
return mprotectAndPatchFunction(FuncId, true);
|
2017-05-04 12:59:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
XRayPatchingStatus
|
|
|
|
__xray_unpatch_function(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
|
2017-12-14 10:51:20 +08:00
|
|
|
return mprotectAndPatchFunction(FuncId, false);
|
2017-05-04 12:59:20 +08:00
|
|
|
}
|
|
|
|
|
2017-06-19 09:30:04 +08:00
|
|
|
int __xray_set_handler_arg1(void (*entry)(int32_t, XRayEntryType, uint64_t)) {
|
2018-06-05 14:12:42 +08:00
|
|
|
if (!atomic_load(&XRayInitialized,
|
|
|
|
memory_order_acquire))
|
2017-03-06 15:25:41 +08:00
|
|
|
return 0;
|
2017-03-27 15:13:35 +08:00
|
|
|
|
2017-03-06 15:25:41 +08:00
|
|
|
// A relaxed write might not be visible even if the current thread gets
|
|
|
|
// scheduled on a different CPU/NUMA node. We need to wait for everyone to
|
|
|
|
// have this handler installed for consistency of collected data across CPUs.
|
2018-06-05 14:12:42 +08:00
|
|
|
atomic_store(&XRayArgLogger, reinterpret_cast<uint64_t>(entry),
|
|
|
|
memory_order_release);
|
2017-03-06 15:25:41 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-05-12 09:07:41 +08:00
|
|
|
|
2017-03-06 15:25:41 +08:00
|
|
|
int __xray_remove_handler_arg1() { return __xray_set_handler_arg1(nullptr); }
|
2017-05-05 09:27:11 +08:00
|
|
|
|
|
|
|
uintptr_t __xray_function_address(int32_t FuncId) XRAY_NEVER_INSTRUMENT {
|
2020-06-17 08:36:11 +08:00
|
|
|
XRaySledMap InstrMap;
|
|
|
|
{
|
|
|
|
SpinMutexLock Guard(&XRayInstrMapMutex);
|
|
|
|
InstrMap = XRayInstrMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (FuncId <= 0 || static_cast<size_t>(FuncId) > InstrMap.Functions)
|
2017-05-05 09:27:11 +08:00
|
|
|
return 0;
|
2020-06-17 08:36:11 +08:00
|
|
|
const XRaySledEntry *Sled = InstrMap.SledsIndex
|
|
|
|
? InstrMap.SledsIndex[FuncId - 1].Begin
|
|
|
|
: findFunctionSleds(FuncId, InstrMap).Begin;
|
|
|
|
return Sled->function()
|
2017-05-18 05:20:00 +08:00
|
|
|
// On PPC, function entries are always aligned to 16 bytes. The beginning of a
|
|
|
|
// sled might be a local entry, which is always +8 based on the global entry.
|
|
|
|
// Always return the global entry.
|
|
|
|
#ifdef __PPC__
|
|
|
|
& ~0xf
|
|
|
|
#endif
|
|
|
|
;
|
2017-05-05 09:27:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t __xray_max_function_id() XRAY_NEVER_INSTRUMENT {
|
2018-06-05 14:12:42 +08:00
|
|
|
SpinMutexLock Guard(&XRayInstrMapMutex);
|
2017-05-05 09:27:11 +08:00
|
|
|
return XRayInstrMap.Functions;
|
|
|
|
}
|