• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_common.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_mutex.h"
20 
21 namespace __sanitizer {
22 
23 #if SANITIZER_FREEBSD || SANITIZER_LINUX
24 struct ProcSelfMapsBuff {
25   char *data;
26   uptr mmaped_size;
27   uptr len;
28 };
29 
30 // Reads process memory map in an OS-specific way.
31 void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
32 #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX
33 
34 class MemoryMappingLayout {
35  public:
36   explicit MemoryMappingLayout(bool cache_enabled);
37   ~MemoryMappingLayout();
38   bool Next(uptr *start, uptr *end, uptr *offset,
39             char filename[], uptr filename_size, uptr *protection);
40   void Reset();
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();
45 
46   // Adds all mapped objects into a vector.
47   void DumpListOfModules(InternalMmapVector<LoadedModule> *modules);
48 
49   // Memory protection masks.
50   static const uptr kProtectionRead = 1;
51   static const uptr kProtectionWrite = 2;
52   static const uptr kProtectionExecute = 4;
53   static const uptr kProtectionShared = 8;
54 
55  private:
56   void LoadFromCache();
57 
58   // FIXME: Hide implementation details for different platforms in
59   // platform-specific files.
60 # if SANITIZER_FREEBSD || SANITIZER_LINUX
61   ProcSelfMapsBuff proc_self_maps_;
62   const char *current_;
63 
64   // Static mappings cache.
65   static ProcSelfMapsBuff cached_proc_self_maps_;
66   static StaticSpinMutex cache_lock_;  // protects cached_proc_self_maps_.
67 # elif SANITIZER_MAC
68   template<u32 kLCSegment, typename SegmentCommand>
69   bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
70                        char filename[], uptr filename_size,
71                        uptr *protection);
72   int current_image_;
73   u32 current_magic_;
74   u32 current_filetype_;
75   int current_load_cmd_count_;
76   char *current_load_cmd_addr_;
77 # endif
78 };
79 
80 typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
81                                /*out*/uptr *stats, uptr stats_size);
82 
83 // Parse the contents of /proc/self/smaps and generate a memory profile.
84 // |cb| is a tool-specific callback that fills the |stats| array containing
85 // |stats_size| elements.
86 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
87 
88 // Returns code range for the specified module.
89 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
90 
91 bool IsDecimal(char c);
92 uptr ParseDecimal(const char **p);
93 bool IsHex(char c);
94 uptr ParseHex(const char **p);
95 
96 }  // namespace __sanitizer
97 
98 #endif  // SANITIZER_PROCMAPS_H
99