aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16-rt/lib/sanitizer_common/sanitizer_procmaps_fuchsia.cpp
blob: 815de7e6c71cd2b2b3cf33f038f42459aaf9a439 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//===-- sanitizer_procmaps_fuchsia.cpp
//----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Information about the process mappings (Fuchsia-specific parts).
//===----------------------------------------------------------------------===//

#include "sanitizer_platform.h"
#if SANITIZER_FUCHSIA
#error #include <zircon/process.h>
#error #include <zircon/syscalls.h>

#include "sanitizer_common.h"
#include "sanitizer_procmaps.h"

namespace __sanitizer {

// The cache flag is ignored on Fuchsia because a process can always get this
// information via its process-self handle.
MemoryMappingLayout::MemoryMappingLayout(bool) { Reset(); }

void MemoryMappingLayout::Reset() {
  data_.data.clear();
  data_.current = 0;

  size_t count;
  zx_status_t status = _zx_object_get_info(
      _zx_process_self(), ZX_INFO_PROCESS_MAPS, nullptr, 0, nullptr, &count);
  if (status != ZX_OK) {
    return;
  }

  size_t filled;
  do {
    data_.data.resize(count);
    status = _zx_object_get_info(
        _zx_process_self(), ZX_INFO_PROCESS_MAPS, data_.data.data(),
        count * sizeof(zx_info_maps_t), &filled, &count);
    if (status != ZX_OK) {
      data_.data.clear();
      return;
    }
  } while (filled < count);
}

MemoryMappingLayout::~MemoryMappingLayout() {}

bool MemoryMappingLayout::Error() const { return data_.data.empty(); }

bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
  while (data_.current < data_.data.size()) {
    const auto &entry = data_.data[data_.current++];
    if (entry.type == ZX_INFO_MAPS_TYPE_MAPPING) {
      segment->start = entry.base;
      segment->end = entry.base + entry.size;
      segment->offset = entry.u.mapping.vmo_offset;
      const auto flags = entry.u.mapping.mmu_flags;
      segment->protection =
          ((flags & ZX_VM_PERM_READ) ? kProtectionRead : 0) |
          ((flags & ZX_VM_PERM_WRITE) ? kProtectionWrite : 0) |
          ((flags & ZX_VM_PERM_EXECUTE) ? kProtectionExecute : 0);
      if (segment->filename && segment->filename_size > 0) {
        uptr len = Min(sizeof(entry.name), segment->filename_size) - 1;
        internal_strncpy(segment->filename, entry.name, len);
        segment->filename[len] = 0;
      }
      return true;
    }
  }
  return false;
}

}  // namespace __sanitizer

#endif  // SANITIZER_FUCHSIA