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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
base / memory / shared_memory_mapping.cc [blame]
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
#pragma allow_unsafe_buffers
#endif
#include "base/memory/shared_memory_mapping.h"
#include <cstdint>
#include <utility>
#include "base/bits.h"
#include "base/logging.h"
#include "base/memory/shared_memory_security_policy.h"
#include "base/memory/shared_memory_tracker.h"
#include "base/system/sys_info.h"
#include "base/unguessable_token.h"
#include "build/build_config.h"
namespace base {
SharedMemoryMapping::SharedMemoryMapping() = default;
SharedMemoryMapping::SharedMemoryMapping(SharedMemoryMapping&& mapping) noexcept
: mapped_span_(std::exchange(mapping.mapped_span_, span<uint8_t>())),
size_(mapping.size_),
guid_(mapping.guid_),
mapper_(mapping.mapper_) {}
SharedMemoryMapping& SharedMemoryMapping::operator=(
SharedMemoryMapping&& mapping) noexcept {
Unmap();
mapped_span_ = std::exchange(mapping.mapped_span_, span<uint8_t>());
size_ = mapping.size_;
guid_ = mapping.guid_;
mapper_ = mapping.mapper_;
return *this;
}
SharedMemoryMapping::~SharedMemoryMapping() {
Unmap();
}
SharedMemoryMapping::SharedMemoryMapping(span<uint8_t> mapped_span,
size_t size,
const UnguessableToken& guid,
SharedMemoryMapper* mapper)
: mapped_span_(mapped_span), size_(size), guid_(guid), mapper_(mapper) {
CHECK_LE(size_, mapped_span_.size());
// Note: except on Windows, `mapped_span_.size() == size_`.
SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this);
}
void SharedMemoryMapping::Unmap() {
if (!IsValid())
return;
SharedMemorySecurityPolicy::ReleaseReservationForMapping(size_);
SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
SharedMemoryMapper* mapper = mapper_;
if (!mapper)
mapper = SharedMemoryMapper::GetDefaultInstance();
// The backing mapper expects offset to be aligned to
// `SysInfo::VMAllocationGranularity()`, so replicate the alignment that was
// done when originally mapping in the region.
uint8_t* aligned_data =
bits::AlignDown(mapped_span_.data(), SysInfo::VMAllocationGranularity());
size_t adjusted_size =
mapped_span_.size() +
static_cast<size_t>(mapped_span_.data() - aligned_data);
mapper->Unmap(span(aligned_data, adjusted_size));
}
ReadOnlySharedMemoryMapping::ReadOnlySharedMemoryMapping() = default;
ReadOnlySharedMemoryMapping::ReadOnlySharedMemoryMapping(
ReadOnlySharedMemoryMapping&&) noexcept = default;
ReadOnlySharedMemoryMapping& ReadOnlySharedMemoryMapping::operator=(
ReadOnlySharedMemoryMapping&&) noexcept = default;
ReadOnlySharedMemoryMapping::ReadOnlySharedMemoryMapping(
span<uint8_t> mapped_span,
size_t size,
const UnguessableToken& guid,
SharedMemoryMapper* mapper)
: SharedMemoryMapping(mapped_span, size, guid, mapper) {}
WritableSharedMemoryMapping::WritableSharedMemoryMapping() = default;
WritableSharedMemoryMapping::WritableSharedMemoryMapping(
WritableSharedMemoryMapping&&) noexcept = default;
WritableSharedMemoryMapping& WritableSharedMemoryMapping::operator=(
WritableSharedMemoryMapping&&) noexcept = default;
WritableSharedMemoryMapping::WritableSharedMemoryMapping(
span<uint8_t> mapped_span,
size_t size,
const UnguessableToken& guid,
SharedMemoryMapper* mapper)
: SharedMemoryMapping(mapped_span, size, guid, mapper) {}
} // namespace base