sanitizer_procmaps.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //===-- sanitizer_procmaps.h ------------------------------------*- C++ -*-===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // This file is shared between AddressSanitizer and ThreadSanitizer.
  9. //
  10. // Information about the process mappings.
  11. //===----------------------------------------------------------------------===//
  12. #ifndef SANITIZER_PROCMAPS_H
  13. #define SANITIZER_PROCMAPS_H
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_internal_defs.h"
  16. #include "sanitizer_mutex.h"
  17. namespace __sanitizer {
  18. #if SANITIZER_FREEBSD || SANITIZER_LINUX
  19. struct ProcSelfMapsBuff {
  20. char *data;
  21. uptr mmaped_size;
  22. uptr len;
  23. };
  24. // Reads process memory map in an OS-specific way.
  25. void ReadProcMaps(ProcSelfMapsBuff *proc_maps);
  26. #endif // SANITIZER_FREEBSD || SANITIZER_LINUX
  27. class MemoryMappingLayout {
  28. public:
  29. explicit MemoryMappingLayout(bool cache_enabled);
  30. ~MemoryMappingLayout();
  31. bool Next(uptr *start, uptr *end, uptr *offset,
  32. char filename[], uptr filename_size, uptr *protection);
  33. void Reset();
  34. // In some cases, e.g. when running under a sandbox on Linux, ASan is unable
  35. // to obtain the memory mappings. It should fall back to pre-cached data
  36. // instead of aborting.
  37. static void CacheMemoryMappings();
  38. // Stores the list of mapped objects into an array.
  39. uptr DumpListOfModules(LoadedModule *modules, uptr max_modules,
  40. string_predicate_t filter);
  41. // Memory protection masks.
  42. static const uptr kProtectionRead = 1;
  43. static const uptr kProtectionWrite = 2;
  44. static const uptr kProtectionExecute = 4;
  45. static const uptr kProtectionShared = 8;
  46. private:
  47. void LoadFromCache();
  48. // FIXME: Hide implementation details for different platforms in
  49. // platform-specific files.
  50. # if SANITIZER_FREEBSD || SANITIZER_LINUX
  51. ProcSelfMapsBuff proc_self_maps_;
  52. const char *current_;
  53. // Static mappings cache.
  54. static ProcSelfMapsBuff cached_proc_self_maps_;
  55. static StaticSpinMutex cache_lock_; // protects cached_proc_self_maps_.
  56. # elif SANITIZER_MAC
  57. template<u32 kLCSegment, typename SegmentCommand>
  58. bool NextSegmentLoad(uptr *start, uptr *end, uptr *offset,
  59. char filename[], uptr filename_size,
  60. uptr *protection);
  61. int current_image_;
  62. u32 current_magic_;
  63. u32 current_filetype_;
  64. int current_load_cmd_count_;
  65. char *current_load_cmd_addr_;
  66. # endif
  67. };
  68. typedef void (*fill_profile_f)(uptr start, uptr rss, bool file,
  69. /*out*/uptr *stats, uptr stats_size);
  70. // Parse the contents of /proc/self/smaps and generate a memory profile.
  71. // |cb| is a tool-specific callback that fills the |stats| array containing
  72. // |stats_size| elements.
  73. void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size);
  74. // Returns code range for the specified module.
  75. bool GetCodeRangeForFile(const char *module, uptr *start, uptr *end);
  76. bool IsDecimal(char c);
  77. uptr ParseDecimal(const char **p);
  78. bool IsHex(char c);
  79. uptr ParseHex(const char **p);
  80. } // namespace __sanitizer
  81. #endif // SANITIZER_PROCMAPS_H