sanitizer_procmaps_bsd.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===-- sanitizer_procmaps_bsd.cpp ----------------------------------------===//
  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. // Information about the process mappings
  10. // (FreeBSD and NetBSD-specific parts).
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_FREEBSD || SANITIZER_NETBSD
  14. #include "sanitizer_common.h"
  15. #include "sanitizer_procmaps.h"
  16. // clang-format off
  17. #include <sys/types.h>
  18. #include <sys/sysctl.h>
  19. // clang-format on
  20. #include <unistd.h>
  21. #if SANITIZER_FREEBSD
  22. #include <sys/user.h>
  23. #endif
  24. #include <limits.h>
  25. namespace __sanitizer {
  26. #if SANITIZER_FREEBSD
  27. void GetMemoryProfile(fill_profile_f cb, uptr *stats) {
  28. const int Mib[] = {
  29. CTL_KERN,
  30. KERN_PROC,
  31. KERN_PROC_PID,
  32. getpid()
  33. };
  34. struct kinfo_proc InfoProc;
  35. uptr Len = sizeof(InfoProc);
  36. CHECK_EQ(internal_sysctl(Mib, ARRAY_SIZE(Mib), nullptr, (uptr *)&InfoProc, &Len, 0), 0);
  37. cb(0, InfoProc.ki_rssize * GetPageSizeCached(), false, stats);
  38. }
  39. #endif
  40. void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
  41. const int Mib[] = {
  42. #if SANITIZER_FREEBSD
  43. CTL_KERN,
  44. KERN_PROC,
  45. KERN_PROC_VMMAP,
  46. getpid()
  47. #elif SANITIZER_NETBSD
  48. CTL_VM,
  49. VM_PROC,
  50. VM_PROC_MAP,
  51. getpid(),
  52. sizeof(struct kinfo_vmentry)
  53. #else
  54. #error "not supported"
  55. #endif
  56. };
  57. uptr Size = 0;
  58. int Err = internal_sysctl(Mib, ARRAY_SIZE(Mib), NULL, &Size, NULL, 0);
  59. CHECK_EQ(Err, 0);
  60. CHECK_GT(Size, 0);
  61. size_t MmapedSize = Size * 4 / 3;
  62. void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
  63. Size = MmapedSize;
  64. Err = internal_sysctl(Mib, ARRAY_SIZE(Mib), VmMap, &Size, NULL, 0);
  65. CHECK_EQ(Err, 0);
  66. proc_maps->data = (char *)VmMap;
  67. proc_maps->mmaped_size = MmapedSize;
  68. proc_maps->len = Size;
  69. }
  70. bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
  71. CHECK(!Error()); // can not fail
  72. char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
  73. if (data_.current >= last)
  74. return false;
  75. const struct kinfo_vmentry *VmEntry =
  76. (const struct kinfo_vmentry *)data_.current;
  77. segment->start = (uptr)VmEntry->kve_start;
  78. segment->end = (uptr)VmEntry->kve_end;
  79. segment->offset = (uptr)VmEntry->kve_offset;
  80. segment->protection = 0;
  81. if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
  82. segment->protection |= kProtectionRead;
  83. if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
  84. segment->protection |= kProtectionWrite;
  85. if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
  86. segment->protection |= kProtectionExecute;
  87. if (segment->filename != NULL && segment->filename_size > 0) {
  88. internal_snprintf(segment->filename,
  89. Min(segment->filename_size, (uptr)PATH_MAX), "%s",
  90. VmEntry->kve_path);
  91. }
  92. #if SANITIZER_FREEBSD
  93. data_.current += VmEntry->kve_structsize;
  94. #else
  95. data_.current += sizeof(*VmEntry);
  96. #endif
  97. return true;
  98. }
  99. } // namespace __sanitizer
  100. #endif