xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/xray/xray_init.cpp (revision 68d75eff68281c1b445e3010bb975eae07aac225)
1*68d75effSDimitry Andric //===-- xray_init.cpp -------------------------------------------*- C++ -*-===//
2*68d75effSDimitry Andric //
3*68d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*68d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*68d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*68d75effSDimitry Andric //
7*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
8*68d75effSDimitry Andric //
9*68d75effSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
10*68d75effSDimitry Andric //
11*68d75effSDimitry Andric // XRay initialisation logic.
12*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
13*68d75effSDimitry Andric 
14*68d75effSDimitry Andric #include <fcntl.h>
15*68d75effSDimitry Andric #include <strings.h>
16*68d75effSDimitry Andric #include <unistd.h>
17*68d75effSDimitry Andric 
18*68d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
19*68d75effSDimitry Andric #include "xray_defs.h"
20*68d75effSDimitry Andric #include "xray_flags.h"
21*68d75effSDimitry Andric #include "xray_interface_internal.h"
22*68d75effSDimitry Andric 
23*68d75effSDimitry Andric extern "C" {
24*68d75effSDimitry Andric void __xray_init();
25*68d75effSDimitry Andric extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak));
26*68d75effSDimitry Andric extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak));
27*68d75effSDimitry Andric extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak));
28*68d75effSDimitry Andric extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak));
29*68d75effSDimitry Andric 
30*68d75effSDimitry Andric #if SANITIZER_MAC
31*68d75effSDimitry Andric // HACK: This is a temporary workaround to make XRay build on
32*68d75effSDimitry Andric // Darwin, but it will probably not work at runtime.
33*68d75effSDimitry Andric const XRaySledEntry __start_xray_instr_map[] = {};
34*68d75effSDimitry Andric extern const XRaySledEntry __stop_xray_instr_map[] = {};
35*68d75effSDimitry Andric extern const XRayFunctionSledIndex __start_xray_fn_idx[] = {};
36*68d75effSDimitry Andric extern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {};
37*68d75effSDimitry Andric #endif
38*68d75effSDimitry Andric }
39*68d75effSDimitry Andric 
40*68d75effSDimitry Andric using namespace __xray;
41*68d75effSDimitry Andric 
42*68d75effSDimitry Andric // When set to 'true' this means the XRay runtime has been initialised. We use
43*68d75effSDimitry Andric // the weak symbols defined above (__start_xray_inst_map and
44*68d75effSDimitry Andric // __stop_xray_instr_map) to initialise the instrumentation map that XRay uses
45*68d75effSDimitry Andric // for runtime patching/unpatching of instrumentation points.
46*68d75effSDimitry Andric //
47*68d75effSDimitry Andric // FIXME: Support DSO instrumentation maps too. The current solution only works
48*68d75effSDimitry Andric // for statically linked executables.
49*68d75effSDimitry Andric atomic_uint8_t XRayInitialized{0};
50*68d75effSDimitry Andric 
51*68d75effSDimitry Andric // This should always be updated before XRayInitialized is updated.
52*68d75effSDimitry Andric SpinMutex XRayInstrMapMutex;
53*68d75effSDimitry Andric XRaySledMap XRayInstrMap;
54*68d75effSDimitry Andric 
55*68d75effSDimitry Andric // Global flag to determine whether the flags have been initialized.
56*68d75effSDimitry Andric atomic_uint8_t XRayFlagsInitialized{0};
57*68d75effSDimitry Andric 
58*68d75effSDimitry Andric // A mutex to allow only one thread to initialize the XRay data structures.
59*68d75effSDimitry Andric SpinMutex XRayInitMutex;
60*68d75effSDimitry Andric 
61*68d75effSDimitry Andric // __xray_init() will do the actual loading of the current process' memory map
62*68d75effSDimitry Andric // and then proceed to look for the .xray_instr_map section/segment.
63*68d75effSDimitry Andric void __xray_init() XRAY_NEVER_INSTRUMENT {
64*68d75effSDimitry Andric   SpinMutexLock Guard(&XRayInitMutex);
65*68d75effSDimitry Andric   // Short-circuit if we've already initialized XRay before.
66*68d75effSDimitry Andric   if (atomic_load(&XRayInitialized, memory_order_acquire))
67*68d75effSDimitry Andric     return;
68*68d75effSDimitry Andric 
69*68d75effSDimitry Andric   // XRAY is not compatible with PaX MPROTECT
70*68d75effSDimitry Andric   CheckMPROTECT();
71*68d75effSDimitry Andric 
72*68d75effSDimitry Andric   if (!atomic_load(&XRayFlagsInitialized, memory_order_acquire)) {
73*68d75effSDimitry Andric     initializeFlags();
74*68d75effSDimitry Andric     atomic_store(&XRayFlagsInitialized, true, memory_order_release);
75*68d75effSDimitry Andric   }
76*68d75effSDimitry Andric 
77*68d75effSDimitry Andric   if (__start_xray_instr_map == nullptr) {
78*68d75effSDimitry Andric     if (Verbosity())
79*68d75effSDimitry Andric       Report("XRay instrumentation map missing. Not initializing XRay.\n");
80*68d75effSDimitry Andric     return;
81*68d75effSDimitry Andric   }
82*68d75effSDimitry Andric 
83*68d75effSDimitry Andric   {
84*68d75effSDimitry Andric     SpinMutexLock Guard(&XRayInstrMapMutex);
85*68d75effSDimitry Andric     XRayInstrMap.Sleds = __start_xray_instr_map;
86*68d75effSDimitry Andric     XRayInstrMap.Entries = __stop_xray_instr_map - __start_xray_instr_map;
87*68d75effSDimitry Andric     XRayInstrMap.SledsIndex = __start_xray_fn_idx;
88*68d75effSDimitry Andric     XRayInstrMap.Functions = __stop_xray_fn_idx - __start_xray_fn_idx;
89*68d75effSDimitry Andric   }
90*68d75effSDimitry Andric   atomic_store(&XRayInitialized, true, memory_order_release);
91*68d75effSDimitry Andric 
92*68d75effSDimitry Andric #ifndef XRAY_NO_PREINIT
93*68d75effSDimitry Andric   if (flags()->patch_premain)
94*68d75effSDimitry Andric     __xray_patch();
95*68d75effSDimitry Andric #endif
96*68d75effSDimitry Andric }
97*68d75effSDimitry Andric 
98*68d75effSDimitry Andric // FIXME: Make check-xray tests work on FreeBSD without
99*68d75effSDimitry Andric // SANITIZER_CAN_USE_PREINIT_ARRAY.
100*68d75effSDimitry Andric // See sanitizer_internal_defs.h where the macro is defined.
101*68d75effSDimitry Andric // Calling unresolved PLT functions in .preinit_array can lead to deadlock on
102*68d75effSDimitry Andric // FreeBSD but here it seems benign.
103*68d75effSDimitry Andric #if !defined(XRAY_NO_PREINIT) &&                                               \
104*68d75effSDimitry Andric     (SANITIZER_CAN_USE_PREINIT_ARRAY || SANITIZER_FREEBSD)
105*68d75effSDimitry Andric // Only add the preinit array initialization if the sanitizers can.
106*68d75effSDimitry Andric __attribute__((section(".preinit_array"),
107*68d75effSDimitry Andric                used)) void (*__local_xray_preinit)(void) = __xray_init;
108*68d75effSDimitry Andric #else
109*68d75effSDimitry Andric // If we cannot use the .preinit_array section, we should instead use dynamic
110*68d75effSDimitry Andric // initialisation.
111*68d75effSDimitry Andric __attribute__ ((constructor (0)))
112*68d75effSDimitry Andric static void __local_xray_dyninit() {
113*68d75effSDimitry Andric   __xray_init();
114*68d75effSDimitry Andric }
115*68d75effSDimitry Andric #endif
116