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