• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
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/logging.h"
13 #include "build/build_config.h"
14 #include "ipc/ipc_message_attachment.h"
15 #include "ipc/ipc_message_attachment_set.h"
16 
17 #if defined(OS_POSIX)
18 #include "base/file_descriptor_posix.h"
19 #include "ipc/ipc_platform_file_attachment_posix.h"
20 #endif
21 
22 namespace {
23 
24 base::AtomicSequenceNumber g_ref_num;
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.
GetRefNumUpper24()29 inline uint32_t GetRefNumUpper24() {
30   base::trace_event::TraceLog* trace_log =
31       base::trace_event::TraceLog::GetInstance();
32   uint32_t pid = trace_log ? trace_log->process_id() : 0;
33   uint32_t count = g_ref_num.GetNext();
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.
38   return ((pid << 14) | (count & 0x3fff)) << 8;
39 }
40 
41 }  // namespace
42 
43 namespace IPC {
44 
45 //------------------------------------------------------------------------------
46 
47 Message::~Message() = default;
48 
Message()49 Message::Message() : base::Pickle(sizeof(Header)) {
50   header()->routing = header()->type = 0;
51   header()->flags = GetRefNumUpper24();
52 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
53   header()->num_fds = 0;
54   header()->pad = 0;
55 #endif
56   Init();
57 }
58 
Message(int32_t routing_id,uint32_t type,PriorityValue priority)59 Message::Message(int32_t routing_id, uint32_t type, PriorityValue priority)
60     : base::Pickle(sizeof(Header)) {
61   header()->routing = routing_id;
62   header()->type = type;
63   DCHECK((priority & 0xffffff00) == 0);
64   header()->flags = priority | GetRefNumUpper24();
65 #if defined(OS_POSIX) || defined(OS_FUCHSIA)
66   header()->num_fds = 0;
67   header()->pad = 0;
68 #endif
69   Init();
70 }
71 
Message(const char * data,int data_len)72 Message::Message(const char* data, int data_len)
73     : base::Pickle(data, data_len) {
74   Init();
75 }
76 
Message(const Message & other)77 Message::Message(const Message& other) : base::Pickle(other) {
78   Init();
79   attachment_set_ = other.attachment_set_;
80 }
81 
Init()82 void Message::Init() {
83   dispatch_error_ = false;
84 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
85   received_time_ = 0;
86   dont_log_ = false;
87   log_data_ = NULL;
88 #endif
89 }
90 
operator =(const Message & other)91 Message& Message::operator=(const Message& other) {
92   *static_cast<base::Pickle*>(this) = other;
93   attachment_set_ = other.attachment_set_;
94   return *this;
95 }
96 
SetHeaderValues(int32_t routing,uint32_t type,uint32_t flags)97 void Message::SetHeaderValues(int32_t routing, uint32_t type, uint32_t flags) {
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 
EnsureMessageAttachmentSet()106 void Message::EnsureMessageAttachmentSet() {
107   if (attachment_set_.get() == NULL)
108     attachment_set_ = new MessageAttachmentSet;
109 }
110 
111 #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
set_sent_time(int64_t time)112 void Message::set_sent_time(int64_t time) {
113   DCHECK((header()->flags & HAS_SENT_TIME_BIT) == 0);
114   header()->flags |= HAS_SENT_TIME_BIT;
115   WriteInt64(time);
116 }
117 
sent_time() const118 int64_t Message::sent_time() const {
119   if ((header()->flags & HAS_SENT_TIME_BIT) == 0)
120     return 0;
121 
122   const char* data = end_of_payload();
123   data -= sizeof(int64_t);
124   return *(reinterpret_cast<const int64_t*>(data));
125 }
126 
set_received_time(int64_t time) const127 void Message::set_received_time(int64_t time) const {
128   received_time_ = time;
129 }
130 #endif  // BUILDFLAG(IPC_MESSAGE_LOG_ENABLED)
131 
NextMessageInfo()132 Message::NextMessageInfo::NextMessageInfo()
133     : message_size(0), message_found(false), pickle_end(nullptr),
134       message_end(nullptr) {}
135 Message::NextMessageInfo::~NextMessageInfo() = default;
136 
137 // static
FindNext(const char * range_start,const char * range_end,NextMessageInfo * info)138 void Message::FindNext(const char* range_start,
139                        const char* range_end,
140                        NextMessageInfo* info) {
141   DCHECK(info);
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))
148     return;
149 
150   bool have_entire_pickle =
151       static_cast<size_t>(range_end - range_start) >= pickle_size;
152 
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 
160   info->message_end = pickle_end;
161 
162   info->pickle_end = pickle_end;
163   info->message_found = true;
164 }
165 
WriteAttachment(scoped_refptr<base::Pickle::Attachment> attachment)166 bool Message::WriteAttachment(
167     scoped_refptr<base::Pickle::Attachment> attachment) {
168   size_t index;
169   bool success = attachment_set()->AddAttachment(
170       base::WrapRefCounted(static_cast<MessageAttachment*>(attachment.get())),
171       &index);
172   DCHECK(success);
173 
174   // Write the index of the descriptor so that we don't have to
175   // keep the current descriptor as extra decoding state when deserialising.
176   WriteInt(static_cast<int>(index));
177 
178   return success;
179 }
180 
ReadAttachment(base::PickleIterator * iter,scoped_refptr<base::Pickle::Attachment> * attachment) const181 bool Message::ReadAttachment(
182     base::PickleIterator* iter,
183     scoped_refptr<base::Pickle::Attachment>* attachment) const {
184   int index;
185   if (!iter->ReadInt(&index))
186     return false;
187 
188   MessageAttachmentSet* attachment_set = attachment_set_.get();
189   if (!attachment_set)
190     return false;
191 
192   *attachment = attachment_set->GetAttachmentAt(index);
193 
194   return nullptr != attachment->get();
195 }
196 
HasAttachments() const197 bool Message::HasAttachments() const {
198   return attachment_set_.get() && !attachment_set_->empty();
199 }
200 
201 }  // namespace IPC
202