1af526226Smrg //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
2af526226Smrg //
3*490215a3Smrg // This file is distributed under the University of Illinois Open Source
4*490215a3Smrg // License. See LICENSE.TXT for details.
5af526226Smrg //
6af526226Smrg //===----------------------------------------------------------------------===//
7af526226Smrg //
8af526226Smrg // This file is shared between AddressSanitizer and ThreadSanitizer.
9af526226Smrg //
10af526226Smrg // Information about the process mappings.
11af526226Smrg //===----------------------------------------------------------------------===//
12af526226Smrg #ifndef SANITIZER_PROCMAPS_H
13af526226Smrg #define SANITIZER_PROCMAPS_H
14af526226Smrg 
152c35a912Smrg #include "sanitizer_platform.h"
162c35a912Smrg 
17ff135a7aSmrg #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD ||                \
18ff135a7aSmrg     SANITIZER_OPENBSD || SANITIZER_MAC || SANITIZER_SOLARIS
192c35a912Smrg 
204646d632Smrg #include "sanitizer_common.h"
21af526226Smrg #include "sanitizer_internal_defs.h"
222c35a912Smrg #include "sanitizer_linux.h"
232c35a912Smrg #include "sanitizer_mac.h"
24af526226Smrg #include "sanitizer_mutex.h"
25af526226Smrg 
26af526226Smrg namespace __sanitizer {
27af526226Smrg 
284646d632Smrg // Memory protection masks.
294646d632Smrg static const uptr kProtectionRead = 1;
304646d632Smrg static const uptr kProtectionWrite = 2;
314646d632Smrg static const uptr kProtectionExecute = 4;
324646d632Smrg static const uptr kProtectionShared = 8;
33af526226Smrg 
342c35a912Smrg struct MemoryMappedSegmentData;
352c35a912Smrg 
362c35a912Smrg class MemoryMappedSegment {
372c35a912Smrg  public:
38*490215a3Smrg   MemoryMappedSegment(char *buff = nullptr, uptr size = 0)
filename(buff)392c35a912Smrg       : filename(buff), filename_size(size), data_(nullptr) {}
~MemoryMappedSegment()402c35a912Smrg   ~MemoryMappedSegment() {}
412c35a912Smrg 
IsReadable()422c35a912Smrg   bool IsReadable() const { return protection & kProtectionRead; }
IsWritable()432c35a912Smrg   bool IsWritable() const { return protection & kProtectionWrite; }
IsExecutable()442c35a912Smrg   bool IsExecutable() const { return protection & kProtectionExecute; }
IsShared()452c35a912Smrg   bool IsShared() const { return protection & kProtectionShared; }
462c35a912Smrg 
472c35a912Smrg   void AddAddressRanges(LoadedModule *module);
482c35a912Smrg 
492c35a912Smrg   uptr start;
502c35a912Smrg   uptr end;
512c35a912Smrg   uptr offset;
522c35a912Smrg   char *filename;  // owned by caller
532c35a912Smrg   uptr filename_size;
542c35a912Smrg   uptr protection;
552c35a912Smrg   ModuleArch arch;
562c35a912Smrg   u8 uuid[kModuleUUIDSize];
572c35a912Smrg 
582c35a912Smrg  private:
592c35a912Smrg   friend class MemoryMappingLayout;
602c35a912Smrg 
612c35a912Smrg   // This field is assigned and owned by MemoryMappingLayout if needed
622c35a912Smrg   MemoryMappedSegmentData *data_;
632c35a912Smrg };
642c35a912Smrg 
652c35a912Smrg class MemoryMappingLayout {
662c35a912Smrg  public:
672c35a912Smrg   explicit MemoryMappingLayout(bool cache_enabled);
682c35a912Smrg   ~MemoryMappingLayout();
692c35a912Smrg   bool Next(MemoryMappedSegment *segment);
702c35a912Smrg   void Reset();
712c35a912Smrg   // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
722c35a912Smrg   // to obtain the memory mappings. It should fall back to pre-cached data
732c35a912Smrg   // instead of aborting.
742c35a912Smrg   static void CacheMemoryMappings();
752c35a912Smrg 
762c35a912Smrg   // Adds all mapped objects into a vector.
772c35a912Smrg   void DumpListOfModules(InternalMmapVectorNoCtor<LoadedModule> *modules);
782c35a912Smrg 
79af526226Smrg  private:
80af526226Smrg   void LoadFromCache();
81af526226Smrg 
822c35a912Smrg   MemoryMappingLayoutData data_;
83af526226Smrg };
84af526226Smrg 
854646d632Smrg // Returns code range for the specified module.
864646d632Smrg bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
874646d632Smrg 
884646d632Smrg bool IsDecimal(char c);
894646d632Smrg uptr ParseDecimal(const char **p);
904646d632Smrg bool IsHex(char c);
914646d632Smrg uptr ParseHex(const char **p);
92af526226Smrg 
93af526226Smrg }  // namespace __sanitizer
94af526226Smrg 
95ff135a7aSmrg #endif
96af526226Smrg #endif  // SANITIZER_PROCMAPS_H
97