]>
Commit | Line | Data |
---|---|---|
ef1b3fda KS |
1 | //===-- sanitizer_posix_libcdep.cc ----------------------------------------===// |
2 | // | |
3 | // This file is distributed under the University of Illinois Open Source | |
4 | // License. See LICENSE.TXT for details. | |
5 | // | |
6 | //===----------------------------------------------------------------------===// | |
7 | // | |
8 | // This file is shared between AddressSanitizer and ThreadSanitizer | |
9 | // run-time libraries and implements libc-dependent POSIX-specific functions | |
10 | // from sanitizer_libc.h. | |
11 | //===----------------------------------------------------------------------===// | |
12 | ||
13 | #include "sanitizer_platform.h" | |
14 | ||
dee5ea7a | 15 | #if SANITIZER_POSIX |
696d846a | 16 | |
ef1b3fda | 17 | #include "sanitizer_common.h" |
dee5ea7a | 18 | #include "sanitizer_flags.h" |
5d3805fc | 19 | #include "sanitizer_platform_limits_netbsd.h" |
dee5ea7a | 20 | #include "sanitizer_platform_limits_posix.h" |
696d846a MO |
21 | #include "sanitizer_posix.h" |
22 | #include "sanitizer_procmaps.h" | |
ef1b3fda | 23 | #include "sanitizer_stacktrace.h" |
696d846a | 24 | #include "sanitizer_symbolizer.h" |
ef1b3fda KS |
25 | |
26 | #include <errno.h> | |
696d846a | 27 | #include <fcntl.h> |
ef1b3fda | 28 | #include <pthread.h> |
dee5ea7a | 29 | #include <signal.h> |
ef1b3fda KS |
30 | #include <stdlib.h> |
31 | #include <sys/mman.h> | |
32 | #include <sys/resource.h> | |
696d846a | 33 | #include <sys/stat.h> |
ef1b3fda KS |
34 | #include <sys/time.h> |
35 | #include <sys/types.h> | |
10189819 | 36 | #include <sys/wait.h> |
ef1b3fda KS |
37 | #include <unistd.h> |
38 | ||
696d846a MO |
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 | ||
10189819 MO |
46 | typedef void (*sa_sigaction_t)(int, siginfo_t *, void *); |
47 | ||
ef1b3fda KS |
48 | namespace __sanitizer { |
49 | ||
50 | u32 GetUid() { | |
51 | return getuid(); | |
52 | } | |
53 | ||
54 | uptr GetThreadSelf() { | |
55 | return (uptr)pthread_self(); | |
56 | } | |
57 | ||
5d3805fc JJ |
58 | void ReleaseMemoryPagesToOS(uptr beg, uptr end) { |
59 | uptr page_size = GetPageSizeCached(); | |
60 | uptr beg_aligned = RoundUpTo(beg, page_size); | |
61 | uptr end_aligned = RoundDownTo(end, page_size); | |
62 | if (beg_aligned < end_aligned) | |
63 | madvise((void*)beg_aligned, end_aligned - beg_aligned, MADV_DONTNEED); | |
ef1b3fda KS |
64 | } |
65 | ||
696d846a MO |
66 | void NoHugePagesInRegion(uptr addr, uptr size) { |
67 | #ifdef MADV_NOHUGEPAGE // May not be defined on old systems. | |
68 | madvise((void *)addr, size, MADV_NOHUGEPAGE); | |
69 | #endif // MADV_NOHUGEPAGE | |
70 | } | |
71 | ||
72 | void DontDumpShadowMemory(uptr addr, uptr length) { | |
73 | #ifdef MADV_DONTDUMP | |
74 | madvise((void *)addr, length, MADV_DONTDUMP); | |
75 | #endif | |
76 | } | |
77 | ||
866e32ad KS |
78 | static rlim_t getlim(int res) { |
79 | rlimit rlim; | |
80 | CHECK_EQ(0, getrlimit(res, &rlim)); | |
81 | return rlim.rlim_cur; | |
82 | } | |
83 | ||
84 | static void setlim(int res, rlim_t lim) { | |
85 | // The following magic is to prevent clang from replacing it with memset. | |
86 | volatile struct rlimit rlim; | |
87 | rlim.rlim_cur = lim; | |
88 | rlim.rlim_max = lim; | |
696d846a | 89 | if (setrlimit(res, const_cast<struct rlimit *>(&rlim))) { |
866e32ad KS |
90 | Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName, errno); |
91 | Die(); | |
92 | } | |
93 | } | |
94 | ||
95 | void DisableCoreDumperIfNecessary() { | |
96 | if (common_flags()->disable_coredump) { | |
97 | setlim(RLIMIT_CORE, 0); | |
98 | } | |
ef1b3fda KS |
99 | } |
100 | ||
101 | bool StackSizeIsUnlimited() { | |
866e32ad KS |
102 | rlim_t stack_size = getlim(RLIMIT_STACK); |
103 | return (stack_size == RLIM_INFINITY); | |
ef1b3fda KS |
104 | } |
105 | ||
10189819 MO |
106 | uptr GetStackSizeLimitInBytes() { |
107 | return (uptr)getlim(RLIMIT_STACK); | |
108 | } | |
109 | ||
ef1b3fda | 110 | void SetStackSizeLimitInBytes(uptr limit) { |
866e32ad | 111 | setlim(RLIMIT_STACK, (rlim_t)limit); |
ef1b3fda KS |
112 | CHECK(!StackSizeIsUnlimited()); |
113 | } | |
114 | ||
866e32ad KS |
115 | bool AddressSpaceIsUnlimited() { |
116 | rlim_t as_size = getlim(RLIMIT_AS); | |
117 | return (as_size == RLIM_INFINITY); | |
118 | } | |
119 | ||
120 | void SetAddressSpaceUnlimited() { | |
121 | setlim(RLIMIT_AS, RLIM_INFINITY); | |
122 | CHECK(AddressSpaceIsUnlimited()); | |
123 | } | |
124 | ||
ef1b3fda KS |
125 | void SleepForSeconds(int seconds) { |
126 | sleep(seconds); | |
127 | } | |
128 | ||
129 | void SleepForMillis(int millis) { | |
130 | usleep(millis * 1000); | |
131 | } | |
132 | ||
133 | void Abort() { | |
10189819 MO |
134 | #if !SANITIZER_GO |
135 | // If we are handling SIGABRT, unhandle it first. | |
5d3805fc JJ |
136 | // TODO(vitalybuka): Check if handler belongs to sanitizer. |
137 | if (GetHandleSignalMode(SIGABRT) != kHandleSignalNo) { | |
10189819 MO |
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 | ||
ef1b3fda KS |
145 | abort(); |
146 | } | |
147 | ||
148 | int Atexit(void (*function)(void)) { | |
10189819 | 149 | #if !SANITIZER_GO |
ef1b3fda KS |
150 | return atexit(function); |
151 | #else | |
152 | return 0; | |
153 | #endif | |
154 | } | |
155 | ||
696d846a MO |
156 | bool SupportsColoredOutput(fd_t fd) { |
157 | return isatty(fd) != 0; | |
ef1b3fda KS |
158 | } |
159 | ||
10189819 | 160 | #if !SANITIZER_GO |
dee5ea7a KS |
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; | |
696d846a | 166 | CHECK_EQ(0, sigaltstack(nullptr, &oldstack)); |
dee5ea7a KS |
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; | |
696d846a | 177 | CHECK_EQ(0, sigaltstack(&altstack, nullptr)); |
dee5ea7a KS |
178 | } |
179 | ||
180 | void UnsetAlternateSignalStack() { | |
181 | stack_t altstack, oldstack; | |
696d846a | 182 | altstack.ss_sp = nullptr; |
dee5ea7a KS |
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 | ||
dee5ea7a KS |
189 | static void MaybeInstallSigaction(int signum, |
190 | SignalHandlerType handler) { | |
5d3805fc JJ |
191 | if (GetHandleSignalMode(signum) == kHandleSignalNo) return; |
192 | ||
dee5ea7a KS |
193 | struct sigaction sigact; |
194 | internal_memset(&sigact, 0, sizeof(sigact)); | |
195 | sigact.sa_sigaction = (sa_sigaction_t)handler; | |
866e32ad KS |
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; | |
dee5ea7a | 199 | if (common_flags()->use_sigaltstack) sigact.sa_flags |= SA_ONSTACK; |
696d846a | 200 | CHECK_EQ(0, internal_sigaction(signum, &sigact, nullptr)); |
dee5ea7a KS |
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); | |
696d846a MO |
211 | MaybeInstallSigaction(SIGABRT, handler); |
212 | MaybeInstallSigaction(SIGFPE, handler); | |
10189819 | 213 | MaybeInstallSigaction(SIGILL, handler); |
dee5ea7a | 214 | } |
5d3805fc JJ |
215 | |
216 | bool SignalContext::IsStackOverflow() const { | |
217 | // Access at a reasonable offset above SP, or slightly below it (to account | |
218 | // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is | |
219 | // probably a stack overflow. | |
220 | #ifdef __s390__ | |
221 | // On s390, the fault address in siginfo points to start of the page, not | |
222 | // to the precise word that was accessed. Mask off the low bits of sp to | |
223 | // take it into account. | |
224 | bool IsStackAccess = addr >= (sp & ~0xFFF) && addr < sp + 0xFFFF; | |
225 | #else | |
226 | bool IsStackAccess = addr + 512 > sp && addr < sp + 0xFFFF; | |
227 | #endif | |
228 | ||
229 | #if __powerpc__ | |
230 | // Large stack frames can be allocated with e.g. | |
231 | // lis r0,-10000 | |
232 | // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000 | |
233 | // If the store faults then sp will not have been updated, so test above | |
234 | // will not work, because the fault address will be more than just "slightly" | |
235 | // below sp. | |
236 | if (!IsStackAccess && IsAccessibleMemoryRange(pc, 4)) { | |
237 | u32 inst = *(unsigned *)pc; | |
238 | u32 ra = (inst >> 16) & 0x1F; | |
239 | u32 opcd = inst >> 26; | |
240 | u32 xo = (inst >> 1) & 0x3FF; | |
241 | // Check for store-with-update to sp. The instructions we accept are: | |
242 | // stbu rs,d(ra) stbux rs,ra,rb | |
243 | // sthu rs,d(ra) sthux rs,ra,rb | |
244 | // stwu rs,d(ra) stwux rs,ra,rb | |
245 | // stdu rs,ds(ra) stdux rs,ra,rb | |
246 | // where ra is r1 (the stack pointer). | |
247 | if (ra == 1 && | |
248 | (opcd == 39 || opcd == 45 || opcd == 37 || opcd == 62 || | |
249 | (opcd == 31 && (xo == 247 || xo == 439 || xo == 183 || xo == 181)))) | |
250 | IsStackAccess = true; | |
251 | } | |
252 | #endif // __powerpc__ | |
253 | ||
254 | // We also check si_code to filter out SEGV caused by something else other | |
255 | // then hitting the guard page or unmapped memory, like, for example, | |
256 | // unaligned memory access. | |
257 | auto si = static_cast<const siginfo_t *>(siginfo); | |
258 | return IsStackAccess && | |
259 | (si->si_code == si_SEGV_MAPERR || si->si_code == si_SEGV_ACCERR); | |
260 | } | |
261 | ||
dee5ea7a KS |
262 | #endif // SANITIZER_GO |
263 | ||
866e32ad KS |
264 | bool IsAccessibleMemoryRange(uptr beg, uptr size) { |
265 | uptr page_size = GetPageSizeCached(); | |
266 | // Checking too large memory ranges is slow. | |
267 | CHECK_LT(size, page_size * 10); | |
268 | int sock_pair[2]; | |
269 | if (pipe(sock_pair)) | |
270 | return false; | |
271 | uptr bytes_written = | |
272 | internal_write(sock_pair[1], reinterpret_cast<void *>(beg), size); | |
273 | int write_errno; | |
274 | bool result; | |
275 | if (internal_iserror(bytes_written, &write_errno)) { | |
276 | CHECK_EQ(EFAULT, write_errno); | |
277 | result = false; | |
278 | } else { | |
279 | result = (bytes_written == size); | |
280 | } | |
281 | internal_close(sock_pair[0]); | |
282 | internal_close(sock_pair[1]); | |
283 | return result; | |
284 | } | |
285 | ||
696d846a MO |
286 | void PrepareForSandboxing(__sanitizer_sandbox_arguments *args) { |
287 | // Some kinds of sandboxes may forbid filesystem access, so we won't be able | |
288 | // to read the file mappings from /proc/self/maps. Luckily, neither the | |
289 | // process will be able to load additional libraries, so it's fine to use the | |
290 | // cached mappings. | |
291 | MemoryMappingLayout::CacheMemoryMappings(); | |
292 | // Same for /proc/self/exe in the symbolizer. | |
293 | #if !SANITIZER_GO | |
294 | Symbolizer::GetOrInit()->PrepareForSandboxing(); | |
696d846a MO |
295 | #endif |
296 | } | |
297 | ||
55aea9f5 | 298 | #if SANITIZER_ANDROID || SANITIZER_GO |
696d846a MO |
299 | int GetNamedMappingFd(const char *name, uptr size) { |
300 | return -1; | |
301 | } | |
302 | #else | |
303 | int GetNamedMappingFd(const char *name, uptr size) { | |
304 | if (!common_flags()->decorate_proc_maps) | |
305 | return -1; | |
306 | char shmname[200]; | |
307 | CHECK(internal_strlen(name) < sizeof(shmname) - 10); | |
308 | internal_snprintf(shmname, sizeof(shmname), "%zu [%s]", internal_getpid(), | |
309 | name); | |
310 | int fd = shm_open(shmname, O_RDWR | O_CREAT | O_TRUNC, S_IRWXU); | |
311 | CHECK_GE(fd, 0); | |
312 | int res = internal_ftruncate(fd, size); | |
313 | CHECK_EQ(0, res); | |
314 | res = shm_unlink(shmname); | |
315 | CHECK_EQ(0, res); | |
316 | return fd; | |
317 | } | |
318 | #endif | |
319 | ||
320 | void *MmapFixedNoReserve(uptr fixed_addr, uptr size, const char *name) { | |
321 | int fd = name ? GetNamedMappingFd(name, size) : -1; | |
322 | unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE; | |
323 | if (fd == -1) flags |= MAP_ANON; | |
324 | ||
325 | uptr PageSize = GetPageSizeCached(); | |
326 | uptr p = internal_mmap((void *)(fixed_addr & ~(PageSize - 1)), | |
327 | RoundUpTo(size, PageSize), PROT_READ | PROT_WRITE, | |
328 | flags, fd, 0); | |
329 | int reserrno; | |
330 | if (internal_iserror(p, &reserrno)) | |
331 | Report("ERROR: %s failed to " | |
332 | "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n", | |
333 | SanitizerToolName, size, size, fixed_addr, reserrno); | |
334 | IncreaseTotalMmap(size); | |
335 | return (void *)p; | |
336 | } | |
337 | ||
10189819 | 338 | void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) { |
696d846a MO |
339 | int fd = name ? GetNamedMappingFd(name, size) : -1; |
340 | unsigned flags = MAP_PRIVATE | MAP_FIXED | MAP_NORESERVE; | |
341 | if (fd == -1) flags |= MAP_ANON; | |
342 | ||
343 | return (void *)internal_mmap((void *)fixed_addr, size, PROT_NONE, flags, fd, | |
344 | 0); | |
345 | } | |
55aea9f5 | 346 | |
10189819 MO |
347 | void *MmapNoAccess(uptr size) { |
348 | unsigned flags = MAP_PRIVATE | MAP_ANON | MAP_NORESERVE; | |
349 | return (void *)internal_mmap(nullptr, size, PROT_NONE, flags, -1, 0); | |
350 | } | |
351 | ||
55aea9f5 MO |
352 | // This function is defined elsewhere if we intercepted pthread_attr_getstack. |
353 | extern "C" { | |
354 | SANITIZER_WEAK_ATTRIBUTE int | |
355 | real_pthread_attr_getstack(void *attr, void **addr, size_t *size); | |
356 | } // extern "C" | |
357 | ||
358 | int my_pthread_attr_getstack(void *attr, void **addr, uptr *size) { | |
359 | #if !SANITIZER_GO && !SANITIZER_MAC | |
360 | if (&real_pthread_attr_getstack) | |
361 | return real_pthread_attr_getstack((pthread_attr_t *)attr, addr, | |
362 | (size_t *)size); | |
363 | #endif | |
364 | return pthread_attr_getstack((pthread_attr_t *)attr, addr, (size_t *)size); | |
365 | } | |
366 | ||
367 | #if !SANITIZER_GO | |
368 | void AdjustStackSize(void *attr_) { | |
369 | pthread_attr_t *attr = (pthread_attr_t *)attr_; | |
370 | uptr stackaddr = 0; | |
371 | uptr stacksize = 0; | |
372 | my_pthread_attr_getstack(attr, (void**)&stackaddr, &stacksize); | |
373 | // GLibC will return (0 - stacksize) as the stack address in the case when | |
374 | // stacksize is set, but stackaddr is not. | |
375 | bool stack_set = (stackaddr != 0) && (stackaddr + stacksize != 0); | |
376 | // We place a lot of tool data into TLS, account for that. | |
377 | const uptr minstacksize = GetTlsSize() + 128*1024; | |
378 | if (stacksize < minstacksize) { | |
379 | if (!stack_set) { | |
380 | if (stacksize != 0) { | |
381 | VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize, | |
382 | minstacksize); | |
383 | pthread_attr_setstacksize(attr, minstacksize); | |
384 | } | |
385 | } else { | |
386 | Printf("Sanitizer: pre-allocated stack size is insufficient: " | |
387 | "%zu < %zu\n", stacksize, minstacksize); | |
388 | Printf("Sanitizer: pthread_create is likely to fail.\n"); | |
389 | } | |
390 | } | |
391 | } | |
392 | #endif // !SANITIZER_GO | |
393 | ||
10189819 MO |
394 | pid_t StartSubprocess(const char *program, const char *const argv[], |
395 | fd_t stdin_fd, fd_t stdout_fd, fd_t stderr_fd) { | |
396 | auto file_closer = at_scope_exit([&] { | |
397 | if (stdin_fd != kInvalidFd) { | |
398 | internal_close(stdin_fd); | |
399 | } | |
400 | if (stdout_fd != kInvalidFd) { | |
401 | internal_close(stdout_fd); | |
402 | } | |
403 | if (stderr_fd != kInvalidFd) { | |
404 | internal_close(stderr_fd); | |
405 | } | |
406 | }); | |
407 | ||
408 | int pid = internal_fork(); | |
409 | ||
410 | if (pid < 0) { | |
411 | int rverrno; | |
412 | if (internal_iserror(pid, &rverrno)) { | |
413 | Report("WARNING: failed to fork (errno %d)\n", rverrno); | |
414 | } | |
415 | return pid; | |
416 | } | |
417 | ||
418 | if (pid == 0) { | |
419 | // Child subprocess | |
420 | if (stdin_fd != kInvalidFd) { | |
421 | internal_close(STDIN_FILENO); | |
422 | internal_dup2(stdin_fd, STDIN_FILENO); | |
423 | internal_close(stdin_fd); | |
424 | } | |
425 | if (stdout_fd != kInvalidFd) { | |
426 | internal_close(STDOUT_FILENO); | |
427 | internal_dup2(stdout_fd, STDOUT_FILENO); | |
428 | internal_close(stdout_fd); | |
429 | } | |
430 | if (stderr_fd != kInvalidFd) { | |
431 | internal_close(STDERR_FILENO); | |
432 | internal_dup2(stderr_fd, STDERR_FILENO); | |
433 | internal_close(stderr_fd); | |
434 | } | |
435 | ||
436 | for (int fd = sysconf(_SC_OPEN_MAX); fd > 2; fd--) internal_close(fd); | |
437 | ||
438 | execv(program, const_cast<char **>(&argv[0])); | |
439 | internal__exit(1); | |
440 | } | |
441 | ||
442 | return pid; | |
443 | } | |
444 | ||
445 | bool IsProcessRunning(pid_t pid) { | |
446 | int process_status; | |
447 | uptr waitpid_status = internal_waitpid(pid, &process_status, WNOHANG); | |
448 | int local_errno; | |
449 | if (internal_iserror(waitpid_status, &local_errno)) { | |
450 | VReport(1, "Waiting on the process failed (errno %d).\n", local_errno); | |
451 | return false; | |
452 | } | |
453 | return waitpid_status == 0; | |
454 | } | |
455 | ||
456 | int WaitForProcess(pid_t pid) { | |
457 | int process_status; | |
458 | uptr waitpid_status = internal_waitpid(pid, &process_status, 0); | |
459 | int local_errno; | |
460 | if (internal_iserror(waitpid_status, &local_errno)) { | |
461 | VReport(1, "Waiting on the process failed (errno %d).\n", local_errno); | |
462 | return -1; | |
463 | } | |
464 | return process_status; | |
465 | } | |
466 | ||
5d3805fc JJ |
467 | bool IsStateDetached(int state) { |
468 | return state == PTHREAD_CREATE_DETACHED; | |
469 | } | |
470 | ||
696d846a | 471 | } // namespace __sanitizer |
ef1b3fda | 472 | |
696d846a | 473 | #endif // SANITIZER_POSIX |