Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 1 | //===-- sanitizer_linux.h ---------------------------------------*- C++ -*-===// |
| 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 | // Linux-specific syscall wrappers and classes. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | #ifndef SANITIZER_LINUX_H |
| 14 | #define SANITIZER_LINUX_H |
| 15 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 16 | #include "sanitizer_platform.h" |
| 17 | #if SANITIZER_LINUX |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 18 | #include "sanitizer_common.h" |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 19 | #include "sanitizer_internal_defs.h" |
| 20 | |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 21 | struct link_map; // Opaque type returned by dlopen(). |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 22 | struct sigaltstack; |
| 23 | |
| 24 | namespace __sanitizer { |
| 25 | // Dirent structure for getdents(). Note that this structure is different from |
| 26 | // the one in <dirent.h>, which is used by readdir(). |
| 27 | struct linux_dirent; |
| 28 | |
| 29 | // Syscall wrappers. |
Peter Collingbourne | 9578a3e | 2013-05-08 14:43:49 +0000 | [diff] [blame] | 30 | uptr internal_getdents(fd_t fd, struct linux_dirent *dirp, unsigned int count); |
| 31 | uptr internal_prctl(int option, uptr arg2, uptr arg3, uptr arg4, uptr arg5); |
| 32 | uptr internal_sigaltstack(const struct sigaltstack* ss, |
| 33 | struct sigaltstack* oss); |
Sergey Matveev | cb33910 | 2013-09-02 11:36:19 +0000 | [diff] [blame] | 34 | #ifdef __x86_64__ |
| 35 | uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg, |
| 36 | int *parent_tidptr, void *newtls, int *child_tidptr); |
| 37 | #endif |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 38 | |
| 39 | // This class reads thread IDs from /proc/<pid>/task using only syscalls. |
| 40 | class ThreadLister { |
| 41 | public: |
| 42 | explicit ThreadLister(int pid); |
| 43 | ~ThreadLister(); |
| 44 | // GetNextTID returns -1 if the list of threads is exhausted, or if there has |
| 45 | // been an error. |
| 46 | int GetNextTID(); |
| 47 | void Reset(); |
| 48 | bool error(); |
| 49 | |
| 50 | private: |
| 51 | bool GetDirectoryEntries(); |
| 52 | |
| 53 | int pid_; |
| 54 | int descriptor_; |
Alexey Samsonov | 10f3ab7 | 2013-04-05 07:41:21 +0000 | [diff] [blame] | 55 | InternalScopedBuffer<char> buffer_; |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 56 | bool error_; |
| 57 | struct linux_dirent* entry_; |
| 58 | int bytes_read_; |
| 59 | }; |
Evgeniy Stepanov | b9bf700 | 2013-03-19 09:30:52 +0000 | [diff] [blame] | 60 | |
| 61 | void AdjustStackSizeLinux(void *attr, int verbosity); |
| 62 | |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 63 | // Exposed for testing. |
| 64 | uptr ThreadDescriptorSize(); |
Sergey Matveev | 4c08644 | 2013-05-29 13:07:42 +0000 | [diff] [blame] | 65 | uptr ThreadSelf(); |
| 66 | uptr ThreadSelfOffset(); |
Sergey Matveev | 24323de | 2013-05-07 14:41:43 +0000 | [diff] [blame] | 67 | |
Sergey Matveev | 3de0086 | 2013-05-14 13:24:46 +0000 | [diff] [blame] | 68 | // Matches a library's file name against a base name (stripping path and version |
| 69 | // information). |
| 70 | bool LibraryNameIs(const char *full_name, const char *base_name); |
| 71 | |
Peter Collingbourne | 51c963a | 2013-05-29 12:11:43 +0000 | [diff] [blame] | 72 | // Read the name of the current binary from /proc/self/exe. |
| 73 | uptr ReadBinaryName(/*out*/char *buf, uptr buf_len); |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 74 | // Cache the value of /proc/self/exe. |
| 75 | void CacheBinaryName(); |
Peter Collingbourne | 51c963a | 2013-05-29 12:11:43 +0000 | [diff] [blame] | 76 | |
Peter Collingbourne | 2e75ac9 | 2013-07-29 19:09:49 +0000 | [diff] [blame] | 77 | // Call cb for each region mapped by map. |
| 78 | void ForEachMappedRegion(link_map *map, void (*cb)(const void *, uptr)); |
| 79 | |
Sergey Matveev | 6eb836f | 2013-10-11 12:09:49 +0000 | [diff] [blame^] | 80 | // PTHREAD_DESTRUCTOR_ITERATIONS from glibc. |
| 81 | const uptr kPthreadDestructorIterations = 4; |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 82 | } // namespace __sanitizer |
| 83 | |
Alexey Samsonov | 7847d77 | 2013-09-10 14:36:16 +0000 | [diff] [blame] | 84 | #endif // SANITIZER_LINUX |
Kostya Serebryany | 6fb47af | 2013-02-27 11:22:40 +0000 | [diff] [blame] | 85 | #endif // SANITIZER_LINUX_H |