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 /* VMO that contains the coverage data shared across all modules. This symbol 38 * has default visibility and is exported in each module (executable or DSO) 39 * that statically links in the profiling runtime. 40 */ 41 zx_handle_t __llvm_profile_vmo; 42 /* Current offset within the VMO where data should be written next. This symbol 43 * has default visibility and is exported in each module (executable or DSO) 44 * that statically links in the profiling runtime. 45 */ 46 uint64_t __llvm_profile_offset; 47 48 static const char ProfileSinkName[] = "llvm-profile"; 49 50 static inline void lprofWrite(const char *fmt, ...) { 51 char s[256]; 52 53 va_list ap; 54 va_start(ap, fmt); 55 int ret = vsnprintf(s, sizeof(s), fmt, ap); 56 va_end(ap); 57 58 __sanitizer_log_write(s, ret + 1); 59 } 60 61 static void createVMO() { 62 /* Don't create VMO if it has been alread created. */ 63 if (__llvm_profile_vmo != ZX_HANDLE_INVALID) 64 return; 65 66 /* Get information about the current process. */ 67 zx_info_handle_basic_t Info; 68 zx_status_t Status = 69 _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info, 70 sizeof(Info), NULL, NULL); 71 if (Status != ZX_OK) { 72 lprofWrite("LLVM Profile: cannot get info about current process: %s\n", 73 _zx_status_get_string(Status)); 74 return; 75 } 76 77 /* Create VMO to hold the profile data. */ 78 Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &__llvm_profile_vmo); 79 if (Status != ZX_OK) { 80 lprofWrite("LLVM Profile: cannot create VMO: %s\n", 81 _zx_status_get_string(Status)); 82 return; 83 } 84 85 /* Give the VMO a name including our process KOID so it's easy to spot. */ 86 char VmoName[ZX_MAX_NAME_LEN]; 87 snprintf(VmoName, sizeof(VmoName), "%s.%" PRIu64, ProfileSinkName, Info.koid); 88 _zx_object_set_property(__llvm_profile_vmo, ZX_PROP_NAME, VmoName, 89 strlen(VmoName)); 90 91 /* Duplicate the handle since __sanitizer_publish_data consumes it. */ 92 zx_handle_t Handle; 93 Status = 94 _zx_handle_duplicate(__llvm_profile_vmo, ZX_RIGHT_SAME_RIGHTS, &Handle); 95 if (Status != ZX_OK) { 96 lprofWrite("LLVM Profile: cannot duplicate VMO handle: %s\n", 97 _zx_status_get_string(Status)); 98 _zx_handle_close(__llvm_profile_vmo); 99 __llvm_profile_vmo = ZX_HANDLE_INVALID; 100 return; 101 } 102 103 /* Publish the VMO which contains profile data to the system. */ 104 __sanitizer_publish_data(ProfileSinkName, Handle); 105 106 /* Use the dumpfile symbolizer markup element to write the name of VMO. */ 107 lprofWrite("LLVM Profile: {{{dumpfile:%s:%s}}}\n", ProfileSinkName, VmoName); 108 } 109 110 static uint32_t lprofVMOWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs, 111 uint32_t NumIOVecs) { 112 /* Compute the total length of data to be written. */ 113 size_t Length = 0; 114 for (uint32_t I = 0; I < NumIOVecs; I++) 115 Length += IOVecs[I].ElmSize * IOVecs[I].NumElm; 116 117 /* Resize the VMO to ensure there's sufficient space for the data. */ 118 zx_status_t Status = 119 _zx_vmo_set_size(__llvm_profile_vmo, __llvm_profile_offset + Length); 120 if (Status != ZX_OK) 121 return -1; 122 123 /* Copy the data into VMO. */ 124 for (uint32_t I = 0; I < NumIOVecs; I++) { 125 size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm; 126 if (IOVecs[I].Data) { 127 Status = _zx_vmo_write(__llvm_profile_vmo, IOVecs[I].Data, 128 __llvm_profile_offset, Length); 129 if (Status != ZX_OK) 130 return -1; 131 } else if (IOVecs[I].UseZeroPadding) { 132 /* Resizing the VMO should zero fill. */ 133 } 134 __llvm_profile_offset += Length; 135 } 136 137 return 0; 138 } 139 140 static void initVMOWriter(ProfDataWriter *This) { 141 This->Write = lprofVMOWriter; 142 This->WriterCtx = NULL; 143 } 144 145 static int dump(void) { 146 if (lprofProfileDumped()) { 147 lprofWrite("LLVM Profile: data not published: already written.\n"); 148 return 0; 149 } 150 151 /* Check if there is llvm/runtime version mismatch. */ 152 if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) { 153 lprofWrite("LLVM Profile: runtime and instrumentation version mismatch: " 154 "expected %d, but got %d\n", 155 INSTR_PROF_RAW_VERSION, 156 (int)GET_VERSION(__llvm_profile_get_version())); 157 return -1; 158 } 159 160 /* Write the profile data into the mapped region. */ 161 ProfDataWriter VMOWriter; 162 initVMOWriter(&VMOWriter); 163 if (lprofWriteData(&VMOWriter, lprofGetVPDataReader(), 0) != 0) 164 return -1; 165 166 return 0; 167 } 168 169 COMPILER_RT_VISIBILITY 170 int __llvm_profile_dump(void) { 171 int rc = dump(); 172 lprofSetProfileDumped(); 173 return rc; 174 } 175 176 static void dumpWithoutReturn(void) { dump(); } 177 178 /* This method is invoked by the runtime initialization hook 179 * InstrProfilingRuntime.o if it is linked in. 180 */ 181 COMPILER_RT_VISIBILITY 182 void __llvm_profile_initialize_file(void) { createVMO(); } 183 184 COMPILER_RT_VISIBILITY 185 int __llvm_profile_register_write_file_atexit(void) { 186 static bool HasBeenRegistered = false; 187 188 if (HasBeenRegistered) 189 return 0; 190 191 lprofSetupValueProfiler(); 192 193 HasBeenRegistered = true; 194 return atexit(dumpWithoutReturn); 195 } 196 197 #endif 198