| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/channel.h" | 5 #include "mojo/edk/system/channel.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 | 116 |
| 117 Channel::Message::~Message() { | 117 Channel::Message::~Message() { |
| 118 base::AlignedFree(data_); | 118 base::AlignedFree(data_); |
| 119 } | 119 } |
| 120 | 120 |
| 121 // static | 121 // static |
| 122 Channel::MessagePtr Channel::Message::Deserialize(const void* data, | 122 Channel::MessagePtr Channel::Message::Deserialize(const void* data, |
| 123 size_t data_num_bytes) { | 123 size_t data_num_bytes) { |
| 124 #if !defined(OS_WIN) && !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
| 125 // We only serialize messages into other messages when performing message | |
| 126 // relay on Windows and OSX. | |
| 127 NOTREACHED(); | |
| 128 return nullptr; | |
| 129 #else | |
| 130 if (data_num_bytes < sizeof(Header)) | 124 if (data_num_bytes < sizeof(Header)) |
| 131 return nullptr; | 125 return nullptr; |
| 132 | 126 |
| 133 const Header* header = reinterpret_cast<const Header*>(data); | 127 const Header* header = reinterpret_cast<const Header*>(data); |
| 134 if (header->num_bytes != data_num_bytes) { | 128 if (header->num_bytes != data_num_bytes) { |
| 135 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes | 129 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes |
| 136 << " != " << data_num_bytes; | 130 << " != " << data_num_bytes; |
| 137 return nullptr; | 131 return nullptr; |
| 138 } | 132 } |
| 139 | 133 |
| 134 #if defined(MOJO_EDK_LEGACY_PROTOCOL) |
| 135 size_t payload_size = data_num_bytes - sizeof(Header); |
| 136 const char* payload = static_cast<const char*>(data) + sizeof(Header); |
| 137 #else |
| 140 if (header->num_bytes < header->num_header_bytes || | 138 if (header->num_bytes < header->num_header_bytes || |
| 141 header->num_header_bytes < sizeof(Header)) { | 139 header->num_header_bytes < sizeof(Header)) { |
| 142 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes << " < " | 140 DLOG(ERROR) << "Decoding invalid message: " << header->num_bytes << " < " |
| 143 << header->num_header_bytes; | 141 << header->num_header_bytes; |
| 144 return nullptr; | 142 return nullptr; |
| 145 } | 143 } |
| 146 | 144 |
| 147 uint32_t extra_header_size = header->num_header_bytes - sizeof(Header); | 145 uint32_t extra_header_size = header->num_header_bytes - sizeof(Header); |
| 146 size_t payload_size = data_num_bytes - header->num_header_bytes; |
| 147 const char* payload = |
| 148 static_cast<const char*>(data) + header->num_header_bytes; |
| 149 #endif // defined(MOJO_EDK_LEGACY_PROTOCOL) |
| 150 |
| 148 #if defined(OS_WIN) | 151 #if defined(OS_WIN) |
| 149 uint32_t max_handles = extra_header_size / sizeof(HandleEntry); | 152 uint32_t max_handles = extra_header_size / sizeof(HandleEntry); |
| 150 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 153 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
| 151 if (extra_header_size < sizeof(MachPortsExtraHeader)) { | 154 if (extra_header_size < sizeof(MachPortsExtraHeader)) { |
| 152 DLOG(ERROR) << "Decoding invalid message: " << extra_header_size << " < " | 155 DLOG(ERROR) << "Decoding invalid message: " << extra_header_size << " < " |
| 153 << sizeof(MachPortsExtraHeader); | 156 << sizeof(MachPortsExtraHeader); |
| 154 return nullptr; | 157 return nullptr; |
| 155 } | 158 } |
| 156 uint32_t max_handles = (extra_header_size - sizeof(MachPortsExtraHeader)) / | 159 uint32_t max_handles = (extra_header_size - sizeof(MachPortsExtraHeader)) / |
| 157 sizeof(MachPortsEntry); | 160 sizeof(MachPortsEntry); |
| 158 #endif | 161 #else |
| 162 const uint32_t max_handles = 0; |
| 163 #endif // defined(OS_WIN) |
| 164 |
| 159 if (header->num_handles > max_handles || max_handles > kMaxAttachedHandles) { | 165 if (header->num_handles > max_handles || max_handles > kMaxAttachedHandles) { |
| 160 DLOG(ERROR) << "Decoding invalid message:" << header->num_handles | 166 DLOG(ERROR) << "Decoding invalid message:" << header->num_handles |
| 161 << " > " << max_handles; | 167 << " > " << max_handles; |
| 162 return nullptr; | 168 return nullptr; |
| 163 } | 169 } |
| 164 | 170 |
| 165 MessagePtr message(new Message(data_num_bytes - header->num_header_bytes, | 171 MessagePtr message(new Message(payload_size, max_handles)); |
| 166 max_handles)); | |
| 167 DCHECK_EQ(message->data_num_bytes(), data_num_bytes); | 172 DCHECK_EQ(message->data_num_bytes(), data_num_bytes); |
| 173 |
| 174 // Copy all payload bytes. |
| 175 if (payload_size) |
| 176 memcpy(message->mutable_payload(), payload, payload_size); |
| 177 |
| 178 #if !defined(MOJO_EDK_LEGACY_PROTOCOL) |
| 168 DCHECK_EQ(message->extra_header_size(), extra_header_size); | 179 DCHECK_EQ(message->extra_header_size(), extra_header_size); |
| 169 DCHECK_EQ(message->header_->num_header_bytes, header->num_header_bytes); | 180 DCHECK_EQ(message->header_->num_header_bytes, header->num_header_bytes); |
| 170 | 181 |
| 171 if (data_num_bytes > header->num_header_bytes) { | |
| 172 // Copy all payload bytes. | |
| 173 memcpy(message->mutable_payload(), | |
| 174 static_cast<const char*>(data) + header->num_header_bytes, | |
| 175 data_num_bytes - header->num_header_bytes); | |
| 176 } | |
| 177 | |
| 178 if (message->extra_header_size()) { | 182 if (message->extra_header_size()) { |
| 179 // Copy extra header bytes. | 183 // Copy extra header bytes. |
| 180 memcpy(message->mutable_extra_header(), | 184 memcpy(message->mutable_extra_header(), |
| 181 static_cast<const char*>(data) + sizeof(Header), | 185 static_cast<const char*>(data) + sizeof(Header), |
| 182 message->extra_header_size()); | 186 message->extra_header_size()); |
| 183 } | 187 } |
| 188 #endif |
| 184 | 189 |
| 185 message->header_->num_handles = header->num_handles; | 190 message->header_->num_handles = header->num_handles; |
| 186 #if defined(OS_WIN) | 191 #if defined(OS_WIN) |
| 187 ScopedPlatformHandleVectorPtr handles( | 192 ScopedPlatformHandleVectorPtr handles( |
| 188 new PlatformHandleVector(header->num_handles)); | 193 new PlatformHandleVector(header->num_handles)); |
| 189 for (size_t i = 0; i < header->num_handles; i++) { | 194 for (size_t i = 0; i < header->num_handles; i++) { |
| 190 (*handles)[i].handle = reinterpret_cast<HANDLE>( | 195 (*handles)[i].handle = reinterpret_cast<HANDLE>( |
| 191 static_cast<uintptr_t>(message->handles_[i].handle)); | 196 static_cast<uintptr_t>(message->handles_[i].handle)); |
| 192 } | 197 } |
| 193 message->SetHandles(std::move(handles)); | 198 message->SetHandles(std::move(handles)); |
| 194 #endif | 199 #endif |
| 195 | 200 |
| 196 return message; | 201 return message; |
| 197 #endif | |
| 198 } | 202 } |
| 199 | 203 |
| 200 size_t Channel::Message::payload_size() const { | 204 size_t Channel::Message::payload_size() const { |
| 201 #if defined(MOJO_EDK_LEGACY_PROTOCOL) | 205 #if defined(MOJO_EDK_LEGACY_PROTOCOL) |
| 202 return header_->num_bytes - sizeof(Header); | 206 return header_->num_bytes - sizeof(Header); |
| 203 #else | 207 #else |
| 204 return size_ - header_->num_header_bytes; | 208 return size_ - header_->num_header_bytes; |
| 205 #endif | 209 #endif |
| 206 } | 210 } |
| 207 | 211 |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 574 | 578 |
| 575 bool Channel::OnControlMessage(Message::Header::MessageType message_type, | 579 bool Channel::OnControlMessage(Message::Header::MessageType message_type, |
| 576 const void* payload, | 580 const void* payload, |
| 577 size_t payload_size, | 581 size_t payload_size, |
| 578 ScopedPlatformHandleVectorPtr handles) { | 582 ScopedPlatformHandleVectorPtr handles) { |
| 579 return false; | 583 return false; |
| 580 } | 584 } |
| 581 | 585 |
| 582 } // namespace edk | 586 } // namespace edk |
| 583 } // namespace mojo | 587 } // namespace mojo |
| OLD | NEW |