Skip to content

Commit 81b700e

Browse files
frobtechpetrhosek
authored andcommitted
[sanitizer_common] Implement MemoryMappingLayout for Fuchsia
This is needed to port lsan to Fuchsia. Patch By: mcgrathr Differential Revision: https://reviews.llvm.org/D72886
1 parent 303fdde commit 81b700e

File tree

4 files changed

+91
-3
lines changed

4 files changed

+91
-3
lines changed

compiler-rt/lib/sanitizer_common/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ set(SANITIZER_SOURCES_NOTERMINATION
2929
sanitizer_printf.cpp
3030
sanitizer_procmaps_common.cpp
3131
sanitizer_procmaps_bsd.cpp
32+
sanitizer_procmaps_fuchsia.cpp
3233
sanitizer_procmaps_linux.cpp
3334
sanitizer_procmaps_mac.cpp
3435
sanitizer_procmaps_solaris.cpp

compiler-rt/lib/sanitizer_common/sanitizer_fuchsia.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,18 @@
1818
#include "sanitizer_common.h"
1919

2020
#include <zircon/sanitizer.h>
21+
#include <zircon/syscalls/object.h>
2122

2223
namespace __sanitizer {
2324

2425
extern uptr MainThreadStackBase, MainThreadStackSize;
2526
extern sanitizer_shadow_bounds_t ShadowBounds;
2627

28+
struct MemoryMappingLayoutData {
29+
InternalMmapVector<zx_info_maps_t> data;
30+
size_t current; // Current index into the vector.
31+
};
32+
2733
} // namespace __sanitizer
2834

2935
#endif // SANITIZER_FUCHSIA

compiler-rt/lib/sanitizer_common/sanitizer_procmaps.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,19 @@
1515

1616
#include "sanitizer_platform.h"
1717

18-
#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
19-
SANITIZER_OPENBSD || SANITIZER_MAC || SANITIZER_SOLARIS
18+
#if SANITIZER_LINUX || SANITIZER_FREEBSD || SANITIZER_NETBSD || \
19+
SANITIZER_OPENBSD || SANITIZER_MAC || SANITIZER_SOLARIS || \
20+
SANITIZER_FUCHSIA
2021

2122
#include "sanitizer_common.h"
2223
#include "sanitizer_internal_defs.h"
24+
#include "sanitizer_fuchsia.h"
2325
#include "sanitizer_linux.h"
2426
#include "sanitizer_mac.h"
2527
#include "sanitizer_mutex.h"
2628

2729
namespace __sanitizer {
2830

29-
3031
// Memory protection masks.
3132
static const uptr kProtectionRead = 1;
3233
static const uptr kProtectionWrite = 2;
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
13+
#include "sanitizer_platform.h"
14+
#if SANITIZER_FUCHSIA
15+
#include <zircon/process.h>
16+
#include <zircon/syscalls.h>
17+
18+
#include "sanitizer_common.h"
19+
#include "sanitizer_procmaps.h"
20+
21+
namespace __sanitizer {
22+
23+
// The cache flag is ignored on Fuchsia because a process can always get this
24+
// information via its process-self handle.
25+
MemoryMappingLayout::MemoryMappingLayout(bool) { Reset(); }
26+
27+
void MemoryMappingLayout::Reset() {
28+
data_.data.clear();
29+
data_.current = 0;
30+
31+
size_t count;
32+
zx_status_t status = _zx_object_get_info(
33+
_zx_process_self(), ZX_INFO_PROCESS_MAPS, nullptr, 0, nullptr, &count);
34+
if (status != ZX_OK) {
35+
return;
36+
}
37+
38+
size_t filled;
39+
do {
40+
data_.data.resize(count);
41+
status = _zx_object_get_info(
42+
_zx_process_self(), ZX_INFO_PROCESS_MAPS, data_.data.data(),
43+
count * sizeof(zx_info_maps_t), &filled, &count);
44+
if (status != ZX_OK) {
45+
data_.data.clear();
46+
return;
47+
}
48+
} while (filled < count);
49+
}
50+
51+
MemoryMappingLayout::~MemoryMappingLayout() {}
52+
53+
bool MemoryMappingLayout::Error() const { return data_.data.empty(); }
54+
55+
bool MemoryMappingLayout::Next(MemoryMappedSegment *segment) {
56+
while (data_.current < data_.data.size()) {
57+
const auto &entry = data_.data[data_.current++];
58+
if (entry.type == ZX_INFO_MAPS_TYPE_MAPPING) {
59+
segment->start = entry.base;
60+
segment->end = entry.base + entry.size;
61+
segment->offset = entry.u.mapping.vmo_offset;
62+
const auto flags = entry.u.mapping.mmu_flags;
63+
segment->protection =
64+
((flags & ZX_VM_PERM_READ) ? kProtectionRead : 0) |
65+
((flags & ZX_VM_PERM_WRITE) ? kProtectionWrite : 0) |
66+
((flags & ZX_VM_PERM_EXECUTE) ? kProtectionExecute : 0);
67+
if (segment->filename && segment->filename_size > 0) {
68+
uptr len = Min(sizeof(entry.name), segment->filename_size) - 1;
69+
internal_strncpy(segment->filename, entry.name, len);
70+
segment->filename[len] = 0;
71+
}
72+
return true;
73+
}
74+
}
75+
return false;
76+
}
77+
78+
} // namespace __sanitizer
79+
80+
#endif // SANITIZER_FUCHSIA

0 commit comments

Comments
 (0)