Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 1 | //===-- asan_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 a part of AddressSanitizer, an address sanity checker. |
| 11 | // |
| 12 | // Linux-specific details. |
| 13 | //===----------------------------------------------------------------------===// |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 14 | #ifdef __linux__ |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 15 | |
| 16 | #include "asan_internal.h" |
| 17 | |
| 18 | #include <sys/mman.h> |
| 19 | #include <sys/syscall.h> |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <fcntl.h> |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
| 23 | |
| 24 | extern char _DYNAMIC[]; |
| 25 | |
| 26 | namespace __asan { |
| 27 | |
| 28 | void *AsanDoesNotSupportStaticLinkage() { |
| 29 | // This will fail to link with -static. |
| 30 | return &_DYNAMIC; |
| 31 | } |
| 32 | |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 33 | void *asan_mmap(void *addr, size_t length, int prot, int flags, |
| 34 | int fd, uint64_t offset) { |
| 35 | # if __WORDSIZE == 64 |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 36 | return (void *)syscall(__NR_mmap, addr, length, prot, flags, fd, offset); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 37 | # else |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 38 | return (void *)syscall(__NR_mmap2, addr, length, prot, flags, fd, offset); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 39 | # endif |
| 40 | } |
| 41 | |
Kostya Serebryany | 6c4bd80 | 2011-12-28 22:58:01 +0000 | [diff] [blame] | 42 | void *AsanMmapSomewhereOrDie(size_t size, const char *mem_type) { |
| 43 | size = RoundUpTo(size, kPageSize); |
| 44 | void *res = asan_mmap(0, size, |
| 45 | PROT_READ | PROT_WRITE, |
| 46 | MAP_PRIVATE | MAP_ANON, -1, 0); |
| 47 | if (res == (void*)-1) { |
| 48 | OutOfMemoryMessageAndDie(mem_type, size); |
| 49 | } |
| 50 | return res; |
| 51 | } |
| 52 | |
| 53 | void AsanUnmapOrDie(void *addr, size_t size) { |
| 54 | if (!addr || !size) return; |
| 55 | int res = syscall(__NR_munmap, addr, size); |
| 56 | if (res != 0) { |
| 57 | Report("Failed to unmap\n"); |
| 58 | ASAN_DIE; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | ssize_t AsanWrite(int fd, const void *buf, size_t count) { |
| 63 | return (ssize_t)syscall(__NR_write, fd, buf, count); |
| 64 | } |
| 65 | |
| 66 | int AsanOpenReadonly(const char* filename) { |
| 67 | return open(filename, O_RDONLY); |
| 68 | } |
| 69 | |
| 70 | ssize_t AsanRead(int fd, void *buf, size_t count) { |
| 71 | return (ssize_t)syscall(__NR_read, fd, buf, count); |
| 72 | } |
| 73 | |
| 74 | int AsanClose(int fd) { |
| 75 | return close(fd); |
Kostya Serebryany | 019b76f | 2011-11-30 01:07:02 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | } // namespace __asan |
Kostya Serebryany | 5dfa4da | 2011-12-01 21:40:52 +0000 | [diff] [blame] | 79 | |
| 80 | #endif // __linux__ |