sanitizer_procmaps_linux.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //===-- sanitizer_procmaps_linux.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 (Linux-specific parts).
  10. //===----------------------------------------------------------------------===//
  11. #include "sanitizer_platform.h"
  12. #if SANITIZER_LINUX
  13. #include "sanitizer_common.h"
  14. #include "sanitizer_procmaps.h"
  15. namespace __sanitizer {
  16. void ReadProcMaps(ProcSelfMapsBuff *proc_maps) {
  17. if (!ReadFileToBuffer("/proc/self/maps", &proc_maps->data,
  18. &proc_maps->mmaped_size, &proc_maps->len)) {
  19. proc_maps->data = nullptr;
  20. proc_maps->mmaped_size = 0;
  21. proc_maps->len = 0;
  22. }
  23. }
  24. static bool IsOneOf(char c, char c1, char c2) {
  25. return c == c1 || c == c2;
  26. }
  27. bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
  28. if (Error()) return false; // simulate empty maps
  29. char *last = data_.proc_self_maps.data + data_.proc_self_maps.len;
  30. if (data_.current >= last) return false;
  31. char *next_line =
  32. (char *)internal_memchr(data_.current, '\n', last - data_.current);
  33. if (next_line == 0)
  34. next_line = last;
  35. // Example: 08048000-08056000 r-xp 00000000 03:0c 64593 /foo/bar
  36. segment->start = ParseHex(&data_.current);
  37. CHECK_EQ(*data_.current++, '-');
  38. segment->end = ParseHex(&data_.current);
  39. CHECK_EQ(*data_.current++, ' ');
  40. CHECK(IsOneOf(*data_.current, '-', 'r'));
  41. segment->protection = 0;
  42. if (*data_.current++ == 'r') segment->protection |= kProtectionRead;
  43. CHECK(IsOneOf(*data_.current, '-', 'w'));
  44. if (*data_.current++ == 'w') segment->protection |= kProtectionWrite;
  45. CHECK(IsOneOf(*data_.current, '-', 'x'));
  46. if (*data_.current++ == 'x') segment->protection |= kProtectionExecute;
  47. CHECK(IsOneOf(*data_.current, 's', 'p'));
  48. if (*data_.current++ == 's') segment->protection |= kProtectionShared;
  49. CHECK_EQ(*data_.current++, ' ');
  50. segment->offset = ParseHex(&data_.current);
  51. CHECK_EQ(*data_.current++, ' ');
  52. ParseHex(&data_.current);
  53. CHECK_EQ(*data_.current++, ':');
  54. ParseHex(&data_.current);
  55. CHECK_EQ(*data_.current++, ' ');
  56. while (IsDecimal(*data_.current)) data_.current++;
  57. // Qemu may lack the trailing space.
  58. // https://github.com/google/sanitizers/issues/160
  59. // CHECK_EQ(*data_.current++, ' ');
  60. // Skip spaces.
  61. while (data_.current < next_line && *data_.current == ' ') data_.current++;
  62. // Fill in the filename.
  63. if (segment->filename) {
  64. uptr len =
  65. Min((uptr)(next_line - data_.current), segment->filename_size - 1);
  66. internal_strncpy(segment->filename, data_.current, len);
  67. segment->filename[len] = 0;
  68. }
  69. data_.current = next_line + 1;
  70. return true;
  71. }
  72. } // namespace __sanitizer
  73. #endif // SANITIZER_LINUX