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