Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "RpcState" |
| 18 | |
| 19 | #include "RpcState.h" |
| 20 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 21 | #include <android-base/scopeguard.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 22 | #include <binder/BpBinder.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 24 | #include <binder/RpcServer.h> |
| 25 | |
| 26 | #include "Debug.h" |
| 27 | #include "RpcWireFormat.h" |
| 28 | |
| 29 | #include <inttypes.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 33 | using base::ScopeGuard; |
| 34 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 35 | RpcState::RpcState() {} |
| 36 | RpcState::~RpcState() {} |
| 37 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 38 | status_t RpcState::onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 39 | RpcAddress* outAddress) { |
| 40 | bool isRemote = binder->remoteBinder(); |
| 41 | bool isRpc = isRemote && binder->remoteBinder()->isRpcBinder(); |
| 42 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 43 | if (isRpc && binder->remoteBinder()->getPrivateAccessorForId().rpcSession() != session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 44 | // We need to be able to send instructions over the socket for how to |
| 45 | // connect to a different server, and we also need to let the host |
| 46 | // process know that this is happening. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 47 | ALOGE("Cannot send binder from unrelated binder RPC session."); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 48 | return INVALID_OPERATION; |
| 49 | } |
| 50 | |
| 51 | if (isRemote && !isRpc) { |
| 52 | // Without additional work, this would have the effect of using this |
| 53 | // process to proxy calls from the socket over to the other process, and |
| 54 | // it would make those calls look like they come from us (not over the |
| 55 | // sockets). In order to make this work transparently like binder, we |
| 56 | // would instead need to send instructions over the socket for how to |
| 57 | // connect to the host process, and we also need to let the host process |
| 58 | // know this was happening. |
| 59 | ALOGE("Cannot send binder proxy %p over sockets", binder.get()); |
| 60 | return INVALID_OPERATION; |
| 61 | } |
| 62 | |
| 63 | std::lock_guard<std::mutex> _l(mNodeMutex); |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 64 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 65 | |
| 66 | // TODO(b/182939933): maybe move address out of BpBinder, and keep binder->address map |
| 67 | // in RpcState |
| 68 | for (auto& [addr, node] : mNodeForAddress) { |
| 69 | if (binder == node.binder) { |
| 70 | if (isRpc) { |
| 71 | const RpcAddress& actualAddr = |
| 72 | binder->remoteBinder()->getPrivateAccessorForId().rpcAddress(); |
| 73 | // TODO(b/182939933): this is only checking integrity of data structure |
| 74 | // a different data structure doesn't need this |
| 75 | LOG_ALWAYS_FATAL_IF(addr < actualAddr, "Address mismatch"); |
| 76 | LOG_ALWAYS_FATAL_IF(actualAddr < addr, "Address mismatch"); |
| 77 | } |
| 78 | node.timesSent++; |
| 79 | node.sentRef = binder; // might already be set |
| 80 | *outAddress = addr; |
| 81 | return OK; |
| 82 | } |
| 83 | } |
| 84 | LOG_ALWAYS_FATAL_IF(isRpc, "RPC binder must have known address at this point"); |
| 85 | |
| 86 | auto&& [it, inserted] = mNodeForAddress.insert({RpcAddress::unique(), |
| 87 | BinderNode{ |
| 88 | .binder = binder, |
| 89 | .timesSent = 1, |
| 90 | .sentRef = binder, |
| 91 | }}); |
| 92 | // TODO(b/182939933): better organization could avoid needing this log |
| 93 | LOG_ALWAYS_FATAL_IF(!inserted); |
| 94 | |
| 95 | *outAddress = it->first; |
| 96 | return OK; |
| 97 | } |
| 98 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 99 | status_t RpcState::onBinderEntering(const sp<RpcSession>& session, const RpcAddress& address, |
| 100 | sp<IBinder>* out) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 101 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 102 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 103 | |
| 104 | if (auto it = mNodeForAddress.find(address); it != mNodeForAddress.end()) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 105 | *out = it->second.binder.promote(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 106 | |
| 107 | // implicitly have strong RPC refcount, since we received this binder |
| 108 | it->second.timesRecd++; |
| 109 | |
| 110 | _l.unlock(); |
| 111 | |
| 112 | // We have timesRecd RPC refcounts, but we only need to hold on to one |
| 113 | // when we keep the object. All additional dec strongs are sent |
| 114 | // immediately, we wait to send the last one in BpBinder::onLastDecStrong. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 115 | (void)session->sendDecStrong(address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 116 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 117 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | auto&& [it, inserted] = mNodeForAddress.insert({address, BinderNode{}}); |
| 121 | LOG_ALWAYS_FATAL_IF(!inserted, "Failed to insert binder when creating proxy"); |
| 122 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 123 | // Currently, all binders are assumed to be part of the same session (no |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 124 | // device global binders in the RPC world). |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 125 | it->second.binder = *out = BpBinder::create(session, it->first); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 126 | it->second.timesRecd = 1; |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 127 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | size_t RpcState::countBinders() { |
| 131 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 132 | return mNodeForAddress.size(); |
| 133 | } |
| 134 | |
| 135 | void RpcState::dump() { |
| 136 | std::lock_guard<std::mutex> _l(mNodeMutex); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 137 | dumpLocked(); |
| 138 | } |
| 139 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 140 | void RpcState::clear() { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 141 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 142 | |
| 143 | if (mTerminated) { |
| 144 | LOG_ALWAYS_FATAL_IF(!mNodeForAddress.empty(), |
| 145 | "New state should be impossible after terminating!"); |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | if (SHOULD_LOG_RPC_DETAIL) { |
| 150 | ALOGE("RpcState::clear()"); |
| 151 | dumpLocked(); |
| 152 | } |
| 153 | |
| 154 | // if the destructor of a binder object makes another RPC call, then calling |
| 155 | // decStrong could deadlock. So, we must hold onto these binders until |
| 156 | // mNodeMutex is no longer taken. |
| 157 | std::vector<sp<IBinder>> tempHoldBinder; |
| 158 | |
| 159 | mTerminated = true; |
| 160 | for (auto& [address, node] : mNodeForAddress) { |
| 161 | sp<IBinder> binder = node.binder.promote(); |
| 162 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "Binder %p expected to be owned.", binder.get()); |
| 163 | |
| 164 | if (node.sentRef != nullptr) { |
| 165 | tempHoldBinder.push_back(node.sentRef); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | mNodeForAddress.clear(); |
| 170 | |
| 171 | _l.unlock(); |
| 172 | tempHoldBinder.clear(); // explicit |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void RpcState::dumpLocked() { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 176 | ALOGE("DUMP OF RpcState %p", this); |
| 177 | ALOGE("DUMP OF RpcState (%zu nodes)", mNodeForAddress.size()); |
| 178 | for (const auto& [address, node] : mNodeForAddress) { |
| 179 | sp<IBinder> binder = node.binder.promote(); |
| 180 | |
| 181 | const char* desc; |
| 182 | if (binder) { |
| 183 | if (binder->remoteBinder()) { |
| 184 | if (binder->remoteBinder()->isRpcBinder()) { |
| 185 | desc = "(rpc binder proxy)"; |
| 186 | } else { |
| 187 | desc = "(binder proxy)"; |
| 188 | } |
| 189 | } else { |
| 190 | desc = "(local binder)"; |
| 191 | } |
| 192 | } else { |
| 193 | desc = "(null)"; |
| 194 | } |
| 195 | |
| 196 | ALOGE("- BINDER NODE: %p times sent:%zu times recd: %zu a:%s type:%s", |
| 197 | node.binder.unsafe_get(), node.timesSent, node.timesRecd, address.toString().c_str(), |
| 198 | desc); |
| 199 | } |
| 200 | ALOGE("END DUMP OF RpcState"); |
| 201 | } |
| 202 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 203 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 204 | RpcState::CommandData::CommandData(size_t size) : mSize(size) { |
| 205 | // The maximum size for regular binder is 1MB for all concurrent |
| 206 | // transactions. A very small proportion of transactions are even |
| 207 | // larger than a page, but we need to avoid allocating too much |
| 208 | // data on behalf of an arbitrary client, or we could risk being in |
| 209 | // a position where a single additional allocation could run out of |
| 210 | // memory. |
| 211 | // |
| 212 | // Note, this limit may not reflect the total amount of data allocated for a |
| 213 | // transaction (in some cases, additional fixed size amounts are added), |
| 214 | // though for rough consistency, we should avoid cases where this data type |
| 215 | // is used for multiple dynamic allocations for a single transaction. |
| 216 | constexpr size_t kMaxTransactionAllocation = 100 * 1000; |
| 217 | if (size == 0) return; |
| 218 | if (size > kMaxTransactionAllocation) { |
| 219 | ALOGW("Transaction requested too much data allocation %zu", size); |
| 220 | return; |
| 221 | } |
| 222 | mData.reset(new (std::nothrow) uint8_t[size]); |
| 223 | } |
| 224 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 225 | status_t RpcState::rpcSend(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 226 | const char* what, const void* data, size_t size) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 227 | LOG_RPC_DETAIL("Sending %s on fd %d: %s", what, fd.get(), hexString(data, size).c_str()); |
| 228 | |
| 229 | if (size > std::numeric_limits<ssize_t>::max()) { |
| 230 | ALOGE("Cannot send %s at size %zu (too big)", what, size); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 231 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 232 | return BAD_VALUE; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Steven Moreland | c6ddf36 | 2021-04-02 01:13:36 +0000 | [diff] [blame] | 235 | ssize_t sent = TEMP_FAILURE_RETRY(send(fd.get(), data, size, MSG_NOSIGNAL)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 236 | |
| 237 | if (sent < 0 || sent != static_cast<ssize_t>(size)) { |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 238 | int savedErrno = errno; |
Steven Moreland | c12c9d9 | 2021-05-26 18:44:11 +0000 | [diff] [blame] | 239 | LOG_RPC_DETAIL("Failed to send %s (sent %zd of %zu bytes) on fd %d, error: %s", what, sent, |
| 240 | size, fd.get(), strerror(savedErrno)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 241 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 242 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 243 | return -savedErrno; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 246 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 249 | status_t RpcState::rpcRec(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 250 | const char* what, void* data, size_t size) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 251 | if (size > std::numeric_limits<ssize_t>::max()) { |
| 252 | ALOGE("Cannot rec %s at size %zu (too big)", what, size); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 253 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 254 | return BAD_VALUE; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 257 | if (status_t status = session->mShutdownTrigger->interruptableReadFully(fd.get(), data, size); |
| 258 | status != OK) { |
Steven Moreland | c12c9d9 | 2021-05-26 18:44:11 +0000 | [diff] [blame] | 259 | LOG_RPC_DETAIL("Failed to read %s (%zu bytes) on fd %d, error: %s", what, size, fd.get(), |
| 260 | statusToString(status).c_str()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 261 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 264 | LOG_RPC_DETAIL("Received %s on fd %d: %s", what, fd.get(), hexString(data, size).c_str()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 265 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 268 | sp<IBinder> RpcState::getRootObject(const base::unique_fd& fd, const sp<RpcSession>& session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 269 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 270 | data.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 271 | Parcel reply; |
| 272 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 273 | status_t status = transactAddress(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_ROOT, data, |
| 274 | session, &reply, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 275 | if (status != OK) { |
| 276 | ALOGE("Error getting root object: %s", statusToString(status).c_str()); |
| 277 | return nullptr; |
| 278 | } |
| 279 | |
| 280 | return reply.readStrongBinder(); |
| 281 | } |
| 282 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 283 | status_t RpcState::getMaxThreads(const base::unique_fd& fd, const sp<RpcSession>& session, |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 284 | size_t* maxThreadsOut) { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 285 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 286 | data.markForRpc(session); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 287 | Parcel reply; |
| 288 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 289 | status_t status = transactAddress(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, |
| 290 | data, session, &reply, 0); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 291 | if (status != OK) { |
| 292 | ALOGE("Error getting max threads: %s", statusToString(status).c_str()); |
| 293 | return status; |
| 294 | } |
| 295 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 296 | int32_t maxThreads; |
| 297 | status = reply.readInt32(&maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 298 | if (status != OK) return status; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 299 | if (maxThreads <= 0) { |
| 300 | ALOGE("Error invalid max maxThreads: %d", maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 301 | return BAD_VALUE; |
| 302 | } |
| 303 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 304 | *maxThreadsOut = maxThreads; |
| 305 | return OK; |
| 306 | } |
| 307 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 308 | status_t RpcState::getSessionId(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 309 | int32_t* sessionIdOut) { |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 310 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 311 | data.markForRpc(session); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 312 | Parcel reply; |
| 313 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 314 | status_t status = transactAddress(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_SESSION_ID, |
| 315 | data, session, &reply, 0); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 316 | if (status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 317 | ALOGE("Error getting session ID: %s", statusToString(status).c_str()); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 318 | return status; |
| 319 | } |
| 320 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 321 | int32_t sessionId; |
| 322 | status = reply.readInt32(&sessionId); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 323 | if (status != OK) return status; |
| 324 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 325 | *sessionIdOut = sessionId; |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 326 | return OK; |
| 327 | } |
| 328 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 329 | status_t RpcState::transact(const base::unique_fd& fd, const sp<IBinder>& binder, uint32_t code, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 330 | const Parcel& data, const sp<RpcSession>& session, Parcel* reply, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 331 | uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 332 | if (!data.isForRpc()) { |
| 333 | ALOGE("Refusing to send RPC with parcel not crafted for RPC"); |
| 334 | return BAD_TYPE; |
| 335 | } |
| 336 | |
| 337 | if (data.objectsCount() != 0) { |
| 338 | ALOGE("Parcel at %p has attached objects but is being used in an RPC call", &data); |
| 339 | return BAD_TYPE; |
| 340 | } |
| 341 | |
| 342 | RpcAddress address = RpcAddress::zero(); |
| 343 | if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status; |
| 344 | |
| 345 | return transactAddress(fd, address, code, data, session, reply, flags); |
| 346 | } |
| 347 | |
| 348 | status_t RpcState::transactAddress(const base::unique_fd& fd, const RpcAddress& address, |
| 349 | uint32_t code, const Parcel& data, const sp<RpcSession>& session, |
| 350 | Parcel* reply, uint32_t flags) { |
| 351 | LOG_ALWAYS_FATAL_IF(!data.isForRpc()); |
| 352 | LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0); |
| 353 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 354 | uint64_t asyncNumber = 0; |
| 355 | |
| 356 | if (!address.isZero()) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 357 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 358 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 359 | auto it = mNodeForAddress.find(address); |
| 360 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending transact on unknown address %s", |
| 361 | address.toString().c_str()); |
| 362 | |
| 363 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 364 | asyncNumber = it->second.asyncNumber; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 365 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 366 | _l.unlock(); |
| 367 | (void)session->shutdownAndWait(false); |
| 368 | return DEAD_OBJECT; |
| 369 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 373 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 374 | sizeof(RpcWireTransaction) < |
| 375 | data.dataSize(), |
| 376 | "Too much data %zu", data.dataSize()); |
| 377 | |
| 378 | RpcWireHeader command{ |
| 379 | .command = RPC_COMMAND_TRANSACT, |
| 380 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireTransaction) + data.dataSize()), |
| 381 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 382 | RpcWireTransaction transaction{ |
| 383 | .address = address.viewRawEmbedded(), |
| 384 | .code = code, |
| 385 | .flags = flags, |
| 386 | .asyncNumber = asyncNumber, |
| 387 | }; |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 388 | CommandData transactionData(sizeof(RpcWireHeader) + sizeof(RpcWireTransaction) + |
| 389 | data.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 390 | if (!transactionData.valid()) { |
| 391 | return NO_MEMORY; |
| 392 | } |
| 393 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 394 | memcpy(transactionData.data() + 0, &command, sizeof(RpcWireHeader)); |
| 395 | memcpy(transactionData.data() + sizeof(RpcWireHeader), &transaction, |
| 396 | sizeof(RpcWireTransaction)); |
| 397 | memcpy(transactionData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireTransaction), data.data(), |
| 398 | data.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 399 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 400 | if (status_t status = |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 401 | rpcSend(fd, session, "transaction", transactionData.data(), transactionData.size()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 402 | status != OK) |
| 403 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 404 | |
| 405 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 659416d | 2021-05-11 00:47:50 +0000 | [diff] [blame] | 406 | LOG_RPC_DETAIL("Oneway command, so no longer waiting on %d", fd.get()); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 407 | |
| 408 | // Do not wait on result. |
| 409 | // However, too many oneway calls may cause refcounts to build up and fill up the socket, |
| 410 | // so process those. |
| 411 | return drainCommands(fd, session, CommandType::CONTROL_ONLY); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction."); |
| 415 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 416 | return waitForReply(fd, session, reply); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 419 | static void cleanup_reply_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 420 | const binder_size_t* objects, size_t objectsCount) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 421 | (void)p; |
| 422 | delete[] const_cast<uint8_t*>(data - offsetof(RpcWireReply, data)); |
| 423 | (void)dataSize; |
| 424 | LOG_ALWAYS_FATAL_IF(objects != nullptr); |
| 425 | LOG_ALWAYS_FATAL_IF(objectsCount, 0); |
| 426 | } |
| 427 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 428 | status_t RpcState::waitForReply(const base::unique_fd& fd, const sp<RpcSession>& session, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 429 | Parcel* reply) { |
| 430 | RpcWireHeader command; |
| 431 | while (true) { |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 432 | if (status_t status = rpcRec(fd, session, "command header", &command, sizeof(command)); |
| 433 | status != OK) |
| 434 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 435 | |
| 436 | if (command.command == RPC_COMMAND_REPLY) break; |
| 437 | |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 438 | if (status_t status = processServerCommand(fd, session, command, CommandType::ANY); |
| 439 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 440 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 441 | } |
| 442 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 443 | CommandData data(command.bodySize); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 444 | if (!data.valid()) return NO_MEMORY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 445 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 446 | if (status_t status = rpcRec(fd, session, "reply body", data.data(), command.bodySize); |
| 447 | status != OK) |
| 448 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 449 | |
| 450 | if (command.bodySize < sizeof(RpcWireReply)) { |
| 451 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!", |
| 452 | sizeof(RpcWireReply), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 453 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 454 | return BAD_VALUE; |
| 455 | } |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 456 | RpcWireReply* rpcReply = reinterpret_cast<RpcWireReply*>(data.data()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 457 | if (rpcReply->status != OK) return rpcReply->status; |
| 458 | |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 459 | data.release(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 460 | reply->ipcSetDataReference(rpcReply->data, command.bodySize - offsetof(RpcWireReply, data), |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 461 | nullptr, 0, cleanup_reply_data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 462 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 463 | reply->markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 464 | |
| 465 | return OK; |
| 466 | } |
| 467 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 468 | status_t RpcState::sendDecStrong(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 469 | const RpcAddress& addr) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 470 | { |
| 471 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 472 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 473 | auto it = mNodeForAddress.find(addr); |
| 474 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending dec strong on unknown address %s", |
| 475 | addr.toString().c_str()); |
| 476 | LOG_ALWAYS_FATAL_IF(it->second.timesRecd <= 0, "Bad dec strong %s", |
| 477 | addr.toString().c_str()); |
| 478 | |
| 479 | it->second.timesRecd--; |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 480 | LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(it), |
| 481 | "Bad state. RpcState shouldn't own received binder"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | RpcWireHeader cmd = { |
| 485 | .command = RPC_COMMAND_DEC_STRONG, |
| 486 | .bodySize = sizeof(RpcWireAddress), |
| 487 | }; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 488 | if (status_t status = rpcSend(fd, session, "dec ref header", &cmd, sizeof(cmd)); status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 489 | return status; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 490 | if (status_t status = rpcSend(fd, session, "dec ref body", &addr.viewRawEmbedded(), |
| 491 | sizeof(RpcWireAddress)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 492 | status != OK) |
| 493 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 494 | return OK; |
| 495 | } |
| 496 | |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 497 | status_t RpcState::getAndExecuteCommand(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 498 | CommandType type) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 499 | LOG_RPC_DETAIL("getAndExecuteCommand on fd %d", fd.get()); |
| 500 | |
| 501 | RpcWireHeader command; |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 502 | if (status_t status = rpcRec(fd, session, "command header", &command, sizeof(command)); |
| 503 | status != OK) |
| 504 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 505 | |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 506 | return processServerCommand(fd, session, command, type); |
| 507 | } |
| 508 | |
| 509 | status_t RpcState::drainCommands(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 510 | CommandType type) { |
| 511 | uint8_t buf; |
| 512 | while (0 < TEMP_FAILURE_RETRY(recv(fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT))) { |
| 513 | status_t status = getAndExecuteCommand(fd, session, type); |
| 514 | if (status != OK) return status; |
| 515 | } |
| 516 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 519 | status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session, |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 520 | const RpcWireHeader& command, CommandType type) { |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 521 | IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); |
| 522 | IPCThreadState::SpGuard spGuard{ |
| 523 | .address = __builtin_frame_address(0), |
| 524 | .context = "processing binder RPC command", |
| 525 | }; |
| 526 | const IPCThreadState::SpGuard* origGuard; |
| 527 | if (kernelBinderState != nullptr) { |
| 528 | origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); |
| 529 | } |
| 530 | ScopeGuard guardUnguard = [&]() { |
| 531 | if (kernelBinderState != nullptr) { |
| 532 | kernelBinderState->restoreGetCallingSpGuard(origGuard); |
| 533 | } |
| 534 | }; |
| 535 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 536 | switch (command.command) { |
| 537 | case RPC_COMMAND_TRANSACT: |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 538 | if (type != CommandType::ANY) return BAD_TYPE; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 539 | return processTransact(fd, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 540 | case RPC_COMMAND_DEC_STRONG: |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 541 | return processDecStrong(fd, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // We should always know the version of the opposing side, and since the |
| 545 | // RPC-binder-level wire protocol is not self synchronizing, we have no way |
| 546 | // to understand where the current command ends and the next one begins. We |
| 547 | // also can't consider it a fatal error because this would allow any client |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 548 | // to kill us, so ending the session for misbehaving client. |
| 549 | ALOGE("Unknown RPC command %d - terminating session", command.command); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 550 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 551 | return DEAD_OBJECT; |
| 552 | } |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 553 | status_t RpcState::processTransact(const base::unique_fd& fd, const sp<RpcSession>& session, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 554 | const RpcWireHeader& command) { |
| 555 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command); |
| 556 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 557 | CommandData transactionData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 558 | if (!transactionData.valid()) { |
| 559 | return NO_MEMORY; |
| 560 | } |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 561 | if (status_t status = rpcRec(fd, session, "transaction body", transactionData.data(), |
| 562 | transactionData.size()); |
| 563 | status != OK) |
| 564 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 565 | |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 566 | return processTransactInternal(fd, session, std::move(transactionData), nullptr /*targetRef*/); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 569 | static void do_nothing_to_transact_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 570 | const binder_size_t* objects, size_t objectsCount) { |
| 571 | (void)p; |
| 572 | (void)data; |
| 573 | (void)dataSize; |
| 574 | (void)objects; |
| 575 | (void)objectsCount; |
| 576 | } |
| 577 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 578 | status_t RpcState::processTransactInternal(const base::unique_fd& fd, const sp<RpcSession>& session, |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 579 | CommandData transactionData, sp<IBinder>&& targetRef) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 580 | if (transactionData.size() < sizeof(RpcWireTransaction)) { |
| 581 | ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!", |
| 582 | sizeof(RpcWireTransaction), transactionData.size()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 583 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 584 | return BAD_VALUE; |
| 585 | } |
| 586 | RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data()); |
| 587 | |
| 588 | // TODO(b/182939933): heap allocation just for lookup in mNodeForAddress, |
| 589 | // maybe add an RpcAddress 'view' if the type remains 'heavy' |
| 590 | auto addr = RpcAddress::fromRawEmbedded(&transaction->address); |
| 591 | |
| 592 | status_t replyStatus = OK; |
| 593 | sp<IBinder> target; |
| 594 | if (!addr.isZero()) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 595 | if (!targetRef) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 596 | replyStatus = onBinderEntering(session, addr, &target); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 597 | } else { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 598 | target = targetRef; |
| 599 | } |
| 600 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 601 | if (replyStatus != OK) { |
| 602 | // do nothing |
| 603 | } else if (target == nullptr) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 604 | // This can happen if the binder is remote in this process, and |
| 605 | // another thread has called the last decStrong on this binder. |
| 606 | // However, for local binders, it indicates a misbehaving client |
| 607 | // (any binder which is being transacted on should be holding a |
| 608 | // strong ref count), so in either case, terminating the |
| 609 | // session. |
| 610 | ALOGE("While transacting, binder has been deleted at address %s. Terminating!", |
| 611 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 612 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 613 | replyStatus = BAD_VALUE; |
| 614 | } else if (target->localBinder() == nullptr) { |
| 615 | ALOGE("Unknown binder address or non-local binder, not address %s. Terminating!", |
| 616 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 617 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 618 | replyStatus = BAD_VALUE; |
| 619 | } else if (transaction->flags & IBinder::FLAG_ONEWAY) { |
| 620 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 621 | auto it = mNodeForAddress.find(addr); |
| 622 | if (it->second.binder.promote() != target) { |
| 623 | ALOGE("Binder became invalid during transaction. Bad client? %s", |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 624 | addr.toString().c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 625 | replyStatus = BAD_VALUE; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 626 | } else if (transaction->asyncNumber != it->second.asyncNumber) { |
| 627 | // we need to process some other asynchronous transaction |
| 628 | // first |
| 629 | // TODO(b/183140903): limit enqueues/detect overfill for bad client |
| 630 | // TODO(b/183140903): detect when an object is deleted when it still has |
| 631 | // pending async transactions |
| 632 | it->second.asyncTodo.push(BinderNode::AsyncTodo{ |
| 633 | .ref = target, |
| 634 | .data = std::move(transactionData), |
| 635 | .asyncNumber = transaction->asyncNumber, |
| 636 | }); |
| 637 | LOG_RPC_DETAIL("Enqueuing %" PRId64 " on %s", transaction->asyncNumber, |
| 638 | addr.toString().c_str()); |
| 639 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 640 | } |
| 641 | } |
| 642 | } |
| 643 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 644 | Parcel reply; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 645 | reply.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 646 | |
| 647 | if (replyStatus == OK) { |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 648 | Parcel data; |
| 649 | // transaction->data is owned by this function. Parcel borrows this data and |
| 650 | // only holds onto it for the duration of this function call. Parcel will be |
| 651 | // deleted before the 'transactionData' object. |
| 652 | data.ipcSetDataReference(transaction->data, |
| 653 | transactionData.size() - offsetof(RpcWireTransaction, data), |
| 654 | nullptr /*object*/, 0 /*objectCount*/, |
| 655 | do_nothing_to_transact_data); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 656 | data.markForRpc(session); |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 657 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 658 | if (target) { |
| 659 | replyStatus = target->transact(transaction->code, data, &reply, transaction->flags); |
| 660 | } else { |
| 661 | LOG_RPC_DETAIL("Got special transaction %u", transaction->code); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 662 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 663 | switch (transaction->code) { |
| 664 | case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: { |
| 665 | replyStatus = reply.writeInt32(session->getMaxThreads()); |
| 666 | break; |
| 667 | } |
| 668 | case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: { |
| 669 | // for client connections, this should always report the value |
| 670 | // originally returned from the server |
| 671 | int32_t id = session->mId.value(); |
| 672 | replyStatus = reply.writeInt32(id); |
| 673 | break; |
| 674 | } |
| 675 | default: { |
| 676 | sp<RpcServer> server = session->server().promote(); |
| 677 | if (server) { |
| 678 | switch (transaction->code) { |
| 679 | case RPC_SPECIAL_TRANSACT_GET_ROOT: { |
| 680 | replyStatus = reply.writeStrongBinder(server->getRootObject()); |
| 681 | break; |
| 682 | } |
| 683 | default: { |
| 684 | replyStatus = UNKNOWN_TRANSACTION; |
| 685 | } |
| 686 | } |
| 687 | } else { |
| 688 | ALOGE("Special command sent, but no server object attached."); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 689 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 690 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (transaction->flags & IBinder::FLAG_ONEWAY) { |
| 696 | if (replyStatus != OK) { |
| 697 | ALOGW("Oneway call failed with error: %d", replyStatus); |
| 698 | } |
| 699 | |
| 700 | LOG_RPC_DETAIL("Processed async transaction %" PRId64 " on %s", transaction->asyncNumber, |
| 701 | addr.toString().c_str()); |
| 702 | |
| 703 | // Check to see if there is another asynchronous transaction to process. |
| 704 | // This behavior differs from binder behavior, since in the binder |
| 705 | // driver, asynchronous transactions will be processed after existing |
| 706 | // pending binder transactions on the queue. The downside of this is |
| 707 | // that asynchronous transactions can be drowned out by synchronous |
| 708 | // transactions. However, we have no easy way to queue these |
| 709 | // transactions after the synchronous transactions we may want to read |
| 710 | // from the wire. So, in socket binder here, we have the opposite |
| 711 | // downside: asynchronous transactions may drown out synchronous |
| 712 | // transactions. |
| 713 | { |
| 714 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 715 | auto it = mNodeForAddress.find(addr); |
| 716 | // last refcount dropped after this transaction happened |
| 717 | if (it == mNodeForAddress.end()) return OK; |
| 718 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 719 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 720 | _l.unlock(); |
| 721 | (void)session->shutdownAndWait(false); |
| 722 | return DEAD_OBJECT; |
| 723 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 724 | |
| 725 | if (it->second.asyncTodo.size() == 0) return OK; |
| 726 | if (it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) { |
| 727 | LOG_RPC_DETAIL("Found next async transaction %" PRId64 " on %s", |
| 728 | it->second.asyncNumber, addr.toString().c_str()); |
| 729 | |
| 730 | // justification for const_cast (consider avoiding priority_queue): |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 731 | // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 732 | // - gotta go fast |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 733 | auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top()); |
| 734 | |
| 735 | CommandData nextData = std::move(todo.data); |
| 736 | sp<IBinder> nextRef = std::move(todo.ref); |
| 737 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 738 | it->second.asyncTodo.pop(); |
| 739 | _l.unlock(); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 740 | return processTransactInternal(fd, session, std::move(nextData), |
| 741 | std::move(nextRef)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | return OK; |
| 745 | } |
| 746 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 747 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 748 | sizeof(RpcWireReply) < |
| 749 | reply.dataSize(), |
| 750 | "Too much data for reply %zu", reply.dataSize()); |
| 751 | |
| 752 | RpcWireHeader cmdReply{ |
| 753 | .command = RPC_COMMAND_REPLY, |
| 754 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireReply) + reply.dataSize()), |
| 755 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 756 | RpcWireReply rpcReply{ |
| 757 | .status = replyStatus, |
| 758 | }; |
| 759 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 760 | CommandData replyData(sizeof(RpcWireHeader) + sizeof(RpcWireReply) + reply.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 761 | if (!replyData.valid()) { |
| 762 | return NO_MEMORY; |
| 763 | } |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 764 | memcpy(replyData.data() + 0, &cmdReply, sizeof(RpcWireHeader)); |
| 765 | memcpy(replyData.data() + sizeof(RpcWireHeader), &rpcReply, sizeof(RpcWireReply)); |
| 766 | memcpy(replyData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireReply), reply.data(), |
| 767 | reply.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 768 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 769 | return rpcSend(fd, session, "reply", replyData.data(), replyData.size()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 772 | status_t RpcState::processDecStrong(const base::unique_fd& fd, const sp<RpcSession>& session, |
| 773 | const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 774 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command); |
| 775 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 776 | CommandData commandData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 777 | if (!commandData.valid()) { |
| 778 | return NO_MEMORY; |
| 779 | } |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 780 | if (status_t status = |
| 781 | rpcRec(fd, session, "dec ref body", commandData.data(), commandData.size()); |
| 782 | status != OK) |
| 783 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 784 | |
| 785 | if (command.bodySize < sizeof(RpcWireAddress)) { |
| 786 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireAddress. Terminating!", |
| 787 | sizeof(RpcWireAddress), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 788 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 789 | return BAD_VALUE; |
| 790 | } |
| 791 | RpcWireAddress* address = reinterpret_cast<RpcWireAddress*>(commandData.data()); |
| 792 | |
| 793 | // TODO(b/182939933): heap allocation just for lookup |
| 794 | auto addr = RpcAddress::fromRawEmbedded(address); |
| 795 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 796 | auto it = mNodeForAddress.find(addr); |
| 797 | if (it == mNodeForAddress.end()) { |
| 798 | ALOGE("Unknown binder address %s for dec strong.", addr.toString().c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 799 | return OK; |
| 800 | } |
| 801 | |
| 802 | sp<IBinder> target = it->second.binder.promote(); |
| 803 | if (target == nullptr) { |
| 804 | ALOGE("While requesting dec strong, binder has been deleted at address %s. Terminating!", |
| 805 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 806 | _l.unlock(); |
| 807 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 808 | return BAD_VALUE; |
| 809 | } |
| 810 | |
| 811 | if (it->second.timesSent == 0) { |
| 812 | ALOGE("No record of sending binder, but requested decStrong: %s", addr.toString().c_str()); |
| 813 | return OK; |
| 814 | } |
| 815 | |
| 816 | LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %s", |
| 817 | addr.toString().c_str()); |
| 818 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 819 | it->second.timesSent--; |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 820 | sp<IBinder> tempHold = tryEraseNode(it); |
| 821 | _l.unlock(); |
| 822 | tempHold = nullptr; // destructor may make binder calls on this session |
| 823 | |
| 824 | return OK; |
| 825 | } |
| 826 | |
| 827 | sp<IBinder> RpcState::tryEraseNode(std::map<RpcAddress, BinderNode>::iterator& it) { |
| 828 | sp<IBinder> ref; |
| 829 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 830 | if (it->second.timesSent == 0) { |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 831 | ref = std::move(it->second.sentRef); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 832 | |
| 833 | if (it->second.timesRecd == 0) { |
Steven Moreland | a6e11cf | 2021-06-04 00:58:31 +0000 | [diff] [blame] | 834 | LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(), |
| 835 | "Can't delete binder w/ pending async transactions"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 836 | mNodeForAddress.erase(it); |
| 837 | } |
| 838 | } |
| 839 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 840 | return ref; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame^] | 843 | bool RpcState::nodeProgressAsyncNumber(BinderNode* node) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 844 | // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to |
| 845 | // a single binder |
| 846 | if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) { |
| 847 | ALOGE("Out of async transaction IDs. Terminating"); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 848 | return false; |
| 849 | } |
| 850 | node->asyncNumber++; |
| 851 | return true; |
| 852 | } |
| 853 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 854 | } // namespace android |