sanitizer_linux.cc revision ed996f79710f532bf231537e44d5c8c9c9d57e8d
1ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov//===-- sanitizer_linux.cc ------------------------------------------------===// 2ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// 3ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// The LLVM Compiler Infrastructure 4ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// 5ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// This file is distributed under the University of Illinois Open Source 6ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// License. See LICENSE.TXT for details. 7ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// 8ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov//===----------------------------------------------------------------------===// 9ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// 10ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// This file is shared between AddressSanitizer and ThreadSanitizer 11ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// run-time libraries and implements linux-specific functions from 12ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov// sanitizer_libc.h. 13ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov//===----------------------------------------------------------------------===// 14ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#ifdef __linux__ 15ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov 166895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov#include "sanitizer_common.h" 1794b5036ee6ba866e1702848855b6d687d1e70afaAlexey Samsonov#include "sanitizer_internal_defs.h" 18ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#include "sanitizer_libc.h" 196895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov#include "sanitizer_procmaps.h" 20ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov 21c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonov#include <fcntl.h> 22e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov#include <pthread.h> 23ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#include <sys/mman.h> 24e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov#include <sys/resource.h> 25c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonov#include <sys/stat.h> 26ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#include <sys/syscall.h> 27e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov#include <sys/time.h> 28ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#include <sys/types.h> 29ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#include <unistd.h> 30ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov 31ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonovnamespace __sanitizer { 32ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov 33e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov// --------------- sanitizer_libc.h 34ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonovvoid *internal_mmap(void *addr, uptr length, int prot, int flags, 35ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov int fd, u64 offset) { 36ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#if __WORDSIZE == 64 37ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); 38ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#else 39ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); 40ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#endif 41ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov} 42ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov 431f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonovint internal_munmap(void *addr, uptr length) { 441f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov return syscall(__NR_munmap, addr, length); 451f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov} 461f11d31faa5ed89b74f7d543b1182fe8de198be5Alexey Samsonov 47a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonovint internal_close(fd_t fd) { 48a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov return syscall(__NR_close, fd); 49a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov} 50a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov 51c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonovfd_t internal_open(const char *filename, bool write) { 52c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonov return syscall(__NR_open, filename, 539b8a9c1b1ce4659457178ff4c0838ac1b35ca9dcKostya Serebryany write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660); 54c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonov} 55c5d465136b911bf925f2a631e2b79f1c03e8a1b0Alexey Samsonov 56a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonovuptr internal_read(fd_t fd, void *buf, uptr count) { 57a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov return (uptr)syscall(__NR_read, fd, buf, count); 58a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov} 59a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov 60a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonovuptr internal_write(fd_t fd, const void *buf, uptr count) { 61a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov return (uptr)syscall(__NR_write, fd, buf, count); 62a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov} 63a56aefd2e01940fcf88d1426f9de3d5e4b1ee203Alexey Samsonov 648e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonovuptr internal_filesize(fd_t fd) { 658e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov struct stat st = {}; 668e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov if (syscall(__NR_fstat, fd, &st)) 678e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov return -1; 688e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov return (uptr)st.st_size; 698e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov} 708e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov 718e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonovint internal_dup2(int oldfd, int newfd) { 728e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov return syscall(__NR_dup2, oldfd, newfd); 738e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov} 748e820fcf7aafeb8101322182d742fcf99255d972Alexey Samsonov 75e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov// ----------------- sanitizer_common.h 76ed996f79710f532bf231537e44d5c8c9c9d57e8dAlexey Samsonovvoid GetThreadStackTopAndBottom(bool at_initialization, uptr *stack_top, 77e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov uptr *stack_bottom) { 78e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov static const uptr kMaxThreadStackSize = 256 * (1 << 20); // 256M 79e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov CHECK(stack_top); 80e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov CHECK(stack_bottom); 81ed996f79710f532bf231537e44d5c8c9c9d57e8dAlexey Samsonov if (at_initialization) { 82e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov // This is the main thread. Libpthread may not be initialized yet. 83e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov struct rlimit rl; 84e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov CHECK(getrlimit(RLIMIT_STACK, &rl) == 0); 85e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov 86e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov // Find the mapping that contains a stack variable. 87e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov ProcessMaps proc_maps; 88e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov uptr start, end, offset; 89e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov uptr prev_end = 0; 90e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov while (proc_maps.Next(&start, &end, &offset, 0, 0)) { 91e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov if ((uptr)&rl < end) 92e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov break; 93e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov prev_end = end; 94e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov } 95e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov CHECK((uptr)&rl >= start && (uptr)&rl < end); 96e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov 97e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov // Get stacksize from rlimit, but clip it so that it does not overlap 98e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov // with other mappings. 99e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov uptr stacksize = rl.rlim_cur; 100e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov if (stacksize > end - prev_end) 101e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov stacksize = end - prev_end; 102e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov // When running with unlimited stack size, we still want to set some limit. 103e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov // The unlimited stack size is caused by 'ulimit -s unlimited'. 104e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov // Also, for some reason, GNU make spawns subprocesses with unlimited stack. 105e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov if (stacksize > kMaxThreadStackSize) 106e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov stacksize = kMaxThreadStackSize; 107e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov *stack_top = end; 108e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov *stack_bottom = end - stacksize; 109e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov return; 110e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov } 111e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov pthread_attr_t attr; 112e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov CHECK(pthread_getattr_np(pthread_self(), &attr) == 0); 113e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov uptr stacksize = 0; 114e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov void *stackaddr = 0; 115e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov pthread_attr_getstack(&attr, &stackaddr, (size_t*)&stacksize); 116e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov pthread_attr_destroy(&attr); 117e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov 118e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov *stack_top = (uptr)stackaddr + stacksize; 119e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov *stack_bottom = (uptr)stackaddr; 120e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov CHECK(stacksize < kMaxThreadStackSize); // Sanity check. 121e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov} 122e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov 123e5931fd7d2a74fd7fb60bd8d7f644cca51a24364Alexey Samsonov// ----------------- sanitizer_procmaps.h 1246895adc39c4e09371154c8037366ad4464163ed0Alexey SamsonovProcessMaps::ProcessMaps() { 1256895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov proc_self_maps_buff_len_ = 1266895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_, 1276895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov &proc_self_maps_buff_mmaped_size_, 1 << 26); 1286895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov CHECK(proc_self_maps_buff_len_ > 0); 1296895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov // internal_write(2, proc_self_maps_buff_, proc_self_maps_buff_len_); 1306895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov Reset(); 1316895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov} 1326895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov 1336895adc39c4e09371154c8037366ad4464163ed0Alexey SamsonovProcessMaps::~ProcessMaps() { 1346895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov UnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_); 1356895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov} 1366895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov 1376895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonovvoid ProcessMaps::Reset() { 1386895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov current_ = proc_self_maps_buff_; 1396895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov} 1406895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov 1416895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonovbool ProcessMaps::Next(uptr *start, uptr *end, uptr *offset, 1426895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov char filename[], uptr filename_size) { 1436895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_; 1446895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (current_ >= last) return false; 1456895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov int consumed = 0; 1466895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov char flags[10]; 1476895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov int major, minor; 1486895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov uptr inode; 1496895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov uptr dummy; 1506895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (!start) start = &dummy; 1516895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (!end) end = &dummy; 1526895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (!offset) offset = &dummy; 1536895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov char *next_line = (char*)internal_memchr(current_, '\n', last - current_); 1546895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (next_line == 0) 1556895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov next_line = last; 1566895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (internal_sscanf(current_, "%lx-%lx %4s %lx %x:%x %ld %n", 1576895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov start, end, flags, offset, &major, &minor, 1586895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov &inode, &consumed) != 7) 1596895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov return false; 1606895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov current_ += consumed; 1616895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov // Skip spaces. 1626895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov while (current_ < next_line && *current_ == ' ') 1636895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov current_++; 1646895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov // Fill in the filename. 1656895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov uptr i = 0; 1666895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov while (current_ < next_line) { 1676895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (filename && i < filename_size - 1) 1686895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov filename[i++] = *current_; 1696895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov current_++; 1706895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov } 1716895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov if (filename && i < filename_size) 1726895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov filename[i] = 0; 1736895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov current_ = next_line + 1; 1746895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov return true; 1756895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov} 1766895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov 1776895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov// Gets the object name and the offset by walking ProcessMaps. 1786895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonovbool ProcessMaps::GetObjectNameAndOffset(uptr addr, uptr *offset, 1796895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov char filename[], 1806895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov uptr filename_size) { 1816895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov return IterateForObjectNameAndOffset(addr, offset, filename, filename_size); 1826895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov} 1836895adc39c4e09371154c8037366ad4464163ed0Alexey Samsonov 184ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov} // namespace __sanitizer 185ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov 186ae4d9caa4f47fa6abcd641719e9f520622940c17Alexey Samsonov#endif // __linux__ 187