OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/edk/system/transport_data.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <utility> | |
11 | |
12 #include "base/logging.h" | |
13 #include "mojo/edk/system/configuration.h" | |
14 #include "mojo/edk/system/message_in_transit.h" | |
15 #include "mojo/edk/system/raw_channel.h" | |
16 | |
17 namespace mojo { | |
18 namespace edk { | |
19 | |
20 // The maximum amount of space needed per platform handle. | |
21 // (|{Channel,RawChannel}::GetSerializedPlatformHandleSize()| should always | |
22 // return a value which is at most this. This is only used to calculate | |
23 // |TransportData::kMaxBufferSize|. This value should be a multiple of the | |
24 // alignment in order to simplify calculations, even though the actual amount of | |
25 // space needed need not be a multiple of the alignment. | |
26 const size_t kMaxSizePerPlatformHandle = 8; | |
27 static_assert(kMaxSizePerPlatformHandle % MessageInTransit::kMessageAlignment == | |
28 0, | |
29 "kMaxSizePerPlatformHandle not a multiple of alignment"); | |
30 | |
31 MOJO_STATIC_CONST_MEMBER_DEFINITION const size_t | |
32 TransportData::kMaxSerializedDispatcherSize; | |
33 | |
34 // static | |
35 size_t TransportData::GetMaxBufferSize() { | |
36 // In additional to the header, for each attached (Mojo) handle there'll be a | |
37 // handle table entry and serialized dispatcher data. | |
38 return sizeof(Header) + | |
39 GetConfiguration().max_message_num_handles * | |
40 (sizeof(HandleTableEntry) + kMaxSerializedDispatcherSize) + | |
41 GetMaxPlatformHandles() * kMaxSizePerPlatformHandle; | |
42 } | |
43 | |
44 // static | |
45 size_t TransportData::GetMaxPlatformHandles() { | |
46 return GetConfiguration().max_message_num_handles; | |
47 } | |
48 | |
49 struct TransportData::PrivateStructForCompileAsserts { | |
50 static_assert(sizeof(Header) % MessageInTransit::kMessageAlignment == 0, | |
51 "sizeof(MessageInTransit::Header) not a multiple of alignment"); | |
52 static_assert(kMaxSerializedDispatcherSize % | |
53 MessageInTransit::kMessageAlignment == | |
54 0, | |
55 "kMaxSerializedDispatcherSize not a multiple of alignment"); | |
56 static_assert(sizeof(HandleTableEntry) % | |
57 MessageInTransit::kMessageAlignment == | |
58 0, | |
59 "sizeof(MessageInTransit::HandleTableEntry) not a multiple of " | |
60 "alignment"); | |
61 }; | |
62 | |
63 TransportData::TransportData(scoped_ptr<DispatcherVector> dispatchers) | |
64 : buffer_size_() { | |
65 DCHECK(dispatchers); | |
66 | |
67 const size_t num_handles = dispatchers->size(); | |
68 DCHECK_GT(num_handles, 0u); | |
69 | |
70 // The offset to the start of the (Mojo) handle table. | |
71 const size_t handle_table_start_offset = sizeof(Header); | |
72 // The offset to the start of the serialized dispatcher data. | |
73 const size_t serialized_dispatcher_start_offset = | |
74 handle_table_start_offset + num_handles * sizeof(HandleTableEntry); | |
75 // The estimated size of the secondary buffer. We compute this estimate below. | |
76 // It must be at least as big as the (eventual) actual size. | |
77 size_t estimated_size = serialized_dispatcher_start_offset; | |
78 size_t estimated_num_platform_handles = 0; | |
79 #if DCHECK_IS_ON() | |
80 std::vector<size_t> all_max_sizes(num_handles); | |
81 std::vector<size_t> all_max_platform_handles(num_handles); | |
82 #endif | |
83 for (size_t i = 0; i < num_handles; i++) { | |
84 if (Dispatcher* dispatcher = (*dispatchers)[i].get()) { | |
85 size_t max_size = 0; | |
86 size_t max_platform_handles = 0; | |
87 Dispatcher::TransportDataAccess::StartSerialize( | |
88 dispatcher, &max_size, &max_platform_handles); | |
89 | |
90 DCHECK_LE(max_size, kMaxSerializedDispatcherSize); | |
91 estimated_size += MessageInTransit::RoundUpMessageAlignment(max_size); | |
92 DCHECK_LE(estimated_size, GetMaxBufferSize()); | |
93 | |
94 estimated_num_platform_handles += max_platform_handles; | |
95 // We don't expect more than 10K Mojo handles in one process at a time, | |
96 // since each is backed by a FD. If we're hitting the check below, we have | |
97 // bigger problems of reducing the number of FDs or possibly multiplexing. | |
98 CHECK_LE(estimated_num_platform_handles, GetMaxPlatformHandles()); | |
99 | |
100 #if DCHECK_IS_ON() | |
101 all_max_sizes[i] = max_size; | |
102 all_max_platform_handles[i] = max_platform_handles; | |
103 #endif | |
104 } | |
105 } | |
106 | |
107 size_t size_per_platform_handle = 0; | |
108 if (estimated_num_platform_handles > 0) { | |
109 size_per_platform_handle = RawChannel::GetSerializedPlatformHandleSize(); | |
110 DCHECK_LE(size_per_platform_handle, kMaxSizePerPlatformHandle); | |
111 estimated_size += estimated_num_platform_handles * size_per_platform_handle; | |
112 estimated_size = MessageInTransit::RoundUpMessageAlignment(estimated_size); | |
113 DCHECK_LE(estimated_size, GetMaxBufferSize()); | |
114 } | |
115 | |
116 buffer_.reset(static_cast<char*>( | |
117 base::AlignedAlloc(estimated_size, MessageInTransit::kMessageAlignment))); | |
118 // Entirely clear out the secondary buffer, since then we won't have to worry | |
119 // about clearing padding or unused space (e.g., if a dispatcher fails to | |
120 // serialize). | |
121 memset(buffer_.get(), 0, estimated_size); | |
122 | |
123 if (estimated_num_platform_handles > 0) { | |
124 DCHECK(!platform_handles_); | |
125 platform_handles_.reset(new PlatformHandleVector()); | |
126 } | |
127 | |
128 Header* header = reinterpret_cast<Header*>(buffer_.get()); | |
129 header->num_handles = static_cast<uint32_t>(num_handles); | |
130 // (Okay to leave |platform_handle_table_offset|, |num_platform_handles|, and | |
131 // |unused| be zero; we'll set the former two later if necessary.) | |
132 | |
133 HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>( | |
134 buffer_.get() + handle_table_start_offset); | |
135 size_t current_offset = serialized_dispatcher_start_offset; | |
136 for (size_t i = 0; i < num_handles; i++) { | |
137 Dispatcher* dispatcher = (*dispatchers)[i].get(); | |
138 if (!dispatcher) { | |
139 static_assert(static_cast<int32_t>(Dispatcher::Type::UNKNOWN) == 0, | |
140 "Value of Dispatcher::Type::UNKNOWN must be 0"); | |
141 continue; | |
142 } | |
143 | |
144 #if DCHECK_IS_ON() | |
145 size_t old_platform_handles_size = | |
146 platform_handles_ ? platform_handles_->size() : 0; | |
147 #endif | |
148 | |
149 void* destination = buffer_.get() + current_offset; | |
150 size_t actual_size = 0; | |
151 if (Dispatcher::TransportDataAccess::EndSerializeAndClose( | |
152 dispatcher, destination, &actual_size, | |
153 platform_handles_.get())) { | |
154 handle_table[i].type = static_cast<int32_t>(dispatcher->GetType()); | |
155 handle_table[i].offset = static_cast<uint32_t>(current_offset); | |
156 handle_table[i].size = static_cast<uint32_t>(actual_size); | |
157 // (Okay to not set |unused| since we cleared the entire buffer.) | |
158 | |
159 #if DCHECK_IS_ON() | |
160 DCHECK_LE(actual_size, all_max_sizes[i]); | |
161 DCHECK_LE(platform_handles_ | |
162 ? (platform_handles_->size() - old_platform_handles_size) | |
163 : 0, | |
164 all_max_platform_handles[i]); | |
165 #endif | |
166 } else { | |
167 // Nothing to do on failure, since |buffer_| was cleared, and | |
168 // |Type::UNKNOWN| is zero. The handle was simply closed. | |
169 LOG(ERROR) << "Failed to serialize handle to remote message pipe"; | |
170 } | |
171 | |
172 current_offset += MessageInTransit::RoundUpMessageAlignment(actual_size); | |
173 DCHECK_LE(current_offset, estimated_size); | |
174 DCHECK_LE(platform_handles_ ? platform_handles_->size() : 0, | |
175 estimated_num_platform_handles); | |
176 } | |
177 | |
178 if (platform_handles_ && platform_handles_->size() > 0) { | |
179 header->platform_handle_table_offset = | |
180 static_cast<uint32_t>(current_offset); | |
181 header->num_platform_handles = | |
182 static_cast<uint32_t>(platform_handles_->size()); | |
183 current_offset += platform_handles_->size() * size_per_platform_handle; | |
184 current_offset = MessageInTransit::RoundUpMessageAlignment(current_offset); | |
185 } | |
186 | |
187 // There's no aligned realloc, so it's no good way to release unused space (if | |
188 // we overshot our estimated space requirements). | |
189 buffer_size_ = current_offset; | |
190 | |
191 // |dispatchers_| will be destroyed as it goes out of scope. | |
192 } | |
193 | |
194 TransportData::TransportData(ScopedPlatformHandleVectorPtr platform_handles, | |
195 size_t serialized_platform_handle_size) | |
196 : buffer_size_(), platform_handles_(std::move(platform_handles)) { | |
197 buffer_size_ = MessageInTransit::RoundUpMessageAlignment( | |
198 sizeof(Header) + | |
199 platform_handles_->size() * serialized_platform_handle_size); | |
200 buffer_.reset(static_cast<char*>( | |
201 base::AlignedAlloc(buffer_size_, MessageInTransit::kMessageAlignment))); | |
202 memset(buffer_.get(), 0, buffer_size_); | |
203 | |
204 Header* header = reinterpret_cast<Header*>(buffer_.get()); | |
205 header->platform_handle_table_offset = static_cast<uint32_t>(sizeof(Header)); | |
206 header->num_platform_handles = | |
207 static_cast<uint32_t>(platform_handles_->size()); | |
208 } | |
209 | |
210 TransportData::~TransportData() { | |
211 } | |
212 | |
213 // static | |
214 const char* TransportData::ValidateBuffer( | |
215 size_t serialized_platform_handle_size, | |
216 const void* buffer, | |
217 size_t buffer_size) { | |
218 DCHECK(buffer); | |
219 DCHECK_GT(buffer_size, 0u); | |
220 | |
221 // Always make sure that the buffer size is sane; if it's not, someone's | |
222 // messing with us. | |
223 if (buffer_size < sizeof(Header) || buffer_size > GetMaxBufferSize() || | |
224 buffer_size % MessageInTransit::kMessageAlignment != 0) | |
225 return "Invalid message secondary buffer size"; | |
226 | |
227 const Header* header = static_cast<const Header*>(buffer); | |
228 const size_t num_handles = header->num_handles; | |
229 | |
230 // Sanity-check |num_handles| (before multiplying it against anything). | |
231 if (num_handles > GetConfiguration().max_message_num_handles) | |
232 return "Message handle payload too large"; | |
233 | |
234 if (buffer_size < sizeof(Header) + num_handles * sizeof(HandleTableEntry)) | |
235 return "Message secondary buffer too small"; | |
236 | |
237 if (header->num_platform_handles == 0) { | |
238 // Then |platform_handle_table_offset| should also be zero. | |
239 if (header->platform_handle_table_offset != 0) { | |
240 return "Message has no handles attached, but platform handle table " | |
241 "present"; | |
242 } | |
243 } else { | |
244 if (header->num_platform_handles > GetMaxPlatformHandles()) | |
245 return "Message has too many platform handles attached"; | |
246 | |
247 static const char kInvalidPlatformHandleTableOffset[] = | |
248 "Message has invalid platform handle table offset"; | |
249 // This doesn't check that the platform handle table doesn't alias other | |
250 // stuff, but it doesn't matter, since it's all read-only. | |
251 if (header->platform_handle_table_offset % | |
252 MessageInTransit::kMessageAlignment != | |
253 0) | |
254 return kInvalidPlatformHandleTableOffset; | |
255 | |
256 // ">" instead of ">=" since the size per handle may be zero. | |
257 if (header->platform_handle_table_offset > buffer_size) | |
258 return kInvalidPlatformHandleTableOffset; | |
259 | |
260 // We already checked |platform_handle_table_offset| and | |
261 // |num_platform_handles|, so the addition and multiplication are okay. | |
262 if (header->platform_handle_table_offset + | |
263 header->num_platform_handles * serialized_platform_handle_size > | |
264 buffer_size) | |
265 return kInvalidPlatformHandleTableOffset; | |
266 } | |
267 | |
268 const HandleTableEntry* handle_table = | |
269 reinterpret_cast<const HandleTableEntry*>( | |
270 static_cast<const char*>(buffer) + sizeof(Header)); | |
271 static const char kInvalidSerializedDispatcher[] = | |
272 "Message contains invalid serialized dispatcher"; | |
273 for (size_t i = 0; i < num_handles; i++) { | |
274 size_t offset = handle_table[i].offset; | |
275 if (offset % MessageInTransit::kMessageAlignment != 0) | |
276 return kInvalidSerializedDispatcher; | |
277 | |
278 size_t size = handle_table[i].size; | |
279 if (size > kMaxSerializedDispatcherSize || size > buffer_size) | |
280 return kInvalidSerializedDispatcher; | |
281 | |
282 // Note: This is an overflow-safe check for |offset + size > buffer_size| | |
283 // (we know that |size <= buffer_size| from the previous check). | |
284 if (offset > buffer_size - size) | |
285 return kInvalidSerializedDispatcher; | |
286 } | |
287 | |
288 return nullptr; | |
289 } | |
290 | |
291 // static | |
292 void TransportData::GetPlatformHandleTable(const void* transport_data_buffer, | |
293 size_t* num_platform_handles, | |
294 const void** platform_handle_table) { | |
295 DCHECK(transport_data_buffer); | |
296 DCHECK(num_platform_handles); | |
297 DCHECK(platform_handle_table); | |
298 | |
299 const Header* header = static_cast<const Header*>(transport_data_buffer); | |
300 *num_platform_handles = header->num_platform_handles; | |
301 *platform_handle_table = static_cast<const char*>(transport_data_buffer) + | |
302 header->platform_handle_table_offset; | |
303 } | |
304 | |
305 // static | |
306 scoped_ptr<DispatcherVector> TransportData::DeserializeDispatchers( | |
307 const void* buffer, | |
308 size_t buffer_size, | |
309 ScopedPlatformHandleVectorPtr platform_handles) { | |
310 DCHECK(buffer); | |
311 DCHECK_GT(buffer_size, 0u); | |
312 | |
313 const Header* header = static_cast<const Header*>(buffer); | |
314 const size_t num_handles = header->num_handles; | |
315 scoped_ptr<DispatcherVector> dispatchers(new DispatcherVector(num_handles)); | |
316 | |
317 const HandleTableEntry* handle_table = | |
318 reinterpret_cast<const HandleTableEntry*>( | |
319 static_cast<const char*>(buffer) + sizeof(Header)); | |
320 for (size_t i = 0; i < num_handles; i++) { | |
321 size_t offset = handle_table[i].offset; | |
322 size_t size = handle_table[i].size; | |
323 // Should already have been checked by |ValidateBuffer()|: | |
324 DCHECK_EQ(offset % MessageInTransit::kMessageAlignment, 0u); | |
325 DCHECK_LE(offset, buffer_size); | |
326 DCHECK_LE(offset + size, buffer_size); | |
327 | |
328 const void* source = static_cast<const char*>(buffer) + offset; | |
329 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( | |
330 handle_table[i].type, source, size, platform_handles.get()); | |
331 } | |
332 | |
333 return dispatchers; | |
334 } | |
335 | |
336 } // namespace edk | |
337 } // namespace mojo | |
OLD | NEW |