OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/system/message_in_transit.h" | 5 #include "mojo/system/transport_data.h" |
6 | 6 |
7 #include <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <new> | |
10 | |
11 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 10 #include "base/logging.h" |
13 #include "mojo/system/constants.h" | 11 #include "mojo/system/constants.h" |
12 #include "mojo/system/message_in_transit.h" | |
14 | 13 |
15 namespace mojo { | 14 namespace mojo { |
16 namespace system { | 15 namespace system { |
17 | 16 |
17 STATIC_CONST_MEMBER_DEFINITION const size_t | |
18 TransportData::kMaxSerializedDispatcherSize; | |
19 STATIC_CONST_MEMBER_DEFINITION const size_t | |
20 TransportData::kMaxSerializedDispatcherPlatformHandles; | |
21 | |
22 // In additional to the header, for each attached (Mojo) handle there'll be a | |
23 // handle table entry and serialized dispatcher data. | |
24 // static | |
25 const size_t TransportData::kMaxBufferSize = | |
26 sizeof(Header) + kMaxMessageNumHandles * (sizeof(HandleTableEntry) + | |
27 kMaxSerializedDispatcherSize); | |
28 | |
29 // static | |
30 const size_t TransportData::kMaxPlatformHandles = | |
31 kMaxMessageNumHandles * kMaxSerializedDispatcherPlatformHandles; | |
32 | |
33 struct TransportData::PrivateStructForCompileAsserts { | |
darin (slow to review)
2014/05/02 22:51:40
neat, i hadn't considered such a pattern for compi
| |
34 // The size of |Header| must be a multiple of the alignment. | |
35 COMPILE_ASSERT(sizeof(Header) % MessageInTransit::kMessageAlignment == 0, | |
36 sizeof_MessageInTransit_Header_invalid); | |
37 | |
38 // The maximum serialized dispatcher size must be a multiple of the alignment. | |
39 COMPILE_ASSERT(kMaxSerializedDispatcherSize % | |
40 MessageInTransit::kMessageAlignment == 0, | |
41 kMaxSerializedDispatcherSize_not_a_multiple_of_alignment); | |
42 | |
43 // The size of |HandleTableEntry| must be a multiple of the alignment. | |
44 COMPILE_ASSERT(sizeof(HandleTableEntry) % | |
45 MessageInTransit::kMessageAlignment == 0, | |
46 sizeof_MessageInTransit_HandleTableEntry_invalid); | |
47 }; | |
48 | |
49 TransportData::TransportData( | |
50 scoped_ptr<std::vector<scoped_refptr<Dispatcher> > > dispatchers, | |
51 Channel* channel) | |
52 : buffer_size_(0) { | |
53 DCHECK(dispatchers); | |
54 DCHECK(channel); | |
55 | |
56 const size_t num_handles = dispatchers->size(); | |
57 DCHECK_GT(num_handles, 0u); | |
58 | |
59 // The offset to the start of the (Mojo) handle table. | |
60 const size_t handle_table_start_offset = sizeof(Header); | |
61 // The offset to the start of the serialized dispatcher data. | |
62 const size_t serialized_dispatcher_start_offset = | |
63 handle_table_start_offset + num_handles * sizeof(HandleTableEntry); | |
64 // The estimated size of the secondary buffer. We compute this estimate below. | |
65 // It must be at least as big as the (eventual) actual size. | |
66 size_t estimated_size = serialized_dispatcher_start_offset; | |
67 size_t num_platform_handles = 0; | |
68 #if DCHECK_IS_ON | |
69 std::vector<size_t> all_max_sizes(num_handles); | |
70 std::vector<size_t> all_max_platform_handles(num_handles); | |
71 #endif | |
72 for (size_t i = 0; i < num_handles; i++) { | |
73 if (Dispatcher* dispatcher = (*dispatchers)[i].get()) { | |
74 size_t max_size = 0; | |
75 size_t max_platform_handles = 0; | |
76 Dispatcher::TransportDataAccess::StartSerialize( | |
77 dispatcher, channel, &max_size, &max_platform_handles); | |
78 | |
79 DCHECK_LE(max_size, kMaxSerializedDispatcherSize); | |
80 estimated_size += MessageInTransit::RoundUpMessageAlignment(max_size); | |
81 DCHECK_LE(estimated_size, kMaxBufferSize); | |
82 | |
83 DCHECK_LE(max_platform_handles, | |
84 kMaxSerializedDispatcherPlatformHandles); | |
85 num_platform_handles += max_platform_handles; | |
86 DCHECK_LE(num_platform_handles, kMaxPlatformHandles); | |
87 | |
88 #if DCHECK_IS_ON | |
89 all_max_sizes[i] = max_size; | |
90 all_max_platform_handles[i] = max_platform_handles; | |
91 #endif | |
92 } | |
93 } | |
94 | |
95 buffer_.reset(static_cast<char*>( | |
96 base::AlignedAlloc(estimated_size, MessageInTransit::kMessageAlignment))); | |
97 // Entirely clear out the secondary buffer, since then we won't have to worry | |
98 // about clearing padding or unused space (e.g., if a dispatcher fails to | |
99 // serialize). | |
100 memset(buffer_.get(), 0, estimated_size); | |
101 | |
102 if (num_platform_handles > 0) { | |
103 DCHECK(!platform_handles_); | |
104 platform_handles_.reset(new std::vector<embedder::PlatformHandle>()); | |
105 } | |
106 | |
107 Header* header = reinterpret_cast<Header*>(buffer_.get()); | |
108 header->num_handles = static_cast<uint32_t>(num_handles); | |
109 // TODO(vtl): platform_handle_table_offset and num_platform_handles | |
110 | |
111 HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>( | |
112 buffer_.get() + handle_table_start_offset); | |
113 size_t current_offset = serialized_dispatcher_start_offset; | |
114 for (size_t i = 0; i < num_handles; i++) { | |
115 Dispatcher* dispatcher = (*dispatchers)[i].get(); | |
116 if (!dispatcher) { | |
117 COMPILE_ASSERT(Dispatcher::kTypeUnknown == 0, | |
118 value_of_Dispatcher_kTypeUnknown_must_be_zero); | |
119 continue; | |
120 } | |
121 | |
122 #if DCHECK_IS_ON | |
123 size_t old_platform_handles_size = | |
124 platform_handles_ ? platform_handles_->size() : 0; | |
125 #endif | |
126 | |
127 void* destination = buffer_.get() + current_offset; | |
128 size_t actual_size = 0; | |
129 if (Dispatcher::TransportDataAccess::EndSerializeAndClose( | |
130 dispatcher, channel, destination, &actual_size, | |
131 platform_handles_.get())) { | |
132 handle_table[i].type = static_cast<int32_t>(dispatcher->GetType()); | |
133 handle_table[i].offset = static_cast<uint32_t>(current_offset); | |
134 handle_table[i].size = static_cast<uint32_t>(actual_size); | |
135 | |
136 #if DCHECK_IS_ON | |
137 DCHECK_LE(actual_size, all_max_sizes[i]); | |
138 DCHECK_LE(platform_handles_ ? (platform_handles_->size() - | |
139 old_platform_handles_size) : 0, | |
140 all_max_platform_handles[i]); | |
141 #endif | |
142 } else { | |
143 // Nothing to do on failure, since |buffer_| was cleared, and | |
144 // |kTypeUnknown| is zero. The handle was simply closed. | |
145 LOG(ERROR) << "Failed to serialize handle to remote message pipe"; | |
146 } | |
147 | |
148 current_offset += MessageInTransit::RoundUpMessageAlignment(actual_size); | |
149 DCHECK_LE(current_offset, estimated_size); | |
150 DCHECK_LE(platform_handles_ ? platform_handles_->size() : 0, | |
151 num_platform_handles); | |
152 } | |
153 | |
154 // There's no aligned realloc, so it's no good way to release unused space (if | |
155 // we overshot our estimated space requirements). | |
156 buffer_size_ = current_offset; | |
157 | |
158 // |dispatchers_| will be destroyed as it goes out of scope. | |
159 } | |
160 | |
161 TransportData::~TransportData() { | |
162 if (platform_handles_) { | |
163 for (size_t i = 0; i < platform_handles_->size(); i++) | |
164 (*platform_handles_)[i].CloseIfNecessary(); | |
165 } | |
166 } | |
167 | |
168 // static | |
169 const char* TransportData::ValidateBuffer(const void* buffer, | |
170 size_t buffer_size) { | |
171 DCHECK(buffer); | |
172 DCHECK_GT(buffer_size, 0u); | |
173 | |
174 // Always make sure that the buffer size is sane; if it's not, someone's | |
175 // messing with us. | |
176 if (buffer_size < sizeof(Header) || buffer_size > kMaxBufferSize || | |
177 buffer_size % MessageInTransit::kMessageAlignment != 0) | |
178 return "Invalid message secondary buffer size"; | |
179 | |
180 const Header* header = static_cast<const Header*>(buffer); | |
181 const size_t num_handles = header->num_handles; | |
182 if (num_handles == 0) | |
183 return "Message has no handles attached, but secondary buffer present"; | |
184 | |
185 // Sanity-check |num_handles| (before multiplying it against anything). | |
186 if (num_handles > kMaxMessageNumHandles) | |
187 return "Message handle payload too large"; | |
188 | |
189 if (buffer_size < sizeof(Header) + num_handles * sizeof(HandleTableEntry)) | |
190 return "Message secondary buffer too small"; | |
191 | |
192 // TODO(vtl): Check |platform_handle_table_offset| and |num_platform_handles| | |
193 // once they're used. | |
194 if (header->platform_handle_table_offset != 0 || | |
195 header->num_platform_handles != 0) | |
196 return "Bad message secondary buffer header values"; | |
197 | |
198 const HandleTableEntry* handle_table = | |
199 reinterpret_cast<const HandleTableEntry*>( | |
200 static_cast<const char*>(buffer) + sizeof(Header)); | |
201 static const char kInvalidSerializedDispatcher[] = | |
202 "Message contains invalid serialized dispatcher"; | |
203 for (size_t i = 0; i < num_handles; i++) { | |
204 size_t offset = handle_table[i].offset; | |
205 if (offset % MessageInTransit::kMessageAlignment != 0) | |
206 return kInvalidSerializedDispatcher; | |
207 | |
208 size_t size = handle_table[i].size; | |
209 if (size > kMaxSerializedDispatcherSize || size > buffer_size) | |
210 return kInvalidSerializedDispatcher; | |
211 | |
212 // Note: This is an overflow-safe check for |offset + size > buffer_size| | |
213 // (we know that |size <= buffer_size| from the previous check). | |
214 if (offset > buffer_size - size) | |
215 return kInvalidSerializedDispatcher; | |
216 } | |
217 | |
218 return NULL; | |
219 } | |
220 | |
221 // static | |
222 scoped_ptr<std::vector<scoped_refptr<Dispatcher> > > | |
223 TransportData::DeserializeDispatchersFromBuffer(const void* buffer, | |
224 size_t buffer_size, | |
225 Channel* channel) { | |
226 DCHECK(buffer); | |
227 DCHECK_GT(buffer_size, 0u); | |
228 DCHECK(channel); | |
229 | |
230 const Header* header = static_cast<const Header*>(buffer); | |
231 const size_t num_handles = header->num_handles; | |
232 scoped_ptr<std::vector<scoped_refptr<Dispatcher> > > dispatchers( | |
233 new std::vector<scoped_refptr<Dispatcher> >(num_handles)); | |
234 | |
235 const HandleTableEntry* handle_table = | |
236 reinterpret_cast<const HandleTableEntry*>( | |
237 static_cast<const char*>(buffer) + sizeof(Header)); | |
238 for (size_t i = 0; i < num_handles; i++) { | |
239 size_t offset = handle_table[i].offset; | |
240 size_t size = handle_table[i].size; | |
241 // Should already have been checked by |ValidateBuffer()|: | |
242 DCHECK_EQ(offset % MessageInTransit::kMessageAlignment, 0u); | |
243 DCHECK_LE(offset, buffer_size); | |
244 DCHECK_LE(offset + size, buffer_size); | |
245 | |
246 const void* source = static_cast<const char*>(buffer) + offset; | |
247 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( | |
248 channel, handle_table[i].type, source, size); | |
249 } | |
250 | |
251 return dispatchers.Pass(); | |
252 } | |
253 | |
254 | |
255 | |
256 #if 0 | |
darin (slow to review)
2014/05/02 22:51:40
nit!
viettrungluu
2014/05/03 00:12:50
Oops!
| |
18 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type | 257 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type |
19 MessageInTransit::kTypeMessagePipeEndpoint; | 258 MessageInTransit::kTypeMessagePipeEndpoint; |
20 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type | 259 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type |
21 MessageInTransit::kTypeMessagePipe; | 260 MessageInTransit::kTypeMessagePipe; |
22 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type | 261 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type |
23 MessageInTransit::kTypeChannel; | 262 MessageInTransit::kTypeChannel; |
24 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype | 263 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype |
25 MessageInTransit::kSubtypeMessagePipeEndpointData; | 264 MessageInTransit::kSubtypeMessagePipeEndpointData; |
26 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype | 265 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype |
27 MessageInTransit::kSubtypeChannelRunMessagePipeEndpoint; | 266 MessageInTransit::kSubtypeChannelRunMessagePipeEndpoint; |
(...skipping 26 matching lines...) Expand all Loading... | |
54 | 293 |
55 // The maximum serialized dispatcher size must be a multiple of the alignment. | 294 // The maximum serialized dispatcher size must be a multiple of the alignment. |
56 COMPILE_ASSERT(kMaxSerializedDispatcherSize % kMessageAlignment == 0, | 295 COMPILE_ASSERT(kMaxSerializedDispatcherSize % kMessageAlignment == 0, |
57 kMaxSerializedDispatcherSize_not_a_multiple_of_alignment); | 296 kMaxSerializedDispatcherSize_not_a_multiple_of_alignment); |
58 | 297 |
59 // The size of |HandleTableEntry| must be a multiple of the alignment. | 298 // The size of |HandleTableEntry| must be a multiple of the alignment. |
60 COMPILE_ASSERT(sizeof(HandleTableEntry) % kMessageAlignment == 0, | 299 COMPILE_ASSERT(sizeof(HandleTableEntry) % kMessageAlignment == 0, |
61 sizeof_MessageInTransit_HandleTableEntry_invalid); | 300 sizeof_MessageInTransit_HandleTableEntry_invalid); |
62 }; | 301 }; |
63 | 302 |
64 // For each attached (Mojo) handle, there'll be a handle table entry and | |
65 // serialized dispatcher data. | |
66 // static | |
67 const size_t MessageInTransit::kMaxSecondaryBufferSize = kMaxMessageNumHandles * | |
68 (sizeof(HandleTableEntry) + kMaxSerializedDispatcherSize); | |
69 | |
70 // static | |
71 const size_t MessageInTransit::kMaxPlatformHandles = | |
72 kMaxMessageNumHandles * kMaxSerializedDispatcherPlatformHandles; | |
73 | |
74 MessageInTransit::View::View(size_t message_size, const void* buffer) | 303 MessageInTransit::View::View(size_t message_size, const void* buffer) |
75 : buffer_(buffer) { | 304 : buffer_(buffer) { |
76 size_t next_message_size = 0; | 305 size_t next_message_size = 0; |
77 DCHECK(MessageInTransit::GetNextMessageSize(buffer_, message_size, | 306 DCHECK(MessageInTransit::GetNextMessageSize(buffer_, message_size, |
78 &next_message_size)); | 307 &next_message_size)); |
79 DCHECK_EQ(message_size, next_message_size); | 308 DCHECK_EQ(message_size, next_message_size); |
80 // This should be equivalent. | 309 // This should be equivalent. |
81 DCHECK_EQ(message_size, total_size()); | 310 DCHECK_EQ(message_size, total_size()); |
82 } | 311 } |
83 | 312 |
(...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
336 size_t handle_table_size = num_handles() * sizeof(HandleTableEntry); | 565 size_t handle_table_size = num_handles() * sizeof(HandleTableEntry); |
337 // Already checked by |View::IsValid()|: | 566 // Already checked by |View::IsValid()|: |
338 DCHECK_LE(handle_table_size, secondary_buffer_size_); | 567 DCHECK_LE(handle_table_size, secondary_buffer_size_); |
339 | 568 |
340 const HandleTableEntry* handle_table = | 569 const HandleTableEntry* handle_table = |
341 reinterpret_cast<const HandleTableEntry*>(secondary_buffer_.get()); | 570 reinterpret_cast<const HandleTableEntry*>(secondary_buffer_.get()); |
342 for (size_t i = 0; i < num_handles(); i++) { | 571 for (size_t i = 0; i < num_handles(); i++) { |
343 size_t offset = handle_table[i].offset; | 572 size_t offset = handle_table[i].offset; |
344 size_t size = handle_table[i].size; | 573 size_t size = handle_table[i].size; |
345 // Already checked by |View::IsValid()|: | 574 // Already checked by |View::IsValid()|: |
346 DCHECK_EQ(offset % kMessageAlignment, 0u); | 575 DCHECK_EQ(offset % MessageInTransit::kMessageAlignment, 0u); |
347 DCHECK_LE(offset, secondary_buffer_size_); | 576 DCHECK_LE(offset, secondary_buffer_size_); |
348 DCHECK_LE(offset + size, secondary_buffer_size_); | 577 DCHECK_LE(offset + size, secondary_buffer_size_); |
349 | 578 |
350 const void* source = secondary_buffer_.get() + offset; | 579 const void* source = secondary_buffer_.get() + offset; |
351 (*dispatchers_)[i] = Dispatcher::MessageInTransitAccess::Deserialize( | 580 (*dispatchers_)[i] = Dispatcher::MessageInTransitAccess::Deserialize( |
352 channel, handle_table[i].type, source, size); | 581 channel, handle_table[i].type, source, size); |
353 } | 582 } |
354 } | 583 } |
355 | 584 |
356 // Validates the secondary buffer. Returns null on success, or a human-readable | 585 // Validates the secondary buffer. Returns null on success, or a human-readable |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
404 | 633 |
405 return NULL; | 634 return NULL; |
406 } | 635 } |
407 | 636 |
408 void MessageInTransit::UpdateTotalSize() { | 637 void MessageInTransit::UpdateTotalSize() { |
409 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); | 638 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); |
410 DCHECK_EQ(secondary_buffer_size_ % kMessageAlignment, 0u); | 639 DCHECK_EQ(secondary_buffer_size_ % kMessageAlignment, 0u); |
411 header()->total_size = | 640 header()->total_size = |
412 static_cast<uint32_t>(main_buffer_size_ + secondary_buffer_size_); | 641 static_cast<uint32_t>(main_buffer_size_ + secondary_buffer_size_); |
413 } | 642 } |
643 #endif | |
414 | 644 |
415 } // namespace system | 645 } // namespace system |
416 } // namespace mojo | 646 } // namespace mojo |
OLD | NEW |