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 <string.h> | |
8 | |
9 #include "base/compiler_specific.h" | |
10 #include "base/logging.h" | |
11 #include "mojo/edk/system/channel.h" | |
12 #include "mojo/edk/system/configuration.h" | |
13 #include "mojo/edk/system/message_in_transit.h" | |
14 | |
15 namespace mojo { | |
16 namespace system { | |
17 | |
18 // The maximum amount of space needed per platform handle. | |
19 // (|{Channel,RawChannel}::GetSerializedPlatformHandleSize()| should always | |
20 // return a value which is at most this. This is only used to calculate | |
21 // |TransportData::kMaxBufferSize|. This value should be a multiple of the | |
22 // alignment in order to simplify calculations, even though the actual amount of | |
23 // space needed need not be a multiple of the alignment. | |
24 const size_t kMaxSizePerPlatformHandle = 8; | |
25 static_assert(kMaxSizePerPlatformHandle % MessageInTransit::kMessageAlignment == | |
26 0, | |
27 "kMaxSizePerPlatformHandle not a multiple of alignment"); | |
28 | |
29 STATIC_CONST_MEMBER_DEFINITION const size_t | |
30 TransportData::kMaxSerializedDispatcherSize; | |
31 STATIC_CONST_MEMBER_DEFINITION const size_t | |
32 TransportData::kMaxSerializedDispatcherPlatformHandles; | |
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 kMaxSerializedDispatcherPlatformHandles; | |
48 } | |
49 | |
50 struct TransportData::PrivateStructForCompileAsserts { | |
51 static_assert(sizeof(Header) % MessageInTransit::kMessageAlignment == 0, | |
52 "sizeof(MessageInTransit::Header) not a multiple of alignment"); | |
53 static_assert(kMaxSerializedDispatcherSize % | |
54 MessageInTransit::kMessageAlignment == | |
55 0, | |
56 "kMaxSerializedDispatcherSize not a multiple of alignment"); | |
57 static_assert(sizeof(HandleTableEntry) % | |
58 MessageInTransit::kMessageAlignment == | |
59 0, | |
60 "sizeof(MessageInTransit::HandleTableEntry) not a multiple of " | |
61 "alignment"); | |
62 }; | |
63 | |
64 TransportData::TransportData(scoped_ptr<DispatcherVector> dispatchers, | |
65 Channel* channel) { | |
66 DCHECK(dispatchers); | |
67 DCHECK(channel); | |
68 | |
69 const size_t num_handles = dispatchers->size(); | |
70 DCHECK_GT(num_handles, 0u); | |
71 | |
72 // The offset to the start of the (Mojo) handle table. | |
73 const size_t handle_table_start_offset = sizeof(Header); | |
74 // The offset to the start of the serialized dispatcher data. | |
75 const size_t serialized_dispatcher_start_offset = | |
76 handle_table_start_offset + num_handles * sizeof(HandleTableEntry); | |
77 // The estimated size of the secondary buffer. We compute this estimate below. | |
78 // It must be at least as big as the (eventual) actual size. | |
79 size_t estimated_size = serialized_dispatcher_start_offset; | |
80 size_t estimated_num_platform_handles = 0; | |
81 #if DCHECK_IS_ON() | |
82 std::vector<size_t> all_max_sizes(num_handles); | |
83 std::vector<size_t> all_max_platform_handles(num_handles); | |
84 #endif | |
85 for (size_t i = 0; i < num_handles; i++) { | |
86 if (Dispatcher* dispatcher = (*dispatchers)[i].get()) { | |
87 size_t max_size = 0; | |
88 size_t max_platform_handles = 0; | |
89 Dispatcher::TransportDataAccess::StartSerialize( | |
90 dispatcher, channel, &max_size, &max_platform_handles); | |
91 | |
92 DCHECK_LE(max_size, kMaxSerializedDispatcherSize); | |
93 estimated_size += MessageInTransit::RoundUpMessageAlignment(max_size); | |
94 DCHECK_LE(estimated_size, GetMaxBufferSize()); | |
95 | |
96 DCHECK_LE(max_platform_handles, kMaxSerializedDispatcherPlatformHandles); | |
97 estimated_num_platform_handles += max_platform_handles; | |
98 DCHECK_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 = channel->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 embedder::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(Dispatcher::kTypeUnknown == 0, | |
140 "Value of Dispatcher::kTypeUnknown 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, channel, 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 // |kTypeUnknown| 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( | |
195 embedder::ScopedPlatformHandleVectorPtr platform_handles) | |
196 : buffer_size_(sizeof(Header)), platform_handles_(platform_handles.Pass()) { | |
197 buffer_.reset(static_cast<char*>( | |
198 base::AlignedAlloc(buffer_size_, MessageInTransit::kMessageAlignment))); | |
199 memset(buffer_.get(), 0, buffer_size_); | |
200 | |
201 Header* header = reinterpret_cast<Header*>(buffer_.get()); | |
202 header->num_platform_handles = | |
203 static_cast<uint32_t>(platform_handles_->size()); | |
204 } | |
205 | |
206 TransportData::~TransportData() { | |
207 } | |
208 | |
209 // static | |
210 const char* TransportData::ValidateBuffer( | |
211 size_t serialized_platform_handle_size, | |
212 const void* buffer, | |
213 size_t buffer_size) { | |
214 DCHECK(buffer); | |
215 DCHECK_GT(buffer_size, 0u); | |
216 | |
217 // Always make sure that the buffer size is sane; if it's not, someone's | |
218 // messing with us. | |
219 if (buffer_size < sizeof(Header) || buffer_size > GetMaxBufferSize() || | |
220 buffer_size % MessageInTransit::kMessageAlignment != 0) | |
221 return "Invalid message secondary buffer size"; | |
222 | |
223 const Header* header = static_cast<const Header*>(buffer); | |
224 const size_t num_handles = header->num_handles; | |
225 | |
226 // Sanity-check |num_handles| (before multiplying it against anything). | |
227 if (num_handles > GetConfiguration().max_message_num_handles) | |
228 return "Message handle payload too large"; | |
229 | |
230 if (buffer_size < sizeof(Header) + num_handles * sizeof(HandleTableEntry)) | |
231 return "Message secondary buffer too small"; | |
232 | |
233 if (header->num_platform_handles == 0) { | |
234 // Then |platform_handle_table_offset| should also be zero. | |
235 if (header->platform_handle_table_offset != 0) { | |
236 return "Message has no handles attached, but platform handle table " | |
237 "present"; | |
238 } | |
239 } else { | |
240 if (header->num_platform_handles > | |
241 GetConfiguration().max_message_num_handles * | |
242 kMaxSerializedDispatcherPlatformHandles) | |
243 return "Message has too many platform handles attached"; | |
244 | |
245 static const char kInvalidPlatformHandleTableOffset[] = | |
246 "Message has invalid platform handle table offset"; | |
247 // This doesn't check that the platform handle table doesn't alias other | |
248 // stuff, but it doesn't matter, since it's all read-only. | |
249 if (header->platform_handle_table_offset % | |
250 MessageInTransit::kMessageAlignment != | |
251 0) | |
252 return kInvalidPlatformHandleTableOffset; | |
253 | |
254 // ">" instead of ">=" since the size per handle may be zero. | |
255 if (header->platform_handle_table_offset > buffer_size) | |
256 return kInvalidPlatformHandleTableOffset; | |
257 | |
258 // We already checked |platform_handle_table_offset| and | |
259 // |num_platform_handles|, so the addition and multiplication are okay. | |
260 if (header->platform_handle_table_offset + | |
261 header->num_platform_handles * serialized_platform_handle_size > | |
262 buffer_size) | |
263 return kInvalidPlatformHandleTableOffset; | |
264 } | |
265 | |
266 const HandleTableEntry* handle_table = | |
267 reinterpret_cast<const HandleTableEntry*>( | |
268 static_cast<const char*>(buffer) + sizeof(Header)); | |
269 static const char kInvalidSerializedDispatcher[] = | |
270 "Message contains invalid serialized dispatcher"; | |
271 for (size_t i = 0; i < num_handles; i++) { | |
272 size_t offset = handle_table[i].offset; | |
273 if (offset % MessageInTransit::kMessageAlignment != 0) | |
274 return kInvalidSerializedDispatcher; | |
275 | |
276 size_t size = handle_table[i].size; | |
277 if (size > kMaxSerializedDispatcherSize || size > buffer_size) | |
278 return kInvalidSerializedDispatcher; | |
279 | |
280 // Note: This is an overflow-safe check for |offset + size > buffer_size| | |
281 // (we know that |size <= buffer_size| from the previous check). | |
282 if (offset > buffer_size - size) | |
283 return kInvalidSerializedDispatcher; | |
284 } | |
285 | |
286 return nullptr; | |
287 } | |
288 | |
289 // static | |
290 void TransportData::GetPlatformHandleTable(const void* transport_data_buffer, | |
291 size_t* num_platform_handles, | |
292 const void** platform_handle_table) { | |
293 DCHECK(transport_data_buffer); | |
294 DCHECK(num_platform_handles); | |
295 DCHECK(platform_handle_table); | |
296 | |
297 const Header* header = static_cast<const Header*>(transport_data_buffer); | |
298 *num_platform_handles = header->num_platform_handles; | |
299 *platform_handle_table = static_cast<const char*>(transport_data_buffer) + | |
300 header->platform_handle_table_offset; | |
301 } | |
302 | |
303 // static | |
304 scoped_ptr<DispatcherVector> TransportData::DeserializeDispatchers( | |
305 const void* buffer, | |
306 size_t buffer_size, | |
307 embedder::ScopedPlatformHandleVectorPtr platform_handles, | |
308 Channel* channel) { | |
309 DCHECK(buffer); | |
310 DCHECK_GT(buffer_size, 0u); | |
311 DCHECK(channel); | |
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 channel, handle_table[i].type, source, size, platform_handles.get()); | |
331 } | |
332 | |
333 return dispatchers.Pass(); | |
334 } | |
335 | |
336 } // namespace system | |
337 } // namespace mojo | |
OLD | NEW |