Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 1 | //===-- 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 Samsonov | 5bbf829 | 2012-06-05 14:25:27 +0000 | [diff] [blame] | 16 | #include "sanitizer_internal_defs.h" |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 17 | #include "sanitizer_libc.h" |
| 18 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 19 | #include <fcntl.h> |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 20 | #include <sys/mman.h> |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 21 | #include <sys/stat.h> |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 22 | #include <sys/syscall.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
| 26 | namespace __sanitizer { |
| 27 | |
| 28 | void *internal_mmap(void *addr, uptr length, int prot, int flags, |
| 29 | int fd, u64 offset) { |
| 30 | #if __WORDSIZE == 64 |
| 31 | return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
| 32 | #else |
| 33 | return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); |
| 34 | #endif |
| 35 | } |
| 36 | |
Alexey Samsonov | 7ac77d6 | 2012-06-05 09:49:25 +0000 | [diff] [blame] | 37 | int internal_munmap(void *addr, uptr length) { |
| 38 | return syscall(__NR_munmap, addr, length); |
| 39 | } |
| 40 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 41 | int internal_close(fd_t fd) { |
| 42 | return syscall(__NR_close, fd); |
| 43 | } |
| 44 | |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 45 | fd_t internal_open(const char *filename, bool write) { |
| 46 | return syscall(__NR_open, filename, |
Kostya Serebryany | 64166ca | 2012-06-06 14:11:31 +0000 | [diff] [blame^] | 47 | write ? O_WRONLY | O_CREAT /*| O_CLOEXEC*/ : O_RDONLY, 0660); |
Alexey Samsonov | dde1f11 | 2012-06-05 07:05:10 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Alexey Samsonov | 03c8b84 | 2012-06-05 08:32:53 +0000 | [diff] [blame] | 50 | uptr internal_read(fd_t fd, void *buf, uptr count) { |
| 51 | return (uptr)syscall(__NR_read, fd, buf, count); |
| 52 | } |
| 53 | |
| 54 | uptr internal_write(fd_t fd, const void *buf, uptr count) { |
| 55 | return (uptr)syscall(__NR_write, fd, buf, count); |
| 56 | } |
| 57 | |
Alexey Samsonov | ca2b5d7 | 2012-06-06 07:30:33 +0000 | [diff] [blame] | 58 | uptr internal_filesize(fd_t fd) { |
| 59 | struct stat st = {}; |
| 60 | if (syscall(__NR_fstat, fd, &st)) |
| 61 | return -1; |
| 62 | return (uptr)st.st_size; |
| 63 | } |
| 64 | |
| 65 | int internal_dup2(int oldfd, int newfd) { |
| 66 | return syscall(__NR_dup2, oldfd, newfd); |
| 67 | } |
| 68 | |
Alexey Samsonov | 2c5fc3b | 2012-06-04 14:27:50 +0000 | [diff] [blame] | 69 | } // namespace __sanitizer |
| 70 | |
| 71 | #endif // __linux__ |