blob: 429f5de28f7dde826f08a6078f6bccf23a0d44b3 [file] [log] [blame]
Peter Collingbourne04a22812013-05-21 10:27:07 +00001//===-- 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_LINUX || SANITIZER_MAC
18#include "sanitizer_common.h"
19#include "sanitizer_stacktrace.h"
20
21#include <errno.h>
22#include <pthread.h>
23#include <stdlib.h>
24#include <sys/mman.h>
25#include <sys/resource.h>
26#include <sys/time.h>
27#include <sys/types.h>
28#include <unistd.h>
29
30namespace __sanitizer {
31
32u32 GetUid() {
33 return getuid();
34}
35
36uptr GetThreadSelf() {
37 return (uptr)pthread_self();
38}
39
40void FlushUnneededShadowMemory(uptr addr, uptr size) {
41 madvise((void*)addr, size, MADV_DONTNEED);
42}
43
44void DisableCoreDumper() {
45 struct rlimit nocore;
46 nocore.rlim_cur = 0;
47 nocore.rlim_max = 0;
48 setrlimit(RLIMIT_CORE, &nocore);
49}
50
51bool StackSizeIsUnlimited() {
52 struct rlimit rlim;
53 CHECK_EQ(0, getrlimit(RLIMIT_STACK, &rlim));
54 return (rlim.rlim_cur == (uptr)-1);
55}
56
57void SetStackSizeLimitInBytes(uptr limit) {
58 struct rlimit rlim;
59 rlim.rlim_cur = limit;
60 rlim.rlim_max = limit;
61 if (setrlimit(RLIMIT_STACK, &rlim)) {
62 Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno);
63 Die();
64 }
65 CHECK(!StackSizeIsUnlimited());
66}
67
68void SleepForSeconds(int seconds) {
69 sleep(seconds);
70}
71
72void SleepForMillis(int millis) {
73 usleep(millis * 1000);
74}
75
76void Abort() {
77 abort();
78}
79
80int Atexit(void (*function)(void)) {
81#ifndef SANITIZER_GO
82 return atexit(function);
83#else
84 return 0;
85#endif
86}
87
88int internal_isatty(fd_t fd) {
89 return isatty(fd);
90}
91
92#ifndef SANITIZER_GO
93void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp,
94 uptr stack_top, uptr stack_bottom, bool fast) {
95#if !SANITIZER_CAN_FAST_UNWIND
96 fast = false;
97#endif
98#if SANITIZER_MAC
99 // Always unwind fast on Mac.
100 (void)fast;
101#else
Sergey Matveev0550a3f2013-06-06 14:19:36 +0000102 if (!fast)
Peter Collingbourne04a22812013-05-21 10:27:07 +0000103 return stack->SlowUnwindStack(pc, max_s);
104#endif // SANITIZER_MAC
105 stack->size = 0;
106 stack->trace[0] = pc;
107 if (max_s > 1) {
108 stack->max_size = max_s;
109 stack->FastUnwindStack(pc, bp, stack_top, stack_bottom);
110 }
111}
112#endif // SANITIZER_GO
113
114} // namespace __sanitizer
115
116#endif