blob: 9cd336efea030d73d0cac7fd05b5634a096279a6 [file] [log] [blame]
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +00001//===-- sanitizer_linux.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 linux-specific functions from
12// sanitizer_libc.h.
13//===----------------------------------------------------------------------===//
14#ifdef __linux__
15
Alexey Samsonov28a98952012-06-07 06:15:12 +000016#include "sanitizer_common.h"
Alexey Samsonov5bbf8292012-06-05 14:25:27 +000017#include "sanitizer_internal_defs.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000018#include "sanitizer_libc.h"
Alexey Samsonov28a98952012-06-07 06:15:12 +000019#include "sanitizer_procmaps.h"
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000020
Alexey Samsonovdde1f112012-06-05 07:05:10 +000021#include <fcntl.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000022#include <sys/mman.h>
Alexey Samsonovdde1f112012-06-05 07:05:10 +000023#include <sys/stat.h>
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +000024#include <sys/syscall.h>
25#include <sys/types.h>
26#include <unistd.h>
27
28namespace __sanitizer {
29
30void *internal_mmap(void *addr, uptr length, int prot, int flags,
31 int fd, u64 offset) {
32#if __WORDSIZE == 64
33 return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset);
34#else
35 return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset);
36#endif
37}
38
Alexey Samsonov7ac77d62012-06-05 09:49:25 +000039int internal_munmap(void *addr, uptr length) {
40 return syscall(__NR_munmap, addr, length);
41}
42
Alexey Samsonov03c8b842012-06-05 08:32:53 +000043int internal_close(fd_t fd) {
44 return syscall(__NR_close, fd);
45}
46
Alexey Samsonovdde1f112012-06-05 07:05:10 +000047fd_t internal_open(const char *filename, bool write) {
48 return syscall(__NR_open, filename,
Kostya Serebryany64166ca2012-06-06 14:11:31 +000049 write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660);
Alexey Samsonovdde1f112012-06-05 07:05:10 +000050}
51
Alexey Samsonov03c8b842012-06-05 08:32:53 +000052uptr internal_read(fd_t fd, void *buf, uptr count) {
53 return (uptr)syscall(__NR_read, fd, buf, count);
54}
55
56uptr internal_write(fd_t fd, const void *buf, uptr count) {
57 return (uptr)syscall(__NR_write, fd, buf, count);
58}
59
Alexey Samsonovca2b5d72012-06-06 07:30:33 +000060uptr internal_filesize(fd_t fd) {
61 struct stat st = {};
62 if (syscall(__NR_fstat, fd, &st))
63 return -1;
64 return (uptr)st.st_size;
65}
66
67int internal_dup2(int oldfd, int newfd) {
68 return syscall(__NR_dup2, oldfd, newfd);
69}
70
Alexey Samsonov28a98952012-06-07 06:15:12 +000071// ----------------- ProcessMaps implementation.
72ProcessMaps::ProcessMaps() {
73 proc_self_maps_buff_len_ =
74 ReadFileToBuffer("/proc/self/maps", &proc_self_maps_buff_,
75 &proc_self_maps_buff_mmaped_size_, 1 << 26);
76 CHECK(proc_self_maps_buff_len_ > 0);
77 // internal_write(2, proc_self_maps_buff_, proc_self_maps_buff_len_);
78 Reset();
79}
80
81ProcessMaps::~ProcessMaps() {
82 UnmapOrDie(proc_self_maps_buff_, proc_self_maps_buff_mmaped_size_);
83}
84
85void ProcessMaps::Reset() {
86 current_ = proc_self_maps_buff_;
87}
88
89bool ProcessMaps::Next(uptr *start, uptr *end, uptr *offset,
90 char filename[], uptr filename_size) {
91 char *last = proc_self_maps_buff_ + proc_self_maps_buff_len_;
92 if (current_ >= last) return false;
93 int consumed = 0;
94 char flags[10];
95 int major, minor;
96 uptr inode;
97 uptr dummy;
98 if (!start) start = &dummy;
99 if (!end) end = &dummy;
100 if (!offset) offset = &dummy;
101 char *next_line = (char*)internal_memchr(current_, '\n', last - current_);
102 if (next_line == 0)
103 next_line = last;
104 if (internal_sscanf(current_, "%lx-%lx %4s %lx %x:%x %ld %n",
105 start, end, flags, offset, &major, &minor,
106 &inode, &consumed) != 7)
107 return false;
108 current_ += consumed;
109 // Skip spaces.
110 while (current_ < next_line && *current_ == ' ')
111 current_++;
112 // Fill in the filename.
113 uptr i = 0;
114 while (current_ < next_line) {
115 if (filename && i < filename_size - 1)
116 filename[i++] = *current_;
117 current_++;
118 }
119 if (filename && i < filename_size)
120 filename[i] = 0;
121 current_ = next_line + 1;
122 return true;
123}
124
125// Gets the object name and the offset by walking ProcessMaps.
126bool ProcessMaps::GetObjectNameAndOffset(uptr addr, uptr *offset,
127 char filename[],
128 uptr filename_size) {
129 return IterateForObjectNameAndOffset(addr, offset, filename, filename_size);
130}
131
Alexey Samsonov2c5fc3b2012-06-04 14:27:50 +0000132} // namespace __sanitizer
133
134#endif // __linux__