Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_init.cpp
35265 views
//===-- xray_init.cpp -------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file is a part of XRay, a dynamic runtime instrumentation system.9//10// XRay initialisation logic.11//===----------------------------------------------------------------------===//1213#include <fcntl.h>14#include <strings.h>15#include <unistd.h>1617#include "sanitizer_common/sanitizer_common.h"18#include "xray_defs.h"19#include "xray_flags.h"20#include "xray_interface_internal.h"2122extern "C" {23void __xray_init();24extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak));25extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak));26extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak));27extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak));2829#if SANITIZER_APPLE30// HACK: This is a temporary workaround to make XRay build on31// Darwin, but it will probably not work at runtime.32const XRaySledEntry __start_xray_instr_map[] = {};33extern const XRaySledEntry __stop_xray_instr_map[] = {};34extern const XRayFunctionSledIndex __start_xray_fn_idx[] = {};35extern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {};36#endif37}3839using namespace __xray;4041// When set to 'true' this means the XRay runtime has been initialised. We use42// the weak symbols defined above (__start_xray_inst_map and43// __stop_xray_instr_map) to initialise the instrumentation map that XRay uses44// for runtime patching/unpatching of instrumentation points.45//46// FIXME: Support DSO instrumentation maps too. The current solution only works47// for statically linked executables.48atomic_uint8_t XRayInitialized{0};4950// This should always be updated before XRayInitialized is updated.51SpinMutex XRayInstrMapMutex;52XRaySledMap XRayInstrMap;5354// Global flag to determine whether the flags have been initialized.55atomic_uint8_t XRayFlagsInitialized{0};5657// A mutex to allow only one thread to initialize the XRay data structures.58SpinMutex XRayInitMutex;5960// __xray_init() will do the actual loading of the current process' memory map61// and then proceed to look for the .xray_instr_map section/segment.62void __xray_init() XRAY_NEVER_INSTRUMENT {63SpinMutexLock Guard(&XRayInitMutex);64// Short-circuit if we've already initialized XRay before.65if (atomic_load(&XRayInitialized, memory_order_acquire))66return;6768// XRAY is not compatible with PaX MPROTECT69CheckMPROTECT();7071if (!atomic_load(&XRayFlagsInitialized, memory_order_acquire)) {72initializeFlags();73atomic_store(&XRayFlagsInitialized, true, memory_order_release);74}7576if (__start_xray_instr_map == nullptr) {77if (Verbosity())78Report("XRay instrumentation map missing. Not initializing XRay.\n");79return;80}8182{83SpinMutexLock Guard(&XRayInstrMapMutex);84XRayInstrMap.Sleds = __start_xray_instr_map;85XRayInstrMap.Entries = __stop_xray_instr_map - __start_xray_instr_map;86if (__start_xray_fn_idx != nullptr) {87XRayInstrMap.SledsIndex = __start_xray_fn_idx;88XRayInstrMap.Functions = __stop_xray_fn_idx - __start_xray_fn_idx;89} else {90size_t CountFunctions = 0;91uint64_t LastFnAddr = 0;9293for (std::size_t I = 0; I < XRayInstrMap.Entries; I++) {94const auto &Sled = XRayInstrMap.Sleds[I];95const auto Function = Sled.function();96if (Function != LastFnAddr) {97CountFunctions++;98LastFnAddr = Function;99}100}101102XRayInstrMap.Functions = CountFunctions;103}104}105atomic_store(&XRayInitialized, true, memory_order_release);106107#ifndef XRAY_NO_PREINIT108if (flags()->patch_premain)109__xray_patch();110#endif111}112113// FIXME: Make check-xray tests work on FreeBSD without114// SANITIZER_CAN_USE_PREINIT_ARRAY.115// See sanitizer_internal_defs.h where the macro is defined.116// Calling unresolved PLT functions in .preinit_array can lead to deadlock on117// FreeBSD but here it seems benign.118#if !defined(XRAY_NO_PREINIT) && \119(SANITIZER_CAN_USE_PREINIT_ARRAY || SANITIZER_FREEBSD)120// Only add the preinit array initialization if the sanitizers can.121__attribute__((section(".preinit_array"),122used)) void (*__local_xray_preinit)(void) = __xray_init;123#else124// If we cannot use the .preinit_array section, we should instead use dynamic125// initialisation.126__attribute__ ((constructor (0)))127static void __local_xray_dyninit() {128__xray_init();129}130#endif131132133