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