xref: /openbsd-src/gnu/llvm/compiler-rt/lib/profile/InstrProfilingPlatformFuchsia.c (revision 810390e339a5425391477d5d41c78d7cab2424ac)
13cab2bb3Spatrick /*===- InstrProfilingPlatformFuchsia.c - Profile data Fuchsia platform ----===*\
23cab2bb3Spatrick |*
33cab2bb3Spatrick |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
43cab2bb3Spatrick |* See https://llvm.org/LICENSE.txt for license information.
53cab2bb3Spatrick |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63cab2bb3Spatrick |*
73cab2bb3Spatrick \*===----------------------------------------------------------------------===*/
83cab2bb3Spatrick /*
93cab2bb3Spatrick  * This file implements the profiling runtime for Fuchsia and defines the
103cab2bb3Spatrick  * shared profile runtime interface. Each module (executable or DSO) statically
113cab2bb3Spatrick  * links in the whole profile runtime to satisfy the calls from its
123cab2bb3Spatrick  * instrumented code. Several modules in the same program might be separately
133cab2bb3Spatrick  * compiled and even use different versions of the instrumentation ABI and data
143cab2bb3Spatrick  * format. All they share in common is the VMO and the offset, which live in
153cab2bb3Spatrick  * exported globals so that exactly one definition will be shared across all
163cab2bb3Spatrick  * modules. Each module has its own independent runtime that registers its own
173cab2bb3Spatrick  * atexit hook to append its own data into the shared VMO which is published
183cab2bb3Spatrick  * via the data sink hook provided by Fuchsia's dynamic linker.
193cab2bb3Spatrick  */
203cab2bb3Spatrick 
213cab2bb3Spatrick #if defined(__Fuchsia__)
223cab2bb3Spatrick 
233cab2bb3Spatrick #include <inttypes.h>
243cab2bb3Spatrick #include <stdarg.h>
253cab2bb3Spatrick #include <stdbool.h>
263cab2bb3Spatrick #include <stdlib.h>
273cab2bb3Spatrick 
283cab2bb3Spatrick #include <zircon/process.h>
293cab2bb3Spatrick #include <zircon/sanitizer.h>
303cab2bb3Spatrick #include <zircon/status.h>
313cab2bb3Spatrick #include <zircon/syscalls.h>
323cab2bb3Spatrick 
333cab2bb3Spatrick #include "InstrProfiling.h"
343cab2bb3Spatrick #include "InstrProfilingInternal.h"
353cab2bb3Spatrick #include "InstrProfilingUtil.h"
363cab2bb3Spatrick 
37d89ec533Spatrick /* This variable is an external reference to symbol defined by the compiler. */
38d89ec533Spatrick COMPILER_RT_VISIBILITY extern intptr_t INSTR_PROF_PROFILE_COUNTER_BIAS_VAR;
39d89ec533Spatrick 
lprofProfileDumped(void)40*810390e3Srobert COMPILER_RT_VISIBILITY unsigned lprofProfileDumped(void) {
411f9cb04fSpatrick   return 1;
421f9cb04fSpatrick }
lprofSetProfileDumped(unsigned Value)431f9cb04fSpatrick COMPILER_RT_VISIBILITY void lprofSetProfileDumped(unsigned Value) {}
441f9cb04fSpatrick 
453cab2bb3Spatrick static const char ProfileSinkName[] = "llvm-profile";
463cab2bb3Spatrick 
lprofWrite(const char * fmt,...)473cab2bb3Spatrick static inline void lprofWrite(const char *fmt, ...) {
483cab2bb3Spatrick   char s[256];
493cab2bb3Spatrick 
503cab2bb3Spatrick   va_list ap;
513cab2bb3Spatrick   va_start(ap, fmt);
523cab2bb3Spatrick   int ret = vsnprintf(s, sizeof(s), fmt, ap);
533cab2bb3Spatrick   va_end(ap);
543cab2bb3Spatrick 
55*810390e3Srobert   __sanitizer_log_write(s, ret);
563cab2bb3Spatrick }
573cab2bb3Spatrick 
581f9cb04fSpatrick struct lprofVMOWriterCtx {
591f9cb04fSpatrick   /* VMO that contains the profile data for this module. */
601f9cb04fSpatrick   zx_handle_t Vmo;
611f9cb04fSpatrick   /* Current offset within the VMO where data should be written next. */
621f9cb04fSpatrick   uint64_t Offset;
631f9cb04fSpatrick };
643cab2bb3Spatrick 
lprofVMOWriter(ProfDataWriter * This,ProfDataIOVec * IOVecs,uint32_t NumIOVecs)653cab2bb3Spatrick static uint32_t lprofVMOWriter(ProfDataWriter *This, ProfDataIOVec *IOVecs,
663cab2bb3Spatrick                                uint32_t NumIOVecs) {
671f9cb04fSpatrick   struct lprofVMOWriterCtx *Ctx = (struct lprofVMOWriterCtx *)This->WriterCtx;
681f9cb04fSpatrick 
693cab2bb3Spatrick   /* Compute the total length of data to be written. */
703cab2bb3Spatrick   size_t Length = 0;
713cab2bb3Spatrick   for (uint32_t I = 0; I < NumIOVecs; I++)
723cab2bb3Spatrick     Length += IOVecs[I].ElmSize * IOVecs[I].NumElm;
733cab2bb3Spatrick 
743cab2bb3Spatrick   /* Resize the VMO to ensure there's sufficient space for the data. */
751f9cb04fSpatrick   zx_status_t Status = _zx_vmo_set_size(Ctx->Vmo, Ctx->Offset + Length);
763cab2bb3Spatrick   if (Status != ZX_OK)
773cab2bb3Spatrick     return -1;
783cab2bb3Spatrick 
793cab2bb3Spatrick   /* Copy the data into VMO. */
803cab2bb3Spatrick   for (uint32_t I = 0; I < NumIOVecs; I++) {
813cab2bb3Spatrick     size_t Length = IOVecs[I].ElmSize * IOVecs[I].NumElm;
823cab2bb3Spatrick     if (IOVecs[I].Data) {
831f9cb04fSpatrick       Status = _zx_vmo_write(Ctx->Vmo, IOVecs[I].Data, Ctx->Offset, Length);
843cab2bb3Spatrick       if (Status != ZX_OK)
853cab2bb3Spatrick         return -1;
863cab2bb3Spatrick     } else if (IOVecs[I].UseZeroPadding) {
873cab2bb3Spatrick       /* Resizing the VMO should zero fill. */
883cab2bb3Spatrick     }
891f9cb04fSpatrick     Ctx->Offset += Length;
903cab2bb3Spatrick   }
913cab2bb3Spatrick 
921f9cb04fSpatrick   /* Record the profile size as a property of the VMO. */
931f9cb04fSpatrick   _zx_object_set_property(Ctx->Vmo, ZX_PROP_VMO_CONTENT_SIZE, &Ctx->Offset,
941f9cb04fSpatrick                           sizeof(Ctx->Offset));
951f9cb04fSpatrick 
963cab2bb3Spatrick   return 0;
973cab2bb3Spatrick }
983cab2bb3Spatrick 
initVMOWriter(ProfDataWriter * This,struct lprofVMOWriterCtx * Ctx)991f9cb04fSpatrick static void initVMOWriter(ProfDataWriter *This, struct lprofVMOWriterCtx *Ctx) {
1003cab2bb3Spatrick   This->Write = lprofVMOWriter;
1011f9cb04fSpatrick   This->WriterCtx = Ctx;
1023cab2bb3Spatrick }
1033cab2bb3Spatrick 
1041f9cb04fSpatrick /* This method is invoked by the runtime initialization hook
1051f9cb04fSpatrick  * InstrProfilingRuntime.o if it is linked in. */
1061f9cb04fSpatrick COMPILER_RT_VISIBILITY
__llvm_profile_initialize(void)1071f9cb04fSpatrick void __llvm_profile_initialize(void) {
1083cab2bb3Spatrick   /* Check if there is llvm/runtime version mismatch. */
1093cab2bb3Spatrick   if (GET_VERSION(__llvm_profile_get_version()) != INSTR_PROF_RAW_VERSION) {
1103cab2bb3Spatrick     lprofWrite("LLVM Profile: runtime and instrumentation version mismatch: "
1113cab2bb3Spatrick                "expected %d, but got %d\n",
1123cab2bb3Spatrick                INSTR_PROF_RAW_VERSION,
1133cab2bb3Spatrick                (int)GET_VERSION(__llvm_profile_get_version()));
1141f9cb04fSpatrick     return;
1153cab2bb3Spatrick   }
1163cab2bb3Spatrick 
1171f9cb04fSpatrick   const __llvm_profile_data *DataBegin = __llvm_profile_begin_data();
1181f9cb04fSpatrick   const __llvm_profile_data *DataEnd = __llvm_profile_end_data();
119*810390e3Srobert   const char *CountersBegin = __llvm_profile_begin_counters();
120*810390e3Srobert   const char *CountersEnd = __llvm_profile_end_counters();
1211f9cb04fSpatrick   const uint64_t DataSize = __llvm_profile_get_data_size(DataBegin, DataEnd);
122*810390e3Srobert   const uint64_t CountersOffset =
123*810390e3Srobert       sizeof(__llvm_profile_header) + __llvm_write_binary_ids(NULL) + DataSize;
124*810390e3Srobert   uint64_t CountersSize =
125*810390e3Srobert       __llvm_profile_get_counters_size(CountersBegin, CountersEnd);
126d89ec533Spatrick 
127d89ec533Spatrick   /* Don't publish a VMO if there are no counters. */
128d89ec533Spatrick   if (!CountersSize)
129d89ec533Spatrick     return;
1301f9cb04fSpatrick 
1311f9cb04fSpatrick   zx_status_t Status;
1321f9cb04fSpatrick 
133d89ec533Spatrick   /* Create a VMO to hold the profile data. */
1341f9cb04fSpatrick   zx_handle_t Vmo = ZX_HANDLE_INVALID;
1351f9cb04fSpatrick   Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &Vmo);
1361f9cb04fSpatrick   if (Status != ZX_OK) {
1371f9cb04fSpatrick     lprofWrite("LLVM Profile: cannot create VMO: %s\n",
1381f9cb04fSpatrick                _zx_status_get_string(Status));
1391f9cb04fSpatrick     return;
1401f9cb04fSpatrick   }
1411f9cb04fSpatrick 
1421f9cb04fSpatrick   /* Give the VMO a name that includes the module signature. */
1431f9cb04fSpatrick   char VmoName[ZX_MAX_NAME_LEN];
1441f9cb04fSpatrick   snprintf(VmoName, sizeof(VmoName), "%" PRIu64 ".profraw",
1451f9cb04fSpatrick            lprofGetLoadModuleSignature());
1461f9cb04fSpatrick   _zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName));
1471f9cb04fSpatrick 
1483cab2bb3Spatrick   /* Write the profile data into the mapped region. */
1493cab2bb3Spatrick   ProfDataWriter VMOWriter;
1501f9cb04fSpatrick   struct lprofVMOWriterCtx Ctx = {.Vmo = Vmo, .Offset = 0};
1511f9cb04fSpatrick   initVMOWriter(&VMOWriter, &Ctx);
1521f9cb04fSpatrick   if (lprofWriteData(&VMOWriter, 0, 0) != 0) {
1531f9cb04fSpatrick     lprofWrite("LLVM Profile: failed to write data\n");
1541f9cb04fSpatrick     _zx_handle_close(Vmo);
1551f9cb04fSpatrick     return;
1563cab2bb3Spatrick   }
1573cab2bb3Spatrick 
1581f9cb04fSpatrick   uint64_t Len = 0;
1591f9cb04fSpatrick   Status = _zx_vmo_get_size(Vmo, &Len);
1601f9cb04fSpatrick   if (Status != ZX_OK) {
1611f9cb04fSpatrick     lprofWrite("LLVM Profile: failed to get the VMO size: %s\n",
1621f9cb04fSpatrick                _zx_status_get_string(Status));
1631f9cb04fSpatrick     _zx_handle_close(Vmo);
1641f9cb04fSpatrick     return;
1653cab2bb3Spatrick   }
1663cab2bb3Spatrick 
1671f9cb04fSpatrick   uintptr_t Mapping;
1681f9cb04fSpatrick   Status =
1691f9cb04fSpatrick       _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
1701f9cb04fSpatrick                    Vmo, 0, Len, &Mapping);
1711f9cb04fSpatrick   if (Status != ZX_OK) {
1721f9cb04fSpatrick     lprofWrite("LLVM Profile: failed to map the VMO: %s\n",
1731f9cb04fSpatrick                _zx_status_get_string(Status));
1741f9cb04fSpatrick     _zx_handle_close(Vmo);
1751f9cb04fSpatrick     return;
1761f9cb04fSpatrick   }
1773cab2bb3Spatrick 
1781f9cb04fSpatrick   /* Publish the VMO which contains profile data to the system. Note that this
1791f9cb04fSpatrick    * also consumes the VMO handle. */
1801f9cb04fSpatrick   __sanitizer_publish_data(ProfileSinkName, Vmo);
1813cab2bb3Spatrick 
1821f9cb04fSpatrick   /* Update the profile fields based on the current mapping. */
183d89ec533Spatrick   INSTR_PROF_PROFILE_COUNTER_BIAS_VAR =
184d89ec533Spatrick       (intptr_t)Mapping - (uintptr_t)CountersBegin + CountersOffset;
185d89ec533Spatrick 
186d89ec533Spatrick   /* Return the memory allocated for counters to OS. */
187d89ec533Spatrick   lprofReleaseMemoryPagesToOS((uintptr_t)CountersBegin, (uintptr_t)CountersEnd);
1883cab2bb3Spatrick }
1893cab2bb3Spatrick 
1903cab2bb3Spatrick #endif
191