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.
5 #include "mojo/edk/system/handle_table.h"
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "mojo/edk/system/configuration.h"
11 #include "mojo/edk/system/dispatcher.h"
16 HandleTable::Entry::Entry() : busy(false) {
19 HandleTable::Entry::Entry(const scoped_refptr
<Dispatcher
>& dispatcher
)
20 : dispatcher(dispatcher
), busy(false) {
23 HandleTable::Entry::~Entry() {
27 HandleTable::HandleTable() : next_handle_(MOJO_HANDLE_INVALID
+ 1) {
30 HandleTable::~HandleTable() {
31 // This should usually not be reached (the only instance should be owned by
32 // the singleton |Core|, which lives forever), except in tests.
35 Dispatcher
* HandleTable::GetDispatcher(MojoHandle handle
) {
36 DCHECK_NE(handle
, MOJO_HANDLE_INVALID
);
38 HandleToEntryMap::iterator it
= handle_to_entry_map_
.find(handle
);
39 if (it
== handle_to_entry_map_
.end())
41 return it
->second
.dispatcher
.get();
44 MojoResult
HandleTable::GetAndRemoveDispatcher(
46 scoped_refptr
<Dispatcher
>* dispatcher
) {
47 DCHECK_NE(handle
, MOJO_HANDLE_INVALID
);
50 HandleToEntryMap::iterator it
= handle_to_entry_map_
.find(handle
);
51 if (it
== handle_to_entry_map_
.end())
52 return MOJO_RESULT_INVALID_ARGUMENT
;
54 return MOJO_RESULT_BUSY
;
55 *dispatcher
= it
->second
.dispatcher
;
56 handle_to_entry_map_
.erase(it
);
58 return MOJO_RESULT_OK
;
61 MojoHandle
HandleTable::AddDispatcher(
62 const scoped_refptr
<Dispatcher
>& dispatcher
) {
63 if (handle_to_entry_map_
.size() >= GetConfiguration().max_handle_table_size
)
64 return MOJO_HANDLE_INVALID
;
65 return AddDispatcherNoSizeCheck(dispatcher
);
68 std::pair
<MojoHandle
, MojoHandle
> HandleTable::AddDispatcherPair(
69 const scoped_refptr
<Dispatcher
>& dispatcher0
,
70 const scoped_refptr
<Dispatcher
>& dispatcher1
) {
71 if (handle_to_entry_map_
.size() + 1 >=
72 GetConfiguration().max_handle_table_size
)
73 return std::make_pair(MOJO_HANDLE_INVALID
, MOJO_HANDLE_INVALID
);
74 return std::make_pair(AddDispatcherNoSizeCheck(dispatcher0
),
75 AddDispatcherNoSizeCheck(dispatcher1
));
78 bool HandleTable::AddDispatcherVector(const DispatcherVector
& dispatchers
,
79 MojoHandle
* handles
) {
80 size_t max_message_num_handles
= GetConfiguration().max_message_num_handles
;
81 size_t max_handle_table_size
= GetConfiguration().max_handle_table_size
;
83 DCHECK_LE(dispatchers
.size(), max_message_num_handles
);
86 static_cast<uint64_t>(max_handle_table_size
) + max_message_num_handles
,
87 std::numeric_limits
<size_t>::max())
88 << "Addition may overflow";
90 if (handle_to_entry_map_
.size() + dispatchers
.size() > max_handle_table_size
)
93 for (size_t i
= 0; i
< dispatchers
.size(); i
++) {
95 handles
[i
] = AddDispatcherNoSizeCheck(dispatchers
[i
]);
97 LOG(WARNING
) << "Invalid dispatcher at index " << i
;
98 handles
[i
] = MOJO_HANDLE_INVALID
;
104 MojoResult
HandleTable::MarkBusyAndStartTransport(
105 MojoHandle disallowed_handle
,
106 const MojoHandle
* handles
,
107 uint32_t num_handles
,
108 std::vector
<DispatcherTransport
>* transports
) {
109 DCHECK_NE(disallowed_handle
, MOJO_HANDLE_INVALID
);
111 DCHECK_LE(num_handles
, GetConfiguration().max_message_num_handles
);
113 DCHECK_EQ(transports
->size(), num_handles
);
115 std::vector
<Entry
*> entries(num_handles
);
117 // First verify all the handles and get their dispatchers.
119 MojoResult error_result
= MOJO_RESULT_INTERNAL
;
120 for (i
= 0; i
< num_handles
; i
++) {
121 // Sending your own handle is not allowed (and, for consistency, returns
123 if (handles
[i
] == disallowed_handle
) {
124 error_result
= MOJO_RESULT_BUSY
;
128 HandleToEntryMap::iterator it
= handle_to_entry_map_
.find(handles
[i
]);
129 if (it
== handle_to_entry_map_
.end()) {
130 error_result
= MOJO_RESULT_INVALID_ARGUMENT
;
134 entries
[i
] = &it
->second
;
135 if (entries
[i
]->busy
) {
136 error_result
= MOJO_RESULT_BUSY
;
139 // Note: By marking the handle as busy here, we're also preventing the
140 // same handle from being sent multiple times in the same message.
141 entries
[i
]->busy
= true;
143 // Try to start the transport.
144 DispatcherTransport transport
=
145 Dispatcher::HandleTableAccess::TryStartTransport(
146 entries
[i
]->dispatcher
.get());
147 if (!transport
.is_valid()) {
148 // Only log for Debug builds, since this is not a problem with the system
149 // code, but with user code.
150 DLOG(WARNING
) << "Likely race condition in user code detected: attempt "
151 "to transfer handle " << handles
[i
]
152 << " while it is in use on a different thread";
154 // Unset the busy flag (since it won't be unset below).
155 entries
[i
]->busy
= false;
156 error_result
= MOJO_RESULT_BUSY
;
160 // Check if the dispatcher is busy (e.g., in a two-phase read/write).
161 // (Note that this must be done after the dispatcher's lock is acquired.)
162 if (transport
.IsBusy()) {
163 // Unset the busy flag and end the transport (since it won't be done
165 entries
[i
]->busy
= false;
167 error_result
= MOJO_RESULT_BUSY
;
171 // Hang on to the transport (which we'll need to end the transport).
172 (*transports
)[i
] = transport
;
174 if (i
< num_handles
) {
175 DCHECK_NE(error_result
, MOJO_RESULT_INTERNAL
);
177 // Unset the busy flags and release the locks.
178 for (uint32_t j
= 0; j
< i
; j
++) {
179 DCHECK(entries
[j
]->busy
);
180 entries
[j
]->busy
= false;
181 (*transports
)[j
].End();
186 return MOJO_RESULT_OK
;
189 MojoHandle
HandleTable::AddDispatcherNoSizeCheck(
190 const scoped_refptr
<Dispatcher
>& dispatcher
) {
192 DCHECK_LT(handle_to_entry_map_
.size(),
193 GetConfiguration().max_handle_table_size
);
194 DCHECK_NE(next_handle_
, MOJO_HANDLE_INVALID
);
196 // TODO(vtl): Maybe we want to do something different/smarter. (Or maybe try
197 // assigning randomly?)
198 while (handle_to_entry_map_
.find(next_handle_
) !=
199 handle_to_entry_map_
.end()) {
201 if (next_handle_
== MOJO_HANDLE_INVALID
)
205 MojoHandle new_handle
= next_handle_
;
206 handle_to_entry_map_
[new_handle
] = Entry(dispatcher
);
209 if (next_handle_
== MOJO_HANDLE_INVALID
)
215 void HandleTable::RemoveBusyHandles(const MojoHandle
* handles
,
216 uint32_t num_handles
) {
218 DCHECK_LE(num_handles
, GetConfiguration().max_message_num_handles
);
220 for (uint32_t i
= 0; i
< num_handles
; i
++) {
221 HandleToEntryMap::iterator it
= handle_to_entry_map_
.find(handles
[i
]);
222 DCHECK(it
!= handle_to_entry_map_
.end());
223 DCHECK(it
->second
.busy
);
224 it
->second
.busy
= false; // For the sake of a |DCHECK()|.
225 handle_to_entry_map_
.erase(it
);
229 void HandleTable::RestoreBusyHandles(const MojoHandle
* handles
,
230 uint32_t num_handles
) {
232 DCHECK_LE(num_handles
, GetConfiguration().max_message_num_handles
);
234 for (uint32_t i
= 0; i
< num_handles
; i
++) {
235 HandleToEntryMap::iterator it
= handle_to_entry_map_
.find(handles
[i
]);
236 DCHECK(it
!= handle_to_entry_map_
.end());
237 DCHECK(it
->second
.busy
);
238 it
->second
.busy
= false;
242 } // namespace system