1 /*===- InstrProfilingUtil.c - Support library for PGO instrumentation -----===*\
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 #ifdef _WIN32
10 #include <direct.h>
11 #include <process.h>
12 #include <windows.h>
13 #include "WindowsMMap.h"
14 #else
15 #include <errno.h>
16 #include <fcntl.h>
17 #include <sys/file.h>
18 #include <sys/mman.h>
19 #include <sys/stat.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 #endif
23 
24 #ifdef COMPILER_RT_HAS_UNAME
25 #include <sys/utsname.h>
26 #endif
27 
28 #include <stdlib.h>
29 #include <string.h>
30 
31 #if defined(__linux__)
32 #include <signal.h>
33 #include <sys/prctl.h>
34 #endif
35 
36 #if defined(__Fuchsia__)
37 #include <zircon/syscalls.h>
38 #endif
39 
40 #include "InstrProfiling.h"
41 #include "InstrProfilingUtil.h"
42 
43 COMPILER_RT_WEAK unsigned lprofDirMode = 0755;
44 
45 COMPILER_RT_VISIBILITY
46 void __llvm_profile_recursive_mkdir(char *path) {
47   int i;
48   int start = 1;
49 
50 #if defined(__ANDROID__) && defined(__ANDROID_API__) &&                        \
51     defined(__ANDROID_API_FUTURE__) &&                                         \
52     __ANDROID_API__ == __ANDROID_API_FUTURE__
53   // Avoid spammy selinux denial messages in Android by not attempting to
54   // create directories in GCOV_PREFIX.  These denials occur when creating (or
55   // even attempting to stat()) top-level directories like "/data".
56   //
57   // Do so by ignoring ${GCOV_PREFIX} when invoking mkdir().
58   const char *gcov_prefix = getenv("GCOV_PREFIX");
59   if (gcov_prefix != NULL) {
60     const int gcov_prefix_len = strlen(gcov_prefix);
61     if (strncmp(path, gcov_prefix, gcov_prefix_len) == 0)
62       start = gcov_prefix_len;
63   }
64 #endif
65 
66   for (i = start; path[i] != '\0'; ++i) {
67     char save = path[i];
68     if (!IS_DIR_SEPARATOR(path[i]))
69       continue;
70     path[i] = '\0';
71 #ifdef _WIN32
72     _mkdir(path);
73 #else
74     /* Some of these will fail, ignore it. */
75     mkdir(path, __llvm_profile_get_dir_mode());
76 #endif
77     path[i] = save;
78   }
79 }
80 
81 COMPILER_RT_VISIBILITY
82 void __llvm_profile_set_dir_mode(unsigned Mode) { lprofDirMode = Mode; }
83 
84 COMPILER_RT_VISIBILITY
85 unsigned __llvm_profile_get_dir_mode(void) { return lprofDirMode; }
86 
87 #if COMPILER_RT_HAS_ATOMICS != 1
88 COMPILER_RT_VISIBILITY
89 uint32_t lprofBoolCmpXchg(void **Ptr, void *OldV, void *NewV) {
90   void *R = *Ptr;
91   if (R == OldV) {
92     *Ptr = NewV;
93     return 1;
94   }
95   return 0;
96 }
97 COMPILER_RT_VISIBILITY
98 void *lprofPtrFetchAdd(void **Mem, long ByteIncr) {
99   void *Old = *Mem;
100   *((char **)Mem) += ByteIncr;
101   return Old;
102 }
103 
104 #endif
105 
106 #ifdef _WIN32
107 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
108   WCHAR Buffer[COMPILER_RT_MAX_HOSTLEN];
109   DWORD BufferSize = sizeof(Buffer);
110   BOOL Result =
111       GetComputerNameExW(ComputerNameDnsFullyQualified, Buffer, &BufferSize);
112   if (!Result)
113     return -1;
114   if (WideCharToMultiByte(CP_UTF8, 0, Buffer, -1, Name, Len, NULL, NULL) == 0)
115     return -1;
116   return 0;
117 }
118 #elif defined(COMPILER_RT_HAS_UNAME)
119 COMPILER_RT_VISIBILITY int lprofGetHostName(char *Name, int Len) {
120   struct utsname N;
121   int R = uname(&N);
122   if (R >= 0) {
123     strncpy(Name, N.nodename, Len);
124     return 0;
125   }
126   return R;
127 }
128 #endif
129 
130 COMPILER_RT_VISIBILITY int lprofLockFd(int fd) {
131 #ifdef COMPILER_RT_HAS_FCNTL_LCK
132   struct flock s_flock;
133 
134   s_flock.l_whence = SEEK_SET;
135   s_flock.l_start = 0;
136   s_flock.l_len = 0; /* Until EOF.  */
137   s_flock.l_pid = getpid();
138   s_flock.l_type = F_WRLCK;
139 
140   while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
141     if (errno != EINTR) {
142       if (errno == ENOLCK) {
143         return -1;
144       }
145       break;
146     }
147   }
148   return 0;
149 #else
150   flock(fd, LOCK_EX);
151   return 0;
152 #endif
153 }
154 
155 COMPILER_RT_VISIBILITY int lprofUnlockFd(int fd) {
156 #ifdef COMPILER_RT_HAS_FCNTL_LCK
157   struct flock s_flock;
158 
159   s_flock.l_whence = SEEK_SET;
160   s_flock.l_start = 0;
161   s_flock.l_len = 0; /* Until EOF.  */
162   s_flock.l_pid = getpid();
163   s_flock.l_type = F_UNLCK;
164 
165   while (fcntl(fd, F_SETLKW, &s_flock) == -1) {
166     if (errno != EINTR) {
167       if (errno == ENOLCK) {
168         return -1;
169       }
170       break;
171     }
172   }
173   return 0;
174 #else
175   flock(fd, LOCK_UN);
176   return 0;
177 #endif
178 }
179 
180 COMPILER_RT_VISIBILITY int lprofLockFileHandle(FILE *F) {
181   int fd;
182 #if defined(_WIN32)
183   fd = _fileno(F);
184 #else
185   fd = fileno(F);
186 #endif
187   return lprofLockFd(fd);
188 }
189 
190 COMPILER_RT_VISIBILITY int lprofUnlockFileHandle(FILE *F) {
191   int fd;
192 #if defined(_WIN32)
193   fd = _fileno(F);
194 #else
195   fd = fileno(F);
196 #endif
197   return lprofUnlockFd(fd);
198 }
199 
200 COMPILER_RT_VISIBILITY FILE *lprofOpenFileEx(const char *ProfileName) {
201   FILE *f;
202   int fd;
203 #ifdef COMPILER_RT_HAS_FCNTL_LCK
204   fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
205   if (fd < 0)
206     return NULL;
207 
208   if (lprofLockFd(fd) != 0)
209     PROF_WARN("Data may be corrupted during profile merging : %s\n",
210               "Fail to obtain file lock due to system limit.");
211 
212   f = fdopen(fd, "r+b");
213 #elif defined(_WIN32)
214   // FIXME: Use the wide variants to handle Unicode filenames.
215   HANDLE h = CreateFileA(ProfileName, GENERIC_READ | GENERIC_WRITE,
216                          FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS,
217                          FILE_ATTRIBUTE_NORMAL, 0);
218   if (h == INVALID_HANDLE_VALUE)
219     return NULL;
220 
221   fd = _open_osfhandle((intptr_t)h, 0);
222   if (fd == -1) {
223     CloseHandle(h);
224     return NULL;
225   }
226 
227   if (lprofLockFd(fd) != 0)
228     PROF_WARN("Data may be corrupted during profile merging : %s\n",
229               "Fail to obtain file lock due to system limit.");
230 
231   f = _fdopen(fd, "r+b");
232   if (f == 0) {
233     CloseHandle(h);
234     return NULL;
235   }
236 #else
237   /* Worst case no locking applied.  */
238   PROF_WARN("Concurrent file access is not supported : %s\n",
239             "lack file locking");
240   fd = open(ProfileName, O_RDWR | O_CREAT, 0666);
241   if (fd < 0)
242     return NULL;
243   f = fdopen(fd, "r+b");
244 #endif
245 
246   return f;
247 }
248 
249 COMPILER_RT_VISIBILITY const char *lprofGetPathPrefix(int *PrefixStrip,
250                                                       size_t *PrefixLen) {
251   const char *Prefix = getenv("GCOV_PREFIX");
252   const char *PrefixStripStr = getenv("GCOV_PREFIX_STRIP");
253 
254   *PrefixLen = 0;
255   *PrefixStrip = 0;
256   if (Prefix == NULL || Prefix[0] == '\0')
257     return NULL;
258 
259   if (PrefixStripStr) {
260     *PrefixStrip = atoi(PrefixStripStr);
261 
262     /* Negative GCOV_PREFIX_STRIP values are ignored */
263     if (*PrefixStrip < 0)
264       *PrefixStrip = 0;
265   } else {
266     *PrefixStrip = 0;
267   }
268   *PrefixLen = strlen(Prefix);
269 
270   return Prefix;
271 }
272 
273 COMPILER_RT_VISIBILITY void
274 lprofApplyPathPrefix(char *Dest, const char *PathStr, const char *Prefix,
275                      size_t PrefixLen, int PrefixStrip) {
276 
277   const char *Ptr;
278   int Level;
279   const char *StrippedPathStr = PathStr;
280 
281   for (Level = 0, Ptr = PathStr + 1; Level < PrefixStrip; ++Ptr) {
282     if (*Ptr == '\0')
283       break;
284 
285     if (!IS_DIR_SEPARATOR(*Ptr))
286       continue;
287 
288     StrippedPathStr = Ptr;
289     ++Level;
290   }
291 
292   memcpy(Dest, Prefix, PrefixLen);
293 
294   if (!IS_DIR_SEPARATOR(Prefix[PrefixLen - 1]))
295     Dest[PrefixLen++] = DIR_SEPARATOR;
296 
297   memcpy(Dest + PrefixLen, StrippedPathStr, strlen(StrippedPathStr) + 1);
298 }
299 
300 COMPILER_RT_VISIBILITY const char *
301 lprofFindFirstDirSeparator(const char *Path) {
302   const char *Sep = strchr(Path, DIR_SEPARATOR);
303 #if defined(DIR_SEPARATOR_2)
304   const char *Sep2 = strchr(Path, DIR_SEPARATOR_2);
305   if (Sep2 && (!Sep || Sep2 < Sep))
306     Sep = Sep2;
307 #endif
308   return Sep;
309 }
310 
311 COMPILER_RT_VISIBILITY const char *lprofFindLastDirSeparator(const char *Path) {
312   const char *Sep = strrchr(Path, DIR_SEPARATOR);
313 #if defined(DIR_SEPARATOR_2)
314   const char *Sep2 = strrchr(Path, DIR_SEPARATOR_2);
315   if (Sep2 && (!Sep || Sep2 > Sep))
316     Sep = Sep2;
317 #endif
318   return Sep;
319 }
320 
321 COMPILER_RT_VISIBILITY int lprofSuspendSigKill() {
322 #if defined(__linux__)
323   int PDeachSig = 0;
324   /* Temporarily suspend getting SIGKILL upon exit of the parent process. */
325   if (prctl(PR_GET_PDEATHSIG, &PDeachSig) == 0 && PDeachSig == SIGKILL)
326     prctl(PR_SET_PDEATHSIG, 0);
327   return (PDeachSig == SIGKILL);
328 #else
329   return 0;
330 #endif
331 }
332 
333 COMPILER_RT_VISIBILITY void lprofRestoreSigKill() {
334 #if defined(__linux__)
335   prctl(PR_SET_PDEATHSIG, SIGKILL);
336 #endif
337 }
338 
339 COMPILER_RT_VISIBILITY int lprofReleaseMemoryPagesToOS(uintptr_t Begin,
340                                                        uintptr_t End) {
341   size_t PageSize = getpagesize();
342   uintptr_t BeginAligned = lprofRoundUpTo((uintptr_t)Begin, PageSize);
343   uintptr_t EndAligned = lprofRoundDownTo((uintptr_t)End, PageSize);
344   if (BeginAligned < EndAligned) {
345 #if defined(__Fuchsia__)
346     return _zx_vmar_op_range(_zx_vmar_root_self(), ZX_VMAR_OP_DECOMMIT,
347                              (zx_vaddr_t)BeginAligned,
348                              EndAligned - BeginAligned, NULL, 0);
349 #else
350     return madvise((void *)BeginAligned, EndAligned - BeginAligned,
351                    MADV_DONTNEED);
352 #endif
353   }
354   return 0;
355 }
356