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 static const char ProfileSinkName[] = "llvm-profile"; 48 49 static inline void lprofWrite(const char *fmt, ...) { 50 char s[256]; 51 52 va_list ap; 53 va_start(ap, fmt); 54 int ret = vsnprintf(s, sizeof(s), fmt, ap); 55 va_end(ap); 56 57 __sanitizer_log_write(s, ret + 1); 58 } 59 60 struct lprofVMOWriterCtx { 61 /* VMO that contains the profile data for this module. */ 62 zx_handle_t Vmo; 63 /* Current offset within the VMO where data should be written next. */ 64 uint64_t Offset; 65 }; 66 67 static uint32_t lprofVMOWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, 68 uint32_t NumIOVecs) { 69 struct lprofVMOWriterCtx *Ctx = (struct lprofVMOWriterCtx *)This->WriterCtx; 70 71 /* Compute the total length of data to be written. */ 72 size_t Length = 0; 73 for (uint32_t I = 0; I < NumIOVecs; I++) 74 Length += IOVecs[I].ElmSize * IOVecs[I].NumElm; 75 76 /* Resize the VMO to ensure there's sufficient space for the data. */ 77 zx_status_t Status = _zx_vmo_set_size(Ctx->Vmo, Ctx->Offset + Length); 78 if (Status != ZX_OK) 79 return -1; 80 81 /* Copy the data into VMO. */ 82 for (uint32_t I = 0; I < NumIOVecs; I++) { 83 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; 84 if (IOVecs[I].Data) { 85 Status = _zx_vmo_write(Ctx->Vmo, IOVecs[I].Data, Ctx->Offset, Length); 86 if (Status != ZX_OK) 87 return -1; 88 } else if (IOVecs[I].UseZeroPadding) { 89 /* Resizing the VMO should zero fill. */ 90 } 91 Ctx->Offset += Length; 92 } 93 94 /* Record the profile size as a property of the VMO. */ 95 _zx_object_set_property(Ctx->Vmo, ZX_PROP_VMO_CONTENT_SIZE, &Ctx->Offset, 96 sizeof(Ctx->Offset)); 97 98 return 0; 99 } 100 101 static void initVMOWriter(ProfDataWriter *This, struct lprofVMOWriterCtx *Ctx) { 102 This->Write = lprofVMOWriter; 103 This->WriterCtx = Ctx; 104 } 105 106 /* This method is invoked by the runtime initialization hook 107 * InstrProfilingRuntime.o if it is linked in. */ 108 COMPILER_RT_VISIBILITY 109 void __llvm_profile_initialize(void) { 110 /* Check if there is llvm/runtime version mismatch. */ 111 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { 112 lprofWrite("LLVM Profile: runtime and instrumentation version mismatch: " 113 "expected %d, but got %d\n", 114 INSTR_PROF_RAW_VERSION, 115 (int)GET_VERSION(__llvm_profile_get_version())); 116 return; 117 } 118 119 /* This symbol is defined as weak and initialized to -1 by the runtimer, but 120 * compiler will generate a strong definition initialized to 0 when runtime 121 * counter relocation is used. */ 122 if (__llvm_profile_counter_bias == -1) { 123 lprofWrite("LLVM Profile: counter relocation at runtime is required\n"); 124 return; 125 } 126 127 const __llvm_profile_data *DataBegin = __llvm_profile_begin_data(); 128 const __llvm_profile_data *DataEnd = __llvm_profile_end_data(); 129 const uint64_t *CountersBegin = __llvm_profile_begin_counters(); 130 const uint64_t *CountersEnd = __llvm_profile_end_counters(); 131 const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); 132 const uint64_t CountersOffset = 133 sizeof(__llvm_profile_header) + (DataSize * sizeof(__llvm_profile_data)); 134 uint64_t CountersSize = CountersEnd - CountersBegin; 135 136 /* Don't publish a VMO if there are no counters. */ 137 if (!CountersSize) 138 return; 139 140 zx_status_t Status; 141 142 /* Create a VMO to hold the profile data. */ 143 zx_handle_t Vmo = ZX_HANDLE_INVALID; 144 Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &Vmo); 145 if (Status != ZX_OK) { 146 lprofWrite("LLVM Profile: cannot create VMO: %s\n", 147 _zx_status_get_string(Status)); 148 return; 149 } 150 151 /* Give the VMO a name that includes the module signature. */ 152 char VmoName[ZX_MAX_NAME_LEN]; 153 snprintf(VmoName, sizeof(VmoName), "%" PRIu64 ".profraw", 154 lprofGetLoadModuleSignature()); 155 _zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName)); 156 157 /* Write the profile data into the mapped region. */ 158 ProfDataWriter VMOWriter; 159 struct lprofVMOWriterCtx Ctx = {.Vmo = Vmo, .Offset = 0}; 160 initVMOWriter(&VMOWriter, &Ctx); 161 if (lprofWriteData(&VMOWriter, 0, 0) != 0) { 162 lprofWrite("LLVM Profile: failed to write data\n"); 163 _zx_handle_close(Vmo); 164 return; 165 } 166 167 uint64_t Len = 0; 168 Status = _zx_vmo_get_size(Vmo, &Len); 169 if (Status != ZX_OK) { 170 lprofWrite("LLVM Profile: failed to get the VMO size: %s\n", 171 _zx_status_get_string(Status)); 172 _zx_handle_close(Vmo); 173 return; 174 } 175 176 uintptr_t Mapping; 177 Status = 178 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0, 179 Vmo, 0, Len, &Mapping); 180 if (Status != ZX_OK) { 181 lprofWrite("LLVM Profile: failed to map the VMO: %s\n", 182 _zx_status_get_string(Status)); 183 _zx_handle_close(Vmo); 184 return; 185 } 186 187 /* Publish the VMO which contains profile data to the system. Note that this 188 * also consumes the VMO handle. */ 189 __sanitizer_publish_data(ProfileSinkName, Vmo); 190 191 /* Use the dumpfile symbolizer markup element to write the name of VMO. */ 192 lprofWrite("LLVM Profile: {{{dumpfile:%s:%s}}}\n", ProfileSinkName, VmoName); 193 194 /* Update the profile fields based on the current mapping. */ 195 __llvm_profile_counter_bias = (intptr_t)Mapping - 196 (uintptr_t)__llvm_profile_begin_counters() + 197 CountersOffset; 198 } 199 200 #endif 201