| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 1 | //===-- sanitizer_procmaps.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 | // This file is shared between AddressSanitizer and ThreadSanitizer. |
| 11 | // |
| 12 | // Information about the process mappings. |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | #ifndef SANITIZER_PROCMAPS_H |
| 15 | #define SANITIZER_PROCMAPS_H |
| 16 | |
| 17 | #include "sanitizer_internal_defs.h" |
| Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 18 | #include "sanitizer_mutex.h" |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 19 | |
| 20 | namespace __sanitizer { |
| 21 | |
| Evgeniy Stepanov | 95eaa21 | 2013-03-19 14:54:17 +0000 | [diff] [blame] | 22 | #if SANITIZER_WINDOWS |
| Alexey Samsonov | cc62211 | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 23 | class MemoryMappingLayout { |
| Alexey Samsonov | 28d8be2 | 2012-08-27 14:08:53 +0000 | [diff] [blame] | 24 | public: |
| Alexey Samsonov | 384de7c | 2012-08-28 08:27:08 +0000 | [diff] [blame] | 25 | MemoryMappingLayout() {} |
| Alexey Samsonov | cae486c | 2012-08-28 07:22:24 +0000 | [diff] [blame] | 26 | bool GetObjectNameAndOffset(uptr addr, uptr *offset, |
| Alexey Samsonov | 91f833a | 2013-03-13 07:39:25 +0000 | [diff] [blame] | 27 | char filename[], uptr filename_size, |
| 28 | uptr *protection) { |
| Alexey Samsonov | cae486c | 2012-08-28 07:22:24 +0000 | [diff] [blame] | 29 | UNIMPLEMENTED(); |
| Alexey Samsonov | cae486c | 2012-08-28 07:22:24 +0000 | [diff] [blame] | 30 | } |
| Alexey Samsonov | cc62211 | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | #else // _WIN32 |
| Evgeniy Stepanov | 0af6723 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 34 | #if SANITIZER_LINUX |
| Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 35 | struct ProcSelfMapsBuff { |
| 36 | char *data; |
| 37 | uptr mmaped_size; |
| 38 | uptr len; |
| 39 | }; |
| Evgeniy Stepanov | 0af6723 | 2013-03-19 14:33:38 +0000 | [diff] [blame] | 40 | #endif // SANITIZER_LINUX |
| Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 41 | |
| Alexey Samsonov | cc62211 | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 42 | class MemoryMappingLayout { |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 43 | public: |
| Alexander Potapenko | f8109dd | 2013-03-26 10:34:37 +0000 | [diff] [blame^] | 44 | explicit MemoryMappingLayout(bool cache_enabled); |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 45 | bool Next(uptr *start, uptr *end, uptr *offset, |
| Alexey Samsonov | 06d3aa4 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 46 | char filename[], uptr filename_size, uptr *protection); |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 47 | void Reset(); |
| 48 | // Gets the object file name and the offset in that object for a given |
| 49 | // address 'addr'. Returns true on success. |
| 50 | bool GetObjectNameAndOffset(uptr addr, uptr *offset, |
| Alexey Samsonov | 06d3aa4 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 51 | char filename[], uptr filename_size, |
| 52 | uptr *protection); |
| Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 53 | // In some cases, e.g. when running under a sandbox on Linux, ASan is unable |
| 54 | // to obtain the memory mappings. It should fall back to pre-cached data |
| 55 | // instead of aborting. |
| 56 | static void CacheMemoryMappings(); |
| Alexey Samsonov | cc62211 | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 57 | ~MemoryMappingLayout(); |
| Kostya Serebryany | 98390d0 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 58 | |
| Alexey Samsonov | 06d3aa4 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 59 | // Memory protection masks. |
| 60 | static const uptr kProtectionRead = 1; |
| 61 | static const uptr kProtectionWrite = 2; |
| 62 | static const uptr kProtectionExecute = 4; |
| 63 | static const uptr kProtectionShared = 8; |
| 64 | |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 65 | private: |
| Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 66 | void LoadFromCache(); |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 67 | // Default implementation of GetObjectNameAndOffset. |
| 68 | // Quite slow, because it iterates through the whole process map for each |
| 69 | // lookup. |
| 70 | bool IterateForObjectNameAndOffset(uptr addr, uptr *offset, |
| Alexey Samsonov | 06d3aa4 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 71 | char filename[], uptr filename_size, |
| 72 | uptr *protection) { |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 73 | Reset(); |
| 74 | uptr start, end, file_offset; |
| Alexey Samsonov | 06d3aa4 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 75 | for (int i = 0; Next(&start, &end, &file_offset, filename, filename_size, |
| 76 | protection); |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 77 | i++) { |
| 78 | if (addr >= start && addr < end) { |
| Alexey Samsonov | 961276a | 2012-07-03 08:24:14 +0000 | [diff] [blame] | 79 | // Don't subtract 'start' for the first entry: |
| 80 | // * If a binary is compiled w/o -pie, then the first entry in |
| 81 | // process maps is likely the binary itself (all dynamic libs |
| 82 | // are mapped higher in address space). For such a binary, |
| 83 | // instruction offset in binary coincides with the actual |
| 84 | // instruction address in virtual memory (as code section |
| 85 | // is mapped to a fixed memory range). |
| 86 | // * If a binary is compiled with -pie, all the modules are |
| 87 | // mapped high at address space (in particular, higher than |
| 88 | // shadow memory of the tool), so the module can't be the |
| 89 | // first entry. |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 90 | *offset = (addr - (i ? start : 0)) + file_offset; |
| 91 | return true; |
| 92 | } |
| 93 | } |
| 94 | if (filename_size) |
| 95 | filename[0] = '\0'; |
| 96 | return false; |
| 97 | } |
| 98 | |
| Alexey Samsonov | cae486c | 2012-08-28 07:22:24 +0000 | [diff] [blame] | 99 | # if defined __linux__ |
| Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 100 | ProcSelfMapsBuff proc_self_maps_; |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 101 | char *current_; |
| Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 102 | |
| 103 | // Static mappings cache. |
| Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 104 | static ProcSelfMapsBuff cached_proc_self_maps_; |
| 105 | static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_. |
| Alexey Samsonov | cae486c | 2012-08-28 07:22:24 +0000 | [diff] [blame] | 106 | # elif defined __APPLE__ |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 107 | template<u32 kLCSegment, typename SegmentCommand> |
| 108 | bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset, |
| Alexey Samsonov | 91f833a | 2013-03-13 07:39:25 +0000 | [diff] [blame] | 109 | char filename[], uptr filename_size, |
| 110 | uptr *protection); |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 111 | int current_image_; |
| 112 | u32 current_magic_; |
| Alexander Potapenko | 77c0ac2 | 2012-10-02 15:42:24 +0000 | [diff] [blame] | 113 | u32 current_filetype_; |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 114 | int current_load_cmd_count_; |
| 115 | char *current_load_cmd_addr_; |
| Alexey Samsonov | cae486c | 2012-08-28 07:22:24 +0000 | [diff] [blame] | 116 | # endif |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| Alexey Samsonov | cc62211 | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 119 | #endif // _WIN32 |
| 120 | |
| Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 121 | } // namespace __sanitizer |
| 122 | |
| 123 | #endif // SANITIZER_PROCMAPS_H |