]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | |
92a42be0 | 18 | |
1a4d82fc JJ |
19 | #include "sanitizer_common.h" |
20 | #include "sanitizer_flags.h" | |
21 | #include "sanitizer_platform_limits_posix.h" | |
92a42be0 SL |
22 | #include "sanitizer_posix.h" |
23 | #include "sanitizer_procmaps.h" | |
1a4d82fc | 24 | #include "sanitizer_stacktrace.h" |
92a42be0 | 25 | #include "sanitizer_symbolizer.h" |
1a4d82fc JJ |
26 | |
27 | #include <errno.h> | |
92a42be0 | 28 | #include <fcntl.h> |
1a4d82fc JJ |
29 | #include <pthread.h> |
30 | #include <signal.h> | |
31 | #include <stdlib.h> | |
32 | #include <sys/mman.h> | |
33 | #include <sys/resource.h> | |
92a42be0 | 34 | #include <sys/stat.h> |
1a4d82fc JJ |
35 | #include <sys/time.h> |
36 | #include <sys/types.h> | |
37 | #include <unistd.h> | |
38 | ||
92a42be0 SL |
39 | #if SANITIZER_FREEBSD |
40 | // The MAP_NORESERVE define has been removed in FreeBSD 11.x, and even before | |
41 | // that, it was never implemented. So just define it to zero. | |
42 | #undef MAP_NORESERVE | |
43 | #define MAP_NORESERVE 0 | |
44 | #endif | |
45 | ||
1a4d82fc JJ |
46 | namespace __sanitizer { |
47 | ||
48 | u32 GetUid() { | |
49 | return getuid(); | |
50 | } | |
51 | ||
52 | uptr GetThreadSelf() { | |
53 | return (uptr)pthread_self(); | |
54 | } | |
55 | ||
56 | void FlushUnneededShadowMemory(uptr addr, uptr size) { | |
57 | madvise((void*)addr, size, MADV_DONTNEED); | |
58 | } | |
59 | ||
92a42be0 SL |
60 | void NoHugePagesInRegion(uptr addr, uptr size) { |
61 | #ifdef MADV_NOHUGEPAGE // May not be defined on old systems. | |
62 | madvise((void *)addr, size, MADV_NOHUGEPAGE); | |
63 | #endif // MADV_NOHUGEPAGE | |
1a4d82fc JJ |
64 | } |
65 | ||
92a42be0 SL |
66 | void DontDumpShadowMemory(uptr addr, uptr length) { |
67 | #ifdef MADV_DONTDUMP | |
68 | madvise((void *)addr, length, MADV_DONTDUMP); | |
69 | #endif | |
1a4d82fc JJ |
70 | } |
71 | ||
92a42be0 SL |
72 | static rlim_t getlim(int res) { |
73 | rlimit rlim; | |
74 | CHECK_EQ(0, getrlimit(res, &rlim)); | |
75 | return rlim.rlim_cur; | |
76 | } | |
77 | ||
78 | static void setlim(int res, rlim_t lim) { | |
79 | // The following magic is to prevent clang from replacing it with memset. | |
80 | volatile struct rlimit rlim; | |
81 | rlim.rlim_cur = lim; | |
82 | rlim.rlim_max = lim; | |
83 | if (setrlimit(res, const_cast<struct rlimit *>(&rlim))) { | |
1a4d82fc JJ |
84 | Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno); |
85 | Die(); | |
86 | } | |
92a42be0 SL |
87 | } |
88 | ||
89 | void DisableCoreDumperIfNecessary() { | |
90 | if (common_flags()->disable_coredump) { | |
91 | setlim(RLIMIT_CORE, 0); | |
92 | } | |
93 | } | |
94 | ||
95 | bool StackSizeIsUnlimited() { | |
96 | rlim_t stack_size = getlim(RLIMIT_STACK); | |
97 | return (stack_size == RLIM_INFINITY); | |
98 | } | |
99 | ||
100 | void SetStackSizeLimitInBytes(uptr limit) { | |
101 | setlim(RLIMIT_STACK, (rlim_t)limit); | |
1a4d82fc JJ |
102 | CHECK(!StackSizeIsUnlimited()); |
103 | } | |
104 | ||
92a42be0 SL |
105 | bool AddressSpaceIsUnlimited() { |
106 | rlim_t as_size = getlim(RLIMIT_AS); | |
107 | return (as_size == RLIM_INFINITY); | |
108 | } | |
109 | ||
110 | void SetAddressSpaceUnlimited() { | |
111 | setlim(RLIMIT_AS, RLIM_INFINITY); | |
112 | CHECK(AddressSpaceIsUnlimited()); | |
113 | } | |
114 | ||
1a4d82fc JJ |
115 | void SleepForSeconds(int seconds) { |
116 | sleep(seconds); | |
117 | } | |
118 | ||
119 | void SleepForMillis(int millis) { | |
120 | usleep(millis * 1000); | |
121 | } | |
122 | ||
123 | void Abort() { | |
124 | abort(); | |
125 | } | |
126 | ||
127 | int Atexit(void (*function)(void)) { | |
128 | #ifndef SANITIZER_GO | |
129 | return atexit(function); | |
130 | #else | |
131 | return 0; | |
132 | #endif | |
133 | } | |
134 | ||
92a42be0 SL |
135 | bool SupportsColoredOutput(fd_t fd) { |
136 | return isatty(fd) != 0; | |
1a4d82fc JJ |
137 | } |
138 | ||
139 | #ifndef SANITIZER_GO | |
140 | // TODO(glider): different tools may require different altstack size. | |
141 | static const uptr kAltStackSize = SIGSTKSZ * 4; // SIGSTKSZ is not enough. | |
142 | ||
143 | void SetAlternateSignalStack() { | |
144 | stack_t altstack, oldstack; | |
92a42be0 | 145 | CHECK_EQ(0, sigaltstack(nullptr, &oldstack)); |
1a4d82fc JJ |
146 | // If the alternate stack is already in place, do nothing. |
147 | // Android always sets an alternate stack, but it's too small for us. | |
148 | if (!SANITIZER_ANDROID && !(oldstack.ss_flags & SS_DISABLE)) return; | |
149 | // TODO(glider): the mapped stack should have the MAP_STACK flag in the | |
150 | // future. It is not required by man 2 sigaltstack now (they're using | |
151 | // malloc()). | |
152 | void* base = MmapOrDie(kAltStackSize, __func__); | |
153 | altstack.ss_sp = (char*) base; | |
154 | altstack.ss_flags = 0; | |
155 | altstack.ss_size = kAltStackSize; | |
92a42be0 | 156 | CHECK_EQ(0, sigaltstack(&altstack, nullptr)); |
1a4d82fc JJ |
157 | } |
158 | ||
159 | void UnsetAlternateSignalStack() { | |
160 | stack_t altstack, oldstack; | |
92a42be0 | 161 | altstack.ss_sp = nullptr; |
1a4d82fc JJ |
162 | altstack.ss_flags = SS_DISABLE; |
163 | altstack.ss_size = kAltStackSize; // Some sane value required on Darwin. | |
164 | CHECK_EQ(0, sigaltstack(&altstack, &oldstack)); | |
165 | UnmapOrDie(oldstack.ss_sp, oldstack.ss_size); | |
166 | } | |
167 | ||
168 | typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); | |
169 | static void MaybeInstallSigaction(int signum, | |
170 | SignalHandlerType handler) { | |
171 | if (!IsDeadlySignal(signum)) | |
172 | return; | |
173 | struct sigaction sigact; | |
174 | internal_memset(&sigact, 0, sizeof(sigact)); | |
175 | sigact.sa_sigaction = (sa_sigaction_t)handler; | |
92a42be0 SL |
176 | // Do not block the signal from being received in that signal's handler. |
177 | // Clients are responsible for handling this correctly. | |
178 | sigact.sa_flags = SA_SIGINFO | SA_NODEFER; | |
1a4d82fc | 179 | if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK; |
92a42be0 | 180 | CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr)); |
1a4d82fc JJ |
181 | VReport(1, "Installed the sigaction for signal %d\n", signum); |
182 | } | |
183 | ||
184 | void InstallDeadlySignalHandlers(SignalHandlerType handler) { | |
185 | // Set the alternate signal stack for the main thread. | |
186 | // This will cause SetAlternateSignalStack to be called twice, but the stack | |
187 | // will be actually set only once. | |
188 | if (common_flags()->use_sigaltstack) SetAlternateSignalStack(); | |
189 | MaybeInstallSigaction(SIGSEGV, handler); | |
190 | MaybeInstallSigaction(SIGBUS, handler); | |
92a42be0 SL |
191 | MaybeInstallSigaction(SIGABRT, handler); |
192 | MaybeInstallSigaction(SIGFPE, handler); | |
1a4d82fc JJ |
193 | } |
194 | #endif // SANITIZER_GO | |
195 | ||
92a42be0 SL |
196 | bool IsAccessibleMemoryRange(uptr beg, uptr size) { |
197 | uptr page_size = GetPageSizeCached(); | |
198 | // Checking too large memory ranges is slow. | |
199 | CHECK_LT(size, page_size * 10); | |
200 | int sock_pair[2]; | |
201 | if (pipe(sock_pair)) | |
202 | return false; | |
203 | uptr bytes_written = | |
204 | internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size); | |
205 | int write_errno; | |
206 | bool result; | |
207 | if (internal_iserror(bytes_written, &write_errno)) { | |
208 | CHECK_EQ(EFAULT, write_errno); | |
209 | result = false; | |
210 | } else { | |
211 | result = (bytes_written == size); | |
212 | } | |
213 | internal_close(sock_pair[0]); | |
214 | internal_close(sock_pair[1]); | |
215 | return result; | |
216 | } | |
217 | ||
218 | void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) { | |
219 | // Some kinds of sandboxes may forbid filesystem access, so we won't be able | |
220 | // to read the file mappings from /proc/self/maps. Luckily, neither the | |
221 | // process will be able to load additional libraries, so it's fine to use the | |
222 | // cached mappings. | |
223 | MemoryMappingLayout::CacheMemoryMappings(); | |
224 | // Same for /proc/self/exe in the symbolizer. | |
225 | #if !SANITIZER_GO | |
226 | Symbolizer::GetOrInit()->PrepareForSandboxing(); | |
227 | CovPrepareForSandboxing(args); | |
228 | #endif | |
229 | } | |
230 | ||
231 | #if SANITIZER_ANDROID | |
232 | int GetNamedMappingFd(const char *name, uptr size) { | |
233 | return -1; | |
234 | } | |
235 | #else | |
236 | int GetNamedMappingFd(const char *name, uptr size) { | |
237 | if (!common_flags()->decorate_proc_maps) | |
238 | return -1; | |
239 | char shmname[200]; | |
240 | CHECK(internal_strlen(name) < sizeof(shmname) - 10); | |
241 | internal_snprintf(shmname, sizeof(shmname), "%zu [%s]", internal_getpid(), | |
242 | name); | |
243 | int fd = shm_open(shmname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); | |
244 | CHECK_GE(fd, 0); | |
245 | int res = internal_ftruncate(fd, size); | |
246 | CHECK_EQ(0, res); | |
247 | res = shm_unlink(shmname); | |
248 | CHECK_EQ(0, res); | |
249 | return fd; | |
250 | } | |
251 | #endif | |
252 | ||
253 | void *MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) { | |
254 | int fd = name ? GetNamedMappingFd(name, size) : -1; | |
255 | unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE; | |
256 | if (fd == -1) flags |= MAP_ANON; | |
257 | ||
258 | uptr PageSize = GetPageSizeCached(); | |
259 | uptr p = internal_mmap((void *)(fixed_addr & ~(PageSize - 1)), | |
260 | RoundUpTo(size, PageSize), PROT_READ | PROT_WRITE, | |
261 | flags, fd, 0); | |
262 | int reserrno; | |
263 | if (internal_iserror(p, &reserrno)) | |
264 | Report("ERROR: %s failed to " | |
265 | "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n", | |
266 | SanitizerToolName, size, size, fixed_addr, reserrno); | |
267 | IncreaseTotalMmap(size); | |
268 | return (void *)p; | |
269 | } | |
270 | ||
271 | void *MmapNoAccess(uptr fixed_addr, uptr size, const char *name) { | |
272 | int fd = name ? GetNamedMappingFd(name, size) : -1; | |
273 | unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE; | |
274 | if (fd == -1) flags |= MAP_ANON; | |
275 | ||
276 | return (void *)internal_mmap((void *)fixed_addr, size, PROT_NONE, flags, fd, | |
277 | 0); | |
278 | } | |
279 | ||
280 | // This function is defined elsewhere if we intercepted pthread_attr_getstack. | |
281 | extern "C" { | |
282 | SANITIZER_WEAK_ATTRIBUTE int | |
283 | real_pthread_attr_getstack(void *attr, void **addr, size_t *size); | |
284 | } // extern "C" | |
285 | ||
286 | int my_pthread_attr_getstack(void *attr, void **addr, uptr *size) { | |
287 | #if !SANITIZER_GO && !SANITIZER_MAC | |
288 | if (&real_pthread_attr_getstack) | |
289 | return real_pthread_attr_getstack((pthread_attr_t *)attr, addr, | |
290 | (size_t *)size); | |
291 | #endif | |
292 | return pthread_attr_getstack((pthread_attr_t *)attr, addr, (size_t *)size); | |
293 | } | |
294 | ||
295 | #if !SANITIZER_GO | |
296 | void AdjustStackSize(void *attr_) { | |
297 | pthread_attr_t *attr = (pthread_attr_t *)attr_; | |
298 | uptr stackaddr = 0; | |
299 | uptr stacksize = 0; | |
300 | my_pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize); | |
301 | // GLibC will return (0 - stacksize) as the stack address in the case when | |
302 | // stacksize is set, but stackaddr is not. | |
303 | bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0); | |
304 | // We place a lot of tool data into TLS, account for that. | |
305 | const uptr minstacksize = GetTlsSize() + 128*1024; | |
306 | if (stacksize < minstacksize) { | |
307 | if (!stack_set) { | |
308 | if (stacksize != 0) { | |
309 | VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize, | |
310 | minstacksize); | |
311 | pthread_attr_setstacksize(attr, minstacksize); | |
312 | } | |
313 | } else { | |
314 | Printf("Sanitizer: pre-allocated stack size is insufficient: " | |
315 | "%zu < %zu\n", stacksize, minstacksize); | |
316 | Printf("Sanitizer: pthread_create is likely to fail.\n"); | |
317 | } | |
318 | } | |
319 | } | |
320 | #endif // !SANITIZER_GO | |
321 | ||
322 | } // namespace __sanitizer | |
1a4d82fc | 323 | |
92a42be0 | 324 | #endif // SANITIZER_POSIX |