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