| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/system/handle_table.h" | 5 #include "mojo/edk/system/handle_table.h" |
| 6 | 6 |
| 7 #include <limits> | |
| 8 #include "base/logging.h" | 7 #include "base/logging.h" |
| 9 #include "base/macros.h" | 8 #include "base/macros.h" |
| 10 #include "mojo/edk/system/configuration.h" | 9 #include "mojo/edk/system/constants.h" |
| 11 #include "mojo/edk/system/dispatcher.h" | 10 #include "mojo/edk/system/dispatcher.h" |
| 12 | 11 |
| 13 namespace mojo { | 12 namespace mojo { |
| 14 namespace system { | 13 namespace system { |
| 15 | 14 |
| 16 HandleTable::Entry::Entry() : busy(false) { | 15 HandleTable::Entry::Entry() : busy(false) { |
| 17 } | 16 } |
| 18 | 17 |
| 19 HandleTable::Entry::Entry(const scoped_refptr<Dispatcher>& dispatcher) | 18 HandleTable::Entry::Entry(const scoped_refptr<Dispatcher>& dispatcher) |
| 20 : dispatcher(dispatcher), busy(false) { | 19 : dispatcher(dispatcher), busy(false) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 if (it->second.busy) | 52 if (it->second.busy) |
| 54 return MOJO_RESULT_BUSY; | 53 return MOJO_RESULT_BUSY; |
| 55 *dispatcher = it->second.dispatcher; | 54 *dispatcher = it->second.dispatcher; |
| 56 handle_to_entry_map_.erase(it); | 55 handle_to_entry_map_.erase(it); |
| 57 | 56 |
| 58 return MOJO_RESULT_OK; | 57 return MOJO_RESULT_OK; |
| 59 } | 58 } |
| 60 | 59 |
| 61 MojoHandle HandleTable::AddDispatcher( | 60 MojoHandle HandleTable::AddDispatcher( |
| 62 const scoped_refptr<Dispatcher>& dispatcher) { | 61 const scoped_refptr<Dispatcher>& dispatcher) { |
| 63 if (handle_to_entry_map_.size() >= GetConfiguration().max_handle_table_size) | 62 if (handle_to_entry_map_.size() >= kMaxHandleTableSize) |
| 64 return MOJO_HANDLE_INVALID; | 63 return MOJO_HANDLE_INVALID; |
| 65 return AddDispatcherNoSizeCheck(dispatcher); | 64 return AddDispatcherNoSizeCheck(dispatcher); |
| 66 } | 65 } |
| 67 | 66 |
| 68 std::pair<MojoHandle, MojoHandle> HandleTable::AddDispatcherPair( | 67 std::pair<MojoHandle, MojoHandle> HandleTable::AddDispatcherPair( |
| 69 const scoped_refptr<Dispatcher>& dispatcher0, | 68 const scoped_refptr<Dispatcher>& dispatcher0, |
| 70 const scoped_refptr<Dispatcher>& dispatcher1) { | 69 const scoped_refptr<Dispatcher>& dispatcher1) { |
| 71 if (handle_to_entry_map_.size() + 1 >= | 70 if (handle_to_entry_map_.size() + 1 >= kMaxHandleTableSize) |
| 72 GetConfiguration().max_handle_table_size) | |
| 73 return std::make_pair(MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID); | 71 return std::make_pair(MOJO_HANDLE_INVALID, MOJO_HANDLE_INVALID); |
| 74 return std::make_pair(AddDispatcherNoSizeCheck(dispatcher0), | 72 return std::make_pair(AddDispatcherNoSizeCheck(dispatcher0), |
| 75 AddDispatcherNoSizeCheck(dispatcher1)); | 73 AddDispatcherNoSizeCheck(dispatcher1)); |
| 76 } | 74 } |
| 77 | 75 |
| 78 bool HandleTable::AddDispatcherVector(const DispatcherVector& dispatchers, | 76 bool HandleTable::AddDispatcherVector(const DispatcherVector& dispatchers, |
| 79 MojoHandle* handles) { | 77 MojoHandle* handles) { |
| 80 size_t max_message_num_handles = GetConfiguration().max_message_num_handles; | 78 DCHECK_LE(dispatchers.size(), kMaxMessageNumHandles); |
| 81 size_t max_handle_table_size = GetConfiguration().max_handle_table_size; | 79 DCHECK(handles); |
| 80 // TODO(vtl): |std::numeric_limits<size_t>::max()| isn't a compile-time |
| 81 // expression in C++03. |
| 82 static_assert( |
| 83 static_cast<uint64_t>(kMaxHandleTableSize) + kMaxMessageNumHandles < |
| 84 (sizeof(size_t) == 8 ? kuint64max |
| 85 : static_cast<uint64_t>(kuint32max)), |
| 86 "Addition may overflow"); |
| 82 | 87 |
| 83 DCHECK_LE(dispatchers.size(), max_message_num_handles); | 88 if (handle_to_entry_map_.size() + dispatchers.size() > kMaxHandleTableSize) |
| 84 DCHECK(handles); | |
| 85 DCHECK_LT( | |
| 86 static_cast<uint64_t>(max_handle_table_size) + max_message_num_handles, | |
| 87 std::numeric_limits<size_t>::max()) | |
| 88 << "Addition may overflow"; | |
| 89 | |
| 90 if (handle_to_entry_map_.size() + dispatchers.size() > max_handle_table_size) | |
| 91 return false; | 89 return false; |
| 92 | 90 |
| 93 for (size_t i = 0; i < dispatchers.size(); i++) { | 91 for (size_t i = 0; i < dispatchers.size(); i++) { |
| 94 if (dispatchers[i].get()) { | 92 if (dispatchers[i].get()) { |
| 95 handles[i] = AddDispatcherNoSizeCheck(dispatchers[i]); | 93 handles[i] = AddDispatcherNoSizeCheck(dispatchers[i]); |
| 96 } else { | 94 } else { |
| 97 LOG(WARNING) << "Invalid dispatcher at index " << i; | 95 LOG(WARNING) << "Invalid dispatcher at index " << i; |
| 98 handles[i] = MOJO_HANDLE_INVALID; | 96 handles[i] = MOJO_HANDLE_INVALID; |
| 99 } | 97 } |
| 100 } | 98 } |
| 101 return true; | 99 return true; |
| 102 } | 100 } |
| 103 | 101 |
| 104 MojoResult HandleTable::MarkBusyAndStartTransport( | 102 MojoResult HandleTable::MarkBusyAndStartTransport( |
| 105 MojoHandle disallowed_handle, | 103 MojoHandle disallowed_handle, |
| 106 const MojoHandle* handles, | 104 const MojoHandle* handles, |
| 107 uint32_t num_handles, | 105 uint32_t num_handles, |
| 108 std::vector<DispatcherTransport>* transports) { | 106 std::vector<DispatcherTransport>* transports) { |
| 109 DCHECK_NE(disallowed_handle, MOJO_HANDLE_INVALID); | 107 DCHECK_NE(disallowed_handle, MOJO_HANDLE_INVALID); |
| 110 DCHECK(handles); | 108 DCHECK(handles); |
| 111 DCHECK_LE(num_handles, GetConfiguration().max_message_num_handles); | 109 DCHECK_LE(num_handles, kMaxMessageNumHandles); |
| 112 DCHECK(transports); | 110 DCHECK(transports); |
| 113 DCHECK_EQ(transports->size(), num_handles); | 111 DCHECK_EQ(transports->size(), num_handles); |
| 114 | 112 |
| 115 std::vector<Entry*> entries(num_handles); | 113 std::vector<Entry*> entries(num_handles); |
| 116 | 114 |
| 117 // First verify all the handles and get their dispatchers. | 115 // First verify all the handles and get their dispatchers. |
| 118 uint32_t i; | 116 uint32_t i; |
| 119 MojoResult error_result = MOJO_RESULT_INTERNAL; | 117 MojoResult error_result = MOJO_RESULT_INTERNAL; |
| 120 for (i = 0; i < num_handles; i++) { | 118 for (i = 0; i < num_handles; i++) { |
| 121 // Sending your own handle is not allowed (and, for consistency, returns | 119 // Sending your own handle is not allowed (and, for consistency, returns |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 } | 180 } |
| 183 return error_result; | 181 return error_result; |
| 184 } | 182 } |
| 185 | 183 |
| 186 return MOJO_RESULT_OK; | 184 return MOJO_RESULT_OK; |
| 187 } | 185 } |
| 188 | 186 |
| 189 MojoHandle HandleTable::AddDispatcherNoSizeCheck( | 187 MojoHandle HandleTable::AddDispatcherNoSizeCheck( |
| 190 const scoped_refptr<Dispatcher>& dispatcher) { | 188 const scoped_refptr<Dispatcher>& dispatcher) { |
| 191 DCHECK(dispatcher.get()); | 189 DCHECK(dispatcher.get()); |
| 192 DCHECK_LT(handle_to_entry_map_.size(), | 190 DCHECK_LT(handle_to_entry_map_.size(), kMaxHandleTableSize); |
| 193 GetConfiguration().max_handle_table_size); | |
| 194 DCHECK_NE(next_handle_, MOJO_HANDLE_INVALID); | 191 DCHECK_NE(next_handle_, MOJO_HANDLE_INVALID); |
| 195 | 192 |
| 196 // TODO(vtl): Maybe we want to do something different/smarter. (Or maybe try | 193 // TODO(vtl): Maybe we want to do something different/smarter. (Or maybe try |
| 197 // assigning randomly?) | 194 // assigning randomly?) |
| 198 while (handle_to_entry_map_.find(next_handle_) != | 195 while (handle_to_entry_map_.find(next_handle_) != |
| 199 handle_to_entry_map_.end()) { | 196 handle_to_entry_map_.end()) { |
| 200 next_handle_++; | 197 next_handle_++; |
| 201 if (next_handle_ == MOJO_HANDLE_INVALID) | 198 if (next_handle_ == MOJO_HANDLE_INVALID) |
| 202 next_handle_++; | 199 next_handle_++; |
| 203 } | 200 } |
| 204 | 201 |
| 205 MojoHandle new_handle = next_handle_; | 202 MojoHandle new_handle = next_handle_; |
| 206 handle_to_entry_map_[new_handle] = Entry(dispatcher); | 203 handle_to_entry_map_[new_handle] = Entry(dispatcher); |
| 207 | 204 |
| 208 next_handle_++; | 205 next_handle_++; |
| 209 if (next_handle_ == MOJO_HANDLE_INVALID) | 206 if (next_handle_ == MOJO_HANDLE_INVALID) |
| 210 next_handle_++; | 207 next_handle_++; |
| 211 | 208 |
| 212 return new_handle; | 209 return new_handle; |
| 213 } | 210 } |
| 214 | 211 |
| 215 void HandleTable::RemoveBusyHandles(const MojoHandle* handles, | 212 void HandleTable::RemoveBusyHandles(const MojoHandle* handles, |
| 216 uint32_t num_handles) { | 213 uint32_t num_handles) { |
| 217 DCHECK(handles); | 214 DCHECK(handles); |
| 218 DCHECK_LE(num_handles, GetConfiguration().max_message_num_handles); | 215 DCHECK_LE(num_handles, kMaxMessageNumHandles); |
| 219 | 216 |
| 220 for (uint32_t i = 0; i < num_handles; i++) { | 217 for (uint32_t i = 0; i < num_handles; i++) { |
| 221 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]); | 218 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]); |
| 222 DCHECK(it != handle_to_entry_map_.end()); | 219 DCHECK(it != handle_to_entry_map_.end()); |
| 223 DCHECK(it->second.busy); | 220 DCHECK(it->second.busy); |
| 224 it->second.busy = false; // For the sake of a |DCHECK()|. | 221 it->second.busy = false; // For the sake of a |DCHECK()|. |
| 225 handle_to_entry_map_.erase(it); | 222 handle_to_entry_map_.erase(it); |
| 226 } | 223 } |
| 227 } | 224 } |
| 228 | 225 |
| 229 void HandleTable::RestoreBusyHandles(const MojoHandle* handles, | 226 void HandleTable::RestoreBusyHandles(const MojoHandle* handles, |
| 230 uint32_t num_handles) { | 227 uint32_t num_handles) { |
| 231 DCHECK(handles); | 228 DCHECK(handles); |
| 232 DCHECK_LE(num_handles, GetConfiguration().max_message_num_handles); | 229 DCHECK_LE(num_handles, kMaxMessageNumHandles); |
| 233 | 230 |
| 234 for (uint32_t i = 0; i < num_handles; i++) { | 231 for (uint32_t i = 0; i < num_handles; i++) { |
| 235 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]); | 232 HandleToEntryMap::iterator it = handle_to_entry_map_.find(handles[i]); |
| 236 DCHECK(it != handle_to_entry_map_.end()); | 233 DCHECK(it != handle_to_entry_map_.end()); |
| 237 DCHECK(it->second.busy); | 234 DCHECK(it->second.busy); |
| 238 it->second.busy = false; | 235 it->second.busy = false; |
| 239 } | 236 } |
| 240 } | 237 } |
| 241 | 238 |
| 242 } // namespace system | 239 } // namespace system |
| 243 } // namespace mojo | 240 } // namespace mojo |
| OLD | NEW |