sanitizer_procmaps_freebsd.cc 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //===-- sanitizer_procmaps_freebsd.cc -------------------------------------===//
  2. //
  3. // This file is distributed under the University of Illinois Open Source
  4. // License. See LICENSE.TXT for details.
  5. //
  6. //===----------------------------------------------------------------------===//
  7. //
  8. // Information about the process mappings (FreeBSD-specific parts).
  9. //===----------------------------------------------------------------------===//
  10. #include "sanitizer_platform.h"
  11. #if SANITIZER_FREEBSD
  12. #include "sanitizer_common.h"
  13. #include "sanitizer_freebsd.h"
  14. #include "sanitizer_procmaps.h"
  15. #include <unistd.h>
  16. #include <sys/sysctl.h>
  17. #include <sys/user.h>
  18. // Fix 'kinfo_vmentry' definition on FreeBSD prior v9.2 in 32-bit mode.
  19. #if SANITIZER_FREEBSD && (SANITIZER_WORDSIZE == 32)
  20. # include <osreldate.h>
  21. # if __FreeBSD_version <= 902001 // v9.2
  22. # define kinfo_vmentry xkinfo_vmentry
  23. # endif
  24. #endif
  25. namespace __sanitizer {
  26. void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
  27. const int Mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid() };
  28. size_t Size = 0;
  29. int Err = sysctl(Mib, 4, NULL, &Size, NULL, 0);
  30. CHECK_EQ(Err, 0);
  31. CHECK_GT(Size, 0);
  32. size_t MmapedSize = Size * 4 / 3;
  33. void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
  34. Size = MmapedSize;
  35. Err = sysctl(Mib, 4, VmMap, &Size, NULL, 0);
  36. CHECK_EQ(Err, 0);
  37. proc_maps->data = (char*)VmMap;
  38. proc_maps->mmaped_size = MmapedSize;
  39. proc_maps->len = Size;
  40. }
  41. bool MemoryMappingLayout::Next(uptr *start, uptr *end, uptr *offset,
  42. char filename[], uptr filename_size,
  43. uptr *protection) {
  44. char *last = proc_self_maps_.data + proc_self_maps_.len;
  45. if (current_ >= last) return false;
  46. uptr dummy;
  47. if (!start) start = &dummy;
  48. if (!end) end = &dummy;
  49. if (!offset) offset = &dummy;
  50. if (!protection) protection = &dummy;
  51. struct kinfo_vmentry *VmEntry = (struct kinfo_vmentry*)current_;
  52. *start = (uptr)VmEntry->kve_start;
  53. *end = (uptr)VmEntry->kve_end;
  54. *offset = (uptr)VmEntry->kve_offset;
  55. *protection = 0;
  56. if ((VmEntry->kve_protection & KVME_PROT_READ) != 0)
  57. *protection |= kProtectionRead;
  58. if ((VmEntry->kve_protection & KVME_PROT_WRITE) != 0)
  59. *protection |= kProtectionWrite;
  60. if ((VmEntry->kve_protection & KVME_PROT_EXEC) != 0)
  61. *protection |= kProtectionExecute;
  62. if (filename != NULL && filename_size > 0) {
  63. internal_snprintf(filename,
  64. Min(filename_size, (uptr)PATH_MAX),
  65. "%s", VmEntry->kve_path);
  66. }
  67. current_ += VmEntry->kve_structsize;
  68. return true;
  69. }
  70. } // namespace __sanitizer
  71. #endif // SANITIZER_FREEBSD