ipc_message.cc 5.5 KB

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