]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cc
Upgrade to OpenSSH 7.4p1.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_posix_libcdep.cc
1 //===-- sanitizer_posix_libcdep.cc ----------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is shared between AddressSanitizer and ThreadSanitizer
11 // run-time libraries and implements libc-dependent POSIX-specific functions
12 // from sanitizer_libc.h.
13 //===----------------------------------------------------------------------===//
14
15 #include "sanitizer_platform.h"
16
17 #if SANITIZER_POSIX
18
19 #include "sanitizer_common.h"
20 #include "sanitizer_flags.h"
21 #include "sanitizer_platform_limits_posix.h"
22 #include "sanitizer_posix.h"
23 #include "sanitizer_procmaps.h"
24 #include "sanitizer_stacktrace.h"
25 #include "sanitizer_symbolizer.h"
26
27 #include <errno.h>
28 #include <fcntl.h>
29 #include <pthread.h>
30 #include <signal.h>
31 #include <stdlib.h>
32 #include <sys/mman.h>
33 #include <sys/resource.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <unistd.h>
39
40 #if SANITIZER_FREEBSD
41 // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before
42 // that, it was never implemented.  So just define it to zero.
43 #undef  MAP_NORESERVE
44 #define MAP_NORESERVE 0
45 #endif
46
47 typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
48
49 namespace __sanitizer {
50
51 u32 GetUid() {
52   return getuid();
53 }
54
55 uptr GetThreadSelf() {
56   return (uptr)pthread_self();
57 }
58
59 void ReleaseMemoryPagesToOS(uptr beg, uptr end) {
60   uptr page_size = GetPageSizeCached();
61   uptr beg_aligned = RoundUpTo(beg, page_size);
62   uptr end_aligned = RoundDownTo(end, page_size);
63   if (beg_aligned < end_aligned)
64     madvise((void*)beg_aligned, end_aligned - beg_aligned, MADV_DONTNEED);
65 }
66
67 void NoHugePagesInRegion(uptr addr, uptr size) {
68 #ifdef MADV_NOHUGEPAGE  // May not be defined on old systems.
69   madvise((void *)addr, size, MADV_NOHUGEPAGE);
70 #endif  // MADV_NOHUGEPAGE
71 }
72
73 void DontDumpShadowMemory(uptr addr, uptr length) {
74 #ifdef MADV_DONTDUMP
75   madvise((void *)addr, length, MADV_DONTDUMP);
76 #endif
77 }
78
79 static rlim_t getlim(int res) {
80   rlimit rlim;
81   CHECK_EQ(0, getrlimit(res, &rlim));
82   return rlim.rlim_cur;
83 }
84
85 static void setlim(int res, rlim_t lim) {
86   // The following magic is to prevent clang from replacing it with memset.
87   volatile struct rlimit rlim;
88   rlim.rlim_cur = lim;
89   rlim.rlim_max = lim;
90   if (setrlimit(res, const_cast<struct rlimit *>(&rlim))) {
91     Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
92     Die();
93   }
94 }
95
96 void DisableCoreDumperIfNecessary() {
97   if (common_flags()->disable_coredump) {
98     setlim(RLIMIT_CORE, 0);
99   }
100 }
101
102 bool StackSizeIsUnlimited() {
103   rlim_t stack_size = getlim(RLIMIT_STACK);
104   return (stack_size == RLIM_INFINITY);
105 }
106
107 uptr GetStackSizeLimitInBytes() {
108   return (uptr)getlim(RLIMIT_STACK);
109 }
110
111 void SetStackSizeLimitInBytes(uptr limit) {
112   setlim(RLIMIT_STACK, (rlim_t)limit);
113   CHECK(!StackSizeIsUnlimited());
114 }
115
116 bool AddressSpaceIsUnlimited() {
117   rlim_t as_size = getlim(RLIMIT_AS);
118   return (as_size == RLIM_INFINITY);
119 }
120
121 void SetAddressSpaceUnlimited() {
122   setlim(RLIMIT_AS, RLIM_INFINITY);
123   CHECK(AddressSpaceIsUnlimited());
124 }
125
126 void SleepForSeconds(int seconds) {
127   sleep(seconds);
128 }
129
130 void SleepForMillis(int millis) {
131   usleep(millis * 1000);
132 }
133
134 void Abort() {
135 #if !SANITIZER_GO
136   // If we are handling SIGABRT, unhandle it first.
137   if (IsHandledDeadlySignal(SIGABRT)) {
138     struct sigaction sigact;
139     internal_memset(&sigact, 0, sizeof(sigact));
140     sigact.sa_sigaction = (sa_sigaction_t)SIG_DFL;
141     internal_sigaction(SIGABRT, &sigact, nullptr);
142   }
143 #endif
144
145   abort();
146 }
147
148 int Atexit(void (*function)(void)) {
149 #if !SANITIZER_GO
150   return atexit(function);
151 #else
152   return 0;
153 #endif
154 }
155
156 bool SupportsColoredOutput(fd_t fd) {
157   return isatty(fd) != 0;
158 }
159
160 #if !SANITIZER_GO
161 // TODO(glider): different tools may require different altstack size.
162 static const uptr kAltStackSize = SIGSTKSZ * 4;  // SIGSTKSZ is not enough.
163
164 void SetAlternateSignalStack() {
165   stack_t altstack, oldstack;
166   CHECK_EQ(0, sigaltstack(nullptr, &oldstack));
167   // If the alternate stack is already in place, do nothing.
168   // Android always sets an alternate stack, but it's too small for us.
169   if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE)) return;
170   // TODO(glider): the mapped stack should have the MAP_STACK flag in the
171   // future. It is not required by man 2 sigaltstack now (they're using
172   // malloc()).
173   void* base = MmapOrDie(kAltStackSize, __func__);
174   altstack.ss_sp = (char*) base;
175   altstack.ss_flags = 0;
176   altstack.ss_size = kAltStackSize;
177   CHECK_EQ(0, sigaltstack(&altstack, nullptr));
178 }
179
180 void UnsetAlternateSignalStack() {
181   stack_t altstack, oldstack;
182   altstack.ss_sp = nullptr;
183   altstack.ss_flags = SS_DISABLE;
184   altstack.ss_size = kAltStackSize;  // Some sane value required on Darwin.
185   CHECK_EQ(0, sigaltstack(&altstack, &oldstack));
186   UnmapOrDie(oldstack.ss_sp, oldstack.ss_size);
187 }
188
189 static void MaybeInstallSigaction(int signum,
190                                   SignalHandlerType handler) {
191   if (!IsHandledDeadlySignal(signum))
192     return;
193   struct sigaction sigact;
194   internal_memset(&sigact, 0, sizeof(sigact));
195   sigact.sa_sigaction = (sa_sigaction_t)handler;
196   // Do not block the signal from being received in that signal's handler.
197   // Clients are responsible for handling this correctly.
198   sigact.sa_flags = SA_SIGINFO | SA_NODEFER;
199   if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK;
200   CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr));
201   VReport(1, "Installed the sigaction for signal %d\n", signum);
202 }
203
204 void InstallDeadlySignalHandlers(SignalHandlerType handler) {
205   // Set the alternate signal stack for the main thread.
206   // This will cause SetAlternateSignalStack to be called twice, but the stack
207   // will be actually set only once.
208   if (common_flags()->use_sigaltstack) SetAlternateSignalStack();
209   MaybeInstallSigaction(SIGSEGV, handler);
210   MaybeInstallSigaction(SIGBUS, handler);
211   MaybeInstallSigaction(SIGABRT, handler);
212   MaybeInstallSigaction(SIGFPE, handler);
213   MaybeInstallSigaction(SIGILL, handler);
214 }
215 #endif  // SANITIZER_GO
216
217 bool IsAccessibleMemoryRange(uptr beg, uptr size) {
218   uptr page_size = GetPageSizeCached();
219   // Checking too large memory ranges is slow.
220   CHECK_LT(size, page_size * 10);
221   int sock_pair[2];
222   if (pipe(sock_pair))
223     return false;
224   uptr bytes_written =
225       internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size);
226   int write_errno;
227   bool result;
228   if (internal_iserror(bytes_written, &write_errno)) {
229     CHECK_EQ(EFAULT, write_errno);
230     result = false;
231   } else {
232     result = (bytes_written == size);
233   }
234   internal_close(sock_pair[0]);
235   internal_close(sock_pair[1]);
236   return result;
237 }
238
239 void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
240   // Some kinds of sandboxes may forbid filesystem access, so we won't be able
241   // to read the file mappings from /proc/self/maps. Luckily, neither the
242   // process will be able to load additional libraries, so it's fine to use the
243   // cached mappings.
244   MemoryMappingLayout::CacheMemoryMappings();
245   // Same for /proc/self/exe in the symbolizer.
246 #if !SANITIZER_GO
247   Symbolizer::GetOrInit()->PrepareForSandboxing();
248   CovPrepareForSandboxing(args);
249 #endif
250 }
251
252 #if SANITIZER_ANDROID || SANITIZER_GO
253 int GetNamedMappingFd(const char *name, uptr size) {
254   return -1;
255 }
256 #else
257 int GetNamedMappingFd(const char *name, uptr size) {
258   if (!common_flags()->decorate_proc_maps)
259     return -1;
260   char shmname[200];
261   CHECK(internal_strlen(name) < sizeof(shmname) - 10);
262   internal_snprintf(shmname, sizeof(shmname), "%zu [%s]", internal_getpid(),
263                     name);
264   int fd = shm_open(shmname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU);
265   CHECK_GE(fd, 0);
266   int res = internal_ftruncate(fd, size);
267   CHECK_EQ(0, res);
268   res = shm_unlink(shmname);
269   CHECK_EQ(0, res);
270   return fd;
271 }
272 #endif
273
274 void *MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) {
275   int fd = name ? GetNamedMappingFd(name, size) : -1;
276   unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE;
277   if (fd == -1) flags |= MAP_ANON;
278
279   uptr PageSize = GetPageSizeCached();
280   uptr p = internal_mmap((void *)(fixed_addr & ~(PageSize - 1)),
281                          RoundUpTo(size, PageSize), PROT_READ | PROT_WRITE,
282                          flags, fd, 0);
283   int reserrno;
284   if (internal_iserror(p, &reserrno))
285     Report("ERROR: %s failed to "
286            "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n",
287            SanitizerToolName, size, size, fixed_addr, reserrno);
288   IncreaseTotalMmap(size);
289   return (void *)p;
290 }
291
292 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
293   int fd = name ? GetNamedMappingFd(name, size) : -1;
294   unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE;
295   if (fd == -1) flags |= MAP_ANON;
296
297   return (void *)internal_mmap((void *)fixed_addr, size, PROT_NONE, flags, fd,
298                                0);
299 }
300
301 void *MmapNoAccess(uptr size) {
302   unsigned flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE;
303   return (void *)internal_mmap(nullptr, size, PROT_NONE, flags, -1, 0);
304 }
305
306 // This function is defined elsewhere if we intercepted pthread_attr_getstack.
307 extern "C" {
308 SANITIZER_WEAK_ATTRIBUTE int
309 real_pthread_attr_getstack(void *attr, void **addr, size_t *size);
310 } // extern "C"
311
312 int my_pthread_attr_getstack(void *attr, void **addr, uptr *size) {
313 #if !SANITIZER_GO && !SANITIZER_MAC
314   if (&real_pthread_attr_getstack)
315     return real_pthread_attr_getstack((pthread_attr_t *)attr, addr,
316                                       (size_t *)size);
317 #endif
318   return pthread_attr_getstack((pthread_attr_t *)attr, addr, (size_t *)size);
319 }
320
321 #if !SANITIZER_GO
322 void AdjustStackSize(void *attr_) {
323   pthread_attr_t *attr = (pthread_attr_t *)attr_;
324   uptr stackaddr = 0;
325   uptr stacksize = 0;
326   my_pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize);
327   // GLibC will return (0 - stacksize) as the stack address in the case when
328   // stacksize is set, but stackaddr is not.
329   bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0);
330   // We place a lot of tool data into TLS, account for that.
331   const uptr minstacksize = GetTlsSize() + 128*1024;
332   if (stacksize < minstacksize) {
333     if (!stack_set) {
334       if (stacksize != 0) {
335         VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize,
336                 minstacksize);
337         pthread_attr_setstacksize(attr, minstacksize);
338       }
339     } else {
340       Printf("Sanitizer: pre-allocated stack size is insufficient: "
341              "%zu < %zu\n", stacksize, minstacksize);
342       Printf("Sanitizer: pthread_create is likely to fail.\n");
343     }
344   }
345 }
346 #endif // !SANITIZER_GO
347
348 pid_t StartSubprocess(const char *program, const char *const argv[],
349                       fd_t stdin_fd, fd_t stdout_fd, fd_t stderr_fd) {
350   auto file_closer = at_scope_exit([&] {
351     if (stdin_fd != kInvalidFd) {
352       internal_close(stdin_fd);
353     }
354     if (stdout_fd != kInvalidFd) {
355       internal_close(stdout_fd);
356     }
357     if (stderr_fd != kInvalidFd) {
358       internal_close(stderr_fd);
359     }
360   });
361
362   int pid = internal_fork();
363
364   if (pid < 0) {
365     int rverrno;
366     if (internal_iserror(pid, &rverrno)) {
367       Report("WARNING: failed to fork (errno %d)\n", rverrno);
368     }
369     return pid;
370   }
371
372   if (pid == 0) {
373     // Child subprocess
374     if (stdin_fd != kInvalidFd) {
375       internal_close(STDIN_FILENO);
376       internal_dup2(stdin_fd, STDIN_FILENO);
377       internal_close(stdin_fd);
378     }
379     if (stdout_fd != kInvalidFd) {
380       internal_close(STDOUT_FILENO);
381       internal_dup2(stdout_fd, STDOUT_FILENO);
382       internal_close(stdout_fd);
383     }
384     if (stderr_fd != kInvalidFd) {
385       internal_close(STDERR_FILENO);
386       internal_dup2(stderr_fd, STDERR_FILENO);
387       internal_close(stderr_fd);
388     }
389
390     for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd);
391
392     execv(program, const_cast<char **>(&argv[0]));
393     internal__exit(1);
394   }
395
396   return pid;
397 }
398
399 bool IsProcessRunning(pid_t pid) {
400   int process_status;
401   uptr waitpid_status = internal_waitpid(pid, &process_status, WNOHANG);
402   int local_errno;
403   if (internal_iserror(waitpid_status, &local_errno)) {
404     VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
405     return false;
406   }
407   return waitpid_status == 0;
408 }
409
410 int WaitForProcess(pid_t pid) {
411   int process_status;
412   uptr waitpid_status = internal_waitpid(pid, &process_status, 0);
413   int local_errno;
414   if (internal_iserror(waitpid_status, &local_errno)) {
415     VReport(1, "Waiting on the process failed (errno %d).\n", local_errno);
416     return -1;
417   }
418   return process_status;
419 }
420
421 } // namespace __sanitizer
422
423 #endif // SANITIZER_POSIX