sanitizer_procmaps_bsd.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. #if SANITIZER_FREEBSD
  16. #include "sanitizer_freebsd.h"
  17. #endif
  18. #include "sanitizer_procmaps.h"
  19. // clang-format off
  20. #include <sys/types.h>
  21. #include <sys/sysctl.h>
  22. // clang-format on
  23. #include <unistd.h>
  24. #if SANITIZER_FREEBSD
  25. #include <sys/user.h>
  26. #endif
  27. #include <limits.h>
  28. // Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
  29. #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
  30. #include <osreldate.h>
  31. #if __FreeBSD_version <= 902001 // v9.2
  32. #define kinfo_vmentry xkinfo_vmentry
  33. #endif
  34. #endif
  35. namespace __sanitizer {
  36. void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
  37. const int Mib[] = {
  38. #if SANITIZER_FREEBSD
  39. CTL_KERN,
  40. KERN_PROC,
  41. KERN_PROC_VMMAP,
  42. getpid()
  43. #elif SANITIZER_NETBSD
  44. CTL_VM,
  45. VM_PROC,
  46. VM_PROC_MAP,
  47. getpid(),
  48. sizeof(struct kinfo_vmentry)
  49. #else
  50. #error "not supported"
  51. #endif
  52. };
  53. uptr Size = 0;
  54. int Err = internal_sysctl(Mib, ARRAY_SIZE(Mib), NULL, &Size, NULL, 0);
  55. CHECK_EQ(Err, 0);
  56. CHECK_GT(Size, 0);
  57. size_t MmapedSize = Size * 4 / 3;
  58. void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
  59. Size = MmapedSize;
  60. Err = internal_sysctl(Mib, ARRAY_SIZE(Mib), VmMap, &Size, NULL, 0);
  61. CHECK_EQ(Err, 0);
  62. proc_maps->data = (char *)VmMap;
  63. proc_maps->mmaped_size = MmapedSize;
  64. proc_maps->len = Size;
  65. }
  66. bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
  67. CHECK(!Error()); // can not fail
  68. char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
  69. if (data_.current >= last)
  70. return false;
  71. const struct kinfo_vmentry *VmEntry =
  72. (const struct kinfo_vmentry *)data_.current;
  73. segment->start = (uptr)VmEntry->kve_start;
  74. segment->end = (uptr)VmEntry->kve_end;
  75. segment->offset = (uptr)VmEntry->kve_offset;
  76. segment->protection = 0;
  77. if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
  78. segment->protection |= kProtectionRead;
  79. if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
  80. segment->protection |= kProtectionWrite;
  81. if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
  82. segment->protection |= kProtectionExecute;
  83. if (segment->filename != NULL && segment->filename_size > 0) {
  84. internal_snprintf(segment->filename,
  85. Min(segment->filename_size, (uptr)PATH_MAX), "%s",
  86. VmEntry->kve_path);
  87. }
  88. #if SANITIZER_FREEBSD
  89. data_.current += VmEntry->kve_structsize;
  90. #else
  91. data_.current += sizeof(*VmEntry);
  92. #endif
  93. return true;
  94. }
  95. } // namespace __sanitizer
  96. #endif