sanitizer_procmaps_solaris.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===-- sanitizer_procmaps_solaris.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 (Solaris-specific parts).
  10. //===----------------------------------------------------------------------===//
  11. // Before Solaris 11.4, <procfs.h> doesn't work in a largefile environment.
  12. #undef _FILE_OFFSET_BITS
  13. #include "sanitizer_platform.h"
  14. #if SANITIZER_SOLARIS
  15. # include <fcntl.h>
  16. # include <limits.h>
  17. # error #include <procfs.h>
  18. # include "sanitizer_common.h"
  19. # include "sanitizer_procmaps.h"
  20. namespace __sanitizer {
  21. void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
  22. uptr fd = internal_open("/proc/self/xmap", O_RDONLY);
  23. CHECK_NE(fd, -1);
  24. uptr Size = internal_filesize(fd);
  25. CHECK_GT(Size, 0);
  26. // Allow for additional entries by following mmap.
  27. size_t MmapedSize = Size * 4 / 3;
  28. void *VmMap = MmapOrDie(MmapedSize, "ReadProcMaps()");
  29. Size = internal_read(fd, VmMap, MmapedSize);
  30. CHECK_NE(Size, -1);
  31. internal_close(fd);
  32. proc_maps->data = (char *)VmMap;
  33. proc_maps->mmaped_size = MmapedSize;
  34. proc_maps->len = Size;
  35. }
  36. bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
  37. if (Error()) return false; // simulate empty maps
  38. char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
  39. if (data_.current >= last) return false;
  40. prxmap_t *xmapentry =
  41. const_cast<prxmap_t *>(reinterpret_cast<const prxmap_t *>(data_.current));
  42. segment->start = (uptr)xmapentry->pr_vaddr;
  43. segment->end = (uptr)(xmapentry->pr_vaddr + xmapentry->pr_size);
  44. segment->offset = (uptr)xmapentry->pr_offset;
  45. segment->protection = 0;
  46. if ((xmapentry->pr_mflags & MA_READ) != 0)
  47. segment->protection |= kProtectionRead;
  48. if ((xmapentry->pr_mflags & MA_WRITE) != 0)
  49. segment->protection |= kProtectionWrite;
  50. if ((xmapentry->pr_mflags & MA_EXEC) != 0)
  51. segment->protection |= kProtectionExecute;
  52. if ((xmapentry->pr_mflags & MA_SHARED) != 0)
  53. segment->protection |= kProtectionShared;
  54. if (segment->filename != NULL && segment->filename_size > 0) {
  55. char proc_path[PATH_MAX + 1];
  56. // Avoid unnecessary readlink on unnamed entires.
  57. if (xmapentry->pr_mapname[0] == '\0')
  58. segment->filename[0] = '\0';
  59. else {
  60. internal_snprintf(proc_path, sizeof(proc_path), "/proc/self/path/%s",
  61. xmapentry->pr_mapname);
  62. ssize_t sz = internal_readlink(proc_path, segment->filename,
  63. segment->filename_size - 1);
  64. // If readlink failed, the map is anonymous.
  65. if (sz == -1)
  66. segment->filename[0] = '\0';
  67. else if ((size_t)sz < segment->filename_size)
  68. // readlink doesn't NUL-terminate.
  69. segment->filename[sz] = '\0';
  70. }
  71. }
  72. data_.current += sizeof(prxmap_t);
  73. return true;
  74. }
  75. } // namespace __sanitizer
  76. #endif // SANITIZER_SOLARIS