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