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 DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd); 130 const uint64_t CountersOffset = 131 sizeof(__llvm_profile_header) + (DataSize * sizeof(__llvm_profile_data)); 132 133 zx_status_t Status; 134 135 /* Create VMO to hold the profile data. */ 136 zx_handle_t Vmo = ZX_HANDLE_INVALID; 137 Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &Vmo); 138 if (Status != ZX_OK) { 139 lprofWrite("LLVM Profile: cannot create VMO: %s\n", 140 _zx_status_get_string(Status)); 141 return; 142 } 143 144 /* Give the VMO a name that includes the module signature. */ 145 char VmoName[ZX_MAX_NAME_LEN]; 146 snprintf(VmoName, sizeof(VmoName), "%" PRIu64 ".profraw", 147 lprofGetLoadModuleSignature()); 148 _zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName)); 149 150 /* Write the profile data into the mapped region. */ 151 ProfDataWriter VMOWriter; 152 struct lprofVMOWriterCtx Ctx = {.Vmo = Vmo, .Offset = 0}; 153 initVMOWriter(&VMOWriter, &Ctx); 154 if (lprofWriteData(&VMOWriter, 0, 0) != 0) { 155 lprofWrite("LLVM Profile: failed to write data\n"); 156 _zx_handle_close(Vmo); 157 return; 158 } 159 160 uint64_t Len = 0; 161 Status = _zx_vmo_get_size(Vmo, &Len); 162 if (Status != ZX_OK) { 163 lprofWrite("LLVM Profile: failed to get the VMO size: %s\n", 164 _zx_status_get_string(Status)); 165 _zx_handle_close(Vmo); 166 return; 167 } 168 169 uintptr_t Mapping; 170 Status = 171 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0, 172 Vmo, 0, Len, &Mapping); 173 if (Status != ZX_OK) { 174 lprofWrite("LLVM Profile: failed to map the VMO: %s\n", 175 _zx_status_get_string(Status)); 176 _zx_handle_close(Vmo); 177 return; 178 } 179 180 /* Publish the VMO which contains profile data to the system. Note that this 181 * also consumes the VMO handle. */ 182 __sanitizer_publish_data(ProfileSinkName, Vmo); 183 184 /* Use the dumpfile symbolizer markup element to write the name of VMO. */ 185 lprofWrite("LLVM Profile: {{{dumpfile:%s:%s}}}\n", ProfileSinkName, VmoName); 186 187 /* Update the profile fields based on the current mapping. */ 188 __llvm_profile_counter_bias = (intptr_t)Mapping - 189 (uintptr_t)__llvm_profile_begin_counters() + 190 CountersOffset; 191 } 192 193 #endif 194