sanitizer_procmaps.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file is shared between AddressSanitizer and ThreadSanitizer.
  10. //
  11. // Information about the process mappings.
  12. //===----------------------------------------------------------------------===//
  13. #ifndef SANITIZER_PROCMAPS_H
  14. #define SANITIZER_PROCMAPS_H
  15. #include "sanitizer_platform.h"
  16. #if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
  17. SANITIZER_APPLE || SANITIZER_SOLARIS || \
  18. SANITIZER_FUCHSIA
  19. #include "sanitizer_common.h"
  20. #include "sanitizer_internal_defs.h"
  21. #include "sanitizer_fuchsia.h"
  22. #include "sanitizer_linux.h"
  23. #include "sanitizer_mac.h"
  24. #include "sanitizer_mutex.h"
  25. namespace __sanitizer {
  26. // Memory protection masks.
  27. static const uptr kProtectionRead = 1;
  28. static const uptr kProtectionWrite = 2;
  29. static const uptr kProtectionExecute = 4;
  30. static const uptr kProtectionShared = 8;
  31. struct MemoryMappedSegmentData;
  32. class MemoryMappedSegment {
  33. public:
  34. explicit MemoryMappedSegment(char *buff = nullptr, uptr size = 0)
  35. : filename(buff), filename_size(size), data_(nullptr) {}
  36. ~MemoryMappedSegment() {}
  37. bool IsReadable() const { return protection & kProtectionRead; }
  38. bool IsWritable() const { return protection & kProtectionWrite; }
  39. bool IsExecutable() const { return protection & kProtectionExecute; }
  40. bool IsShared() const { return protection & kProtectionShared; }
  41. void AddAddressRanges(LoadedModule *module);
  42. uptr start;
  43. uptr end;
  44. uptr offset;
  45. char *filename; // owned by caller
  46. uptr filename_size;
  47. uptr protection;
  48. ModuleArch arch;
  49. u8 uuid[kModuleUUIDSize];
  50. private:
  51. friend class MemoryMappingLayout;
  52. // This field is assigned and owned by MemoryMappingLayout if needed
  53. MemoryMappedSegmentData *data_;
  54. };
  55. class MemoryMappingLayoutBase {
  56. public:
  57. virtual bool Next(MemoryMappedSegment *segment) { UNIMPLEMENTED(); }
  58. virtual bool Error() const { UNIMPLEMENTED(); };
  59. virtual void Reset() { UNIMPLEMENTED(); }
  60. protected:
  61. ~MemoryMappingLayoutBase() {}
  62. };
  63. class MemoryMappingLayout final : public MemoryMappingLayoutBase {
  64. public:
  65. explicit MemoryMappingLayout(bool cache_enabled);
  66. ~MemoryMappingLayout();
  67. virtual bool Next(MemoryMappedSegment *segment) override;
  68. virtual bool Error() const override;
  69. virtual void Reset() override;
  70. // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
  71. // to obtain the memory mappings. It should fall back to pre-cached data
  72. // instead of aborting.
  73. static void CacheMemoryMappings();
  74. // Adds all mapped objects into a vector.
  75. void DumpListOfModules(InternalMmapVectorNoCtor<LoadedModule> *modules);
  76. private:
  77. void LoadFromCache();
  78. MemoryMappingLayoutData data_;
  79. };
  80. // Returns code range for the specified module.
  81. bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
  82. bool IsDecimal(char c);
  83. uptr ParseDecimal(const char **p);
  84. bool IsHex(char c);
  85. uptr ParseHex(const char **p);
  86. } // namespace __sanitizer
  87. #endif
  88. #endif // SANITIZER_PROCMAPS_H