xref: /aosp_15_r20/external/compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h (revision 7c3d14c8b49c529e04be81a3ce6f5cc23712e4c6)
1*7c3d14c8STreehugger Robot //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
2*7c3d14c8STreehugger Robot //
3*7c3d14c8STreehugger Robot //                     The LLVM Compiler Infrastructure
4*7c3d14c8STreehugger Robot //
5*7c3d14c8STreehugger Robot // This file is distributed under the University of Illinois Open Source
6*7c3d14c8STreehugger Robot // License. See LICENSE.TXT for details.
7*7c3d14c8STreehugger Robot //
8*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
9*7c3d14c8STreehugger Robot //
10*7c3d14c8STreehugger Robot // This file is shared between AddressSanitizer and ThreadSanitizer.
11*7c3d14c8STreehugger Robot //
12*7c3d14c8STreehugger Robot // Information about the process mappings.
13*7c3d14c8STreehugger Robot //===----------------------------------------------------------------------===//
14*7c3d14c8STreehugger Robot #ifndef SANITIZER_PROCMAPS_H
15*7c3d14c8STreehugger Robot #define SANITIZER_PROCMAPS_H
16*7c3d14c8STreehugger Robot 
17*7c3d14c8STreehugger Robot #include "sanitizer_common.h"
18*7c3d14c8STreehugger Robot #include "sanitizer_internal_defs.h"
19*7c3d14c8STreehugger Robot #include "sanitizer_mutex.h"
20*7c3d14c8STreehugger Robot 
21*7c3d14c8STreehugger Robot namespace __sanitizer {
22*7c3d14c8STreehugger Robot 
23*7c3d14c8STreehugger Robot #if SANITIZER_FREEBSD || SANITIZER_LINUX
24*7c3d14c8STreehugger Robot struct ProcSelfMapsBuff {
25*7c3d14c8STreehugger Robot   char *data;
26*7c3d14c8STreehugger Robot   uptr mmaped_size;
27*7c3d14c8STreehugger Robot   uptr len;
28*7c3d14c8STreehugger Robot };
29*7c3d14c8STreehugger Robot 
30*7c3d14c8STreehugger Robot // Reads process memory map in an OS-specific way.
31*7c3d14c8STreehugger Robot void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
32*7c3d14c8STreehugger Robot #endif  // SANITIZER_FREEBSD || SANITIZER_LINUX
33*7c3d14c8STreehugger Robot 
34*7c3d14c8STreehugger Robot class MemoryMappingLayout {
35*7c3d14c8STreehugger Robot  public:
36*7c3d14c8STreehugger Robot   explicit MemoryMappingLayout(bool cache_enabled);
37*7c3d14c8STreehugger Robot   ~MemoryMappingLayout();
38*7c3d14c8STreehugger Robot   bool Next(uptr *start, uptr *end, uptr *offset,
39*7c3d14c8STreehugger Robot             char filename[], uptr filename_size, uptr *protection);
40*7c3d14c8STreehugger Robot   void Reset();
41*7c3d14c8STreehugger Robot   // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
42*7c3d14c8STreehugger Robot   // to obtain the memory mappings. It should fall back to pre-cached data
43*7c3d14c8STreehugger Robot   // instead of aborting.
44*7c3d14c8STreehugger Robot   static void CacheMemoryMappings();
45*7c3d14c8STreehugger Robot 
46*7c3d14c8STreehugger Robot   // Adds all mapped objects into a vector.
47*7c3d14c8STreehugger Robot   void DumpListOfModules(InternalMmapVector<LoadedModule> *modules);
48*7c3d14c8STreehugger Robot 
49*7c3d14c8STreehugger Robot   // Memory protection masks.
50*7c3d14c8STreehugger Robot   static const uptr kProtectionRead = 1;
51*7c3d14c8STreehugger Robot   static const uptr kProtectionWrite = 2;
52*7c3d14c8STreehugger Robot   static const uptr kProtectionExecute = 4;
53*7c3d14c8STreehugger Robot   static const uptr kProtectionShared = 8;
54*7c3d14c8STreehugger Robot 
55*7c3d14c8STreehugger Robot  private:
56*7c3d14c8STreehugger Robot   void LoadFromCache();
57*7c3d14c8STreehugger Robot 
58*7c3d14c8STreehugger Robot   // FIXME: Hide implementation details for different platforms in
59*7c3d14c8STreehugger Robot   // platform-specific files.
60*7c3d14c8STreehugger Robot # if SANITIZER_FREEBSD || SANITIZER_LINUX
61*7c3d14c8STreehugger Robot   ProcSelfMapsBuff proc_self_maps_;
62*7c3d14c8STreehugger Robot   const char *current_;
63*7c3d14c8STreehugger Robot 
64*7c3d14c8STreehugger Robot   // Static mappings cache.
65*7c3d14c8STreehugger Robot   static ProcSelfMapsBuff cached_proc_self_maps_;
66*7c3d14c8STreehugger Robot   static StaticSpinMutex cache_lock_;  // protects cached_proc_self_maps_.
67*7c3d14c8STreehugger Robot # elif SANITIZER_MAC
68*7c3d14c8STreehugger Robot   template<u32 kLCSegment, typename SegmentCommand>
69*7c3d14c8STreehugger Robot   bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
70*7c3d14c8STreehugger Robot                        char filename[], uptr filename_size,
71*7c3d14c8STreehugger Robot                        uptr *protection);
72*7c3d14c8STreehugger Robot   int current_image_;
73*7c3d14c8STreehugger Robot   u32 current_magic_;
74*7c3d14c8STreehugger Robot   u32 current_filetype_;
75*7c3d14c8STreehugger Robot   int current_load_cmd_count_;
76*7c3d14c8STreehugger Robot   char *current_load_cmd_addr_;
77*7c3d14c8STreehugger Robot # endif
78*7c3d14c8STreehugger Robot };
79*7c3d14c8STreehugger Robot 
80*7c3d14c8STreehugger Robot typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
81*7c3d14c8STreehugger Robot                                /*out*/uptr *stats, uptr stats_size);
82*7c3d14c8STreehugger Robot 
83*7c3d14c8STreehugger Robot // Parse the contents of /proc/self/smaps and generate a memory profile.
84*7c3d14c8STreehugger Robot // |cb| is a tool-specific callback that fills the |stats| array containing
85*7c3d14c8STreehugger Robot // |stats_size| elements.
86*7c3d14c8STreehugger Robot void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
87*7c3d14c8STreehugger Robot 
88*7c3d14c8STreehugger Robot // Returns code range for the specified module.
89*7c3d14c8STreehugger Robot bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
90*7c3d14c8STreehugger Robot 
91*7c3d14c8STreehugger Robot bool IsDecimal(char c);
92*7c3d14c8STreehugger Robot uptr ParseDecimal(const char **p);
93*7c3d14c8STreehugger Robot bool IsHex(char c);
94*7c3d14c8STreehugger Robot uptr ParseHex(const char **p);
95*7c3d14c8STreehugger Robot 
96*7c3d14c8STreehugger Robot }  // namespace __sanitizer
97*7c3d14c8STreehugger Robot 
98*7c3d14c8STreehugger Robot #endif  // SANITIZER_PROCMAPS_H
99