sanitizer_procmaps_fuchsia.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //===-- sanitizer_procmaps_fuchsia.cpp
  2. //----------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // Information about the process mappings (Fuchsia-specific parts).
  11. //===----------------------------------------------------------------------===//
  12. #include "sanitizer_platform.h"
  13. #if SANITIZER_FUCHSIA
  14. #error #include <zircon/process.h>
  15. #error #include <zircon/syscalls.h>
  16. #include "sanitizer_common.h"
  17. #include "sanitizer_procmaps.h"
  18. namespace __sanitizer {
  19. // The cache flag is ignored on Fuchsia because a process can always get this
  20. // information via its process-self handle.
  21. MemoryMappingLayout::MemoryMappingLayout(bool) { Reset(); }
  22. void MemoryMappingLayout::Reset() {
  23. data_.data.clear();
  24. data_.current = 0;
  25. size_t count;
  26. zx_status_t status = _zx_object_get_info(
  27. _zx_process_self(), ZX_INFO_PROCESS_MAPS, nullptr, 0, nullptr, &count);
  28. if (status != ZX_OK) {
  29. return;
  30. }
  31. size_t filled;
  32. do {
  33. data_.data.resize(count);
  34. status = _zx_object_get_info(
  35. _zx_process_self(), ZX_INFO_PROCESS_MAPS, data_.data.data(),
  36. count * sizeof(zx_info_maps_t), &filled, &count);
  37. if (status != ZX_OK) {
  38. data_.data.clear();
  39. return;
  40. }
  41. } while (filled < count);
  42. }
  43. MemoryMappingLayout::~MemoryMappingLayout() {}
  44. bool MemoryMappingLayout::Error() const { return data_.data.empty(); }
  45. bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
  46. while (data_.current < data_.data.size()) {
  47. const auto &entry = data_.data[data_.current++];
  48. if (entry.type == ZX_INFO_MAPS_TYPE_MAPPING) {
  49. segment->start = entry.base;
  50. segment->end = entry.base + entry.size;
  51. segment->offset = entry.u.mapping.vmo_offset;
  52. const auto flags = entry.u.mapping.mmu_flags;
  53. segment->protection =
  54. ((flags & ZX_VM_PERM_READ) ? kProtectionRead : 0) |
  55. ((flags & ZX_VM_PERM_WRITE) ? kProtectionWrite : 0) |
  56. ((flags & ZX_VM_PERM_EXECUTE) ? kProtectionExecute : 0);
  57. if (segment->filename && segment->filename_size > 0) {
  58. uptr len = Min(sizeof(entry.name), segment->filename_size) - 1;
  59. internal_strncpy(segment->filename, entry.name, len);
  60. segment->filename[len] = 0;
  61. }
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. } // namespace __sanitizer
  68. #endif // SANITIZER_FUCHSIA