sanitizer_procmaps.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. struct ImageHeader;
  56. class MemoryMappingLayoutBase {
  57. public:
  58. virtual bool Next(MemoryMappedSegment *segment) { UNIMPLEMENTED(); }
  59. virtual bool Error() const { UNIMPLEMENTED(); };
  60. virtual void Reset() { UNIMPLEMENTED(); }
  61. protected:
  62. ~MemoryMappingLayoutBase() {}
  63. };
  64. class MemoryMappingLayout : public MemoryMappingLayoutBase {
  65. public:
  66. explicit MemoryMappingLayout(bool cache_enabled);
  67. // This destructor cannot be virtual, as it would cause an operator new() linking
  68. // failures in hwasan test cases. However non-virtual destructors emit warnings
  69. // in macOS build, hence disabling those
  70. #ifdef __clang__
  71. #pragma clang diagnostic push
  72. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  73. #endif
  74. ~MemoryMappingLayout();
  75. #ifdef __clang__
  76. #pragma clang diagnostic pop
  77. #endif
  78. virtual bool Next(MemoryMappedSegment *segment) override;
  79. virtual bool Error() const override;
  80. virtual void Reset() override;
  81. // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
  82. // to obtain the memory mappings. It should fall back to pre-cached data
  83. // instead of aborting.
  84. static void CacheMemoryMappings();
  85. // Adds all mapped objects into a vector.
  86. void DumpListOfModules(InternalMmapVectorNoCtor<LoadedModule> *modules);
  87. protected:
  88. #if SANITIZER_APPLE
  89. virtual const ImageHeader *CurrentImageHeader();
  90. #endif
  91. MemoryMappingLayoutData data_;
  92. private:
  93. void LoadFromCache();
  94. };
  95. // Returns code range for the specified module.
  96. bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
  97. bool IsDecimal(char c);
  98. uptr ParseDecimal(const char **p);
  99. bool IsHex(char c);
  100. uptr ParseHex(const char **p);
  101. } // namespace __sanitizer
  102. #endif
  103. #endif // SANITIZER_PROCMAPS_H