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