]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/xray/xray_init.cc
MFV r340938:
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / xray / xray_init.cc
1 //===-- xray_init.cc --------------------------------------------*- 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 // XRay initialisation logic.
13 //===----------------------------------------------------------------------===//
14
15 #include <fcntl.h>
16 #include <strings.h>
17 #include <unistd.h>
18
19 #include "sanitizer_common/sanitizer_common.h"
20 #include "xray_defs.h"
21 #include "xray_flags.h"
22 #include "xray_interface_internal.h"
23
24 extern "C" {
25 void __xray_init();
26 extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak));
27 extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak));
28 extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak));
29 extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak));
30 }
31
32 using namespace __xray;
33
34 // When set to 'true' this means the XRay runtime has been initialised. We use
35 // the weak symbols defined above (__start_xray_inst_map and
36 // __stop_xray_instr_map) to initialise the instrumentation map that XRay uses
37 // for runtime patching/unpatching of instrumentation points.
38 //
39 // FIXME: Support DSO instrumentation maps too. The current solution only works
40 // for statically linked executables.
41 __sanitizer::atomic_uint8_t XRayInitialized{0};
42
43 // This should always be updated before XRayInitialized is updated.
44 __sanitizer::SpinMutex XRayInstrMapMutex;
45 XRaySledMap XRayInstrMap;
46
47 // Global flag to determine whether the flags have been initialized.
48 __sanitizer::atomic_uint8_t XRayFlagsInitialized{0};
49
50 // A mutex to allow only one thread to initialize the XRay data structures.
51 __sanitizer::SpinMutex XRayInitMutex;
52
53 // __xray_init() will do the actual loading of the current process' memory map
54 // and then proceed to look for the .xray_instr_map section/segment.
55 void __xray_init() XRAY_NEVER_INSTRUMENT {
56   __sanitizer::SpinMutexLock Guard(&XRayInitMutex);
57   // Short-circuit if we've already initialized XRay before.
58   if (__sanitizer::atomic_load(&XRayInitialized,
59                                __sanitizer::memory_order_acquire))
60     return;
61
62   if (!__sanitizer::atomic_load(&XRayFlagsInitialized,
63                                 __sanitizer::memory_order_acquire)) {
64     initializeFlags();
65     __sanitizer::atomic_store(&XRayFlagsInitialized, true,
66                               __sanitizer::memory_order_release);
67   }
68
69   if (__start_xray_instr_map == nullptr) {
70     if (Verbosity())
71       Report("XRay instrumentation map missing. Not initializing XRay.\n");
72     return;
73   }
74
75   {
76     __sanitizer::SpinMutexLock Guard(&XRayInstrMapMutex);
77     XRayInstrMap.Sleds = __start_xray_instr_map;
78     XRayInstrMap.Entries = __stop_xray_instr_map - __start_xray_instr_map;
79     XRayInstrMap.SledsIndex = __start_xray_fn_idx;
80     XRayInstrMap.Functions = __stop_xray_fn_idx - __start_xray_fn_idx;
81   }
82   __sanitizer::atomic_store(&XRayInitialized, true,
83                             __sanitizer::memory_order_release);
84
85 #ifndef XRAY_NO_PREINIT
86   if (flags()->patch_premain)
87     __xray_patch();
88 #endif
89 }
90
91 #if !defined(XRAY_NO_PREINIT) && SANITIZER_CAN_USE_PREINIT_ARRAY
92 // Only add the preinit array initialization if the sanitizers can.
93 __attribute__((section(".preinit_array"),
94                used)) void (*__local_xray_preinit)(void) = __xray_init;
95 #else
96 // If we cannot use the .preinit_array section, we should instead use dynamic
97 // initialisation.
98 static bool UNUSED __local_xray_dyninit = [] {
99   __xray_init();
100   return true;
101 }();
102 #endif