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 #if SANITIZER_MAC 32 // HACK: This is a temporary workaround to make XRay build on 33 // Darwin, but it will probably not work at runtime. 34 const XRaySledEntry __start_xray_instr_map[] = {}; 35 extern const XRaySledEntry __stop_xray_instr_map[] = {}; 36 extern const XRayFunctionSledIndex __start_xray_fn_idx[] = {}; 37 extern const XRayFunctionSledIndex __stop_xray_fn_idx[] = {}; 38 #endif 39 } 40 41 using namespace __xray; 42 43 // When set to 'true' this means the XRay runtime has been initialised. We use 44 // the weak symbols defined above (__start_xray_inst_map and 45 // __stop_xray_instr_map) to initialise the instrumentation map that XRay uses 46 // for runtime patching/unpatching of instrumentation points. 47 // 48 // FIXME: Support DSO instrumentation maps too. The current solution only works 49 // for statically linked executables. 50 atomic_uint8_t XRayInitialized{0}; 51 52 // This should always be updated before XRayInitialized is updated. 53 SpinMutex XRayInstrMapMutex; 54 XRaySledMap XRayInstrMap; 55 56 // Global flag to determine whether the flags have been initialized. 57 atomic_uint8_t XRayFlagsInitialized{0}; 58 59 // A mutex to allow only one thread to initialize the XRay data structures. 60 SpinMutex XRayInitMutex; 61 62 // __xray_init() will do the actual loading of the current process' memory map 63 // and then proceed to look for the .xray_instr_map section/segment. 64 void __xray_init() XRAY_NEVER_INSTRUMENT { 65 SpinMutexLock Guard(&XRayInitMutex); 66 // Short-circuit if we've already initialized XRay before. 67 if (atomic_load(&XRayInitialized, memory_order_acquire)) 68 return; 69 70 // XRAY is not compatible with PaX MPROTECT 71 CheckMPROTECT(); 72 73 if (!atomic_load(&XRayFlagsInitialized, memory_order_acquire)) { 74 initializeFlags(); 75 atomic_store(&XRayFlagsInitialized, true, memory_order_release); 76 } 77 78 if (__start_xray_instr_map == nullptr) { 79 if (Verbosity()) 80 Report("XRay instrumentation map missing. Not initializing XRay.\n"); 81 return; 82 } 83 84 { 85 SpinMutexLock Guard(&XRayInstrMapMutex); 86 XRayInstrMap.Sleds = __start_xray_instr_map; 87 XRayInstrMap.Entries = __stop_xray_instr_map - __start_xray_instr_map; 88 XRayInstrMap.SledsIndex = __start_xray_fn_idx; 89 XRayInstrMap.Functions = __stop_xray_fn_idx - __start_xray_fn_idx; 90 } 91 atomic_store(&XRayInitialized, true, memory_order_release); 92 93 #ifndef XRAY_NO_PREINIT 94 if (flags()->patch_premain) 95 __xray_patch(); 96 #endif 97 } 98 99 // FIXME: Make check-xray tests work on FreeBSD without 100 // SANITIZER_CAN_USE_PREINIT_ARRAY. 101 // See sanitizer_internal_defs.h where the macro is defined. 102 // Calling unresolved PLT functions in .preinit_array can lead to deadlock on 103 // FreeBSD but here it seems benign. 104 #if !defined(XRAY_NO_PREINIT) && \ 105 (SANITIZER_CAN_USE_PREINIT_ARRAY || SANITIZER_FREEBSD) 106 // Only add the preinit array initialization if the sanitizers can. 107 __attribute__((section(".preinit_array"), 108 used)) void (*__local_xray_preinit)(void) = __xray_init; 109 #else 110 // If we cannot use the .preinit_array section, we should instead use dynamic 111 // initialisation. 112 __attribute__ ((constructor (0))) 113 static void __local_xray_dyninit() { 114 __xray_init(); 115 } 116 #endif 117