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