1238901Sandrew//===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
2238901Sandrew//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6238901Sandrew//
7238901Sandrew//===----------------------------------------------------------------------===//
8238901Sandrew//
9238901Sandrew// This file is shared between AddressSanitizer and ThreadSanitizer.
10238901Sandrew//
11238901Sandrew// Information about the process mappings.
12238901Sandrew//===----------------------------------------------------------------------===//
13238901Sandrew#ifndef SANITIZER_PROCMAPS_H
14238901Sandrew#define SANITIZER_PROCMAPS_H
15238901Sandrew
16327952Sdim#include "sanitizer_platform.h"
17327952Sdim
18341825Sdim#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD ||                \
19341825Sdim    SANITIZER_OPENBSD || SANITIZER_MAC || SANITIZER_SOLARIS
20327952Sdim
21276789Sdim#include "sanitizer_common.h"
22238901Sandrew#include "sanitizer_internal_defs.h"
23327952Sdim#include "sanitizer_linux.h"
24327952Sdim#include "sanitizer_mac.h"
25245614Sandrew#include "sanitizer_mutex.h"
26238901Sandrew
27238901Sandrewnamespace __sanitizer {
28238901Sandrew
29245614Sandrew
30321369Sdim// Memory protection masks.
31321369Sdimstatic const uptr kProtectionRead = 1;
32321369Sdimstatic const uptr kProtectionWrite = 2;
33321369Sdimstatic const uptr kProtectionExecute = 4;
34321369Sdimstatic const uptr kProtectionShared = 8;
35321369Sdim
36327952Sdimstruct MemoryMappedSegmentData;
37327952Sdim
38327952Sdimclass MemoryMappedSegment {
39327952Sdim public:
40360784Sdim  explicit MemoryMappedSegment(char *buff = nullptr, uptr size = 0)
41327952Sdim      : filename(buff), filename_size(size), data_(nullptr) {}
42321369Sdim  ~MemoryMappedSegment() {}
43321369Sdim
44327952Sdim  bool IsReadable() const { return protection & kProtectionRead; }
45327952Sdim  bool IsWritable() const { return protection & kProtectionWrite; }
46327952Sdim  bool IsExecutable() const { return protection & kProtectionExecute; }
47327952Sdim  bool IsShared() const { return protection & kProtectionShared; }
48321369Sdim
49327952Sdim  void AddAddressRanges(LoadedModule *module);
50327952Sdim
51321369Sdim  uptr start;
52321369Sdim  uptr end;
53321369Sdim  uptr offset;
54321369Sdim  char *filename;  // owned by caller
55321369Sdim  uptr filename_size;
56321369Sdim  uptr protection;
57321369Sdim  ModuleArch arch;
58321369Sdim  u8 uuid[kModuleUUIDSize];
59327952Sdim
60327952Sdim private:
61327952Sdim  friend class MemoryMappingLayout;
62327952Sdim
63327952Sdim  // This field is assigned and owned by MemoryMappingLayout if needed
64327952Sdim  MemoryMappedSegmentData *data_;
65321369Sdim};
66321369Sdim
67245614Sandrewclass MemoryMappingLayout {
68245614Sandrew public:
69251034Sed  explicit MemoryMappingLayout(bool cache_enabled);
70276789Sdim  ~MemoryMappingLayout();
71321369Sdim  bool Next(MemoryMappedSegment *segment);
72344779Sdim  bool Error() const;
73238901Sandrew  void Reset();
74245614Sandrew  // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
75245614Sandrew  // to obtain the memory mappings. It should fall back to pre-cached data
76245614Sandrew  // instead of aborting.
77245614Sandrew  static void CacheMemoryMappings();
78238901Sandrew
79309124Sdim  // Adds all mapped objects into a vector.
80327952Sdim  void DumpListOfModules(InternalMmapVectorNoCtor<LoadedModule> *modules);
81276789Sdim
82238901Sandrew private:
83245614Sandrew  void LoadFromCache();
84238901Sandrew
85327952Sdim  MemoryMappingLayoutData data_;
86238901Sandrew};
87238901Sandrew
88274201Sdim// Returns code range for the specified module.
89274201Sdimbool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
90274201Sdim
91276789Sdimbool IsDecimal(char c);
92276789Sdimuptr ParseDecimal(const char **p);
93276789Sdimbool IsHex(char c);
94276789Sdimuptr ParseHex(const char **p);
95274201Sdim
96238901Sandrew}  // namespace __sanitizer
97238901Sandrew
98341825Sdim#endif
99238901Sandrew#endif  // SANITIZER_PROCMAPS_H
100