[email protected] | ce208f87 | 2012-03-07 20:42:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 5 | #include "ipc/ipc_message.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 7 | #include <limits.h> |
| 8 | |
[email protected] | dc4fa48 | 2014-02-25 15:30:15 | [diff] [blame] | 9 | #include "base/atomic_sequence_num.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 11 | #include "build/build_config.h" |
erikchen | 87397e0 | 2015-10-10 01:10:24 | [diff] [blame] | 12 | #include "ipc/attachment_broker.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 13 | #include "ipc/ipc_message_attachment.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 14 | #include "ipc/ipc_message_attachment_set.h" |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 15 | #include "ipc/placeholder_brokerable_attachment.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 17 | #if defined(OS_POSIX) |
morrita | 9669385 | 2014-09-24 20:11:45 | [diff] [blame] | 18 | #include "base/file_descriptor_posix.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 19 | #include "ipc/ipc_platform_file_attachment_posix.h" |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 20 | #endif |
| 21 | |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 22 | namespace { |
| 23 | |
[email protected] | dc4fa48 | 2014-02-25 15:30:15 | [diff] [blame] | 24 | base::StaticAtomicSequenceNumber g_ref_num; |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 25 | |
| 26 | // Create a reference number for identifying IPC messages in traces. The return |
| 27 | // values has the reference number stored in the upper 24 bits, leaving the low |
| 28 | // 8 bits set to 0 for use as flags. |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 29 | inline uint32_t GetRefNumUpper24() { |
ssid | 759dd8c | 2015-02-09 21:25:39 | [diff] [blame] | 30 | base::trace_event::TraceLog* trace_log = |
| 31 | base::trace_event::TraceLog::GetInstance(); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 32 | uint32_t pid = trace_log ? trace_log->process_id() : 0; |
| 33 | uint32_t count = g_ref_num.GetNext(); |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 34 | // The 24 bit hash is composed of 14 bits of the count and 10 bits of the |
| 35 | // Process ID. With the current trace event buffer cap, the 14-bit count did |
| 36 | // not appear to wrap during a trace. Note that it is not a big deal if |
| 37 | // collisions occur, as this is only used for debugging and trace analysis. |
[email protected] | 40cb1305 | 2013-08-14 00:37:47 | [diff] [blame] | 38 | return ((pid << 14) | (count & 0x3fff)) << 8; |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | } // namespace |
| 42 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | namespace IPC { |
| 44 | |
| 45 | //------------------------------------------------------------------------------ |
| 46 | |
| 47 | Message::~Message() { |
| 48 | } |
| 49 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 50 | Message::Message() : base::Pickle(sizeof(Header)) { |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 51 | header()->routing = header()->type = 0; |
| 52 | header()->flags = GetRefNumUpper24(); |
erikchen | c32bea67 | 2015-10-19 22:23:32 | [diff] [blame] | 53 | #if USE_ATTACHMENT_BROKER |
erikchen | cd52c88 | 2015-09-29 16:50:05 | [diff] [blame] | 54 | header()->num_brokered_attachments = 0; |
| 55 | #endif |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 56 | #if defined(OS_POSIX) |
| 57 | header()->num_fds = 0; |
[email protected] | 15b97af03 | 2009-12-05 00:00:58 | [diff] [blame] | 58 | header()->pad = 0; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 59 | #endif |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 60 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | } |
| 62 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 63 | Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority) |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 64 | : base::Pickle(sizeof(Header)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | header()->routing = routing_id; |
| 66 | header()->type = type; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 67 | DCHECK((priority & 0xffffff00) == 0); |
| 68 | header()->flags = priority | GetRefNumUpper24(); |
erikchen | c32bea67 | 2015-10-19 22:23:32 | [diff] [blame] | 69 | #if USE_ATTACHMENT_BROKER |
erikchen | cd52c88 | 2015-09-29 16:50:05 | [diff] [blame] | 70 | header()->num_brokered_attachments = 0; |
| 71 | #endif |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 72 | #if defined(OS_POSIX) |
| 73 | header()->num_fds = 0; |
[email protected] | 15b97af03 | 2009-12-05 00:00:58 | [diff] [blame] | 74 | header()->pad = 0; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 75 | #endif |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 76 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | } |
| 78 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 79 | Message::Message(const char* data, int data_len) |
| 80 | : base::Pickle(data, data_len) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 81 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | } |
| 83 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 84 | Message::Message(const Message& other) : base::Pickle(other) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 85 | Init(); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 86 | attachment_set_ = other.attachment_set_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 87 | } |
| 88 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 89 | void Message::Init() { |
| 90 | dispatch_error_ = false; |
erikchen | 3c175a3 | 2015-07-28 23:16:48 | [diff] [blame] | 91 | sender_pid_ = base::kNullProcessId; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 92 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 93 | received_time_ = 0; |
| 94 | dont_log_ = false; |
| 95 | log_data_ = NULL; |
| 96 | #endif |
| 97 | } |
| 98 | |
| 99 | Message& Message::operator=(const Message& other) { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 100 | *static_cast<base::Pickle*>(this) = other; |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 101 | attachment_set_ = other.attachment_set_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 102 | return *this; |
| 103 | } |
| 104 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 105 | void Message::SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags) { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 106 | // This should only be called when the message is already empty. |
| 107 | DCHECK(payload_size() == 0); |
| 108 | |
| 109 | header()->routing = routing; |
| 110 | header()->type = type; |
| 111 | header()->flags = flags; |
| 112 | } |
| 113 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 114 | void Message::EnsureMessageAttachmentSet() { |
| 115 | if (attachment_set_.get() == NULL) |
| 116 | attachment_set_ = new MessageAttachmentSet; |
| 117 | } |
| 118 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | #ifdef IPC_MESSAGE_LOG_ENABLED |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 120 | void Message::set_sent_time(int64_t time) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); |
| 122 | header()->flags |= HAS_SENT_TIME_BIT; |
| 123 | WriteInt64(time); |
| 124 | } |
| 125 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 126 | int64_t Message::sent_time() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | if ((header()->flags & HAS_SENT_TIME_BIT) == 0) |
| 128 | return 0; |
| 129 | |
| 130 | const char* data = end_of_payload(); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 131 | data -= sizeof(int64_t); |
| 132 | return *(reinterpret_cast<const int64_t*>(data)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 133 | } |
| 134 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 135 | void Message::set_received_time(int64_t time) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 136 | received_time_ = time; |
| 137 | } |
| 138 | #endif |
| 139 | |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 140 | Message::NextMessageInfo::NextMessageInfo() |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 141 | : message_size(0), message_found(false), pickle_end(nullptr), |
| 142 | message_end(nullptr) {} |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 143 | Message::NextMessageInfo::~NextMessageInfo() {} |
| 144 | |
erikchen | 3b32506 | 2015-09-22 21:59:10 | [diff] [blame] | 145 | Message::SerializedAttachmentIds |
| 146 | Message::SerializedIdsOfBrokerableAttachments() { |
| 147 | DCHECK(HasBrokerableAttachments()); |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 148 | std::vector<BrokerableAttachment*> attachments = |
| 149 | attachment_set_->GetBrokerableAttachments(); |
erikchen | 3b32506 | 2015-09-22 21:59:10 | [diff] [blame] | 150 | CHECK_LE(attachments.size(), std::numeric_limits<size_t>::max() / |
| 151 | BrokerableAttachment::kNonceSize); |
| 152 | size_t size = attachments.size() * BrokerableAttachment::kNonceSize; |
| 153 | char* buffer = static_cast<char*>(malloc(size)); |
| 154 | for (size_t i = 0; i < attachments.size(); ++i) { |
| 155 | const BrokerableAttachment* attachment = attachments[i]; |
| 156 | char* start_range = buffer + i * BrokerableAttachment::kNonceSize; |
| 157 | BrokerableAttachment::AttachmentId id = attachment->GetIdentifier(); |
| 158 | id.SerializeToBuffer(start_range, BrokerableAttachment::kNonceSize); |
| 159 | } |
| 160 | SerializedAttachmentIds ids; |
| 161 | ids.buffer = buffer; |
| 162 | ids.size = size; |
| 163 | return ids; |
| 164 | } |
| 165 | |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 166 | // static |
| 167 | void Message::FindNext(const char* range_start, |
| 168 | const char* range_end, |
| 169 | NextMessageInfo* info) { |
| 170 | DCHECK(info); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 171 | info->message_found = false; |
| 172 | info->message_size = 0; |
| 173 | |
| 174 | size_t pickle_size = 0; |
| 175 | if (!base::Pickle::PeekNext(sizeof(Header), |
| 176 | range_start, range_end, &pickle_size)) |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 177 | return; |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 178 | |
| 179 | bool have_entire_pickle = |
| 180 | static_cast<size_t>(range_end - range_start) >= pickle_size; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 181 | |
erikchen | 87397e0 | 2015-10-10 01:10:24 | [diff] [blame] | 182 | #if USE_ATTACHMENT_BROKER && defined(OS_MACOSX) && !defined(OS_IOS) |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 183 | // TODO(dskiba): determine message_size when entire pickle is not available |
| 184 | |
| 185 | if (!have_entire_pickle) |
| 186 | return; |
| 187 | |
| 188 | const char* pickle_end = range_start + pickle_size; |
| 189 | |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 190 | // The data is not copied. |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 191 | Message message(range_start, static_cast<int>(pickle_size)); |
| 192 | size_t num_attachments = message.header()->num_brokered_attachments; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 193 | |
| 194 | // Check for possible overflows. |
| 195 | size_t max_size_t = std::numeric_limits<size_t>::max(); |
| 196 | if (num_attachments >= max_size_t / BrokerableAttachment::kNonceSize) |
| 197 | return; |
| 198 | |
| 199 | size_t attachment_length = num_attachments * BrokerableAttachment::kNonceSize; |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 200 | if (pickle_size > max_size_t - attachment_length) |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 201 | return; |
| 202 | |
| 203 | // Check whether the range includes the attachments. |
| 204 | size_t buffer_length = static_cast<size_t>(range_end - range_start); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 205 | if (buffer_length < attachment_length + pickle_size) |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 206 | return; |
| 207 | |
erikchen | 87397e0 | 2015-10-10 01:10:24 | [diff] [blame] | 208 | for (size_t i = 0; i < num_attachments; ++i) { |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 209 | const char* attachment_start = |
| 210 | pickle_end + i * BrokerableAttachment::kNonceSize; |
| 211 | BrokerableAttachment::AttachmentId id(attachment_start, |
| 212 | BrokerableAttachment::kNonceSize); |
| 213 | info->attachment_ids.push_back(id); |
| 214 | } |
| 215 | info->message_end = |
| 216 | pickle_end + num_attachments * BrokerableAttachment::kNonceSize; |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 217 | info->message_size = info->message_end - range_start; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 218 | #else |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 219 | info->message_size = pickle_size; |
| 220 | |
| 221 | if (!have_entire_pickle) |
| 222 | return; |
| 223 | |
| 224 | const char* pickle_end = range_start + pickle_size; |
| 225 | |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 226 | info->message_end = pickle_end; |
| 227 | #endif // USE_ATTACHMENT_BROKER |
| 228 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 229 | info->pickle_end = pickle_end; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 230 | info->message_found = true; |
| 231 | } |
| 232 | |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 233 | bool Message::AddPlaceholderBrokerableAttachmentWithId( |
| 234 | BrokerableAttachment::AttachmentId id) { |
| 235 | scoped_refptr<PlaceholderBrokerableAttachment> attachment( |
| 236 | new PlaceholderBrokerableAttachment(id)); |
| 237 | return attachment_set()->AddAttachment(attachment); |
| 238 | } |
| 239 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 240 | bool Message::WriteAttachment(scoped_refptr<MessageAttachment> attachment) { |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 241 | bool brokerable; |
| 242 | size_t index; |
| 243 | bool success = |
| 244 | attachment_set()->AddAttachment(attachment, &index, &brokerable); |
| 245 | DCHECK(success); |
| 246 | |
| 247 | // Write the type of descriptor. |
| 248 | WriteBool(brokerable); |
| 249 | |
| 250 | // Write the index of the descriptor so that we don't have to |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 251 | // keep the current descriptor as extra decoding state when deserialising. |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 252 | WriteInt(static_cast<int>(index)); |
| 253 | |
| 254 | #if USE_ATTACHMENT_BROKER && defined(OS_MACOSX) && !defined(OS_IOS) |
| 255 | if (brokerable) |
| 256 | header()->num_brokered_attachments++; |
| 257 | #endif |
| 258 | |
| 259 | return success; |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 260 | } |
| 261 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 262 | bool Message::ReadAttachment( |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 263 | base::PickleIterator* iter, |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 264 | scoped_refptr<MessageAttachment>* attachment) const { |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 265 | bool brokerable; |
| 266 | if (!iter->ReadBool(&brokerable)) |
| 267 | return false; |
| 268 | |
| 269 | int index; |
| 270 | if (!iter->ReadInt(&index)) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 271 | return false; |
| 272 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 273 | MessageAttachmentSet* attachment_set = attachment_set_.get(); |
| 274 | if (!attachment_set) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 275 | return false; |
| 276 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 277 | *attachment = brokerable |
| 278 | ? attachment_set->GetBrokerableAttachmentAt(index) |
| 279 | : attachment_set->GetNonBrokerableAttachmentAt(index); |
| 280 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 281 | return nullptr != attachment->get(); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 282 | } |
| 283 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 284 | bool Message::HasAttachments() const { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 285 | return attachment_set_.get() && !attachment_set_->empty(); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 286 | } |
| 287 | |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 288 | bool Message::HasMojoHandles() const { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 289 | return attachment_set_.get() && attachment_set_->num_mojo_handles() > 0; |
| 290 | } |
| 291 | |
| 292 | bool Message::HasBrokerableAttachments() const { |
| 293 | return attachment_set_.get() && |
| 294 | attachment_set_->num_brokerable_attachments() > 0; |
morrita | 81b17e0 | 2015-02-06 00:58:30 | [diff] [blame] | 295 | } |
| 296 | |
[email protected] | d284a5f | 2010-01-05 23:20:30 | [diff] [blame] | 297 | } // namespace IPC |