1 //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is shared between AddressSanitizer and ThreadSanitizer.
12 // Information about the process mappings.
13 //===----------------------------------------------------------------------===//
14 #ifndef SANITIZER_PROCMAPS_H
15 #define SANITIZER_PROCMAPS_H
17 #include "sanitizer_common.h"
18 #include "sanitizer_internal_defs.h"
19 #include "sanitizer_mutex.h"
21 namespace __sanitizer {
23 #if SANITIZER_FREEBSD || SANITIZER_LINUX
24 struct ProcSelfMapsBuff {
30 // Reads process memory map in an OS-specific way.
31 void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
32 #endif // SANITIZER_FREEBSD || SANITIZER_LINUX
34 class MemoryMappingLayout {
36 explicit MemoryMappingLayout(bool cache_enabled);
37 ~MemoryMappingLayout();
38 bool Next(uptr *start, uptr *end, uptr *offset, char filename[],
39 uptr filename_size, uptr *protection, ModuleArch *arch = nullptr,
42 // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
43 // to obtain the memory mappings. It should fall back to pre-cached data
44 // instead of aborting.
45 static void CacheMemoryMappings();
47 // Adds all mapped objects into a vector.
48 void DumpListOfModules(InternalMmapVector<LoadedModule> *modules);
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;
59 // FIXME: Hide implementation details for different platforms in
60 // platform-specific files.
61 # if SANITIZER_FREEBSD || SANITIZER_LINUX
62 ProcSelfMapsBuff proc_self_maps_;
65 // Static mappings cache.
66 static ProcSelfMapsBuff cached_proc_self_maps_;
67 static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_.
69 template <u32 kLCSegment, typename SegmentCommand>
70 bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset, char filename[],
71 uptr filename_size, ModuleArch *arch, u8 *uuid,
73 void GetSegmentAddrRange(uptr *start, uptr *end, uptr vmaddr, uptr vmsize);
76 u32 current_filetype_;
77 ModuleArch current_arch_;
78 u8 current_uuid_[kModuleUUIDSize];
79 int current_load_cmd_count_;
80 char *current_load_cmd_addr_;
81 bool current_instrumented_;
85 typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
86 /*out*/uptr *stats, uptr stats_size);
88 // Parse the contents of /proc/self/smaps and generate a memory profile.
89 // |cb| is a tool-specific callback that fills the |stats| array containing
90 // |stats_size| elements.
91 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
93 // Returns code range for the specified module.
94 bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
96 bool IsDecimal(char c);
97 uptr ParseDecimal(const char **p);
99 uptr ParseHex(const char **p);
101 } // namespace __sanitizer
103 #endif // SANITIZER_PROCMAPS_H