1 //===-- xray_utils.cpp ------------------------------------------*- C++ -*-===//
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 is a part of XRay, a dynamic runtime instrumentation system.
10 //
11 //===----------------------------------------------------------------------===//
12 #include "xray_utils.h"
13 
14 #include "sanitizer_common/sanitizer_allocator_internal.h"
15 #include "sanitizer_common/sanitizer_common.h"
16 #include "xray_allocator.h"
17 #include "xray_defs.h"
18 #include "xray_flags.h"
19 #include <cstdio>
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <iterator>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <tuple>
26 #include <unistd.h>
27 #include <utility>
28 
29 #if SANITIZER_FUCHSIA
30 #include "sanitizer_common/sanitizer_symbolizer_fuchsia.h"
31 
32 #include <inttypes.h>
33 #include <zircon/process.h>
34 #include <zircon/sanitizer.h>
35 #include <zircon/status.h>
36 #include <zircon/syscalls.h>
37 #endif
38 
39 namespace __xray {
40 
41 #if SANITIZER_FUCHSIA
42 constexpr const char* ProfileSinkName = "llvm-xray";
43 
44 LogWriter::~LogWriter() {
45   _zx_handle_close(Vmo);
46 }
47 
48 void LogWriter::WriteAll(const char *Begin, const char *End) XRAY_NEVER_INSTRUMENT {
49   if (Begin == End)
50     return;
51   auto TotalBytes = std::distance(Begin, End);
52 
53   const size_t PageSize = flags()->xray_page_size_override > 0
54                               ? flags()->xray_page_size_override
55                               : GetPageSizeCached();
56   if (RoundUpTo(Offset, PageSize) != RoundUpTo(Offset + TotalBytes, PageSize)) {
57     // Resize the VMO to ensure there's sufficient space for the data.
58     zx_status_t Status = _zx_vmo_set_size(Vmo, Offset + TotalBytes);
59     if (Status != ZX_OK) {
60       Report("Failed to resize VMO: %s\n", _zx_status_get_string(Status));
61       return;
62     }
63   }
64 
65   // Write the data into VMO.
66   zx_status_t Status = _zx_vmo_write(Vmo, Begin, Offset, TotalBytes);
67   if (Status != ZX_OK) {
68     Report("Failed to write: %s\n", _zx_status_get_string(Status));
69     return;
70   }
71   Offset += TotalBytes;
72 
73   // Record the data size as a property of the VMO.
74   _zx_object_set_property(Vmo, ZX_PROP_VMO_CONTENT_SIZE,
75                           &Offset, sizeof(Offset));
76 }
77 
78 void LogWriter::Flush() XRAY_NEVER_INSTRUMENT {
79   // Nothing to do here since WriteAll writes directly into the VMO.
80 }
81 
82 LogWriter *LogWriter::Open() XRAY_NEVER_INSTRUMENT {
83   // Create VMO to hold the profile data.
84   zx_handle_t Vmo;
85   zx_status_t Status = _zx_vmo_create(0, ZX_VMO_RESIZABLE, &Vmo);
86   if (Status != ZX_OK) {
87     Report("XRay: cannot create VMO: %s\n", _zx_status_get_string(Status));
88     return nullptr;
89   }
90 
91   // Get the KOID of the current process to use in the VMO name.
92   zx_info_handle_basic_t Info;
93   Status = _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info,
94                                sizeof(Info), NULL, NULL);
95   if (Status != ZX_OK) {
96     Report("XRay: cannot get basic info about current process handle: %s\n",
97            _zx_status_get_string(Status));
98     return nullptr;
99   }
100 
101   // Give the VMO a name including our process KOID so it's easy to spot.
102   char VmoName[ZX_MAX_NAME_LEN];
103   internal_snprintf(VmoName, sizeof(VmoName), "%s.%zu", ProfileSinkName,
104                     Info.koid);
105   _zx_object_set_property(Vmo, ZX_PROP_NAME, VmoName, strlen(VmoName));
106 
107   // Duplicate the handle since __sanitizer_publish_data consumes it and
108   // LogWriter needs to hold onto it.
109   zx_handle_t Handle;
110   Status =_zx_handle_duplicate(Vmo, ZX_RIGHT_SAME_RIGHTS, &Handle);
111   if (Status != ZX_OK) {
112     Report("XRay: cannot duplicate VMO handle: %s\n",
113            _zx_status_get_string(Status));
114     return nullptr;
115   }
116 
117   // Publish the VMO that receives the logging. Note the VMO's contents can
118   // grow and change after publication. The contents won't be read out until
119   // after the process exits.
120   __sanitizer_publish_data(ProfileSinkName, Handle);
121 
122   // Use the dumpfile symbolizer markup element to write the name of the VMO.
123   Report("XRay: " FORMAT_DUMPFILE "\n", ProfileSinkName, VmoName);
124 
125   LogWriter *LW = reinterpret_cast<LogWriter *>(InternalAlloc(sizeof(LogWriter)));
126   new (LW) LogWriter(Vmo);
127   return LW;
128 }
129 
130 void LogWriter::Close(LogWriter *LW) {
131   LW->~LogWriter();
132   InternalFree(LW);
133 }
134 #else // SANITIZER_FUCHSIA
135 LogWriter::~LogWriter() {
136   internal_close(Fd);
137 }
138 
139 void LogWriter::WriteAll(const char *Begin, const char *End) XRAY_NEVER_INSTRUMENT {
140   if (Begin == End)
141     return;
142   auto TotalBytes = std::distance(Begin, End);
143   while (auto Written = write(Fd, Begin, TotalBytes)) {
144     if (Written < 0) {
145       if (errno == EINTR)
146         continue; // Try again.
147       Report("Failed to write; errno = %d\n", errno);
148       return;
149     }
150     TotalBytes -= Written;
151     if (TotalBytes == 0)
152       break;
153     Begin += Written;
154   }
155 }
156 
157 void LogWriter::Flush() XRAY_NEVER_INSTRUMENT {
158   fsync(Fd);
159 }
160 
161 LogWriter *LogWriter::Open() XRAY_NEVER_INSTRUMENT {
162   // Open a temporary file once for the log.
163   char TmpFilename[256] = {};
164   char TmpWildcardPattern[] = "XXXXXX";
165   auto **Argv = GetArgv();
166   const char *Progname = !Argv ? "(unknown)" : Argv[0];
167   const char *LastSlash = internal_strrchr(Progname, '/');
168 
169   if (LastSlash != nullptr)
170     Progname = LastSlash + 1;
171 
172   int NeededLength = internal_snprintf(
173       TmpFilename, sizeof(TmpFilename), "%s%s.%s",
174       flags()->xray_logfile_base, Progname, TmpWildcardPattern);
175   if (NeededLength > int(sizeof(TmpFilename))) {
176     Report("XRay log file name too long (%d): %s\n", NeededLength, TmpFilename);
177     return nullptr;
178   }
179   int Fd = mkstemp(TmpFilename);
180   if (Fd == -1) {
181     Report("XRay: Failed opening temporary file '%s'; not logging events.\n",
182            TmpFilename);
183     return nullptr;
184   }
185   if (Verbosity())
186     Report("XRay: Log file in '%s'\n", TmpFilename);
187 
188   LogWriter *LW = allocate<LogWriter>();
189   new (LW) LogWriter(Fd);
190   return LW;
191 }
192 
193 void LogWriter::Close(LogWriter *LW) {
194   LW->~LogWriter();
195   deallocate(LW);
196 }
197 #endif // SANITIZER_FUCHSIA
198 
199 } // namespace __xray
200