1353944Sdim//===-- xray_init.cpp -------------------------------------------*- C++ -*-===//
2353944Sdim//
3353944Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353944Sdim// See https://llvm.org/LICENSE.txt for license information.
5353944Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6353944Sdim//
7353944Sdim//===----------------------------------------------------------------------===//
8353944Sdim//
9353944Sdim// This file is a part of XRay, a dynamic runtime instrumentation system.
10353944Sdim//
11353944Sdim// XRay initialisation logic.
12353944Sdim//===----------------------------------------------------------------------===//
13353944Sdim
14353944Sdim#include <fcntl.h>
15353944Sdim#include <strings.h>
16353944Sdim#include <unistd.h>
17353944Sdim
18353944Sdim#include "sanitizer_common/sanitizer_common.h"
19353944Sdim#include "xray_defs.h"
20353944Sdim#include "xray_flags.h"
21353944Sdim#include "xray_interface_internal.h"
22353944Sdim
23353944Sdimextern "C" {
24353944Sdimvoid __xray_init();
25353944Sdimextern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak));
26353944Sdimextern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak));
27353944Sdimextern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak));
28353944Sdimextern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak));
29353944Sdim
30353944Sdim#if SANITIZER_MAC
31353944Sdim// HACK: This is a temporary workaround to make XRay build on
32353944Sdim// Darwin, but it will probably not work at runtime.
33353944Sdimconst XRaySledEntry __start_xray_instr_map[] = {};
34353944Sdimextern const XRaySledEntry __stop_xray_instr_map[] = {};
35353944Sdimextern const XRayFunctionSledIndex __start_xray_fn_idx[] = {};
36353944Sdimextern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {};
37353944Sdim#endif
38353944Sdim}
39353944Sdim
40353944Sdimusing namespace __xray;
41353944Sdim
42353944Sdim// When set to 'true' this means the XRay runtime has been initialised. We use
43353944Sdim// the weak symbols defined above (__start_xray_inst_map and
44353944Sdim// __stop_xray_instr_map) to initialise the instrumentation map that XRay uses
45353944Sdim// for runtime patching/unpatching of instrumentation points.
46353944Sdim//
47353944Sdim// FIXME: Support DSO instrumentation maps too. The current solution only works
48353944Sdim// for statically linked executables.
49353944Sdimatomic_uint8_t XRayInitialized{0};
50353944Sdim
51353944Sdim// This should always be updated before XRayInitialized is updated.
52353944SdimSpinMutex XRayInstrMapMutex;
53353944SdimXRaySledMap XRayInstrMap;
54353944Sdim
55353944Sdim// Global flag to determine whether the flags have been initialized.
56353944Sdimatomic_uint8_t XRayFlagsInitialized{0};
57353944Sdim
58353944Sdim// A mutex to allow only one thread to initialize the XRay data structures.
59353944SdimSpinMutex XRayInitMutex;
60353944Sdim
61353944Sdim// __xray_init() will do the actual loading of the current process' memory map
62353944Sdim// and then proceed to look for the .xray_instr_map section/segment.
63353944Sdimvoid __xray_init() XRAY_NEVER_INSTRUMENT {
64353944Sdim  SpinMutexLock Guard(&XRayInitMutex);
65353944Sdim  // Short-circuit if we've already initialized XRay before.
66353944Sdim  if (atomic_load(&XRayInitialized, memory_order_acquire))
67353944Sdim    return;
68353944Sdim
69353944Sdim  // XRAY is not compatible with PaX MPROTECT
70353944Sdim  CheckMPROTECT();
71353944Sdim
72353944Sdim  if (!atomic_load(&XRayFlagsInitialized, memory_order_acquire)) {
73353944Sdim    initializeFlags();
74353944Sdim    atomic_store(&XRayFlagsInitialized, true, memory_order_release);
75353944Sdim  }
76353944Sdim
77353944Sdim  if (__start_xray_instr_map == nullptr) {
78353944Sdim    if (Verbosity())
79353944Sdim      Report("XRay instrumentation map missing. Not initializing XRay.\n");
80353944Sdim    return;
81353944Sdim  }
82353944Sdim
83353944Sdim  {
84353944Sdim    SpinMutexLock Guard(&XRayInstrMapMutex);
85353944Sdim    XRayInstrMap.Sleds = __start_xray_instr_map;
86353944Sdim    XRayInstrMap.Entries = __stop_xray_instr_map - __start_xray_instr_map;
87353944Sdim    XRayInstrMap.SledsIndex = __start_xray_fn_idx;
88353944Sdim    XRayInstrMap.Functions = __stop_xray_fn_idx - __start_xray_fn_idx;
89353944Sdim  }
90353944Sdim  atomic_store(&XRayInitialized, true, memory_order_release);
91353944Sdim
92353944Sdim#ifndef XRAY_NO_PREINIT
93353944Sdim  if (flags()->patch_premain)
94353944Sdim    __xray_patch();
95353944Sdim#endif
96353944Sdim}
97353944Sdim
98353944Sdim// FIXME: Make check-xray tests work on FreeBSD without
99353944Sdim// SANITIZER_CAN_USE_PREINIT_ARRAY.
100353944Sdim// See sanitizer_internal_defs.h where the macro is defined.
101353944Sdim// Calling unresolved PLT functions in .preinit_array can lead to deadlock on
102353944Sdim// FreeBSD but here it seems benign.
103353944Sdim#if !defined(XRAY_NO_PREINIT) &&                                               \
104353944Sdim    (SANITIZER_CAN_USE_PREINIT_ARRAY || SANITIZER_FREEBSD)
105353944Sdim// Only add the preinit array initialization if the sanitizers can.
106353944Sdim__attribute__((section(".preinit_array"),
107353944Sdim               used)) void (*__local_xray_preinit)(void) = __xray_init;
108353944Sdim#else
109353944Sdim// If we cannot use the .preinit_array section, we should instead use dynamic
110353944Sdim// initialisation.
111353944Sdim__attribute__ ((constructor (0)))
112353944Sdimstatic void __local_xray_dyninit() {
113353944Sdim  __xray_init();
114353944Sdim}
115353944Sdim#endif
116