168d75effSDimitry Andric //===-- xray_init.cpp -------------------------------------------*- C++ -*-===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // This file is a part of XRay, a dynamic runtime instrumentation system.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric // XRay initialisation logic.
1268d75effSDimitry Andric //===----------------------------------------------------------------------===//
1368d75effSDimitry Andric
1468d75effSDimitry Andric #include <fcntl.h>
1568d75effSDimitry Andric #include <strings.h>
1668d75effSDimitry Andric #include <unistd.h>
1768d75effSDimitry Andric
1868d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
1968d75effSDimitry Andric #include "xray_defs.h"
2068d75effSDimitry Andric #include "xray_flags.h"
2168d75effSDimitry Andric #include "xray_interface_internal.h"
2268d75effSDimitry Andric
2368d75effSDimitry Andric extern "C" {
2468d75effSDimitry Andric void __xray_init();
2568d75effSDimitry Andric extern const XRaySledEntry __start_xray_instr_map[] __attribute__((weak));
2668d75effSDimitry Andric extern const XRaySledEntry __stop_xray_instr_map[] __attribute__((weak));
2768d75effSDimitry Andric extern const XRayFunctionSledIndex __start_xray_fn_idx[] __attribute__((weak));
2868d75effSDimitry Andric extern const XRayFunctionSledIndex __stop_xray_fn_idx[] __attribute__((weak));
2968d75effSDimitry Andric
30*81ad6265SDimitry Andric #if SANITIZER_APPLE
3168d75effSDimitry Andric // HACK: This is a temporary workaround to make XRay build on
3268d75effSDimitry Andric // Darwin, but it will probably not work at runtime.
3368d75effSDimitry Andric const XRaySledEntry __start_xray_instr_map[] = {};
3468d75effSDimitry Andric extern const XRaySledEntry __stop_xray_instr_map[] = {};
3568d75effSDimitry Andric extern const XRayFunctionSledIndex __start_xray_fn_idx[] = {};
3668d75effSDimitry Andric extern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {};
3768d75effSDimitry Andric #endif
3868d75effSDimitry Andric }
3968d75effSDimitry Andric
4068d75effSDimitry Andric using namespace __xray;
4168d75effSDimitry Andric
4268d75effSDimitry Andric // When set to 'true' this means the XRay runtime has been initialised. We use
4368d75effSDimitry Andric // the weak symbols defined above (__start_xray_inst_map and
4468d75effSDimitry Andric // __stop_xray_instr_map) to initialise the instrumentation map that XRay uses
4568d75effSDimitry Andric // for runtime patching/unpatching of instrumentation points.
4668d75effSDimitry Andric //
4768d75effSDimitry Andric // FIXME: Support DSO instrumentation maps too. The current solution only works
4868d75effSDimitry Andric // for statically linked executables.
4968d75effSDimitry Andric atomic_uint8_t XRayInitialized{0};
5068d75effSDimitry Andric
5168d75effSDimitry Andric // This should always be updated before XRayInitialized is updated.
5268d75effSDimitry Andric SpinMutex XRayInstrMapMutex;
5368d75effSDimitry Andric XRaySledMap XRayInstrMap;
5468d75effSDimitry Andric
5568d75effSDimitry Andric // Global flag to determine whether the flags have been initialized.
5668d75effSDimitry Andric atomic_uint8_t XRayFlagsInitialized{0};
5768d75effSDimitry Andric
5868d75effSDimitry Andric // A mutex to allow only one thread to initialize the XRay data structures.
5968d75effSDimitry Andric SpinMutex XRayInitMutex;
6068d75effSDimitry Andric
6168d75effSDimitry Andric // __xray_init() will do the actual loading of the current process' memory map
6268d75effSDimitry Andric // and then proceed to look for the .xray_instr_map section/segment.
__xray_init()6368d75effSDimitry Andric void __xray_init() XRAY_NEVER_INSTRUMENT {
6468d75effSDimitry Andric SpinMutexLock Guard(&XRayInitMutex);
6568d75effSDimitry Andric // Short-circuit if we've already initialized XRay before.
6668d75effSDimitry Andric if (atomic_load(&XRayInitialized, memory_order_acquire))
6768d75effSDimitry Andric return;
6868d75effSDimitry Andric
6968d75effSDimitry Andric // XRAY is not compatible with PaX MPROTECT
7068d75effSDimitry Andric CheckMPROTECT();
7168d75effSDimitry Andric
7268d75effSDimitry Andric if (!atomic_load(&XRayFlagsInitialized, memory_order_acquire)) {
7368d75effSDimitry Andric initializeFlags();
7468d75effSDimitry Andric atomic_store(&XRayFlagsInitialized, true, memory_order_release);
7568d75effSDimitry Andric }
7668d75effSDimitry Andric
7768d75effSDimitry Andric if (__start_xray_instr_map == nullptr) {
7868d75effSDimitry Andric if (Verbosity())
7968d75effSDimitry Andric Report("XRay instrumentation map missing. Not initializing XRay.\n");
8068d75effSDimitry Andric return;
8168d75effSDimitry Andric }
8268d75effSDimitry Andric
8368d75effSDimitry Andric {
8468d75effSDimitry Andric SpinMutexLock Guard(&XRayInstrMapMutex);
8568d75effSDimitry Andric XRayInstrMap.Sleds = __start_xray_instr_map;
8668d75effSDimitry Andric XRayInstrMap.Entries = __stop_xray_instr_map - __start_xray_instr_map;
875ffd83dbSDimitry Andric if (__start_xray_fn_idx != nullptr) {
8868d75effSDimitry Andric XRayInstrMap.SledsIndex = __start_xray_fn_idx;
8968d75effSDimitry Andric XRayInstrMap.Functions = __stop_xray_fn_idx - __start_xray_fn_idx;
905ffd83dbSDimitry Andric } else {
915ffd83dbSDimitry Andric size_t CountFunctions = 0;
925ffd83dbSDimitry Andric uint64_t LastFnAddr = 0;
935ffd83dbSDimitry Andric
945ffd83dbSDimitry Andric for (std::size_t I = 0; I < XRayInstrMap.Entries; I++) {
955ffd83dbSDimitry Andric const auto &Sled = XRayInstrMap.Sleds[I];
965ffd83dbSDimitry Andric const auto Function = Sled.function();
975ffd83dbSDimitry Andric if (Function != LastFnAddr) {
985ffd83dbSDimitry Andric CountFunctions++;
995ffd83dbSDimitry Andric LastFnAddr = Function;
1005ffd83dbSDimitry Andric }
1015ffd83dbSDimitry Andric }
1025ffd83dbSDimitry Andric
1035ffd83dbSDimitry Andric XRayInstrMap.Functions = CountFunctions;
1045ffd83dbSDimitry Andric }
10568d75effSDimitry Andric }
10668d75effSDimitry Andric atomic_store(&XRayInitialized, true, memory_order_release);
10768d75effSDimitry Andric
10868d75effSDimitry Andric #ifndef XRAY_NO_PREINIT
10968d75effSDimitry Andric if (flags()->patch_premain)
11068d75effSDimitry Andric __xray_patch();
11168d75effSDimitry Andric #endif
11268d75effSDimitry Andric }
11368d75effSDimitry Andric
11468d75effSDimitry Andric // FIXME: Make check-xray tests work on FreeBSD without
11568d75effSDimitry Andric // SANITIZER_CAN_USE_PREINIT_ARRAY.
11668d75effSDimitry Andric // See sanitizer_internal_defs.h where the macro is defined.
11768d75effSDimitry Andric // Calling unresolved PLT functions in .preinit_array can lead to deadlock on
11868d75effSDimitry Andric // FreeBSD but here it seems benign.
11968d75effSDimitry Andric #if !defined(XRAY_NO_PREINIT) && \
12068d75effSDimitry Andric (SANITIZER_CAN_USE_PREINIT_ARRAY || SANITIZER_FREEBSD)
12168d75effSDimitry Andric // Only add the preinit array initialization if the sanitizers can.
12268d75effSDimitry Andric __attribute__((section(".preinit_array"),
12368d75effSDimitry Andric used)) void (*__local_xray_preinit)(void) = __xray_init;
12468d75effSDimitry Andric #else
12568d75effSDimitry Andric // If we cannot use the .preinit_array section, we should instead use dynamic
12668d75effSDimitry Andric // initialisation.
12768d75effSDimitry Andric __attribute__ ((constructor (0)))
__local_xray_dyninit()12868d75effSDimitry Andric static void __local_xray_dyninit() {
12968d75effSDimitry Andric __xray_init();
13068d75effSDimitry Andric }
13168d75effSDimitry Andric #endif
132