1 /*===- InstrProfilingPlatformFuchsia.c - Profile data Fuchsia platform ----===*\ 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 implements the profiling runtime for Fuchsia and defines the 10 * shared profile runtime interface. Each module (executable or DSO) statically 11 * links in the whole profile runtime to satisfy the calls from its 12 * instrumented code. Several modules in the same program might be separately 13 * compiled and even use different versions of the instrumentation ABI and data 14 * format. All they share in common is the VMO and the offset, which live in 15 * exported globals so that exactly one definition will be shared across all 16 * modules. Each module has its own independent runtime that registers its own 17 * atexit hook to append its own data into the shared VMO which is published 18 * via the data sink hook provided by Fuchsia's dynamic linker. 19 */ 20 21 #if defined(__Fuchsia__) 22 23 #include <inttypes.h> 24 #include <stdarg.h> 25 #include <stdbool.h> 26 #include <stdlib.h> 27 28 #include <zircon/process.h> 29 #include <zircon/sanitizer.h> 30 #include <zircon/status.h> 31 #include <zircon/syscalls.h> 32 33 #include "InstrProfiling.h" 34 #include "InstrProfilingInternal.h" 35 #include "InstrProfilingUtil.h" 36 37 COMPILER_RT_VISIBILITY unsigned lprofProfileDumped() { 38 return 1; 39 } 40 COMPILER_RT_VISIBILITY void lprofSetProfileDumped(unsigned Value) {} 41 42 COMPILER_RT_VISIBILITY unsigned lprofRuntimeCounterRelocation(void) { 43 return 1; 44 } 45 COMPILER_RT_VISIBILITY void lprofSetRuntimeCounterRelocation(unsigned Value) {} 46 47 /* VMO that contains the profile data for this module. */ 48 static zx_handle_t __llvm_profile_vmo; 49 /* Current offset within the VMO where data should be written next. */ 50 static uint64_t __llvm_profile_offset; 51 52 static const char ProfileSinkName[] = "llvm-profile"; 53 54 static inline void lprofWrite(const char *fmt, ...) { 55 char s[256]; 56 57 va_list ap; 58 va_start(ap, fmt); 59 int ret = vsnprintf(s, sizeof(s), fmt, ap); 60 va_end(ap); 61 62 __sanitizer_log_write(s, ret + 1); 63 } 64 65 static uint32_t lprofVMOWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, 66 uint32_t NumIOVecs) { 67 /* Compute the total length of data to be written. */ 68 size_t Length = 0; 69 for (uint32_t I = 0; I < NumIOVecs; I++) 70 Length += IOVecs[I].ElmSize * IOVecs[I].NumElm; 71 72 /* Resize the VMO to ensure there's sufficient space for the data. */ 73 zx_status_t Status = 74 _zx_vmo_set_size(__llvm_profile_vmo, __llvm_profile_offset + Length); 75 if (Status != ZX_OK) 76 return -1; 77 78 /* Copy the data into VMO. */ 79 for (uint32_t I = 0; I < NumIOVecs; I++) { 80 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; 81 if (IOVecs[I].Data) { 82 Status = _zx_vmo_write(__llvm_profile_vmo, IOVecs[I].Data, 83 __llvm_profile_offset, Length); 84 if (Status != ZX_OK) 85 return -1; 86 } else if (IOVecs[I].UseZeroPadding) { 87 /* Resizing the VMO should zero fill. */ 88 } 89 __llvm_profile_offset += Length; 90 } 91 92 /* Record the profile size as a property of the VMO. */ 93 _zx_object_set_property(__llvm_profile_vmo, ZX_PROP_VMO_CONTENT_SIZE, 94 &__llvm_profile_offset, 95 sizeof(__llvm_profile_offset)); 96 97 return 0; 98 } 99 100 static void initVMOWriter(ProfDataWriter *This) { 101 This->Write = lprofVMOWriter; 102 This->WriterCtx = NULL; 103 } 104 105 /* This method is invoked by the runtime initialization hook 106 * InstrProfilingRuntime.o if it is linked in. */ 107 COMPILER_RT_VISIBILITY 108 void __llvm_profile_initialize(void) { 109 /* This symbol is defined as weak and initialized to -1 by the runtimer, but 110 * compiler will generate a strong definition initialized to 0 when runtime 111 * counter relocation is used. */ 112 if (__llvm_profile_counter_bias == -1) { 113 lprofWrite("LLVM Profile: counter relocation at runtime is required\n"); 114 return; 115 } 116 117 /* Don't create VMO if it has been alread created. */ 118 if (__llvm_profile_vmo != ZX_HANDLE_INVALID) { 119 lprofWrite("LLVM Profile: VMO has already been created\n"); 120 return; 121 } 122 123 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); 124 const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); 125 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); 126 const uint64_t CountersOffset = sizeof(__llvm_profile_header) + 127 (DataSize * sizeof(__llvm_profile_data)); 128 129 zx_status_t Status; 130 131 /* Create VMO to hold the profile data. */ 132 Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &__llvm_profile_vmo); 133 if (Status != ZX_OK) { 134 lprofWrite("LLVM Profile: cannot create VMO: %s\n", 135 _zx_status_get_string(Status)); 136 return; 137 } 138 139 /* Give the VMO a name that includes the module signature. */ 140 char VmoName[ZX_MAX_NAME_LEN]; 141 snprintf(VmoName, sizeof(VmoName), "%" PRIu64 ".profraw", 142 lprofGetLoadModuleSignature()); 143 _zx_object_set_property(__llvm_profile_vmo, ZX_PROP_NAME, VmoName, 144 strlen(VmoName)); 145 146 /* Duplicate the handle since __sanitizer_publish_data consumes it. */ 147 zx_handle_t Handle; 148 Status = 149 _zx_handle_duplicate(__llvm_profile_vmo, ZX_RIGHT_SAME_RIGHTS, &Handle); 150 if (Status != ZX_OK) { 151 lprofWrite("LLVM Profile: cannot duplicate VMO handle: %s\n", 152 _zx_status_get_string(Status)); 153 _zx_handle_close(__llvm_profile_vmo); 154 __llvm_profile_vmo = ZX_HANDLE_INVALID; 155 return; 156 } 157 158 /* Publish the VMO which contains profile data to the system. */ 159 __sanitizer_publish_data(ProfileSinkName, Handle); 160 161 /* Use the dumpfile symbolizer markup element to write the name of VMO. */ 162 lprofWrite("LLVM Profile: {{{dumpfile:%s:%s}}}\n", ProfileSinkName, VmoName); 163 164 /* Check if there is llvm/runtime version mismatch. */ 165 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { 166 lprofWrite("LLVM Profile: runtime and instrumentation version mismatch: " 167 "expected %d, but got %d\n", 168 INSTR_PROF_RAW_VERSION, 169 (int)GET_VERSION(__llvm_profile_get_version())); 170 return; 171 } 172 173 /* Write the profile data into the mapped region. */ 174 ProfDataWriter VMOWriter; 175 initVMOWriter(&VMOWriter); 176 if (lprofWriteData(&VMOWriter, 0, 0) != 0) { 177 lprofWrite("LLVM Profile: failed to write data\n"); 178 return; 179 } 180 181 uint64_t Len = 0; 182 Status = _zx_vmo_get_size(__llvm_profile_vmo, &Len); 183 if (Status != ZX_OK) { 184 lprofWrite("LLVM Profile: failed to get the VMO size: %s\n", 185 _zx_status_get_string(Status)); 186 return; 187 } 188 189 uintptr_t Mapping; 190 Status = 191 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 192 0, __llvm_profile_vmo, 0, Len, &Mapping); 193 if (Status != ZX_OK) { 194 lprofWrite("LLVM Profile: failed to map the VMO: %s\n", 195 _zx_status_get_string(Status)); 196 return; 197 } 198 199 /* Update the profile fields based on the current mapping. */ 200 __llvm_profile_counter_bias = (intptr_t)Mapping - 201 (uintptr_t)__llvm_profile_begin_counters() + CountersOffset; 202 } 203 204 #endif 205