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 "mojo/system/transport_data.h" |
| 6 |
| 7 #include <string.h> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" |
| 11 #include "mojo/system/constants.h" |
| 12 #include "mojo/system/message_in_transit.h" |
| 13 |
| 14 namespace mojo { |
| 15 namespace system { |
| 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 { |
| 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 } // namespace system |
| 255 } // namespace mojo |
OLD | NEW |