[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> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <stdint.h> |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 10 | |
[email protected] | dc4fa48 | 2014-02-25 15:30:15 | [diff] [blame] | 11 | #include "base/atomic_sequence_num.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 13 | #include "build/build_config.h" |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 14 | #include "ipc/ipc_message_attachment.h" |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 15 | #include "ipc/ipc_message_attachment_set.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 | |
tzik | bc4270b | 2017-07-13 04:54:49 | [diff] [blame] | 24 | base::AtomicSequenceNumber 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 | |
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 47 | Message::~Message() = default; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 49 | Message::Message() : base::Pickle(sizeof(Header)) { |
[email protected] | 2a2e3d6 | 2012-09-04 23:08:09 | [diff] [blame] | 50 | header()->routing = header()->type = 0; |
| 51 | header()->flags = GetRefNumUpper24(); |
Fabrice de Gans-Riberi | 894661c | 2018-05-24 18:43:22 | [diff] [blame] | 52 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 53 | header()->num_fds = 0; |
[email protected] | 15b97af03 | 2009-12-05 00:00:58 | [diff] [blame] | 54 | header()->pad = 0; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 55 | #endif |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 56 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 59 | Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority) |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 60 | : base::Pickle(sizeof(Header)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 61 | header()->routing = routing_id; |
| 62 | header()->type = type; |
[email protected] | 753bb25 | 2013-11-04 22:28:12 | [diff] [blame] | 63 | DCHECK((priority & 0xffffff00) == 0); |
| 64 | header()->flags = priority | GetRefNumUpper24(); |
Fabrice de Gans-Riberi | 894661c | 2018-05-24 18:43:22 | [diff] [blame] | 65 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 66 | header()->num_fds = 0; |
[email protected] | 15b97af03 | 2009-12-05 00:00:58 | [diff] [blame] | 67 | header()->pad = 0; |
[email protected] | 526776c | 2009-02-07 00:39:26 | [diff] [blame] | 68 | #endif |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 69 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | } |
| 71 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 72 | Message::Message(const char* data, int data_len) |
| 73 | : base::Pickle(data, data_len) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 74 | Init(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | } |
| 76 | |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 77 | Message::Message(const Message& other) : base::Pickle(other) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 78 | Init(); |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 79 | attachment_set_ = other.attachment_set_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | } |
| 81 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 82 | void Message::Init() { |
| 83 | dispatch_error_ = false; |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 84 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 85 | received_time_ = 0; |
| 86 | dont_log_ = false; |
| 87 | log_data_ = NULL; |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | Message& Message::operator=(const Message& other) { |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 92 | *static_cast<base::Pickle*>(this) = other; |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 93 | attachment_set_ = other.attachment_set_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | return *this; |
| 95 | } |
| 96 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 97 | void Message::SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags) { |
[email protected] | 34d4861 | 2012-06-29 00:05:04 | [diff] [blame] | 98 | // This should only be called when the message is already empty. |
| 99 | DCHECK(payload_size() == 0); |
| 100 | |
| 101 | header()->routing = routing; |
| 102 | header()->type = type; |
| 103 | header()->flags = flags; |
| 104 | } |
| 105 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 106 | void Message::EnsureMessageAttachmentSet() { |
| 107 | if (attachment_set_.get() == NULL) |
| 108 | attachment_set_ = new MessageAttachmentSet; |
| 109 | } |
| 110 | |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 111 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 112 | void Message::set_sent_time(int64_t time) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0); |
| 114 | header()->flags |= HAS_SENT_TIME_BIT; |
| 115 | WriteInt64(time); |
| 116 | } |
| 117 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 118 | int64_t Message::sent_time() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | if ((header()->flags & HAS_SENT_TIME_BIT) == 0) |
| 120 | return 0; |
| 121 | |
| 122 | const char* data = end_of_payload(); |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 123 | data -= sizeof(int64_t); |
| 124 | return *(reinterpret_cast<const int64_t*>(data)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | } |
| 126 | |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 127 | void Message::set_received_time(int64_t time) const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | received_time_ = time; |
| 129 | } |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 130 | #endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 132 | Message::NextMessageInfo::NextMessageInfo() |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 133 | : message_size(0), message_found(false), pickle_end(nullptr), |
| 134 | message_end(nullptr) {} |
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 135 | Message::NextMessageInfo::~NextMessageInfo() = default; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 136 | |
| 137 | // static |
| 138 | void Message::FindNext(const char* range_start, |
| 139 | const char* range_end, |
| 140 | NextMessageInfo* info) { |
| 141 | DCHECK(info); |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 142 | info->message_found = false; |
| 143 | info->message_size = 0; |
| 144 | |
| 145 | size_t pickle_size = 0; |
| 146 | if (!base::Pickle::PeekNext(sizeof(Header), |
| 147 | range_start, range_end, &pickle_size)) |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 148 | return; |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 149 | |
| 150 | bool have_entire_pickle = |
| 151 | static_cast<size_t>(range_end - range_start) >= pickle_size; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 152 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 153 | info->message_size = pickle_size; |
| 154 | |
| 155 | if (!have_entire_pickle) |
| 156 | return; |
| 157 | |
| 158 | const char* pickle_end = range_start + pickle_size; |
| 159 | |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 160 | info->message_end = pickle_end; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 161 | |
dskiba | 6f3790a | 2015-09-30 17:24:30 | [diff] [blame] | 162 | info->pickle_end = pickle_end; |
erikchen | 29e0f2f | 2015-09-11 19:08:53 | [diff] [blame] | 163 | info->message_found = true; |
| 164 | } |
| 165 | |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 166 | bool Message::WriteAttachment( |
| 167 | scoped_refptr<base::Pickle::Attachment> attachment) { |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 168 | size_t index; |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 169 | bool success = attachment_set()->AddAttachment( |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 170 | base::WrapRefCounted(static_cast<MessageAttachment*>(attachment.get())), |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 171 | &index); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 172 | DCHECK(success); |
| 173 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 174 | // Write the index of the descriptor so that we don't have to |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 175 | // keep the current descriptor as extra decoding state when deserialising. |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 176 | WriteInt(static_cast<int>(index)); |
| 177 | |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 178 | return success; |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 179 | } |
| 180 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 181 | bool Message::ReadAttachment( |
brettw | bd4d711 | 2015-06-03 04:29:25 | [diff] [blame] | 182 | base::PickleIterator* iter, |
rockot | 502c94f | 2016-02-03 20:20:16 | [diff] [blame] | 183 | scoped_refptr<base::Pickle::Attachment>* attachment) const { |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 184 | int index; |
| 185 | if (!iter->ReadInt(&index)) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 186 | return false; |
| 187 | |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 188 | MessageAttachmentSet* attachment_set = attachment_set_.get(); |
| 189 | if (!attachment_set) |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 190 | return false; |
| 191 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 192 | *attachment = attachment_set->GetAttachmentAt(index); |
erikchen | ae6d321 | 2015-10-10 02:43:49 | [diff] [blame] | 193 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 194 | return nullptr != attachment->get(); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 195 | } |
| 196 | |
morrita | 1aa788c | 2015-01-31 05:45:42 | [diff] [blame] | 197 | bool Message::HasAttachments() const { |
morrita | 4b5c28e2 | 2015-01-14 21:17:06 | [diff] [blame] | 198 | return attachment_set_.get() && !attachment_set_->empty(); |
[email protected] | 7135bb04 | 2009-02-12 04:05:28 | [diff] [blame] | 199 | } |
| 200 | |
[email protected] | d284a5f | 2010-01-05 23:20:30 | [diff] [blame] | 201 | } // namespace IPC |