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