| 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 "third_party/mojo/src/mojo/edk/system/transport_data.h" | |
| 6 | |
| 7 #include <string.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "third_party/mojo/src/mojo/edk/system/channel.h" | |
| 12 #include "third_party/mojo/src/mojo/edk/system/configuration.h" | |
| 13 #include "third_party/mojo/src/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 = 16; | |
| 25 static_assert(kMaxSizePerPlatformHandle % MessageInTransit::kMessageAlignment == | |
| 26 0, | |
| 27 "kMaxSizePerPlatformHandle not a multiple of alignment"); | |
| 28 | |
| 29 MOJO_STATIC_CONST_MEMBER_DEFINITION const size_t | |
| 30 TransportData::kMaxSerializedDispatcherSize; | |
| 31 MOJO_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 : buffer_size_() { | |
| 67 DCHECK(dispatchers); | |
| 68 DCHECK(channel); | |
| 69 | |
| 70 const size_t num_handles = dispatchers->size(); | |
| 71 DCHECK_GT(num_handles, 0u); | |
| 72 | |
| 73 // The offset to the start of the (Mojo) handle table. | |
| 74 const size_t handle_table_start_offset = sizeof(Header); | |
| 75 // The offset to the start of the serialized dispatcher data. | |
| 76 const size_t serialized_dispatcher_start_offset = | |
| 77 handle_table_start_offset + num_handles * sizeof(HandleTableEntry); | |
| 78 // The estimated size of the secondary buffer. We compute this estimate below. | |
| 79 // It must be at least as big as the (eventual) actual size. | |
| 80 size_t estimated_size = serialized_dispatcher_start_offset; | |
| 81 size_t estimated_num_platform_handles = 0; | |
| 82 #if DCHECK_IS_ON() | |
| 83 std::vector<size_t> all_max_sizes(num_handles); | |
| 84 std::vector<size_t> all_max_platform_handles(num_handles); | |
| 85 #endif | |
| 86 for (size_t i = 0; i < num_handles; i++) { | |
| 87 if (Dispatcher* dispatcher = (*dispatchers)[i].get()) { | |
| 88 size_t max_size = 0; | |
| 89 size_t max_platform_handles = 0; | |
| 90 Dispatcher::TransportDataAccess::StartSerialize( | |
| 91 dispatcher, channel, &max_size, &max_platform_handles); | |
| 92 | |
| 93 DCHECK_LE(max_size, kMaxSerializedDispatcherSize); | |
| 94 estimated_size += MessageInTransit::RoundUpMessageAlignment(max_size); | |
| 95 DCHECK_LE(estimated_size, GetMaxBufferSize()); | |
| 96 | |
| 97 DCHECK_LE(max_platform_handles, kMaxSerializedDispatcherPlatformHandles); | |
| 98 estimated_num_platform_handles += max_platform_handles; | |
| 99 DCHECK_LE(estimated_num_platform_handles, GetMaxPlatformHandles()); | |
| 100 | |
| 101 #if DCHECK_IS_ON() | |
| 102 all_max_sizes[i] = max_size; | |
| 103 all_max_platform_handles[i] = max_platform_handles; | |
| 104 #endif | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 size_t size_per_platform_handle = 0; | |
| 109 if (estimated_num_platform_handles > 0) { | |
| 110 size_per_platform_handle = channel->GetSerializedPlatformHandleSize(); | |
| 111 DCHECK_LE(size_per_platform_handle, kMaxSizePerPlatformHandle); | |
| 112 estimated_size += estimated_num_platform_handles * size_per_platform_handle; | |
| 113 estimated_size = MessageInTransit::RoundUpMessageAlignment(estimated_size); | |
| 114 DCHECK_LE(estimated_size, GetMaxBufferSize()); | |
| 115 } | |
| 116 | |
| 117 buffer_.reset(static_cast<char*>( | |
| 118 base::AlignedAlloc(estimated_size, MessageInTransit::kMessageAlignment))); | |
| 119 // Entirely clear out the secondary buffer, since then we won't have to worry | |
| 120 // about clearing padding or unused space (e.g., if a dispatcher fails to | |
| 121 // serialize). | |
| 122 memset(buffer_.get(), 0, estimated_size); | |
| 123 | |
| 124 if (estimated_num_platform_handles > 0) { | |
| 125 DCHECK(!platform_handles_); | |
| 126 platform_handles_.reset(new embedder::PlatformHandleVector()); | |
| 127 } | |
| 128 | |
| 129 Header* header = reinterpret_cast<Header*>(buffer_.get()); | |
| 130 header->num_handles = static_cast<uint32_t>(num_handles); | |
| 131 // (Okay to leave |platform_handle_table_offset|, |num_platform_handles|, and | |
| 132 // |unused| be zero; we'll set the former two later if necessary.) | |
| 133 | |
| 134 HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>( | |
| 135 buffer_.get() + handle_table_start_offset); | |
| 136 size_t current_offset = serialized_dispatcher_start_offset; | |
| 137 for (size_t i = 0; i < num_handles; i++) { | |
| 138 Dispatcher* dispatcher = (*dispatchers)[i].get(); | |
| 139 if (!dispatcher) { | |
| 140 static_assert(static_cast<int32_t>(Dispatcher::Type::UNKNOWN) == 0, | |
| 141 "Value of Dispatcher::Type::UNKNOWN must be 0"); | |
| 142 continue; | |
| 143 } | |
| 144 | |
| 145 #if DCHECK_IS_ON() | |
| 146 size_t old_platform_handles_size = | |
| 147 platform_handles_ ? platform_handles_->size() : 0; | |
| 148 #endif | |
| 149 | |
| 150 void* destination = buffer_.get() + current_offset; | |
| 151 size_t actual_size = 0; | |
| 152 if (Dispatcher::TransportDataAccess::EndSerializeAndClose( | |
| 153 dispatcher, channel, destination, &actual_size, | |
| 154 platform_handles_.get())) { | |
| 155 handle_table[i].type = static_cast<int32_t>(dispatcher->GetType()); | |
| 156 handle_table[i].offset = static_cast<uint32_t>(current_offset); | |
| 157 handle_table[i].size = static_cast<uint32_t>(actual_size); | |
| 158 // (Okay to not set |unused| since we cleared the entire buffer.) | |
| 159 | |
| 160 #if DCHECK_IS_ON() | |
| 161 DCHECK_LE(actual_size, all_max_sizes[i]); | |
| 162 DCHECK_LE(platform_handles_ | |
| 163 ? (platform_handles_->size() - old_platform_handles_size) | |
| 164 : 0, | |
| 165 all_max_platform_handles[i]); | |
| 166 #endif | |
| 167 } else { | |
| 168 // Nothing to do on failure, since |buffer_| was cleared, and | |
| 169 // |Type::UNKNOWN| is zero. The handle was simply closed. | |
| 170 LOG(ERROR) << "Failed to serialize handle to remote message pipe"; | |
| 171 } | |
| 172 | |
| 173 current_offset += MessageInTransit::RoundUpMessageAlignment(actual_size); | |
| 174 DCHECK_LE(current_offset, estimated_size); | |
| 175 DCHECK_LE(platform_handles_ ? platform_handles_->size() : 0, | |
| 176 estimated_num_platform_handles); | |
| 177 } | |
| 178 | |
| 179 if (platform_handles_ && platform_handles_->size() > 0) { | |
| 180 header->platform_handle_table_offset = | |
| 181 static_cast<uint32_t>(current_offset); | |
| 182 header->num_platform_handles = | |
| 183 static_cast<uint32_t>(platform_handles_->size()); | |
| 184 current_offset += platform_handles_->size() * size_per_platform_handle; | |
| 185 current_offset = MessageInTransit::RoundUpMessageAlignment(current_offset); | |
| 186 } | |
| 187 | |
| 188 // There's no aligned realloc, so it's no good way to release unused space (if | |
| 189 // we overshot our estimated space requirements). | |
| 190 buffer_size_ = current_offset; | |
| 191 | |
| 192 // |dispatchers_| will be destroyed as it goes out of scope. | |
| 193 } | |
| 194 | |
| 195 TransportData::TransportData( | |
| 196 embedder::ScopedPlatformHandleVectorPtr platform_handles, | |
| 197 size_t serialized_platform_handle_size) | |
| 198 : buffer_size_(), platform_handles_(std::move(platform_handles)) { | |
| 199 buffer_size_ = MessageInTransit::RoundUpMessageAlignment( | |
| 200 sizeof(Header) + | |
| 201 platform_handles_->size() * serialized_platform_handle_size); | |
| 202 buffer_.reset(static_cast<char*>( | |
| 203 base::AlignedAlloc(buffer_size_, MessageInTransit::kMessageAlignment))); | |
| 204 memset(buffer_.get(), 0, buffer_size_); | |
| 205 | |
| 206 Header* header = reinterpret_cast<Header*>(buffer_.get()); | |
| 207 header->platform_handle_table_offset = static_cast<uint32_t>(sizeof(Header)); | |
| 208 header->num_platform_handles = | |
| 209 static_cast<uint32_t>(platform_handles_->size()); | |
| 210 } | |
| 211 | |
| 212 TransportData::~TransportData() { | |
| 213 } | |
| 214 | |
| 215 // static | |
| 216 const char* TransportData::ValidateBuffer( | |
| 217 size_t serialized_platform_handle_size, | |
| 218 const void* buffer, | |
| 219 size_t buffer_size) { | |
| 220 DCHECK(buffer); | |
| 221 DCHECK_GT(buffer_size, 0u); | |
| 222 | |
| 223 // Always make sure that the buffer size is sane; if it's not, someone's | |
| 224 // messing with us. | |
| 225 if (buffer_size < sizeof(Header) || buffer_size > GetMaxBufferSize() || | |
| 226 buffer_size % MessageInTransit::kMessageAlignment != 0) | |
| 227 return "Invalid message secondary buffer size"; | |
| 228 | |
| 229 const Header* header = static_cast<const Header*>(buffer); | |
| 230 const size_t num_handles = header->num_handles; | |
| 231 | |
| 232 // Sanity-check |num_handles| (before multiplying it against anything). | |
| 233 if (num_handles > GetConfiguration().max_message_num_handles) | |
| 234 return "Message handle payload too large"; | |
| 235 | |
| 236 if (buffer_size < sizeof(Header) + num_handles * sizeof(HandleTableEntry)) | |
| 237 return "Message secondary buffer too small"; | |
| 238 | |
| 239 if (header->num_platform_handles == 0) { | |
| 240 // Then |platform_handle_table_offset| should also be zero. | |
| 241 if (header->platform_handle_table_offset != 0) { | |
| 242 return "Message has no handles attached, but platform handle table " | |
| 243 "present"; | |
| 244 } | |
| 245 } else { | |
| 246 if (header->num_platform_handles > | |
| 247 GetConfiguration().max_message_num_handles * | |
| 248 kMaxSerializedDispatcherPlatformHandles) | |
| 249 return "Message has too many platform handles attached"; | |
| 250 | |
| 251 static const char kInvalidPlatformHandleTableOffset[] = | |
| 252 "Message has invalid platform handle table offset"; | |
| 253 // This doesn't check that the platform handle table doesn't alias other | |
| 254 // stuff, but it doesn't matter, since it's all read-only. | |
| 255 if (header->platform_handle_table_offset % | |
| 256 MessageInTransit::kMessageAlignment != | |
| 257 0) | |
| 258 return kInvalidPlatformHandleTableOffset; | |
| 259 | |
| 260 // ">" instead of ">=" since the size per handle may be zero. | |
| 261 if (header->platform_handle_table_offset > buffer_size) | |
| 262 return kInvalidPlatformHandleTableOffset; | |
| 263 | |
| 264 // We already checked |platform_handle_table_offset| and | |
| 265 // |num_platform_handles|, so the addition and multiplication are okay. | |
| 266 if (header->platform_handle_table_offset + | |
| 267 header->num_platform_handles * serialized_platform_handle_size > | |
| 268 buffer_size) | |
| 269 return kInvalidPlatformHandleTableOffset; | |
| 270 } | |
| 271 | |
| 272 const HandleTableEntry* handle_table = | |
| 273 reinterpret_cast<const HandleTableEntry*>( | |
| 274 static_cast<const char*>(buffer) + sizeof(Header)); | |
| 275 static const char kInvalidSerializedDispatcher[] = | |
| 276 "Message contains invalid serialized dispatcher"; | |
| 277 for (size_t i = 0; i < num_handles; i++) { | |
| 278 size_t offset = handle_table[i].offset; | |
| 279 if (offset % MessageInTransit::kMessageAlignment != 0) | |
| 280 return kInvalidSerializedDispatcher; | |
| 281 | |
| 282 size_t size = handle_table[i].size; | |
| 283 if (size > kMaxSerializedDispatcherSize || size > buffer_size) | |
| 284 return kInvalidSerializedDispatcher; | |
| 285 | |
| 286 // Note: This is an overflow-safe check for |offset + size > buffer_size| | |
| 287 // (we know that |size <= buffer_size| from the previous check). | |
| 288 if (offset > buffer_size - size) | |
| 289 return kInvalidSerializedDispatcher; | |
| 290 } | |
| 291 | |
| 292 return nullptr; | |
| 293 } | |
| 294 | |
| 295 // static | |
| 296 void TransportData::GetPlatformHandleTable(const void* transport_data_buffer, | |
| 297 size_t* num_platform_handles, | |
| 298 const void** platform_handle_table) { | |
| 299 DCHECK(transport_data_buffer); | |
| 300 DCHECK(num_platform_handles); | |
| 301 DCHECK(platform_handle_table); | |
| 302 | |
| 303 const Header* header = static_cast<const Header*>(transport_data_buffer); | |
| 304 *num_platform_handles = header->num_platform_handles; | |
| 305 *platform_handle_table = static_cast<const char*>(transport_data_buffer) + | |
| 306 header->platform_handle_table_offset; | |
| 307 } | |
| 308 | |
| 309 // static | |
| 310 scoped_ptr<DispatcherVector> TransportData::DeserializeDispatchers( | |
| 311 const void* buffer, | |
| 312 size_t buffer_size, | |
| 313 embedder::ScopedPlatformHandleVectorPtr platform_handles, | |
| 314 Channel* channel) { | |
| 315 DCHECK(buffer); | |
| 316 DCHECK_GT(buffer_size, 0u); | |
| 317 DCHECK(channel); | |
| 318 | |
| 319 const Header* header = static_cast<const Header*>(buffer); | |
| 320 const size_t num_handles = header->num_handles; | |
| 321 scoped_ptr<DispatcherVector> dispatchers(new DispatcherVector(num_handles)); | |
| 322 | |
| 323 const HandleTableEntry* handle_table = | |
| 324 reinterpret_cast<const HandleTableEntry*>( | |
| 325 static_cast<const char*>(buffer) + sizeof(Header)); | |
| 326 for (size_t i = 0; i < num_handles; i++) { | |
| 327 size_t offset = handle_table[i].offset; | |
| 328 size_t size = handle_table[i].size; | |
| 329 // Should already have been checked by |ValidateBuffer()|: | |
| 330 DCHECK_EQ(offset % MessageInTransit::kMessageAlignment, 0u); | |
| 331 DCHECK_LE(offset, buffer_size); | |
| 332 DCHECK_LE(offset + size, buffer_size); | |
| 333 | |
| 334 const void* source = static_cast<const char*>(buffer) + offset; | |
| 335 (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize( | |
| 336 channel, handle_table[i].type, source, size, platform_handles.get()); | |
| 337 } | |
| 338 | |
| 339 return dispatchers; | |
| 340 } | |
| 341 | |
| 342 } // namespace system | |
| 343 } // namespace mojo | |
| OLD | NEW |