OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/edk/system/message_pipe_dispatcher.h" | 5 #include "mojo/edk/system/message_pipe_dispatcher.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <memory> | 8 #include <memory> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 268 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 // that it specifies a size at least as large as the next available payload. | 279 // that it specifies a size at least as large as the next available payload. |
280 // | 280 // |
281 // If |read_any_size| is true, the input value of |*num_bytes| is ignored. | 281 // If |read_any_size| is true, the input value of |*num_bytes| is ignored. |
282 // This flag exists to support both new and old API behavior. | 282 // This flag exists to support both new and old API behavior. |
283 | 283 |
284 ports::ScopedMessage ports_message; | 284 ports::ScopedMessage ports_message; |
285 ReadMessageFilter filter(read_any_size, may_discard, num_bytes, num_handles, | 285 ReadMessageFilter filter(read_any_size, may_discard, num_bytes, num_handles, |
286 &no_space, &invalid_message); | 286 &no_space, &invalid_message); |
287 int rv = node_controller_->node()->GetMessage(port_, &ports_message, &filter); | 287 int rv = node_controller_->node()->GetMessage(port_, &ports_message, &filter); |
288 | 288 |
289 if (invalid_message) | 289 if (invalid_message) { |
| 290 NOTREACHED(); |
290 return MOJO_RESULT_UNKNOWN; | 291 return MOJO_RESULT_UNKNOWN; |
| 292 } |
291 | 293 |
292 if (rv != ports::OK && rv != ports::ERROR_PORT_PEER_CLOSED) { | 294 if (rv != ports::OK && rv != ports::ERROR_PORT_PEER_CLOSED) { |
293 if (rv == ports::ERROR_PORT_UNKNOWN || | 295 if (rv == ports::ERROR_PORT_UNKNOWN || |
294 rv == ports::ERROR_PORT_STATE_UNEXPECTED) | 296 rv == ports::ERROR_PORT_STATE_UNEXPECTED) |
295 return MOJO_RESULT_INVALID_ARGUMENT; | 297 return MOJO_RESULT_INVALID_ARGUMENT; |
296 | 298 |
297 NOTREACHED(); | 299 NOTREACHED(); |
298 return MOJO_RESULT_UNKNOWN; // TODO: Add a better error code here? | 300 return MOJO_RESULT_UNKNOWN; // TODO: Add a better error code here? |
299 } | 301 } |
300 | 302 |
(...skipping 19 matching lines...) Expand all Loading... |
320 // in which to receive it. | 322 // in which to receive it. |
321 | 323 |
322 std::unique_ptr<PortsMessage> msg( | 324 std::unique_ptr<PortsMessage> msg( |
323 static_cast<PortsMessage*>(ports_message.release())); | 325 static_cast<PortsMessage*>(ports_message.release())); |
324 | 326 |
325 const MessageHeader* header = | 327 const MessageHeader* header = |
326 static_cast<const MessageHeader*>(msg->payload_bytes()); | 328 static_cast<const MessageHeader*>(msg->payload_bytes()); |
327 const DispatcherHeader* dispatcher_headers = | 329 const DispatcherHeader* dispatcher_headers = |
328 reinterpret_cast<const DispatcherHeader*>(header + 1); | 330 reinterpret_cast<const DispatcherHeader*>(header + 1); |
329 | 331 |
330 if (header->num_dispatchers > std::numeric_limits<uint16_t>::max()) | 332 if (header->num_dispatchers > std::numeric_limits<uint16_t>::max()) { |
| 333 NOTREACHED(); |
331 return MOJO_RESULT_UNKNOWN; | 334 return MOJO_RESULT_UNKNOWN; |
| 335 } |
332 | 336 |
333 // Deserialize dispatchers. | 337 // Deserialize dispatchers. |
334 if (header->num_dispatchers > 0) { | 338 if (header->num_dispatchers > 0) { |
335 CHECK(handles); | 339 CHECK(handles); |
336 std::vector<DispatcherInTransit> dispatchers(header->num_dispatchers); | 340 std::vector<DispatcherInTransit> dispatchers(header->num_dispatchers); |
337 size_t data_payload_index = sizeof(MessageHeader) + | 341 size_t data_payload_index = sizeof(MessageHeader) + |
338 header->num_dispatchers * sizeof(DispatcherHeader); | 342 header->num_dispatchers * sizeof(DispatcherHeader); |
339 if (data_payload_index > header->header_size) | 343 if (data_payload_index > header->header_size) { |
| 344 NOTREACHED(); |
340 return MOJO_RESULT_UNKNOWN; | 345 return MOJO_RESULT_UNKNOWN; |
| 346 } |
341 const char* dispatcher_data = reinterpret_cast<const char*>( | 347 const char* dispatcher_data = reinterpret_cast<const char*>( |
342 dispatcher_headers + header->num_dispatchers); | 348 dispatcher_headers + header->num_dispatchers); |
343 size_t port_index = 0; | 349 size_t port_index = 0; |
344 size_t platform_handle_index = 0; | 350 size_t platform_handle_index = 0; |
345 ScopedPlatformHandleVectorPtr msg_handles = msg->TakeHandles(); | 351 ScopedPlatformHandleVectorPtr msg_handles = msg->TakeHandles(); |
346 const size_t num_msg_handles = msg_handles ? msg_handles->size() : 0; | 352 const size_t num_msg_handles = msg_handles ? msg_handles->size() : 0; |
347 for (size_t i = 0; i < header->num_dispatchers; ++i) { | 353 for (size_t i = 0; i < header->num_dispatchers; ++i) { |
348 const DispatcherHeader& dh = dispatcher_headers[i]; | 354 const DispatcherHeader& dh = dispatcher_headers[i]; |
349 Type type = static_cast<Type>(dh.type); | 355 Type type = static_cast<Type>(dh.type); |
350 | 356 |
351 size_t next_payload_index = data_payload_index + dh.num_bytes; | 357 size_t next_payload_index = data_payload_index + dh.num_bytes; |
352 if (msg->num_payload_bytes() < next_payload_index || | 358 if (msg->num_payload_bytes() < next_payload_index || |
353 next_payload_index < data_payload_index) { | 359 next_payload_index < data_payload_index) { |
| 360 NOTREACHED(); |
354 return MOJO_RESULT_UNKNOWN; | 361 return MOJO_RESULT_UNKNOWN; |
355 } | 362 } |
356 | 363 |
357 size_t next_port_index = port_index + dh.num_ports; | 364 size_t next_port_index = port_index + dh.num_ports; |
358 if (msg->num_ports() < next_port_index || next_port_index < port_index) | 365 if (msg->num_ports() < next_port_index || next_port_index < port_index) { |
| 366 NOTREACHED(); |
359 return MOJO_RESULT_UNKNOWN; | 367 return MOJO_RESULT_UNKNOWN; |
| 368 } |
360 | 369 |
361 size_t next_platform_handle_index = | 370 size_t next_platform_handle_index = |
362 platform_handle_index + dh.num_platform_handles; | 371 platform_handle_index + dh.num_platform_handles; |
363 if (num_msg_handles < next_platform_handle_index || | 372 if (num_msg_handles < next_platform_handle_index || |
364 next_platform_handle_index < platform_handle_index) { | 373 next_platform_handle_index < platform_handle_index) { |
| 374 NOTREACHED(); |
365 return MOJO_RESULT_UNKNOWN; | 375 return MOJO_RESULT_UNKNOWN; |
366 } | 376 } |
367 | 377 |
368 PlatformHandle* out_handles = | 378 PlatformHandle* out_handles = |
369 num_msg_handles ? msg_handles->data() + platform_handle_index | 379 num_msg_handles ? msg_handles->data() + platform_handle_index |
370 : nullptr; | 380 : nullptr; |
371 dispatchers[i].dispatcher = Dispatcher::Deserialize( | 381 dispatchers[i].dispatcher = Dispatcher::Deserialize( |
372 type, dispatcher_data, dh.num_bytes, msg->ports() + port_index, | 382 type, dispatcher_data, dh.num_bytes, msg->ports() + port_index, |
373 dh.num_ports, out_handles, dh.num_platform_handles); | 383 dh.num_ports, out_handles, dh.num_platform_handles); |
374 if (!dispatchers[i].dispatcher) | 384 if (!dispatchers[i].dispatcher) { |
| 385 NOTREACHED(); |
375 return MOJO_RESULT_UNKNOWN; | 386 return MOJO_RESULT_UNKNOWN; |
| 387 } |
376 | 388 |
377 dispatcher_data += dh.num_bytes; | 389 dispatcher_data += dh.num_bytes; |
378 data_payload_index = next_payload_index; | 390 data_payload_index = next_payload_index; |
379 port_index = next_port_index; | 391 port_index = next_port_index; |
380 platform_handle_index = next_platform_handle_index; | 392 platform_handle_index = next_platform_handle_index; |
381 } | 393 } |
382 | 394 |
383 if (!node_controller_->core()->AddDispatchersFromTransit(dispatchers, | 395 if (!node_controller_->core()->AddDispatchersFromTransit(dispatchers, |
384 handles)) | 396 handles)) { |
| 397 NOTREACHED(); |
385 return MOJO_RESULT_UNKNOWN; | 398 return MOJO_RESULT_UNKNOWN; |
| 399 } |
386 } | 400 } |
387 | 401 |
388 CHECK(msg); | 402 CHECK(msg); |
389 *message = MessageForTransit::WrapPortsMessage(std::move(msg)); | 403 *message = MessageForTransit::WrapPortsMessage(std::move(msg)); |
390 return MOJO_RESULT_OK; | 404 return MOJO_RESULT_OK; |
391 } | 405 } |
392 | 406 |
393 HandleSignalsState | 407 HandleSignalsState |
394 MessagePipeDispatcher::GetHandleSignalsState() const { | 408 MessagePipeDispatcher::GetHandleSignalsState() const { |
395 base::AutoLock lock(signal_lock_); | 409 base::AutoLock lock(signal_lock_); |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
594 << " endpoint " << endpoint_ << " [port=" << port_.name() << "]"; | 608 << " endpoint " << endpoint_ << " [port=" << port_.name() << "]"; |
595 } | 609 } |
596 } | 610 } |
597 #endif | 611 #endif |
598 | 612 |
599 awakables_.AwakeForStateChange(GetHandleSignalsStateNoLock()); | 613 awakables_.AwakeForStateChange(GetHandleSignalsStateNoLock()); |
600 } | 614 } |
601 | 615 |
602 } // namespace edk | 616 } // namespace edk |
603 } // namespace mojo | 617 } // namespace mojo |
OLD | NEW |