| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_in_transit.h" | 5 #include "mojo/edk/system/message_in_transit.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "mojo/edk/system/constants.h" | 11 #include "mojo/edk/system/configuration.h" |
| 12 #include "mojo/edk/system/transport_data.h" | 12 #include "mojo/edk/system/transport_data.h" |
| 13 | 13 |
| 14 namespace mojo { | 14 namespace mojo { |
| 15 namespace system { | 15 namespace system { |
| 16 | 16 |
| 17 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type | 17 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type |
| 18 MessageInTransit::kTypeMessagePipeEndpoint; | 18 MessageInTransit::kTypeMessagePipeEndpoint; |
| 19 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type | 19 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type |
| 20 MessageInTransit::kTypeMessagePipe; | 20 MessageInTransit::kTypeMessagePipe; |
| 21 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type | 21 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type |
| 22 MessageInTransit::kTypeChannel; | 22 MessageInTransit::kTypeChannel; |
| 23 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type | 23 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Type |
| 24 MessageInTransit::kTypeRawChannel; | 24 MessageInTransit::kTypeRawChannel; |
| 25 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype | 25 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype |
| 26 MessageInTransit::kSubtypeMessagePipeEndpointData; | 26 MessageInTransit::kSubtypeMessagePipeEndpointData; |
| 27 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype | 27 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype |
| 28 MessageInTransit::kSubtypeChannelAttachAndRunEndpoint; | 28 MessageInTransit::kSubtypeChannelAttachAndRunEndpoint; |
| 29 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype | 29 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype |
| 30 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpoint; | 30 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpoint; |
| 31 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype | 31 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype |
| 32 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpointAck; | 32 MessageInTransit::kSubtypeChannelRemoveMessagePipeEndpointAck; |
| 33 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype | 33 STATIC_CONST_MEMBER_DEFINITION const MessageInTransit::Subtype |
| 34 MessageInTransit::kSubtypeRawChannelPosixExtraPlatformHandles; | 34 MessageInTransit::kSubtypeRawChannelPosixExtraPlatformHandles; |
| 35 STATIC_CONST_MEMBER_DEFINITION const size_t MessageInTransit::kMessageAlignment; | 35 STATIC_CONST_MEMBER_DEFINITION const size_t MessageInTransit::kMessageAlignment; |
| 36 | 36 |
| 37 struct MessageInTransit::PrivateStructForCompileAsserts { | 37 struct MessageInTransit::PrivateStructForCompileAsserts { |
| 38 // The size of |Header| must be a multiple of the alignment. | 38 // The size of |Header| must be a multiple of the alignment. |
| 39 static_assert(sizeof(Header) % kMessageAlignment == 0, | 39 static_assert(sizeof(Header) % kMessageAlignment == 0, |
| 40 "sizeof(MessageInTransit::Header) invalid"); | 40 "sizeof(MessageInTransit::Header) invalid"); |
| 41 // Avoid dangerous situations, but making sure that the size of the "header" + | |
| 42 // the size of the data fits into a 31-bit number. | |
| 43 static_assert(static_cast<uint64_t>(sizeof(Header)) + kMaxMessageNumBytes <= | |
| 44 0x7fffffffULL, | |
| 45 "kMaxMessageNumBytes too big"); | |
| 46 | |
| 47 // We assume (to avoid extra rounding code) that the maximum message (data) | |
| 48 // size is a multiple of the alignment. | |
| 49 static_assert(kMaxMessageNumBytes % kMessageAlignment == 0, | |
| 50 "kMessageAlignment not a multiple of alignment"); | |
| 51 }; | 41 }; |
| 52 | 42 |
| 53 MessageInTransit::View::View(size_t message_size, const void* buffer) | 43 MessageInTransit::View::View(size_t message_size, const void* buffer) |
| 54 : buffer_(buffer) { | 44 : buffer_(buffer) { |
| 55 size_t next_message_size = 0; | 45 size_t next_message_size = 0; |
| 56 DCHECK(MessageInTransit::GetNextMessageSize( | 46 DCHECK(MessageInTransit::GetNextMessageSize(buffer_, message_size, |
| 57 buffer_, message_size, &next_message_size)); | 47 &next_message_size)); |
| 58 DCHECK_EQ(message_size, next_message_size); | 48 DCHECK_EQ(message_size, next_message_size); |
| 59 // This should be equivalent. | 49 // This should be equivalent. |
| 60 DCHECK_EQ(message_size, total_size()); | 50 DCHECK_EQ(message_size, total_size()); |
| 61 } | 51 } |
| 62 | 52 |
| 63 bool MessageInTransit::View::IsValid(size_t serialized_platform_handle_size, | 53 bool MessageInTransit::View::IsValid(size_t serialized_platform_handle_size, |
| 64 const char** error_message) const { | 54 const char** error_message) const { |
| 55 size_t max_message_num_bytes = GetConfiguration().max_message_num_bytes; |
| 56 // Avoid dangerous situations, but making sure that the size of the "header" + |
| 57 // the size of the data fits into a 31-bit number. |
| 58 DCHECK_LE(static_cast<uint64_t>(sizeof(Header)) + max_message_num_bytes, |
| 59 0x7fffffffULL) |
| 60 << "GetConfiguration().max_message_num_bytes too big"; |
| 61 |
| 62 // We assume (to avoid extra rounding code) that the maximum message (data) |
| 63 // size is a multiple of the alignment. |
| 64 DCHECK_EQ(max_message_num_bytes % kMessageAlignment, 0U) |
| 65 << "GetConfiguration().max_message_num_bytes not a multiple of alignment"; |
| 66 |
| 65 // Note: This also implies a check on the |main_buffer_size()|, which is just | 67 // Note: This also implies a check on the |main_buffer_size()|, which is just |
| 66 // |RoundUpMessageAlignment(sizeof(Header) + num_bytes())|. | 68 // |RoundUpMessageAlignment(sizeof(Header) + num_bytes())|. |
| 67 if (num_bytes() > kMaxMessageNumBytes) { | 69 if (num_bytes() > max_message_num_bytes) { |
| 68 *error_message = "Message data payload too large"; | 70 *error_message = "Message data payload too large"; |
| 69 return false; | 71 return false; |
| 70 } | 72 } |
| 71 | 73 |
| 72 if (transport_data_buffer_size() > 0) { | 74 if (transport_data_buffer_size() > 0) { |
| 73 const char* e = | 75 const char* e = TransportData::ValidateBuffer( |
| 74 TransportData::ValidateBuffer(serialized_platform_handle_size, | 76 serialized_platform_handle_size, transport_data_buffer(), |
| 75 transport_data_buffer(), | 77 transport_data_buffer_size()); |
| 76 transport_data_buffer_size()); | |
| 77 if (e) { | 78 if (e) { |
| 78 *error_message = e; | 79 *error_message = e; |
| 79 return false; | 80 return false; |
| 80 } | 81 } |
| 81 } | 82 } |
| 82 | 83 |
| 83 return true; | 84 return true; |
| 84 } | 85 } |
| 85 | 86 |
| 86 MessageInTransit::MessageInTransit(Type type, | 87 MessageInTransit::MessageInTransit(Type type, |
| 87 Subtype subtype, | 88 Subtype subtype, |
| 88 uint32_t num_bytes, | 89 uint32_t num_bytes, |
| 89 const void* bytes) | 90 const void* bytes) |
| 90 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), | 91 : main_buffer_size_(RoundUpMessageAlignment(sizeof(Header) + num_bytes)), |
| 91 main_buffer_(static_cast<char*>( | 92 main_buffer_(static_cast<char*>( |
| 92 base::AlignedAlloc(main_buffer_size_, kMessageAlignment))) { | 93 base::AlignedAlloc(main_buffer_size_, kMessageAlignment))) { |
| 93 ConstructorHelper(type, subtype, num_bytes); | 94 ConstructorHelper(type, subtype, num_bytes); |
| 94 if (bytes) { | 95 if (bytes) { |
| 95 memcpy(MessageInTransit::bytes(), bytes, num_bytes); | 96 memcpy(MessageInTransit::bytes(), bytes, num_bytes); |
| 96 memset(static_cast<char*>(MessageInTransit::bytes()) + num_bytes, | 97 memset(static_cast<char*>(MessageInTransit::bytes()) + num_bytes, 0, |
| 97 0, | |
| 98 main_buffer_size_ - sizeof(Header) - num_bytes); | 98 main_buffer_size_ - sizeof(Header) - num_bytes); |
| 99 } else { | 99 } else { |
| 100 memset(MessageInTransit::bytes(), 0, main_buffer_size_ - sizeof(Header)); | 100 memset(MessageInTransit::bytes(), 0, main_buffer_size_ - sizeof(Header)); |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 MessageInTransit::MessageInTransit(Type type, | 104 MessageInTransit::MessageInTransit(Type type, |
| 105 Subtype subtype, | 105 Subtype subtype, |
| 106 uint32_t num_bytes, | 106 uint32_t num_bytes, |
| 107 UserPointer<const void> bytes) | 107 UserPointer<const void> bytes) |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 | 188 |
| 189 transport_data_.reset(new TransportData(dispatchers_.Pass(), channel)); | 189 transport_data_.reset(new TransportData(dispatchers_.Pass(), channel)); |
| 190 | 190 |
| 191 // Update the sizes in the message header. | 191 // Update the sizes in the message header. |
| 192 UpdateTotalSize(); | 192 UpdateTotalSize(); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void MessageInTransit::ConstructorHelper(Type type, | 195 void MessageInTransit::ConstructorHelper(Type type, |
| 196 Subtype subtype, | 196 Subtype subtype, |
| 197 uint32_t num_bytes) { | 197 uint32_t num_bytes) { |
| 198 DCHECK_LE(num_bytes, kMaxMessageNumBytes); | 198 DCHECK_LE(num_bytes, GetConfiguration().max_message_num_bytes); |
| 199 | 199 |
| 200 // |total_size| is updated below, from the other values. | 200 // |total_size| is updated below, from the other values. |
| 201 header()->type = type; | 201 header()->type = type; |
| 202 header()->subtype = subtype; | 202 header()->subtype = subtype; |
| 203 header()->source_id = ChannelEndpointId(); | 203 header()->source_id = ChannelEndpointId(); |
| 204 header()->destination_id = ChannelEndpointId(); | 204 header()->destination_id = ChannelEndpointId(); |
| 205 header()->num_bytes = num_bytes; | 205 header()->num_bytes = num_bytes; |
| 206 header()->unused = 0; | 206 header()->unused = 0; |
| 207 // Note: If dispatchers are subsequently attached, then |total_size| will have | 207 // Note: If dispatchers are subsequently attached, then |total_size| will have |
| 208 // to be adjusted. | 208 // to be adjusted. |
| 209 UpdateTotalSize(); | 209 UpdateTotalSize(); |
| 210 } | 210 } |
| 211 | 211 |
| 212 void MessageInTransit::UpdateTotalSize() { | 212 void MessageInTransit::UpdateTotalSize() { |
| 213 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); | 213 DCHECK_EQ(main_buffer_size_ % kMessageAlignment, 0u); |
| 214 header()->total_size = static_cast<uint32_t>(main_buffer_size_); | 214 header()->total_size = static_cast<uint32_t>(main_buffer_size_); |
| 215 if (transport_data_) { | 215 if (transport_data_) { |
| 216 header()->total_size += | 216 header()->total_size += |
| 217 static_cast<uint32_t>(transport_data_->buffer_size()); | 217 static_cast<uint32_t>(transport_data_->buffer_size()); |
| 218 } | 218 } |
| 219 } | 219 } |
| 220 | 220 |
| 221 } // namespace system | 221 } // namespace system |
| 222 } // namespace mojo | 222 } // namespace mojo |
| OLD | NEW |