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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/system/shared_buffer_dispatcher.h"
#include <limits>
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "mojo/embedder/platform_support.h"
#include "mojo/public/c/system/macros.h"
#include "mojo/system/channel.h"
#include "mojo/system/constants.h"
#include "mojo/system/memory.h"
#include "mojo/system/options_validation.h"
namespace mojo {
namespace system {
namespace {
struct SerializedSharedBufferDispatcher {
size_t num_bytes;
size_t platform_handle_index;
};
} // namespace
// static
const MojoCreateSharedBufferOptions
SharedBufferDispatcher::kDefaultCreateOptions = {
static_cast<uint32_t>(sizeof(MojoCreateSharedBufferOptions)),
MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE};
// static
MojoResult SharedBufferDispatcher::ValidateCreateOptions(
UserPointer<const MojoCreateSharedBufferOptions> in_options,
MojoCreateSharedBufferOptions* out_options) {
const MojoCreateSharedBufferOptionsFlags kKnownFlags =
MOJO_CREATE_SHARED_BUFFER_OPTIONS_FLAG_NONE;
*out_options = kDefaultCreateOptions;
if (in_options.IsNull())
return MOJO_RESULT_OK;
UserOptionsReader<MojoCreateSharedBufferOptions> reader(in_options);
if (!reader.is_valid())
return MOJO_RESULT_INVALID_ARGUMENT;
if (!OPTIONS_STRUCT_HAS_MEMBER(MojoCreateSharedBufferOptions, flags, reader))
return MOJO_RESULT_OK;
if ((reader.options().flags & ~kKnownFlags))
return MOJO_RESULT_UNIMPLEMENTED;
out_options->flags = reader.options().flags;
// Checks for fields beyond |flags|:
// (Nothing here yet.)
return MOJO_RESULT_OK;
}
// static
MojoResult SharedBufferDispatcher::Create(
embedder::PlatformSupport* platform_support,
const MojoCreateSharedBufferOptions& /*validated_options*/,
uint64_t num_bytes,
scoped_refptr<SharedBufferDispatcher>* result) {
if (!num_bytes)
return MOJO_RESULT_INVALID_ARGUMENT;
if (num_bytes > kMaxSharedMemoryNumBytes)
return MOJO_RESULT_RESOURCE_EXHAUSTED;
scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer(
platform_support->CreateSharedBuffer(static_cast<size_t>(num_bytes)));
if (!shared_buffer)
return MOJO_RESULT_RESOURCE_EXHAUSTED;
*result = new SharedBufferDispatcher(shared_buffer);
return MOJO_RESULT_OK;
}
Dispatcher::Type SharedBufferDispatcher::GetType() const {
return kTypeSharedBuffer;
}
// static
scoped_refptr<SharedBufferDispatcher> SharedBufferDispatcher::Deserialize(
Channel* channel,
const void* source,
size_t size,
embedder::PlatformHandleVector* platform_handles) {
DCHECK(channel);
if (size != sizeof(SerializedSharedBufferDispatcher)) {
LOG(ERROR) << "Invalid serialized shared buffer dispatcher (bad size)";
return scoped_refptr<SharedBufferDispatcher>();
}
const SerializedSharedBufferDispatcher* serialization =
static_cast<const SerializedSharedBufferDispatcher*>(source);
size_t num_bytes = serialization->num_bytes;
size_t platform_handle_index = serialization->platform_handle_index;
if (!num_bytes) {
LOG(ERROR)
<< "Invalid serialized shared buffer dispatcher (invalid num_bytes)";
return scoped_refptr<SharedBufferDispatcher>();
}
if (!platform_handles || platform_handle_index >= platform_handles->size()) {
LOG(ERROR)
<< "Invalid serialized shared buffer dispatcher (missing handles)";
return scoped_refptr<SharedBufferDispatcher>();
}
// Starts off invalid, which is what we want.
embedder::PlatformHandle platform_handle;
// We take ownership of the handle, so we have to invalidate the one in
// |platform_handles|.
std::swap(platform_handle, (*platform_handles)[platform_handle_index]);
// Wrapping |platform_handle| in a |ScopedPlatformHandle| means that it'll be
// closed even if creation fails.
scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer(
channel->platform_support()->CreateSharedBufferFromHandle(
num_bytes, embedder::ScopedPlatformHandle(platform_handle)));
if (!shared_buffer) {
LOG(ERROR)
<< "Invalid serialized shared buffer dispatcher (invalid num_bytes?)";
return scoped_refptr<SharedBufferDispatcher>();
}
return scoped_refptr<SharedBufferDispatcher>(
new SharedBufferDispatcher(shared_buffer));
}
SharedBufferDispatcher::SharedBufferDispatcher(
scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer)
: shared_buffer_(shared_buffer) {
DCHECK(shared_buffer_);
}
SharedBufferDispatcher::~SharedBufferDispatcher() {
}
// static
MojoResult SharedBufferDispatcher::ValidateDuplicateOptions(
UserPointer<const MojoDuplicateBufferHandleOptions> in_options,
MojoDuplicateBufferHandleOptions* out_options) {
const MojoDuplicateBufferHandleOptionsFlags kKnownFlags =
MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE;
static const MojoDuplicateBufferHandleOptions kDefaultOptions = {
static_cast<uint32_t>(sizeof(MojoDuplicateBufferHandleOptions)),
MOJO_DUPLICATE_BUFFER_HANDLE_OPTIONS_FLAG_NONE};
*out_options = kDefaultOptions;
if (in_options.IsNull())
return MOJO_RESULT_OK;
UserOptionsReader<MojoDuplicateBufferHandleOptions> reader(in_options);
if (!reader.is_valid())
return MOJO_RESULT_INVALID_ARGUMENT;
if (!OPTIONS_STRUCT_HAS_MEMBER(
MojoDuplicateBufferHandleOptions, flags, reader))
return MOJO_RESULT_OK;
if ((reader.options().flags & ~kKnownFlags))
return MOJO_RESULT_UNIMPLEMENTED;
out_options->flags = reader.options().flags;
// Checks for fields beyond |flags|:
// (Nothing here yet.)
return MOJO_RESULT_OK;
}
void SharedBufferDispatcher::CloseImplNoLock() {
lock().AssertAcquired();
DCHECK(shared_buffer_);
shared_buffer_ = NULL;
}
scoped_refptr<Dispatcher>
SharedBufferDispatcher::CreateEquivalentDispatcherAndCloseImplNoLock() {
lock().AssertAcquired();
DCHECK(shared_buffer_);
scoped_refptr<embedder::PlatformSharedBuffer> shared_buffer;
shared_buffer.swap(shared_buffer_);
return scoped_refptr<Dispatcher>(new SharedBufferDispatcher(shared_buffer));
}
MojoResult SharedBufferDispatcher::DuplicateBufferHandleImplNoLock(
UserPointer<const MojoDuplicateBufferHandleOptions> options,
scoped_refptr<Dispatcher>* new_dispatcher) {
lock().AssertAcquired();
MojoDuplicateBufferHandleOptions validated_options;
MojoResult result = ValidateDuplicateOptions(options, &validated_options);
if (result != MOJO_RESULT_OK)
return result;
*new_dispatcher = new SharedBufferDispatcher(shared_buffer_);
return MOJO_RESULT_OK;
}
MojoResult SharedBufferDispatcher::MapBufferImplNoLock(
uint64_t offset,
uint64_t num_bytes,
MojoMapBufferFlags flags,
scoped_ptr<embedder::PlatformSharedBufferMapping>* mapping) {
lock().AssertAcquired();
DCHECK(shared_buffer_);
if (offset > static_cast<uint64_t>(std::numeric_limits<size_t>::max()))
return MOJO_RESULT_INVALID_ARGUMENT;
if (num_bytes > static_cast<uint64_t>(std::numeric_limits<size_t>::max()))
return MOJO_RESULT_INVALID_ARGUMENT;
if (!shared_buffer_->IsValidMap(static_cast<size_t>(offset),
static_cast<size_t>(num_bytes)))
return MOJO_RESULT_INVALID_ARGUMENT;
DCHECK(mapping);
*mapping = shared_buffer_->MapNoCheck(static_cast<size_t>(offset),
static_cast<size_t>(num_bytes));
if (!*mapping)
return MOJO_RESULT_RESOURCE_EXHAUSTED;
return MOJO_RESULT_OK;
}
void SharedBufferDispatcher::StartSerializeImplNoLock(
Channel* /*channel*/,
size_t* max_size,
size_t* max_platform_handles) {
DCHECK(HasOneRef()); // Only one ref => no need to take the lock.
*max_size = sizeof(SerializedSharedBufferDispatcher);
*max_platform_handles = 1;
}
bool SharedBufferDispatcher::EndSerializeAndCloseImplNoLock(
Channel* /*channel*/,
void* destination,
size_t* actual_size,
embedder::PlatformHandleVector* platform_handles) {
DCHECK(HasOneRef()); // Only one ref => no need to take the lock.
DCHECK(shared_buffer_);
SerializedSharedBufferDispatcher* serialization =
static_cast<SerializedSharedBufferDispatcher*>(destination);
// If there's only one reference to |shared_buffer_|, then it's ours (and no
// one else can make any more references to it), so we can just take its
// handle.
embedder::ScopedPlatformHandle platform_handle(
shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle()
: shared_buffer_->DuplicatePlatformHandle());
if (!platform_handle.is_valid()) {
shared_buffer_ = NULL;
return false;
}
serialization->num_bytes = shared_buffer_->GetNumBytes();
serialization->platform_handle_index = platform_handles->size();
platform_handles->push_back(platform_handle.release());
*actual_size = sizeof(SerializedSharedBufferDispatcher);
shared_buffer_ = NULL;
return true;
}
} // namespace system
} // namespace mojo
|