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 | |
Alexey Samsonov | 64ffa59 | 2013-12-25 08:39:38 +0000 | [diff] [blame] | 17 | #include "sanitizer_common.h" |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 18 | #include "sanitizer_internal_defs.h" |
Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 19 | #include "sanitizer_mutex.h" |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 20 | |
| 21 | namespace __sanitizer { |
| 22 | |
Alexey Samsonov | d52b9ba | 2014-03-06 08:58:24 +0000 | [diff] [blame] | 23 | #if SANITIZER_FREEBSD || SANITIZER_LINUX |
Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 24 | struct ProcSelfMapsBuff { |
| 25 | char *data; |
| 26 | uptr mmaped_size; |
| 27 | uptr len; |
| 28 | }; |
Viktor Kutuzov | a37ad09 | 2014-08-06 10:16:52 +0000 | [diff] [blame] | 29 | |
| 30 | // Reads process memory map in an OS-specific way. |
| 31 | void ReadProcMaps(ProcSelfMapsBuff *proc_maps); |
Alexey Samsonov | d52b9ba | 2014-03-06 08:58:24 +0000 | [diff] [blame] | 32 | #endif // SANITIZER_FREEBSD || SANITIZER_LINUX |
Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 33 | |
Alexey Samsonov | cc62211 | 2012-08-27 13:48:48 +0000 | [diff] [blame] | 34 | class MemoryMappingLayout { |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 35 | public: |
Alexander Potapenko | f8109dd | 2013-03-26 10:34:37 +0000 | [diff] [blame] | 36 | explicit MemoryMappingLayout(bool cache_enabled); |
Alexey Samsonov | 64ffa59 | 2013-12-25 08:39:38 +0000 | [diff] [blame] | 37 | ~MemoryMappingLayout(); |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 38 | bool Next(uptr *start, uptr *end, uptr *offset, |
Alexey Samsonov | 06d3aa4 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 39 | char filename[], uptr filename_size, uptr *protection); |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 40 | void Reset(); |
Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 41 | // In some cases, e.g. when running under a sandbox on Linux, ASan is unable |
| 42 | // to obtain the memory mappings. It should fall back to pre-cached data |
| 43 | // instead of aborting. |
| 44 | static void CacheMemoryMappings(); |
Alexey Samsonov | 64ffa59 | 2013-12-25 08:39:38 +0000 | [diff] [blame] | 45 | |
| 46 | // Stores the list of mapped objects into an array. |
| 47 | uptr DumpListOfModules(LoadedModule *modules, uptr max_modules, |
| 48 | string_predicate_t filter); |
Kostya Serebryany | 98390d0 | 2012-06-20 15:19:17 +0000 | [diff] [blame] | 49 | |
Alexey Samsonov | 06d3aa4 | 2013-03-13 06:51:02 +0000 | [diff] [blame] | 50 | // Memory protection masks. |
| 51 | static const uptr kProtectionRead = 1; |
| 52 | static const uptr kProtectionWrite = 2; |
| 53 | static const uptr kProtectionExecute = 4; |
| 54 | static const uptr kProtectionShared = 8; |
| 55 | |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 56 | private: |
Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 57 | void LoadFromCache(); |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 58 | |
Alexey Samsonov | 2f392d2 | 2013-12-25 08:01:16 +0000 | [diff] [blame] | 59 | // FIXME: Hide implementation details for different platforms in |
| 60 | // platform-specific files. |
Alexey Samsonov | d52b9ba | 2014-03-06 08:58:24 +0000 | [diff] [blame] | 61 | # if SANITIZER_FREEBSD || SANITIZER_LINUX |
Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 62 | ProcSelfMapsBuff proc_self_maps_; |
Viktor Kutuzov | a37ad09 | 2014-08-06 10:16:52 +0000 | [diff] [blame] | 63 | const char *current_; |
Alexander Potapenko | 7811425 | 2012-12-01 02:39:45 +0000 | [diff] [blame] | 64 | |
| 65 | // Static mappings cache. |
Alexander Potapenko | e2b6d08 | 2012-12-03 21:21:22 +0000 | [diff] [blame] | 66 | static ProcSelfMapsBuff cached_proc_self_maps_; |
| 67 | static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_. |
Alexey Samsonov | a0e28a7 | 2013-04-03 07:24:35 +0000 | [diff] [blame] | 68 | # elif SANITIZER_MAC |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 69 | template<u32 kLCSegment, typename SegmentCommand> |
| 70 | bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset, |
Alexey Samsonov | 91f833a | 2013-03-13 07:39:25 +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 | int current_image_; |
| 74 | u32 current_magic_; |
Alexander Potapenko | 77c0ac2 | 2012-10-02 15:42:24 +0000 | [diff] [blame] | 75 | u32 current_filetype_; |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 76 | int current_load_cmd_count_; |
| 77 | char *current_load_cmd_addr_; |
Alexey Samsonov | cae486c | 2012-08-28 07:22:24 +0000 | [diff] [blame] | 78 | # endif |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
Alexander Potapenko | 7e1c519 | 2013-09-03 11:09:16 +0000 | [diff] [blame] | 81 | typedef void (*fill_profile_f)(uptr start, uptr rss, bool file, |
| 82 | /*out*/uptr *stats, uptr stats_size); |
| 83 | |
| 84 | // Parse the contents of /proc/self/smaps and generate a memory profile. |
| 85 | // |cb| is a tool-specific callback that fills the |stats| array containing |
| 86 | // |stats_size| elements. |
| 87 | void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size); |
| 88 | |
Dmitry Vyukov | 4e9c091 | 2013-09-21 21:41:08 +0000 | [diff] [blame] | 89 | // Returns code range for the specified module. |
| 90 | bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end); |
| 91 | |
Viktor Kutuzov | a37ad09 | 2014-08-06 10:16:52 +0000 | [diff] [blame] | 92 | bool IsDecimal(char c); |
| 93 | uptr ParseDecimal(const char **p); |
| 94 | bool IsHex(char c); |
| 95 | uptr ParseHex(const char **p); |
| 96 | |
Alexey Samsonov | 28a9895 | 2012-06-07 06:15:12 +0000 | [diff] [blame] | 97 | } // namespace __sanitizer |
| 98 | |
| 99 | #endif // SANITIZER_PROCMAPS_H |