1 //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is shared between AddressSanitizer and ThreadSanitizer.
10 // Information about the process mappings.
11 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_PROCMAPS_H
13 #define SANITIZER_PROCMAPS_H
15 #include "sanitizer_common.h"
16 #include "sanitizer_internal_defs.h"
17 #include "sanitizer_mutex.h"
19 namespace __sanitizer
{
21 #if SANITIZER_FREEBSD || SANITIZER_LINUX
22 struct ProcSelfMapsBuff
{
28 // Reads process memory map in an OS-specific way.
29 void ReadProcMaps(ProcSelfMapsBuff
*proc_maps
);
30 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX
32 class MemoryMappingLayout
{
34 explicit MemoryMappingLayout(bool cache_enabled
);
35 ~MemoryMappingLayout();
36 bool Next(uptr
*start
, uptr
*end
, uptr
*offset
,
37 char filename
[], uptr filename_size
, uptr
*protection
);
39 // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
40 // to obtain the memory mappings. It should fall back to pre-cached data
41 // instead of aborting.
42 static void CacheMemoryMappings();
44 // Stores the list of mapped objects into an array.
45 uptr
DumpListOfModules(LoadedModule
*modules
, uptr max_modules
,
46 string_predicate_t filter
);
48 // Memory protection masks.
49 static const uptr kProtectionRead
= 1;
50 static const uptr kProtectionWrite
= 2;
51 static const uptr kProtectionExecute
= 4;
52 static const uptr kProtectionShared
= 8;
57 // FIXME: Hide implementation details for different platforms in
58 // platform-specific files.
59 # if SANITIZER_FREEBSD || SANITIZER_LINUX
60 ProcSelfMapsBuff proc_self_maps_
;
63 // Static mappings cache.
64 static ProcSelfMapsBuff cached_proc_self_maps_
;
65 static StaticSpinMutex cache_lock_
; // protects cached_proc_self_maps_.
67 template<u32 kLCSegment
, typename SegmentCommand
>
68 bool NextSegmentLoad(uptr
*start
, uptr
*end
, uptr
*offset
,
69 char filename
[], uptr filename_size
,
73 u32 current_filetype_
;
74 int current_load_cmd_count_
;
75 char *current_load_cmd_addr_
;
79 typedef void (*fill_profile_f
)(uptr start
, uptr rss
, bool file
,
80 /*out*/uptr
*stats
, uptr stats_size
);
82 // Parse the contents of /proc/self/smaps and generate a memory profile.
83 // |cb| is a tool-specific callback that fills the |stats| array containing
84 // |stats_size| elements.
85 void GetMemoryProfile(fill_profile_f cb
, uptr
*stats
, uptr stats_size
);
87 // Returns code range for the specified module.
88 bool GetCodeRangeForFile(const char *module
, uptr
*start
, uptr
*end
);
90 bool IsDecimal(char c
);
91 uptr
ParseDecimal(const char **p
);
93 uptr
ParseHex(const char **p
);
95 } // namespace __sanitizer
97 #endif // SANITIZER_PROCMAPS_H