1 // Copyright 2012 The Chromium Authors
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 "ipc/ipc_message.h"
6
7 #include <limits.h>
8 #include <stddef.h>
9 #include <stdint.h>
10
11 #include "base/atomic_sequence_num.h"
12 #include "base/containers/span.h"
13 #include "base/logging.h"
14 #include "base/pickle.h"
15 #include "base/trace_event/trace_event.h"
16 #include "build/build_config.h"
17 #include "ipc/ipc_message_attachment.h"
18 #include "ipc/ipc_message_attachment_set.h"
19
20 #if BUILDFLAG(IS_POSIX)
21 #include "base/file_descriptor_posix.h"
22 #include "ipc/ipc_platform_file_attachment_posix.h"
23 #endif
24
25 namespace {
26
27 base::AtomicSequenceNumber g_ref_num;
28
29 // Create a reference number for identifying IPC messages in traces. The return
30 // values has the reference number stored in the upper 24 bits, leaving the low
31 // 8 bits set to 0 for use as flags.
GetRefNumUpper24()32 inline uint32_t GetRefNumUpper24() {
33 base::trace_event::TraceLog* trace_log =
34 base::trace_event::TraceLog::GetInstance();
35 uint32_t pid = trace_log ? trace_log->process_id() : 0;
36 uint32_t count = g_ref_num.GetNext();
37 // The 24 bit hash is composed of 14 bits of the count and 10 bits of the
38 // Process ID. With the current trace event buffer cap, the 14-bit count did
39 // not appear to wrap during a trace. Note that it is not a big deal if
40 // collisions occur, as this is only used for debugging and trace analysis.
41 return ((pid << 14) | (count & 0x3fff)) << 8;
42 }
43
44 } // namespace
45
46 namespace IPC {
47
48 //------------------------------------------------------------------------------
49
50 Message::~Message() = default;
51
Message()52 Message::Message() : base::Pickle(sizeof(Header)) {
53 header()->routing = header()->type = 0;
54 header()->flags = GetRefNumUpper24();
55 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
56 header()->num_fds = 0;
57 header()->pad = 0;
58 #endif
59 Init();
60 }
61
Message(int32_t routing_id,uint32_t type,PriorityValue priority)62 Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority)
63 : base::Pickle(sizeof(Header)) {
64 header()->routing = routing_id;
65 header()->type = type;
66 DCHECK((priority & 0xffffff00) == 0);
67 header()->flags = priority | GetRefNumUpper24();
68 #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
69 header()->num_fds = 0;
70 header()->pad = 0;
71 #endif
72 Init();
73 }
74
Message(const char * data,size_t data_len)75 Message::Message(const char* data, size_t data_len)
76 : base::Pickle(base::Pickle::kUnownedData,
77 base::as_bytes(base::span(data, data_len))) {
78 Init();
79 }
80
Message(const Message & other)81 Message::Message(const Message& other) : base::Pickle(other) {
82 Init();
83 attachment_set_ = other.attachment_set_;
84 }
85
Init()86 void Message::Init() {
87 dispatch_error_ = false;
88 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
89 received_time_ = 0;
90 dont_log_ = false;
91 log_data_ = nullptr;
92 #endif
93 }
94
operator =(const Message & other)95 Message& Message::operator=(const Message& other) {
96 *static_cast<base::Pickle*>(this) = other;
97 attachment_set_ = other.attachment_set_;
98 return *this;
99 }
100
SetHeaderValues(int32_t routing,uint32_t type,uint32_t flags)101 void Message::SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags) {
102 // This should only be called when the message is already empty.
103 DCHECK(payload_size() == 0);
104
105 header()->routing = routing;
106 header()->type = type;
107 header()->flags = flags;
108 }
109
EnsureMessageAttachmentSet()110 void Message::EnsureMessageAttachmentSet() {
111 if (!attachment_set_.get())
112 attachment_set_ = new MessageAttachmentSet;
113 }
114
115 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
set_sent_time(int64_t time)116 void Message::set_sent_time(int64_t time) {
117 DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
118 header()->flags |= HAS_SENT_TIME_BIT;
119 WriteInt64(time);
120 }
121
sent_time() const122 int64_t Message::sent_time() const {
123 if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
124 return 0;
125
126 const char* data = end_of_payload();
127 data -= sizeof(int64_t);
128 return *(reinterpret_cast<const int64_t*>(data));
129 }
130
set_received_time(int64_t time) const131 void Message::set_received_time(int64_t time) const {
132 received_time_ = time;
133 }
134 #endif // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
135
NextMessageInfo()136 Message::NextMessageInfo::NextMessageInfo()
137 : message_size(0), message_found(false), pickle_end(nullptr),
138 message_end(nullptr) {}
139 Message::NextMessageInfo::~NextMessageInfo() = default;
140
141 // static
FindNext(const char * range_start,const char * range_end,NextMessageInfo * info)142 void Message::FindNext(const char* range_start,
143 const char* range_end,
144 NextMessageInfo* info) {
145 DCHECK(info);
146 info->message_found = false;
147 info->message_size = 0;
148
149 size_t pickle_size = 0;
150 if (!base::Pickle::PeekNext(sizeof(Header),
151 range_start, range_end, &pickle_size))
152 return;
153
154 bool have_entire_pickle =
155 static_cast<size_t>(range_end - range_start) >= pickle_size;
156
157 info->message_size = pickle_size;
158
159 if (!have_entire_pickle)
160 return;
161
162 const char* pickle_end = range_start + pickle_size;
163
164 info->message_end = pickle_end;
165
166 info->pickle_end = pickle_end;
167 info->message_found = true;
168 }
169
WriteAttachment(scoped_refptr<base::Pickle::Attachment> attachment)170 bool Message::WriteAttachment(
171 scoped_refptr<base::Pickle::Attachment> attachment) {
172 size_t index;
173 bool success = attachment_set()->AddAttachment(
174 base::WrapRefCounted(static_cast<MessageAttachment*>(attachment.get())),
175 &index);
176 DCHECK(success);
177
178 // Write the index of the descriptor so that we don't have to
179 // keep the current descriptor as extra decoding state when deserialising.
180 WriteInt(static_cast<int>(index));
181
182 return success;
183 }
184
ReadAttachment(base::PickleIterator * iter,scoped_refptr<base::Pickle::Attachment> * attachment) const185 bool Message::ReadAttachment(
186 base::PickleIterator* iter,
187 scoped_refptr<base::Pickle::Attachment>* attachment) const {
188 int index;
189 if (!iter->ReadInt(&index))
190 return false;
191
192 MessageAttachmentSet* attachment_set = attachment_set_.get();
193 if (!attachment_set)
194 return false;
195
196 *attachment = attachment_set->GetAttachmentAt(index);
197
198 return nullptr != attachment->get();
199 }
200
HasAttachments() const201 bool Message::HasAttachments() const {
202 return attachment_set_.get() && !attachment_set_->empty();
203 }
204
205 } // namespace IPC
206