| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/nacl/nacl_ipc_adapter.h" | 5 #include "chrome/nacl/nacl_ipc_adapter.h" |
| 6 | 6 |
| 7 #include <limits.h> | 7 #include <limits.h> |
| 8 #include <string.h> | 8 #include <string.h> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/shared_memory.h" | 14 #include "base/shared_memory.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "ipc/ipc_channel.h" | 16 #include "ipc/ipc_channel.h" |
| 17 #include "ipc/ipc_message_macros.h" | |
| 18 #include "ipc/ipc_platform_file.h" | 17 #include "ipc/ipc_platform_file.h" |
| 19 #include "native_client/src/trusted/desc/nacl_desc_custom.h" | 18 #include "native_client/src/trusted/desc/nacl_desc_custom.h" |
| 20 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" | 19 #include "native_client/src/trusted/desc/nacl_desc_wrapper.h" |
| 21 #include "ppapi/proxy/ppapi_messages.h" | 20 #include "ppapi/proxy/ppapi_messages.h" |
| 22 #include "ppapi/proxy/resource_message_params.h" | 21 #include "ppapi/proxy/serialized_handle.h" |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 enum BufferSizeStatus { | 25 enum BufferSizeStatus { |
| 27 // The buffer contains a full message with no extra bytes. | 26 // The buffer contains a full message with no extra bytes. |
| 28 MESSAGE_IS_COMPLETE, | 27 MESSAGE_IS_COMPLETE, |
| 29 | 28 |
| 30 // The message doesn't fit and the buffer contains only some of it. | 29 // The message doesn't fit and the buffer contains only some of it. |
| 31 MESSAGE_IS_TRUNCATED, | 30 MESSAGE_IS_TRUNCATED, |
| 32 | 31 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 funcs.SendMsg = NaClDescCustomSendMsg; | 83 funcs.SendMsg = NaClDescCustomSendMsg; |
| 85 funcs.RecvMsg = NaClDescCustomRecvMsg; | 84 funcs.RecvMsg = NaClDescCustomRecvMsg; |
| 86 // NaClDescMakeCustomDesc gives us a reference on the returned NaClDesc. | 85 // NaClDescMakeCustomDesc gives us a reference on the returned NaClDesc. |
| 87 return NaClDescMakeCustomDesc(new DescThunker(adapter), &funcs); | 86 return NaClDescMakeCustomDesc(new DescThunker(adapter), &funcs); |
| 88 } | 87 } |
| 89 | 88 |
| 90 void DeleteChannel(IPC::Channel* channel) { | 89 void DeleteChannel(IPC::Channel* channel) { |
| 91 delete channel; | 90 delete channel; |
| 92 } | 91 } |
| 93 | 92 |
| 94 // TODO(dmichael): Move all this handle conversion code to ppapi/proxy. | |
| 95 // crbug.com/165201 | |
| 96 void WriteHandle(int handle_index, | |
| 97 const ppapi::proxy::SerializedHandle& handle, | |
| 98 IPC::Message* message) { | |
| 99 ppapi::proxy::SerializedHandle::WriteHeader(handle.header(), message); | |
| 100 | |
| 101 // Now write the handle itself in POSIX style. | |
| 102 message->WriteBool(true); // valid == true | |
| 103 message->WriteInt(handle_index); | |
| 104 } | |
| 105 | |
| 106 typedef std::vector<ppapi::proxy::SerializedHandle> Handles; | |
| 107 | |
| 108 // We define overload for catching SerializedHandles, so that we can share | |
| 109 // them correctly to the untrusted side, and another for handling all other | |
| 110 // parameters. See ConvertHandlesImpl for how these get used. | |
| 111 void ConvertHandlesInParam(const ppapi::proxy::SerializedHandle& handle, | |
| 112 Handles* handles, | |
| 113 IPC::Message* msg, | |
| 114 int* handle_index) { | |
| 115 handles->push_back(handle); | |
| 116 if (msg) | |
| 117 WriteHandle((*handle_index)++, handle, msg); | |
| 118 } | |
| 119 | |
| 120 // For PpapiMsg_ResourceReply and the reply to PpapiHostMsg_ResourceSyncCall, | |
| 121 // the handles are carried inside the ResourceMessageReplyParams. | |
| 122 // NOTE: We only translate handles from host->NaCl. The only kind of | |
| 123 // ResourceMessageParams that travels this direction is | |
| 124 // ResourceMessageReplyParams, so that's the only one we need to handle. | |
| 125 void ConvertHandlesInParam( | |
| 126 const ppapi::proxy::ResourceMessageReplyParams& params, | |
| 127 Handles* handles, | |
| 128 IPC::Message* msg, | |
| 129 int* handle_index) { | |
| 130 // First, if we need to rewrite the message parameters, write everything | |
| 131 // before the handles (there's nothing after the handles). | |
| 132 if (msg) { | |
| 133 params.WriteReplyHeader(msg); | |
| 134 // IPC writes the vector length as an int before the contents of the | |
| 135 // vector. | |
| 136 msg->WriteInt(static_cast<int>(params.handles().size())); | |
| 137 } | |
| 138 for (Handles::const_iterator iter = params.handles().begin(); | |
| 139 iter != params.handles().end(); | |
| 140 ++iter) { | |
| 141 // ConvertHandle will write each handle to |msg|, if necessary. | |
| 142 ConvertHandlesInParam(*iter, handles, msg, handle_index); | |
| 143 } | |
| 144 // Tell ResourceMessageReplyParams that we have taken the handles, so it | |
| 145 // shouldn't close them. The NaCl runtime will take ownership of them. | |
| 146 params.ConsumeHandles(); | |
| 147 } | |
| 148 | |
| 149 // This overload is to catch all types other than SerializedHandle or | |
| 150 // ResourceMessageReplyParams. On Windows, |msg| will be a valid pointer, and we | |
| 151 // must write |param| to it. | |
| 152 template <class T> | |
| 153 void ConvertHandlesInParam(const T& param, | |
| 154 Handles* /* handles */, | |
| 155 IPC::Message* msg, | |
| 156 int* /* handle_index */) { | |
| 157 // It's not a handle, so just write to the output message, if necessary. | |
| 158 if (msg) | |
| 159 IPC::WriteParam(msg, param); | |
| 160 } | |
| 161 | |
| 162 // These just break apart the given tuple and run ConvertHandle over each param. | |
| 163 // The idea is to extract any handles in the tuple, while writing all data to | |
| 164 // msg (if msg is valid). The msg will only be valid on Windows, where we need | |
| 165 // to re-write all of the message parameters, writing the handles in POSIX style | |
| 166 // for NaCl. | |
| 167 template <class A> | |
| 168 void ConvertHandlesImpl(const Tuple1<A>& t1, Handles* handles, | |
| 169 IPC::Message* msg) { | |
| 170 int handle_index = 0; | |
| 171 ConvertHandlesInParam(t1.a, handles, msg, &handle_index); | |
| 172 } | |
| 173 template <class A, class B> | |
| 174 void ConvertHandlesImpl(const Tuple2<A, B>& t1, Handles* handles, | |
| 175 IPC::Message* msg) { | |
| 176 int handle_index = 0; | |
| 177 ConvertHandlesInParam(t1.a, handles, msg, &handle_index); | |
| 178 ConvertHandlesInParam(t1.b, handles, msg, &handle_index); | |
| 179 } | |
| 180 template <class A, class B, class C> | |
| 181 void ConvertHandlesImpl(const Tuple3<A, B, C>& t1, Handles* handles, | |
| 182 IPC::Message* msg) { | |
| 183 int handle_index = 0; | |
| 184 ConvertHandlesInParam(t1.a, handles, msg, &handle_index); | |
| 185 ConvertHandlesInParam(t1.b, handles, msg, &handle_index); | |
| 186 ConvertHandlesInParam(t1.c, handles, msg, &handle_index); | |
| 187 } | |
| 188 template <class A, class B, class C, class D> | |
| 189 void ConvertHandlesImpl(const Tuple4<A, B, C, D>& t1, Handles* handles, | |
| 190 IPC::Message* msg) { | |
| 191 int handle_index = 0; | |
| 192 ConvertHandlesInParam(t1.a, handles, msg, &handle_index); | |
| 193 ConvertHandlesInParam(t1.b, handles, msg, &handle_index); | |
| 194 ConvertHandlesInParam(t1.c, handles, msg, &handle_index); | |
| 195 ConvertHandlesInParam(t1.d, handles, msg, &handle_index); | |
| 196 } | |
| 197 | |
| 198 template <class MessageType> | |
| 199 class HandleConverter { | |
| 200 public: | |
| 201 explicit HandleConverter(const IPC::Message* msg) | |
| 202 : msg_(static_cast<const MessageType*>(msg)) { | |
| 203 } | |
| 204 bool ConvertMessage(Handles* handles, IPC::Message* out_msg) { | |
| 205 typename TupleTypes<typename MessageType::Schema::Param>::ValueTuple params; | |
| 206 if (!MessageType::Read(msg_, ¶ms)) | |
| 207 return false; | |
| 208 ConvertHandlesImpl(params, handles, out_msg); | |
| 209 return true; | |
| 210 } | |
| 211 | |
| 212 bool ConvertReply(Handles* handles, IPC::SyncMessage* out_msg) { | |
| 213 typename TupleTypes<typename MessageType::Schema::ReplyParam>::ValueTuple | |
| 214 params; | |
| 215 if (!MessageType::ReadReplyParam(msg_, ¶ms)) | |
| 216 return false; | |
| 217 // If we need to rewrite the message (i.e., on Windows), we need to make | |
| 218 // sure we write the message id first. | |
| 219 if (out_msg) { | |
| 220 out_msg->set_reply(); | |
| 221 int id = IPC::SyncMessage::GetMessageId(*msg_); | |
| 222 out_msg->WriteInt(id); | |
| 223 } | |
| 224 ConvertHandlesImpl(params, handles, out_msg); | |
| 225 return true; | |
| 226 } | |
| 227 // TODO(dmichael): Add ConvertSyncMessage for outgoing sync messages, if we | |
| 228 // ever pass handles in one of those. | |
| 229 | |
| 230 private: | |
| 231 const MessageType* msg_; | |
| 232 }; | |
| 233 | |
| 234 } // namespace | 93 } // namespace |
| 235 | 94 |
| 236 class NaClIPCAdapter::RewrittenMessage | 95 class NaClIPCAdapter::RewrittenMessage |
| 237 : public base::RefCounted<RewrittenMessage> { | 96 : public base::RefCounted<RewrittenMessage> { |
| 238 public: | 97 public: |
| 239 RewrittenMessage(); | 98 RewrittenMessage(); |
| 240 | 99 |
| 241 bool is_consumed() const { return data_read_cursor_ == data_len_; } | 100 bool is_consumed() const { return data_read_cursor_ == data_len_; } |
| 242 | 101 |
| 243 void SetData(const NaClIPCAdapter::NaClMessageHeader& header, | 102 void SetData(const NaClIPCAdapter::NaClMessageHeader& header, |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 NaClDesc* NaClIPCAdapter::MakeNaClDesc() { | 318 NaClDesc* NaClIPCAdapter::MakeNaClDesc() { |
| 460 return MakeNaClDescCustom(this); | 319 return MakeNaClDescCustom(this); |
| 461 } | 320 } |
| 462 | 321 |
| 463 #if defined(OS_POSIX) | 322 #if defined(OS_POSIX) |
| 464 int NaClIPCAdapter::TakeClientFileDescriptor() { | 323 int NaClIPCAdapter::TakeClientFileDescriptor() { |
| 465 return io_thread_data_.channel_->TakeClientFileDescriptor(); | 324 return io_thread_data_.channel_->TakeClientFileDescriptor(); |
| 466 } | 325 } |
| 467 #endif | 326 #endif |
| 468 | 327 |
| 469 #define CASE_FOR_MESSAGE(MESSAGE_TYPE) \ | |
| 470 case MESSAGE_TYPE::ID: { \ | |
| 471 HandleConverter<MESSAGE_TYPE> extractor(&msg); \ | |
| 472 if (!extractor.ConvertMessage(&handles, new_msg_ptr)) \ | |
| 473 return false; \ | |
| 474 break; \ | |
| 475 } | |
| 476 #define CASE_FOR_REPLY(MESSAGE_TYPE) \ | |
| 477 case MESSAGE_TYPE::ID: { \ | |
| 478 HandleConverter<MESSAGE_TYPE> extractor(&msg); \ | |
| 479 if (!extractor.ConvertReply( \ | |
| 480 &handles, \ | |
| 481 static_cast<IPC::SyncMessage*>(new_msg_ptr))) \ | |
| 482 return false; \ | |
| 483 break; \ | |
| 484 } | |
| 485 | |
| 486 bool NaClIPCAdapter::OnMessageReceived(const IPC::Message& msg) { | 328 bool NaClIPCAdapter::OnMessageReceived(const IPC::Message& msg) { |
| 487 { | 329 { |
| 488 base::AutoLock lock(lock_); | 330 base::AutoLock lock(lock_); |
| 489 | 331 |
| 490 scoped_refptr<RewrittenMessage> rewritten_msg(new RewrittenMessage); | 332 scoped_refptr<RewrittenMessage> rewritten_msg(new RewrittenMessage); |
| 491 | 333 |
| 492 // Pointer to the "new" message we will rewrite on Windows. On posix, this | 334 typedef std::vector<ppapi::proxy::SerializedHandle> Handles; |
| 493 // isn't necessary, so it will stay NULL. | 335 Handles handles; |
| 494 IPC::Message* new_msg_ptr = NULL; | 336 scoped_ptr<IPC::Message> new_msg_ptr; |
| 495 IPC::Message new_msg(msg.routing_id(), msg.type(), msg.priority()); | 337 bool success = locked_data_.handle_converter_.ConvertNativeHandlesToPosix( |
| 496 #if defined(OS_WIN) | 338 msg, &handles, &new_msg_ptr); |
| 497 new_msg_ptr = &new_msg; | 339 if (!success) |
| 498 #else | 340 return false; |
| 499 // Even on POSIX, we have to rewrite messages to create channels, because | |
| 500 // these contain a handle with an invalid (place holder) descriptor. The | |
| 501 // message sending code sees this and doesn't pass the descriptor over | |
| 502 // correctly. | |
| 503 if (msg.type() == PpapiMsg_CreateNaClChannel::ID) | |
| 504 new_msg_ptr = &new_msg; | |
| 505 #endif | |
| 506 | 341 |
| 507 Handles handles; | |
| 508 switch (msg.type()) { | |
| 509 CASE_FOR_MESSAGE(PpapiMsg_CreateNaClChannel) | |
| 510 CASE_FOR_MESSAGE(PpapiMsg_PPBAudio_NotifyAudioStreamCreated) | |
| 511 CASE_FOR_MESSAGE(PpapiPluginMsg_ResourceReply) | |
| 512 case IPC_REPLY_ID: { | |
| 513 int id = IPC::SyncMessage::GetMessageId(msg); | |
| 514 LockedData::PendingSyncMsgMap::iterator iter( | |
| 515 locked_data_.pending_sync_msgs_.find(id)); | |
| 516 if (iter == locked_data_.pending_sync_msgs_.end()) { | |
| 517 NOTREACHED(); | |
| 518 return false; | |
| 519 } | |
| 520 uint32_t type = iter->second; | |
| 521 locked_data_.pending_sync_msgs_.erase(iter); | |
| 522 switch (type) { | |
| 523 CASE_FOR_REPLY(PpapiHostMsg_PPBGraphics3D_GetTransferBuffer) | |
| 524 CASE_FOR_REPLY(PpapiHostMsg_PPBImageData_CreateNaCl) | |
| 525 CASE_FOR_REPLY(PpapiHostMsg_ResourceSyncCall) | |
| 526 default: | |
| 527 // Do nothing for messages we don't know. | |
| 528 break; | |
| 529 } | |
| 530 break; | |
| 531 } | |
| 532 default: | |
| 533 // Do nothing for messages we don't know. | |
| 534 break; | |
| 535 } | |
| 536 // Now add any descriptors we found to rewritten_msg. |handles| is usually | 342 // Now add any descriptors we found to rewritten_msg. |handles| is usually |
| 537 // empty, unless we read a message containing a FD or handle. | 343 // empty, unless we read a message containing a FD or handle. |
| 538 nacl::DescWrapperFactory factory; | 344 nacl::DescWrapperFactory factory; |
| 539 for (Handles::const_iterator iter = handles.begin(); | 345 for (Handles::const_iterator iter = handles.begin(); |
| 540 iter != handles.end(); | 346 iter != handles.end(); |
| 541 ++iter) { | 347 ++iter) { |
| 542 scoped_ptr<nacl::DescWrapper> nacl_desc; | 348 scoped_ptr<nacl::DescWrapper> nacl_desc; |
| 543 switch (iter->type()) { | 349 switch (iter->type()) { |
| 544 case ppapi::proxy::SerializedHandle::SHARED_MEMORY: { | 350 case ppapi::proxy::SerializedHandle::SHARED_MEMORY: { |
| 545 const base::SharedMemoryHandle& shm_handle = iter->shmem(); | 351 const base::SharedMemoryHandle& shm_handle = iter->shmem(); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 669 | 475 |
| 670 // Technically we didn't have to do any of the previous work in the lock. But | 476 // Technically we didn't have to do any of the previous work in the lock. But |
| 671 // sometimes our buffer will point to the to_be_sent_ string which is | 477 // sometimes our buffer will point to the to_be_sent_ string which is |
| 672 // protected by the lock, and it's messier to factor Send() such that it can | 478 // protected by the lock, and it's messier to factor Send() such that it can |
| 673 // unlock for us. Holding the lock for the message construction, which is | 479 // unlock for us. Holding the lock for the message construction, which is |
| 674 // just some memcpys, shouldn't be a big deal. | 480 // just some memcpys, shouldn't be a big deal. |
| 675 lock_.AssertAcquired(); | 481 lock_.AssertAcquired(); |
| 676 if (locked_data_.channel_closed_) | 482 if (locked_data_.channel_closed_) |
| 677 return false; // TODO(brettw) clean up handles here when we add support! | 483 return false; // TODO(brettw) clean up handles here when we add support! |
| 678 | 484 |
| 679 // Store the type of all sync messages so that later we can translate the | |
| 680 // reply if necessary. | |
| 681 if (msg->is_sync()) { | 485 if (msg->is_sync()) { |
| 682 int id = IPC::SyncMessage::GetMessageId(*msg); | 486 locked_data_.handle_converter_.SendingSyncMessage(*msg); |
| 683 locked_data_.pending_sync_msgs_[id] = msg->type(); | |
| 684 } | 487 } |
| 685 // Actual send must be done on the I/O thread. | 488 // Actual send must be done on the I/O thread. |
| 686 task_runner_->PostTask(FROM_HERE, | 489 task_runner_->PostTask(FROM_HERE, |
| 687 base::Bind(&NaClIPCAdapter::SendMessageOnIOThread, this, | 490 base::Bind(&NaClIPCAdapter::SendMessageOnIOThread, this, |
| 688 base::Passed(&msg))); | 491 base::Passed(&msg))); |
| 689 return true; | 492 return true; |
| 690 } | 493 } |
| 691 | 494 |
| 692 void NaClIPCAdapter::ClearToBeSent() { | 495 void NaClIPCAdapter::ClearToBeSent() { |
| 693 lock_.AssertAcquired(); | 496 lock_.AssertAcquired(); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 722 header.payload_size = static_cast<uint32>(msg.payload_size()); | 525 header.payload_size = static_cast<uint32>(msg.payload_size()); |
| 723 header.routing = msg.routing_id(); | 526 header.routing = msg.routing_id(); |
| 724 header.type = msg.type(); | 527 header.type = msg.type(); |
| 725 header.flags = msg.flags(); | 528 header.flags = msg.flags(); |
| 726 header.num_fds = static_cast<int>(rewritten_msg->desc_count()); | 529 header.num_fds = static_cast<int>(rewritten_msg->desc_count()); |
| 727 | 530 |
| 728 rewritten_msg->SetData(header, msg.payload(), msg.payload_size()); | 531 rewritten_msg->SetData(header, msg.payload(), msg.payload_size()); |
| 729 locked_data_.to_be_received_.push(rewritten_msg); | 532 locked_data_.to_be_received_.push(rewritten_msg); |
| 730 } | 533 } |
| 731 | 534 |
| OLD | NEW |