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
21namespace __sanitizer {
22
23#if SANITIZER_FREEBSD || SANITIZER_LINUX
24struct ProcSelfMapsBuff {
25  char *data;
26  uptr mmaped_size;
27  uptr len;
28};
29#endif  // SANITIZER_FREEBSD || SANITIZER_LINUX
30
31class MemoryMappingLayout {
32 public:
33  explicit MemoryMappingLayout(bool cache_enabled);
34  ~MemoryMappingLayout();
35  bool Next(uptr *start, uptr *end, uptr *offset,
36            char filename[], uptr filename_size, uptr *protection);
37  void Reset();
38  // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
39  // to obtain the memory mappings. It should fall back to pre-cached data
40  // instead of aborting.
41  static void CacheMemoryMappings();
42
43  // Stores the list of mapped objects into an array.
44  uptr DumpListOfModules(LoadedModule *modules, uptr max_modules,
45                         string_predicate_t filter);
46
47  // Memory protection masks.
48  static const uptr kProtectionRead = 1;
49  static const uptr kProtectionWrite = 2;
50  static const uptr kProtectionExecute = 4;
51  static const uptr kProtectionShared = 8;
52
53 private:
54  void LoadFromCache();
55
56  // FIXME: Hide implementation details for different platforms in
57  // platform-specific files.
58# if SANITIZER_FREEBSD || SANITIZER_LINUX
59  ProcSelfMapsBuff proc_self_maps_;
60  char *current_;
61
62  // Static mappings cache.
63  static ProcSelfMapsBuff cached_proc_self_maps_;
64  static StaticSpinMutex cache_lock_;  // protects cached_proc_self_maps_.
65# elif SANITIZER_MAC
66  template<u32 kLCSegment, typename SegmentCommand>
67  bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
68                       char filename[], uptr filename_size,
69                       uptr *protection);
70  int current_image_;
71  u32 current_magic_;
72  u32 current_filetype_;
73  int current_load_cmd_count_;
74  char *current_load_cmd_addr_;
75# endif
76};
77
78typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
79                               /*out*/uptr *stats, uptr stats_size);
80
81// Parse the contents of /proc/self/smaps and generate a memory profile.
82// |cb| is a tool-specific callback that fills the |stats| array containing
83// |stats_size| elements.
84void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
85
86// Returns code range for the specified module.
87bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
88
89}  // namespace __sanitizer
90
91#endif  // SANITIZER_PROCMAPS_H
92