1 //===-- sanitizer_posix_libcdep.cpp ---------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and ThreadSanitizer
10 // run-time libraries and implements libc-dependent POSIX-specific functions
11 // from sanitizer_libc.h.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_platform.h"
18 #include "sanitizer_common.h"
19 #include "sanitizer_flags.h"
20 #include "sanitizer_platform_limits_netbsd.h"
21 #include "sanitizer_platform_limits_posix.h"
22 #include "sanitizer_platform_limits_solaris.h"
23 #include "sanitizer_posix.h"
24 #include "sanitizer_procmaps.h"
32 #include <sys/resource.h>
35 #include <sys/types.h>
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.
43 #define MAP_NORESERVE 0
46 typedef void (*sa_sigaction_t
)(int, siginfo_t
*, void *);
48 namespace __sanitizer
{
54 uptr
GetThreadSelf() {
55 return (uptr
)pthread_self();
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 internal_madvise(beg_aligned
, end_aligned
- beg_aligned
,
64 SANITIZER_MADVISE_DONTNEED
);
67 void SetShadowRegionHugePageMode(uptr addr
, uptr size
) {
68 #ifdef MADV_NOHUGEPAGE // May not be defined on old systems.
69 if (common_flags()->no_huge_pages_for_shadow
)
70 internal_madvise(addr
, size
, MADV_NOHUGEPAGE
);
72 internal_madvise(addr
, size
, MADV_HUGEPAGE
);
73 #endif // MADV_NOHUGEPAGE
76 bool DontDumpShadowMemory(uptr addr
, uptr length
) {
77 #if defined(MADV_DONTDUMP)
78 return internal_madvise(addr
, length
, MADV_DONTDUMP
) == 0;
79 #elif defined(MADV_NOCORE)
80 return internal_madvise(addr
, length
, MADV_NOCORE
) == 0;
83 #endif // MADV_DONTDUMP
86 static rlim_t
getlim(int res
) {
88 CHECK_EQ(0, getrlimit(res
, &rlim
));
92 static void setlim(int res
, rlim_t lim
) {
94 if (getrlimit(res
, const_cast<struct rlimit
*>(&rlim
))) {
95 Report("ERROR: %s getrlimit() failed %d\n", SanitizerToolName
, errno
);
99 if (setrlimit(res
, const_cast<struct rlimit
*>(&rlim
))) {
100 Report("ERROR: %s setrlimit() failed %d\n", SanitizerToolName
, errno
);
105 void DisableCoreDumperIfNecessary() {
106 if (common_flags()->disable_coredump
) {
107 setlim(RLIMIT_CORE
, 0);
111 bool StackSizeIsUnlimited() {
112 rlim_t stack_size
= getlim(RLIMIT_STACK
);
113 return (stack_size
== RLIM_INFINITY
);
116 void SetStackSizeLimitInBytes(uptr limit
) {
117 setlim(RLIMIT_STACK
, (rlim_t
)limit
);
118 CHECK(!StackSizeIsUnlimited());
121 bool AddressSpaceIsUnlimited() {
122 rlim_t as_size
= getlim(RLIMIT_AS
);
123 return (as_size
== RLIM_INFINITY
);
126 void SetAddressSpaceUnlimited() {
127 setlim(RLIMIT_AS
, RLIM_INFINITY
);
128 CHECK(AddressSpaceIsUnlimited());
133 // If we are handling SIGABRT, unhandle it first.
134 // TODO(vitalybuka): Check if handler belongs to sanitizer.
135 if (GetHandleSignalMode(SIGABRT
) != kHandleSignalNo
) {
136 struct sigaction sigact
;
137 internal_memset(&sigact
, 0, sizeof(sigact
));
138 sigact
.sa_handler
= SIG_DFL
;
139 internal_sigaction(SIGABRT
, &sigact
, nullptr);
146 int Atexit(void (*function
)(void)) {
148 return atexit(function
);
154 bool CreateDir(const char *pathname
) { return mkdir(pathname
, 0755) == 0; }
156 bool SupportsColoredOutput(fd_t fd
) {
157 return isatty(fd
) != 0;
161 // TODO(glider): different tools may require different altstack size.
162 static uptr
GetAltStackSize() {
163 // Note: since GLIBC_2.31, SIGSTKSZ may be a function call, so this may be
164 // more costly that you think. However GetAltStackSize is only call 2-3 times
165 // per thread so don't cache the evaluation.
169 void SetAlternateSignalStack() {
170 stack_t altstack
, oldstack
;
171 CHECK_EQ(0, sigaltstack(nullptr, &oldstack
));
172 // If the alternate stack is already in place, do nothing.
173 // Android always sets an alternate stack, but it's too small for us.
174 if (!SANITIZER_ANDROID
&& !(oldstack
.ss_flags
& SS_DISABLE
)) return;
175 // TODO(glider): the mapped stack should have the MAP_STACK flag in the
176 // future. It is not required by man 2 sigaltstack now (they're using
178 altstack
.ss_size
= GetAltStackSize();
179 altstack
.ss_sp
= (char *)MmapOrDie(altstack
.ss_size
, __func__
);
180 altstack
.ss_flags
= 0;
181 CHECK_EQ(0, sigaltstack(&altstack
, nullptr));
184 void UnsetAlternateSignalStack() {
185 stack_t altstack
, oldstack
;
186 altstack
.ss_sp
= nullptr;
187 altstack
.ss_flags
= SS_DISABLE
;
188 altstack
.ss_size
= GetAltStackSize(); // Some sane value required on Darwin.
189 CHECK_EQ(0, sigaltstack(&altstack
, &oldstack
));
190 UnmapOrDie(oldstack
.ss_sp
, oldstack
.ss_size
);
193 static void MaybeInstallSigaction(int signum
,
194 SignalHandlerType handler
) {
195 if (GetHandleSignalMode(signum
) == kHandleSignalNo
) return;
197 struct sigaction sigact
;
198 internal_memset(&sigact
, 0, sizeof(sigact
));
199 sigact
.sa_sigaction
= (sa_sigaction_t
)handler
;
200 // Do not block the signal from being received in that signal's handler.
201 // Clients are responsible for handling this correctly.
202 sigact
.sa_flags
= SA_SIGINFO
| SA_NODEFER
;
203 if (common_flags()->use_sigaltstack
) sigact
.sa_flags
|= SA_ONSTACK
;
204 CHECK_EQ(0, internal_sigaction(signum
, &sigact
, nullptr));
205 VReport(1, "Installed the sigaction for signal %d\n", signum
);
208 void InstallDeadlySignalHandlers(SignalHandlerType handler
) {
209 // Set the alternate signal stack for the main thread.
210 // This will cause SetAlternateSignalStack to be called twice, but the stack
211 // will be actually set only once.
212 if (common_flags()->use_sigaltstack
) SetAlternateSignalStack();
213 MaybeInstallSigaction(SIGSEGV
, handler
);
214 MaybeInstallSigaction(SIGBUS
, handler
);
215 MaybeInstallSigaction(SIGABRT
, handler
);
216 MaybeInstallSigaction(SIGFPE
, handler
);
217 MaybeInstallSigaction(SIGILL
, handler
);
218 MaybeInstallSigaction(SIGTRAP
, handler
);
221 bool SignalContext::IsStackOverflow() const {
222 // Access at a reasonable offset above SP, or slightly below it (to account
223 // for x86_64 or PowerPC redzone, ARM push of multiple registers, etc) is
224 // probably a stack overflow.
226 // On s390, the fault address in siginfo points to start of the page, not
227 // to the precise word that was accessed. Mask off the low bits of sp to
228 // take it into account.
229 bool IsStackAccess
= addr
>= (sp
& ~0xFFF) && addr
< sp
+ 0xFFFF;
231 // Let's accept up to a page size away from top of stack. Things like stack
232 // probing can trigger accesses with such large offsets.
233 bool IsStackAccess
= addr
+ GetPageSizeCached() > sp
&& addr
< sp
+ 0xFFFF;
237 // Large stack frames can be allocated with e.g.
239 // stdux r1,r1,r0 # store sp to [sp-10000] and update sp by -10000
240 // If the store faults then sp will not have been updated, so test above
241 // will not work, because the fault address will be more than just "slightly"
243 if (!IsStackAccess
&& IsAccessibleMemoryRange(pc
, 4)) {
244 u32 inst
= *(unsigned *)pc
;
245 u32 ra
= (inst
>> 16) & 0x1F;
246 u32 opcd
= inst
>> 26;
247 u32 xo
= (inst
>> 1) & 0x3FF;
248 // Check for store-with-update to sp. The instructions we accept are:
249 // stbu rs,d(ra) stbux rs,ra,rb
250 // sthu rs,d(ra) sthux rs,ra,rb
251 // stwu rs,d(ra) stwux rs,ra,rb
252 // stdu rs,ds(ra) stdux rs,ra,rb
253 // where ra is r1 (the stack pointer).
255 (opcd
== 39 || opcd
== 45 || opcd
== 37 || opcd
== 62 ||
256 (opcd
== 31 && (xo
== 247 || xo
== 439 || xo
== 183 || xo
== 181))))
257 IsStackAccess
= true;
259 #endif // __powerpc__
261 // We also check si_code to filter out SEGV caused by something else other
262 // then hitting the guard page or unmapped memory, like, for example,
263 // unaligned memory access.
264 auto si
= static_cast<const siginfo_t
*>(siginfo
);
265 return IsStackAccess
&&
266 (si
->si_code
== si_SEGV_MAPERR
|| si
->si_code
== si_SEGV_ACCERR
);
269 #endif // SANITIZER_GO
271 bool IsAccessibleMemoryRange(uptr beg
, uptr size
) {
272 uptr page_size
= GetPageSizeCached();
273 // Checking too large memory ranges is slow.
274 CHECK_LT(size
, page_size
* 10);
279 internal_write(sock_pair
[1], reinterpret_cast<void *>(beg
), size
);
282 if (internal_iserror(bytes_written
, &write_errno
)) {
283 CHECK_EQ(EFAULT
, write_errno
);
286 result
= (bytes_written
== size
);
288 internal_close(sock_pair
[0]);
289 internal_close(sock_pair
[1]);
293 void PlatformPrepareForSandboxing(void *args
) {
294 // Some kinds of sandboxes may forbid filesystem access, so we won't be able
295 // to read the file mappings from /proc/self/maps. Luckily, neither the
296 // process will be able to load additional libraries, so it's fine to use the
298 MemoryMappingLayout::CacheMemoryMappings();
301 static bool MmapFixed(uptr fixed_addr
, uptr size
, int additional_flags
,
303 size
= RoundUpTo(size
, GetPageSizeCached());
304 fixed_addr
= RoundDownTo(fixed_addr
, GetPageSizeCached());
306 MmapNamed((void *)fixed_addr
, size
, PROT_READ
| PROT_WRITE
,
307 MAP_PRIVATE
| MAP_FIXED
| additional_flags
| MAP_ANON
, name
);
309 if (internal_iserror(p
, &reserrno
)) {
310 Report("ERROR: %s failed to "
311 "allocate 0x%zx (%zd) bytes at address %zx (errno: %d)\n",
312 SanitizerToolName
, size
, size
, fixed_addr
, reserrno
);
315 IncreaseTotalMmap(size
);
319 bool MmapFixedNoReserve(uptr fixed_addr
, uptr size
, const char *name
) {
320 return MmapFixed(fixed_addr
, size
, MAP_NORESERVE
, name
);
323 bool MmapFixedSuperNoReserve(uptr fixed_addr
, uptr size
, const char *name
) {
324 #if SANITIZER_FREEBSD
325 if (common_flags()->no_huge_pages_for_shadow
)
326 return MmapFixedNoReserve(fixed_addr
, size
, name
);
327 // MAP_NORESERVE is implicit with FreeBSD
328 return MmapFixed(fixed_addr
, size
, MAP_ALIGNED_SUPER
, name
);
330 bool r
= MmapFixedNoReserve(fixed_addr
, size
, name
);
332 SetShadowRegionHugePageMode(fixed_addr
, size
);
337 uptr
ReservedAddressRange::Init(uptr size
, const char *name
, uptr fixed_addr
) {
338 base_
= fixed_addr
? MmapFixedNoAccess(fixed_addr
, size
, name
)
339 : MmapNoAccess(size
);
342 (void)os_handle_
; // unsupported
343 return reinterpret_cast<uptr
>(base_
);
346 // Uses fixed_addr for now.
347 // Will use offset instead once we've implemented this function for real.
348 uptr
ReservedAddressRange::Map(uptr fixed_addr
, uptr size
, const char *name
) {
349 return reinterpret_cast<uptr
>(
350 MmapFixedOrDieOnFatalError(fixed_addr
, size
, name
));
353 uptr
ReservedAddressRange::MapOrDie(uptr fixed_addr
, uptr size
,
355 return reinterpret_cast<uptr
>(MmapFixedOrDie(fixed_addr
, size
, name
));
358 void ReservedAddressRange::Unmap(uptr addr
, uptr size
) {
359 CHECK_LE(size
, size_
);
360 if (addr
== reinterpret_cast<uptr
>(base_
))
361 // If we unmap the whole range, just null out the base.
362 base_
= (size
== size_
) ? nullptr : reinterpret_cast<void*>(addr
+ size
);
364 CHECK_EQ(addr
+ size
, reinterpret_cast<uptr
>(base_
) + size_
);
366 UnmapOrDie(reinterpret_cast<void*>(addr
), size
);
369 void *MmapFixedNoAccess(uptr fixed_addr
, uptr size
, const char *name
) {
370 return (void *)MmapNamed((void *)fixed_addr
, size
, PROT_NONE
,
371 MAP_PRIVATE
| MAP_FIXED
| MAP_NORESERVE
| MAP_ANON
,
375 void *MmapNoAccess(uptr size
) {
376 unsigned flags
= MAP_PRIVATE
| MAP_ANON
| MAP_NORESERVE
;
377 return (void *)internal_mmap(nullptr, size
, PROT_NONE
, flags
, -1, 0);
380 // This function is defined elsewhere if we intercepted pthread_attr_getstack.
382 SANITIZER_WEAK_ATTRIBUTE
int
383 real_pthread_attr_getstack(void *attr
, void **addr
, size_t *size
);
386 int my_pthread_attr_getstack(void *attr
, void **addr
, uptr
*size
) {
387 #if !SANITIZER_GO && !SANITIZER_MAC
388 if (&real_pthread_attr_getstack
)
389 return real_pthread_attr_getstack((pthread_attr_t
*)attr
, addr
,
392 return pthread_attr_getstack((pthread_attr_t
*)attr
, addr
, (size_t *)size
);
396 void AdjustStackSize(void *attr_
) {
397 pthread_attr_t
*attr
= (pthread_attr_t
*)attr_
;
400 my_pthread_attr_getstack(attr
, (void**)&stackaddr
, &stacksize
);
401 // GLibC will return (0 - stacksize) as the stack address in the case when
402 // stacksize is set, but stackaddr is not.
403 bool stack_set
= (stackaddr
!= 0) && (stackaddr
+ stacksize
!= 0);
404 // We place a lot of tool data into TLS, account for that.
405 const uptr minstacksize
= GetTlsSize() + 128*1024;
406 if (stacksize
< minstacksize
) {
408 if (stacksize
!= 0) {
409 VPrintf(1, "Sanitizer: increasing stacksize %zu->%zu\n", stacksize
,
411 pthread_attr_setstacksize(attr
, minstacksize
);
414 Printf("Sanitizer: pre-allocated stack size is insufficient: "
415 "%zu < %zu\n", stacksize
, minstacksize
);
416 Printf("Sanitizer: pthread_create is likely to fail.\n");
420 #endif // !SANITIZER_GO
422 pid_t
StartSubprocess(const char *program
, const char *const argv
[],
423 const char *const envp
[], fd_t stdin_fd
, fd_t stdout_fd
,
425 auto file_closer
= at_scope_exit([&] {
426 if (stdin_fd
!= kInvalidFd
) {
427 internal_close(stdin_fd
);
429 if (stdout_fd
!= kInvalidFd
) {
430 internal_close(stdout_fd
);
432 if (stderr_fd
!= kInvalidFd
) {
433 internal_close(stderr_fd
);
437 int pid
= internal_fork();
441 if (internal_iserror(pid
, &rverrno
)) {
442 Report("WARNING: failed to fork (errno %d)\n", rverrno
);
449 if (stdin_fd
!= kInvalidFd
) {
450 internal_close(STDIN_FILENO
);
451 internal_dup2(stdin_fd
, STDIN_FILENO
);
452 internal_close(stdin_fd
);
454 if (stdout_fd
!= kInvalidFd
) {
455 internal_close(STDOUT_FILENO
);
456 internal_dup2(stdout_fd
, STDOUT_FILENO
);
457 internal_close(stdout_fd
);
459 if (stderr_fd
!= kInvalidFd
) {
460 internal_close(STDERR_FILENO
);
461 internal_dup2(stderr_fd
, STDERR_FILENO
);
462 internal_close(stderr_fd
);
465 for (int fd
= sysconf(_SC_OPEN_MAX
); fd
> 2; fd
--) internal_close(fd
);
467 internal_execve(program
, const_cast<char **>(&argv
[0]),
468 const_cast<char *const *>(envp
));
475 bool IsProcessRunning(pid_t pid
) {
477 uptr waitpid_status
= internal_waitpid(pid
, &process_status
, WNOHANG
);
479 if (internal_iserror(waitpid_status
, &local_errno
)) {
480 VReport(1, "Waiting on the process failed (errno %d).\n", local_errno
);
483 return waitpid_status
== 0;
486 int WaitForProcess(pid_t pid
) {
488 uptr waitpid_status
= internal_waitpid(pid
, &process_status
, 0);
490 if (internal_iserror(waitpid_status
, &local_errno
)) {
491 VReport(1, "Waiting on the process failed (errno %d).\n", local_errno
);
494 return process_status
;
497 bool IsStateDetached(int state
) {
498 return state
== PTHREAD_CREATE_DETACHED
;
501 } // namespace __sanitizer
503 #endif // SANITIZER_POSIX