PageRenderTime 66ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/mojo/edk/system/transport_data.cc

https://gitlab.com/jonnialva90/iridium-browser
C++ | 336 lines | 243 code | 50 blank | 43 comment | 33 complexity | 0bb0242970c54299341db2cf5d0b31a2 MD5 | raw file
  1. // Copyright 2014 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 "mojo/edk/system/transport_data.h"
  5. #include "base/logging.h"
  6. #include "mojo/edk/system/configuration.h"
  7. #include "mojo/edk/system/message_in_transit.h"
  8. #include "mojo/edk/system/raw_channel.h"
  9. namespace mojo {
  10. namespace edk {
  11. // The maximum amount of space needed per platform handle.
  12. // (|{Channel,RawChannel}::GetSerializedPlatformHandleSize()| should always
  13. // return a value which is at most this. This is only used to calculate
  14. // |TransportData::kMaxBufferSize|. This value should be a multiple of the
  15. // alignment in order to simplify calculations, even though the actual amount of
  16. // space needed need not be a multiple of the alignment.
  17. const size_t kMaxSizePerPlatformHandle = 16;
  18. static_assert(kMaxSizePerPlatformHandle % MessageInTransit::kMessageAlignment ==
  19. 0,
  20. "kMaxSizePerPlatformHandle not a multiple of alignment");
  21. MOJO_STATIC_CONST_MEMBER_DEFINITION const size_t
  22. TransportData::kMaxSerializedDispatcherSize;
  23. MOJO_STATIC_CONST_MEMBER_DEFINITION const size_t
  24. TransportData::kMaxSerializedDispatcherPlatformHandles;
  25. // static
  26. size_t TransportData::GetMaxBufferSize() {
  27. // In additional to the header, for each attached (Mojo) handle there'll be a
  28. // handle table entry and serialized dispatcher data.
  29. return sizeof(Header) +
  30. GetConfiguration().max_message_num_handles *
  31. (sizeof(HandleTableEntry) + kMaxSerializedDispatcherSize) +
  32. GetMaxPlatformHandles() * kMaxSizePerPlatformHandle;
  33. }
  34. // static
  35. size_t TransportData::GetMaxPlatformHandles() {
  36. return GetConfiguration().max_message_num_handles *
  37. kMaxSerializedDispatcherPlatformHandles;
  38. }
  39. struct TransportData::PrivateStructForCompileAsserts {
  40. static_assert(sizeof(Header) % MessageInTransit::kMessageAlignment == 0,
  41. "sizeof(MessageInTransit::Header) not a multiple of alignment");
  42. static_assert(kMaxSerializedDispatcherSize %
  43. MessageInTransit::kMessageAlignment ==
  44. 0,
  45. "kMaxSerializedDispatcherSize not a multiple of alignment");
  46. static_assert(sizeof(HandleTableEntry) %
  47. MessageInTransit::kMessageAlignment ==
  48. 0,
  49. "sizeof(MessageInTransit::HandleTableEntry) not a multiple of "
  50. "alignment");
  51. };
  52. TransportData::TransportData(scoped_ptr<DispatcherVector> dispatchers)
  53. : buffer_size_() {
  54. DCHECK(dispatchers);
  55. const size_t num_handles = dispatchers->size();
  56. DCHECK_GT(num_handles, 0u);
  57. // The offset to the start of the (Mojo) handle table.
  58. const size_t handle_table_start_offset = sizeof(Header);
  59. // The offset to the start of the serialized dispatcher data.
  60. const size_t serialized_dispatcher_start_offset =
  61. handle_table_start_offset + num_handles * sizeof(HandleTableEntry);
  62. // The estimated size of the secondary buffer. We compute this estimate below.
  63. // It must be at least as big as the (eventual) actual size.
  64. size_t estimated_size = serialized_dispatcher_start_offset;
  65. size_t estimated_num_platform_handles = 0;
  66. #if DCHECK_IS_ON()
  67. std::vector<size_t> all_max_sizes(num_handles);
  68. std::vector<size_t> all_max_platform_handles(num_handles);
  69. #endif
  70. for (size_t i = 0; i < num_handles; i++) {
  71. if (Dispatcher* dispatcher = (*dispatchers)[i].get()) {
  72. size_t max_size = 0;
  73. size_t max_platform_handles = 0;
  74. Dispatcher::TransportDataAccess::StartSerialize(
  75. dispatcher, &max_size, &max_platform_handles);
  76. DCHECK_LE(max_size, kMaxSerializedDispatcherSize);
  77. estimated_size += MessageInTransit::RoundUpMessageAlignment(max_size);
  78. DCHECK_LE(estimated_size, GetMaxBufferSize());
  79. DCHECK_LE(max_platform_handles, kMaxSerializedDispatcherPlatformHandles);
  80. estimated_num_platform_handles += max_platform_handles;
  81. DCHECK_LE(estimated_num_platform_handles, GetMaxPlatformHandles());
  82. #if DCHECK_IS_ON()
  83. all_max_sizes[i] = max_size;
  84. all_max_platform_handles[i] = max_platform_handles;
  85. #endif
  86. }
  87. }
  88. size_t size_per_platform_handle = 0;
  89. if (estimated_num_platform_handles > 0) {
  90. size_per_platform_handle = RawChannel::GetSerializedPlatformHandleSize();
  91. DCHECK_LE(size_per_platform_handle, kMaxSizePerPlatformHandle);
  92. estimated_size += estimated_num_platform_handles * size_per_platform_handle;
  93. estimated_size = MessageInTransit::RoundUpMessageAlignment(estimated_size);
  94. DCHECK_LE(estimated_size, GetMaxBufferSize());
  95. }
  96. buffer_.reset(static_cast<char*>(
  97. base::AlignedAlloc(estimated_size, MessageInTransit::kMessageAlignment)));
  98. // Entirely clear out the secondary buffer, since then we won't have to worry
  99. // about clearing padding or unused space (e.g., if a dispatcher fails to
  100. // serialize).
  101. memset(buffer_.get(), 0, estimated_size);
  102. if (estimated_num_platform_handles > 0) {
  103. DCHECK(!platform_handles_);
  104. platform_handles_.reset(new PlatformHandleVector());
  105. }
  106. Header* header = reinterpret_cast<Header*>(buffer_.get());
  107. header->num_handles = static_cast<uint32_t>(num_handles);
  108. // (Okay to leave |platform_handle_table_offset|, |num_platform_handles|, and
  109. // |unused| be zero; we'll set the former two later if necessary.)
  110. HandleTableEntry* handle_table = reinterpret_cast<HandleTableEntry*>(
  111. buffer_.get() + handle_table_start_offset);
  112. size_t current_offset = serialized_dispatcher_start_offset;
  113. for (size_t i = 0; i < num_handles; i++) {
  114. Dispatcher* dispatcher = (*dispatchers)[i].get();
  115. if (!dispatcher) {
  116. static_assert(static_cast<int32_t>(Dispatcher::Type::UNKNOWN) == 0,
  117. "Value of Dispatcher::Type::UNKNOWN must be 0");
  118. continue;
  119. }
  120. #if DCHECK_IS_ON()
  121. size_t old_platform_handles_size =
  122. platform_handles_ ? platform_handles_->size() : 0;
  123. #endif
  124. void* destination = buffer_.get() + current_offset;
  125. size_t actual_size = 0;
  126. if (Dispatcher::TransportDataAccess::EndSerializeAndClose(
  127. dispatcher, destination, &actual_size,
  128. platform_handles_.get())) {
  129. handle_table[i].type = static_cast<int32_t>(dispatcher->GetType());
  130. handle_table[i].offset = static_cast<uint32_t>(current_offset);
  131. handle_table[i].size = static_cast<uint32_t>(actual_size);
  132. // (Okay to not set |unused| since we cleared the entire buffer.)
  133. #if DCHECK_IS_ON()
  134. DCHECK_LE(actual_size, all_max_sizes[i]);
  135. DCHECK_LE(platform_handles_
  136. ? (platform_handles_->size() - old_platform_handles_size)
  137. : 0,
  138. all_max_platform_handles[i]);
  139. #endif
  140. } else {
  141. // Nothing to do on failure, since |buffer_| was cleared, and
  142. // |Type::UNKNOWN| is zero. The handle was simply closed.
  143. LOG(ERROR) << "Failed to serialize handle to remote message pipe";
  144. }
  145. current_offset += MessageInTransit::RoundUpMessageAlignment(actual_size);
  146. DCHECK_LE(current_offset, estimated_size);
  147. DCHECK_LE(platform_handles_ ? platform_handles_->size() : 0,
  148. estimated_num_platform_handles);
  149. }
  150. if (platform_handles_ && platform_handles_->size() > 0) {
  151. header->platform_handle_table_offset =
  152. static_cast<uint32_t>(current_offset);
  153. header->num_platform_handles =
  154. static_cast<uint32_t>(platform_handles_->size());
  155. current_offset += platform_handles_->size() * size_per_platform_handle;
  156. current_offset = MessageInTransit::RoundUpMessageAlignment(current_offset);
  157. }
  158. // There's no aligned realloc, so it's no good way to release unused space (if
  159. // we overshot our estimated space requirements).
  160. buffer_size_ = current_offset;
  161. // |dispatchers_| will be destroyed as it goes out of scope.
  162. }
  163. TransportData::TransportData(
  164. ScopedPlatformHandleVectorPtr platform_handles,
  165. size_t serialized_platform_handle_size)
  166. : buffer_size_(), platform_handles_(platform_handles.Pass()) {
  167. buffer_size_ = MessageInTransit::RoundUpMessageAlignment(
  168. sizeof(Header) +
  169. platform_handles_->size() * serialized_platform_handle_size);
  170. buffer_.reset(static_cast<char*>(
  171. base::AlignedAlloc(buffer_size_, MessageInTransit::kMessageAlignment)));
  172. memset(buffer_.get(), 0, buffer_size_);
  173. Header* header = reinterpret_cast<Header*>(buffer_.get());
  174. header->platform_handle_table_offset = static_cast<uint32_t>(sizeof(Header));
  175. header->num_platform_handles =
  176. static_cast<uint32_t>(platform_handles_->size());
  177. }
  178. TransportData::~TransportData() {
  179. }
  180. // static
  181. const char* TransportData::ValidateBuffer(
  182. size_t serialized_platform_handle_size,
  183. const void* buffer,
  184. size_t buffer_size) {
  185. DCHECK(buffer);
  186. DCHECK_GT(buffer_size, 0u);
  187. // Always make sure that the buffer size is sane; if it's not, someone's
  188. // messing with us.
  189. if (buffer_size < sizeof(Header) || buffer_size > GetMaxBufferSize() ||
  190. buffer_size % MessageInTransit::kMessageAlignment != 0)
  191. return "Invalid message secondary buffer size";
  192. const Header* header = static_cast<const Header*>(buffer);
  193. const size_t num_handles = header->num_handles;
  194. // Sanity-check |num_handles| (before multiplying it against anything).
  195. if (num_handles > GetConfiguration().max_message_num_handles)
  196. return "Message handle payload too large";
  197. if (buffer_size < sizeof(Header) + num_handles * sizeof(HandleTableEntry))
  198. return "Message secondary buffer too small";
  199. if (header->num_platform_handles == 0) {
  200. // Then |platform_handle_table_offset| should also be zero.
  201. if (header->platform_handle_table_offset != 0) {
  202. return "Message has no handles attached, but platform handle table "
  203. "present";
  204. }
  205. } else {
  206. if (header->num_platform_handles >
  207. GetConfiguration().max_message_num_handles *
  208. kMaxSerializedDispatcherPlatformHandles)
  209. return "Message has too many platform handles attached";
  210. static const char kInvalidPlatformHandleTableOffset[] =
  211. "Message has invalid platform handle table offset";
  212. // This doesn't check that the platform handle table doesn't alias other
  213. // stuff, but it doesn't matter, since it's all read-only.
  214. if (header->platform_handle_table_offset %
  215. MessageInTransit::kMessageAlignment !=
  216. 0)
  217. return kInvalidPlatformHandleTableOffset;
  218. // ">" instead of ">=" since the size per handle may be zero.
  219. if (header->platform_handle_table_offset > buffer_size)
  220. return kInvalidPlatformHandleTableOffset;
  221. // We already checked |platform_handle_table_offset| and
  222. // |num_platform_handles|, so the addition and multiplication are okay.
  223. if (header->platform_handle_table_offset +
  224. header->num_platform_handles * serialized_platform_handle_size >
  225. buffer_size)
  226. return kInvalidPlatformHandleTableOffset;
  227. }
  228. const HandleTableEntry* handle_table =
  229. reinterpret_cast<const HandleTableEntry*>(
  230. static_cast<const char*>(buffer) + sizeof(Header));
  231. static const char kInvalidSerializedDispatcher[] =
  232. "Message contains invalid serialized dispatcher";
  233. for (size_t i = 0; i < num_handles; i++) {
  234. size_t offset = handle_table[i].offset;
  235. if (offset % MessageInTransit::kMessageAlignment != 0)
  236. return kInvalidSerializedDispatcher;
  237. size_t size = handle_table[i].size;
  238. if (size > kMaxSerializedDispatcherSize || size > buffer_size)
  239. return kInvalidSerializedDispatcher;
  240. // Note: This is an overflow-safe check for |offset + size > buffer_size|
  241. // (we know that |size <= buffer_size| from the previous check).
  242. if (offset > buffer_size - size)
  243. return kInvalidSerializedDispatcher;
  244. }
  245. return nullptr;
  246. }
  247. // static
  248. void TransportData::GetPlatformHandleTable(const void* transport_data_buffer,
  249. size_t* num_platform_handles,
  250. const void** platform_handle_table) {
  251. DCHECK(transport_data_buffer);
  252. DCHECK(num_platform_handles);
  253. DCHECK(platform_handle_table);
  254. const Header* header = static_cast<const Header*>(transport_data_buffer);
  255. *num_platform_handles = header->num_platform_handles;
  256. *platform_handle_table = static_cast<const char*>(transport_data_buffer) +
  257. header->platform_handle_table_offset;
  258. }
  259. // static
  260. scoped_ptr<DispatcherVector> TransportData::DeserializeDispatchers(
  261. const void* buffer,
  262. size_t buffer_size,
  263. ScopedPlatformHandleVectorPtr platform_handles) {
  264. DCHECK(buffer);
  265. DCHECK_GT(buffer_size, 0u);
  266. const Header* header = static_cast<const Header*>(buffer);
  267. const size_t num_handles = header->num_handles;
  268. scoped_ptr<DispatcherVector> dispatchers(new DispatcherVector(num_handles));
  269. const HandleTableEntry* handle_table =
  270. reinterpret_cast<const HandleTableEntry*>(
  271. static_cast<const char*>(buffer) + sizeof(Header));
  272. for (size_t i = 0; i < num_handles; i++) {
  273. size_t offset = handle_table[i].offset;
  274. size_t size = handle_table[i].size;
  275. // Should already have been checked by |ValidateBuffer()|:
  276. DCHECK_EQ(offset % MessageInTransit::kMessageAlignment, 0u);
  277. DCHECK_LE(offset, buffer_size);
  278. DCHECK_LE(offset + size, buffer_size);
  279. const void* source = static_cast<const char*>(buffer) + offset;
  280. (*dispatchers)[i] = Dispatcher::TransportDataAccess::Deserialize(
  281. handle_table[i].type, source, size, platform_handles.get());
  282. }
  283. return dispatchers.Pass();
  284. }
  285. } // namespace edk
  286. } // namespace mojo