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 | 6212901 | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 21 | #include <android-base/hex.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 22 | #include <android-base/scopeguard.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 23 | #include <binder/BpBinder.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 24 | #include <binder/IPCThreadState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 25 | #include <binder/RpcServer.h> |
| 26 | |
| 27 | #include "Debug.h" |
| 28 | #include "RpcWireFormat.h" |
| 29 | |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 30 | #include <random> |
| 31 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 32 | #include <inttypes.h> |
| 33 | |
| 34 | namespace android { |
| 35 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 36 | using base::ScopeGuard; |
| 37 | |
Devin Moore | 0825643 | 2021-07-02 13:03:49 -0700 | [diff] [blame] | 38 | #if RPC_FLAKE_PRONE |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 39 | void rpcMaybeWaitToFlake() { |
Devin Moore | 0825643 | 2021-07-02 13:03:49 -0700 | [diff] [blame] | 40 | [[clang::no_destroy]] static std::random_device r; |
| 41 | [[clang::no_destroy]] static std::mutex m; |
Steven Moreland | b817679 | 2021-06-22 20:29:21 +0000 | [diff] [blame] | 42 | unsigned num; |
| 43 | { |
| 44 | std::lock_guard<std::mutex> lock(m); |
| 45 | num = r(); |
| 46 | } |
| 47 | if (num % 10 == 0) usleep(num % 1000); |
| 48 | } |
| 49 | #endif |
| 50 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 51 | RpcState::RpcState() {} |
| 52 | RpcState::~RpcState() {} |
| 53 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 54 | status_t RpcState::onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder, |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 55 | uint64_t* outAddress) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 56 | bool isRemote = binder->remoteBinder(); |
| 57 | bool isRpc = isRemote && binder->remoteBinder()->isRpcBinder(); |
| 58 | |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 59 | if (isRpc && binder->remoteBinder()->getPrivateAccessor().rpcSession() != session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 60 | // We need to be able to send instructions over the socket for how to |
| 61 | // connect to a different server, and we also need to let the host |
| 62 | // process know that this is happening. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 63 | ALOGE("Cannot send binder from unrelated binder RPC session."); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 64 | return INVALID_OPERATION; |
| 65 | } |
| 66 | |
| 67 | if (isRemote && !isRpc) { |
| 68 | // Without additional work, this would have the effect of using this |
| 69 | // process to proxy calls from the socket over to the other process, and |
| 70 | // it would make those calls look like they come from us (not over the |
| 71 | // sockets). In order to make this work transparently like binder, we |
| 72 | // would instead need to send instructions over the socket for how to |
| 73 | // connect to the host process, and we also need to let the host process |
| 74 | // know this was happening. |
| 75 | ALOGE("Cannot send binder proxy %p over sockets", binder.get()); |
| 76 | return INVALID_OPERATION; |
| 77 | } |
| 78 | |
| 79 | std::lock_guard<std::mutex> _l(mNodeMutex); |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 80 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 81 | |
| 82 | // TODO(b/182939933): maybe move address out of BpBinder, and keep binder->address map |
| 83 | // in RpcState |
| 84 | for (auto& [addr, node] : mNodeForAddress) { |
| 85 | if (binder == node.binder) { |
| 86 | if (isRpc) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 87 | // check integrity of data structure |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 88 | uint64_t actualAddr = binder->remoteBinder()->getPrivateAccessor().rpcAddress(); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL_IF(addr != actualAddr, "Address mismatch %" PRIu64 " vs %" PRIu64, |
| 90 | addr, actualAddr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 91 | } |
| 92 | node.timesSent++; |
| 93 | node.sentRef = binder; // might already be set |
| 94 | *outAddress = addr; |
| 95 | return OK; |
| 96 | } |
| 97 | } |
| 98 | LOG_ALWAYS_FATAL_IF(isRpc, "RPC binder must have known address at this point"); |
| 99 | |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 100 | bool forServer = session->server() != nullptr; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 101 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 102 | // arbitrary limit for maximum number of nodes in a process (otherwise we |
| 103 | // might run out of addresses) |
| 104 | if (mNodeForAddress.size() > 100000) { |
| 105 | return NO_MEMORY; |
| 106 | } |
| 107 | |
| 108 | while (true) { |
| 109 | RpcWireAddress address{ |
| 110 | .options = RPC_WIRE_ADDRESS_OPTION_CREATED, |
| 111 | .address = mNextId, |
| 112 | }; |
| 113 | if (forServer) { |
| 114 | address.options |= RPC_WIRE_ADDRESS_OPTION_FOR_SERVER; |
| 115 | } |
| 116 | |
| 117 | // avoid ubsan abort |
| 118 | if (mNextId >= std::numeric_limits<uint32_t>::max()) { |
| 119 | mNextId = 0; |
| 120 | } else { |
| 121 | mNextId++; |
| 122 | } |
| 123 | |
| 124 | auto&& [it, inserted] = mNodeForAddress.insert({RpcWireAddress::toRaw(address), |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 125 | BinderNode{ |
| 126 | .binder = binder, |
| 127 | .timesSent = 1, |
| 128 | .sentRef = binder, |
| 129 | }}); |
| 130 | if (inserted) { |
| 131 | *outAddress = it->first; |
| 132 | return OK; |
| 133 | } |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 134 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 137 | status_t RpcState::onBinderEntering(const sp<RpcSession>& session, uint64_t address, |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 138 | sp<IBinder>* out) { |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 139 | // ensure that: if we want to use addresses for something else in the future (for |
| 140 | // instance, allowing transitive binder sends), that we don't accidentally |
| 141 | // send those addresses to old server. Accidentally ignoring this in that |
| 142 | // case and considering the binder to be recognized could cause this |
| 143 | // process to accidentally proxy transactions for that binder. Of course, |
| 144 | // if we communicate with a binder, it could always be proxying |
| 145 | // information. However, we want to make sure that isn't done on accident |
| 146 | // by a client. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 147 | RpcWireAddress addr = RpcWireAddress::fromRaw(address); |
| 148 | constexpr uint32_t kKnownOptions = |
| 149 | RPC_WIRE_ADDRESS_OPTION_CREATED | RPC_WIRE_ADDRESS_OPTION_FOR_SERVER; |
| 150 | if (addr.options & ~kKnownOptions) { |
| 151 | ALOGE("Address is of an unknown type, rejecting: %" PRIu64, address); |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 152 | return BAD_VALUE; |
| 153 | } |
| 154 | |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 155 | std::lock_guard<std::mutex> _l(mNodeMutex); |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 156 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 157 | |
| 158 | if (auto it = mNodeForAddress.find(address); it != mNodeForAddress.end()) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 159 | *out = it->second.binder.promote(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 160 | |
| 161 | // implicitly have strong RPC refcount, since we received this binder |
| 162 | it->second.timesRecd++; |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 163 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 164 | } |
| 165 | |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 166 | // we don't know about this binder, so the other side of the connection |
| 167 | // should have created it. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 168 | if ((addr.options & RPC_WIRE_ADDRESS_OPTION_FOR_SERVER) == !!session->server()) { |
| 169 | ALOGE("Server received unrecognized address which we should own the creation of %" PRIu64, |
| 170 | address); |
Steven Moreland | 9153824 | 2021-06-10 23:35:35 +0000 | [diff] [blame] | 171 | return BAD_VALUE; |
| 172 | } |
| 173 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 174 | auto&& [it, inserted] = mNodeForAddress.insert({address, BinderNode{}}); |
| 175 | LOG_ALWAYS_FATAL_IF(!inserted, "Failed to insert binder when creating proxy"); |
| 176 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 177 | // 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] | 178 | // device global binders in the RPC world). |
Steven Moreland | 9915762 | 2021-09-13 16:27:34 -0700 | [diff] [blame] | 179 | it->second.binder = *out = BpBinder::PrivateAccessor::create(session, it->first); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 180 | it->second.timesRecd = 1; |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 181 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 184 | status_t RpcState::flushExcessBinderRefs(const sp<RpcSession>& session, uint64_t address, |
| 185 | const sp<IBinder>& binder) { |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame^] | 186 | // We can flush all references when the binder is destroyed. No need to send |
| 187 | // extra reference counting packets now. |
| 188 | if (binder->remoteBinder()) return OK; |
| 189 | |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 190 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 191 | if (mTerminated) return DEAD_OBJECT; |
| 192 | |
| 193 | auto it = mNodeForAddress.find(address); |
| 194 | |
| 195 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Can't be deleted while we hold sp<>"); |
| 196 | LOG_ALWAYS_FATAL_IF(it->second.binder != binder, |
| 197 | "Caller of flushExcessBinderRefs using inconsistent arguments"); |
| 198 | |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame^] | 199 | LOG_ALWAYS_FATAL_IF(it->second.timesSent <= 0, "Local binder must have been sent %p", |
| 200 | binder.get()); |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 201 | |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame^] | 202 | // For a local binder, we only need to know that we sent it. Now that we |
| 203 | // have an sp<> for this call, we don't need anything more. If the other |
| 204 | // process is done with this binder, it needs to know we received the |
| 205 | // refcount associated with this call, so we can acknowledge that we |
| 206 | // received it. Once (or if) it has no other refcounts, it would reply with |
| 207 | // its own decStrong so that it could be removed from this session. |
| 208 | if (it->second.timesRecd != 0) { |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 209 | _l.unlock(); |
| 210 | |
Steven Moreland | e96ed0e | 2021-09-27 17:43:53 -0700 | [diff] [blame^] | 211 | return session->sendDecStrongToTarget(address, 0); |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | return OK; |
| 215 | } |
| 216 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 217 | size_t RpcState::countBinders() { |
| 218 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 219 | return mNodeForAddress.size(); |
| 220 | } |
| 221 | |
| 222 | void RpcState::dump() { |
| 223 | std::lock_guard<std::mutex> _l(mNodeMutex); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 224 | dumpLocked(); |
| 225 | } |
| 226 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 227 | void RpcState::clear() { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 228 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 229 | |
| 230 | if (mTerminated) { |
| 231 | LOG_ALWAYS_FATAL_IF(!mNodeForAddress.empty(), |
| 232 | "New state should be impossible after terminating!"); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | if (SHOULD_LOG_RPC_DETAIL) { |
| 237 | ALOGE("RpcState::clear()"); |
| 238 | dumpLocked(); |
| 239 | } |
| 240 | |
| 241 | // if the destructor of a binder object makes another RPC call, then calling |
| 242 | // decStrong could deadlock. So, we must hold onto these binders until |
| 243 | // mNodeMutex is no longer taken. |
| 244 | std::vector<sp<IBinder>> tempHoldBinder; |
| 245 | |
| 246 | mTerminated = true; |
| 247 | for (auto& [address, node] : mNodeForAddress) { |
| 248 | sp<IBinder> binder = node.binder.promote(); |
| 249 | LOG_ALWAYS_FATAL_IF(binder == nullptr, "Binder %p expected to be owned.", binder.get()); |
| 250 | |
| 251 | if (node.sentRef != nullptr) { |
| 252 | tempHoldBinder.push_back(node.sentRef); |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | mNodeForAddress.clear(); |
| 257 | |
| 258 | _l.unlock(); |
| 259 | tempHoldBinder.clear(); // explicit |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | void RpcState::dumpLocked() { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 263 | ALOGE("DUMP OF RpcState %p", this); |
| 264 | ALOGE("DUMP OF RpcState (%zu nodes)", mNodeForAddress.size()); |
| 265 | for (const auto& [address, node] : mNodeForAddress) { |
| 266 | sp<IBinder> binder = node.binder.promote(); |
| 267 | |
| 268 | const char* desc; |
| 269 | if (binder) { |
| 270 | if (binder->remoteBinder()) { |
| 271 | if (binder->remoteBinder()->isRpcBinder()) { |
| 272 | desc = "(rpc binder proxy)"; |
| 273 | } else { |
| 274 | desc = "(binder proxy)"; |
| 275 | } |
| 276 | } else { |
| 277 | desc = "(local binder)"; |
| 278 | } |
| 279 | } else { |
| 280 | desc = "(null)"; |
| 281 | } |
| 282 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 283 | ALOGE("- BINDER NODE: %p times sent:%zu times recd: %zu a: %" PRIu64 " type: %s", |
| 284 | node.binder.unsafe_get(), node.timesSent, node.timesRecd, address, desc); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 285 | } |
| 286 | ALOGE("END DUMP OF RpcState"); |
| 287 | } |
| 288 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 289 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 290 | RpcState::CommandData::CommandData(size_t size) : mSize(size) { |
| 291 | // The maximum size for regular binder is 1MB for all concurrent |
| 292 | // transactions. A very small proportion of transactions are even |
| 293 | // larger than a page, but we need to avoid allocating too much |
| 294 | // data on behalf of an arbitrary client, or we could risk being in |
| 295 | // a position where a single additional allocation could run out of |
| 296 | // memory. |
| 297 | // |
| 298 | // Note, this limit may not reflect the total amount of data allocated for a |
| 299 | // transaction (in some cases, additional fixed size amounts are added), |
| 300 | // though for rough consistency, we should avoid cases where this data type |
| 301 | // is used for multiple dynamic allocations for a single transaction. |
| 302 | constexpr size_t kMaxTransactionAllocation = 100 * 1000; |
| 303 | if (size == 0) return; |
| 304 | if (size > kMaxTransactionAllocation) { |
| 305 | ALOGW("Transaction requested too much data allocation %zu", size); |
| 306 | return; |
| 307 | } |
| 308 | mData.reset(new (std::nothrow) uint8_t[size]); |
| 309 | } |
| 310 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 311 | status_t RpcState::rpcSend(const sp<RpcSession::RpcConnection>& connection, |
| 312 | const sp<RpcSession>& session, const char* what, const void* data, |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 313 | size_t size, const std::function<status_t()>& altPoll) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 314 | LOG_RPC_DETAIL("Sending %s on RpcTransport %p: %s", what, connection->rpcTransport.get(), |
Steven Moreland | 6212901 | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 315 | android::base::HexString(data, size).c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 316 | |
| 317 | if (size > std::numeric_limits<ssize_t>::max()) { |
| 318 | ALOGE("Cannot send %s at size %zu (too big)", what, size); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 319 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 320 | return BAD_VALUE; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 323 | if (status_t status = |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 324 | connection->rpcTransport->interruptableWriteFully(session->mShutdownTrigger.get(), |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 325 | data, size, altPoll); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 326 | status != OK) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 327 | LOG_RPC_DETAIL("Failed to write %s (%zu bytes) on RpcTransport %p, error: %s", what, size, |
| 328 | connection->rpcTransport.get(), statusToString(status).c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 329 | (void)session->shutdownAndWait(false); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 330 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 333 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 336 | status_t RpcState::rpcRec(const sp<RpcSession::RpcConnection>& connection, |
| 337 | const sp<RpcSession>& session, const char* what, void* data, |
| 338 | size_t size) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 339 | if (size > std::numeric_limits<ssize_t>::max()) { |
| 340 | ALOGE("Cannot rec %s at size %zu (too big)", what, size); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 341 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 342 | return BAD_VALUE; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 345 | if (status_t status = |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 346 | connection->rpcTransport->interruptableReadFully(session->mShutdownTrigger.get(), |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 347 | data, size, {}); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 348 | status != OK) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 349 | LOG_RPC_DETAIL("Failed to read %s (%zu bytes) on RpcTransport %p, error: %s", what, size, |
| 350 | connection->rpcTransport.get(), statusToString(status).c_str()); |
Steven Moreland | ae58f43 | 2021-08-05 17:53:16 -0700 | [diff] [blame] | 351 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 352 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 355 | LOG_RPC_DETAIL("Received %s on RpcTransport %p: %s", what, connection->rpcTransport.get(), |
Steven Moreland | 6212901 | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 356 | android::base::HexString(data, size).c_str()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 357 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 360 | status_t RpcState::readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection, |
| 361 | const sp<RpcSession>& session, uint32_t* version) { |
| 362 | RpcNewSessionResponse response; |
| 363 | if (status_t status = |
| 364 | rpcRec(connection, session, "new session response", &response, sizeof(response)); |
| 365 | status != OK) { |
| 366 | return status; |
| 367 | } |
| 368 | *version = response.version; |
| 369 | return OK; |
| 370 | } |
| 371 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 372 | status_t RpcState::sendConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 373 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 374 | RpcOutgoingConnectionInit init{ |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 375 | .msg = RPC_CONNECTION_INIT_OKAY, |
| 376 | }; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 377 | return rpcSend(connection, session, "connection init", &init, sizeof(init)); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 380 | status_t RpcState::readConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 381 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 382 | RpcOutgoingConnectionInit init; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 383 | if (status_t status = rpcRec(connection, session, "connection init", &init, sizeof(init)); |
| 384 | status != OK) |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 385 | return status; |
| 386 | |
| 387 | static_assert(sizeof(init.msg) == sizeof(RPC_CONNECTION_INIT_OKAY)); |
| 388 | if (0 != strncmp(init.msg, RPC_CONNECTION_INIT_OKAY, sizeof(init.msg))) { |
| 389 | ALOGE("Connection init message unrecognized %.*s", static_cast<int>(sizeof(init.msg)), |
| 390 | init.msg); |
| 391 | return BAD_VALUE; |
| 392 | } |
| 393 | return OK; |
| 394 | } |
| 395 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 396 | sp<IBinder> RpcState::getRootObject(const sp<RpcSession::RpcConnection>& connection, |
| 397 | const sp<RpcSession>& session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 398 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 399 | data.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 400 | Parcel reply; |
| 401 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 402 | status_t status = |
| 403 | transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_ROOT, data, session, &reply, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 404 | if (status != OK) { |
| 405 | ALOGE("Error getting root object: %s", statusToString(status).c_str()); |
| 406 | return nullptr; |
| 407 | } |
| 408 | |
| 409 | return reply.readStrongBinder(); |
| 410 | } |
| 411 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 412 | status_t RpcState::getMaxThreads(const sp<RpcSession::RpcConnection>& connection, |
| 413 | const sp<RpcSession>& session, size_t* maxThreadsOut) { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 414 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 415 | data.markForRpc(session); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 416 | Parcel reply; |
| 417 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 418 | status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, data, |
| 419 | session, &reply, 0); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 420 | if (status != OK) { |
| 421 | ALOGE("Error getting max threads: %s", statusToString(status).c_str()); |
| 422 | return status; |
| 423 | } |
| 424 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 425 | int32_t maxThreads; |
| 426 | status = reply.readInt32(&maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 427 | if (status != OK) return status; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 428 | if (maxThreads <= 0) { |
| 429 | ALOGE("Error invalid max maxThreads: %d", maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 430 | return BAD_VALUE; |
| 431 | } |
| 432 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 433 | *maxThreadsOut = maxThreads; |
| 434 | return OK; |
| 435 | } |
| 436 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 437 | status_t RpcState::getSessionId(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 438 | const sp<RpcSession>& session, std::vector<uint8_t>* sessionIdOut) { |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 439 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 440 | data.markForRpc(session); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 441 | Parcel reply; |
| 442 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 443 | status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_SESSION_ID, data, |
| 444 | session, &reply, 0); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 445 | if (status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 446 | ALOGE("Error getting session ID: %s", statusToString(status).c_str()); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 447 | return status; |
| 448 | } |
| 449 | |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 450 | return reply.readByteVector(sessionIdOut); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 453 | status_t RpcState::transact(const sp<RpcSession::RpcConnection>& connection, |
| 454 | const sp<IBinder>& binder, uint32_t code, const Parcel& data, |
| 455 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 456 | if (!data.isForRpc()) { |
| 457 | ALOGE("Refusing to send RPC with parcel not crafted for RPC"); |
| 458 | return BAD_TYPE; |
| 459 | } |
| 460 | |
| 461 | if (data.objectsCount() != 0) { |
| 462 | ALOGE("Parcel at %p has attached objects but is being used in an RPC call", &data); |
| 463 | return BAD_TYPE; |
| 464 | } |
| 465 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 466 | uint64_t address; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 467 | if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status; |
| 468 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 469 | return transactAddress(connection, address, code, data, session, reply, flags); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 472 | status_t RpcState::transactAddress(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 473 | uint64_t address, uint32_t code, const Parcel& data, |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 474 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 475 | LOG_ALWAYS_FATAL_IF(!data.isForRpc()); |
| 476 | LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0); |
| 477 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 478 | uint64_t asyncNumber = 0; |
| 479 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 480 | if (address != 0) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 481 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 482 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 483 | auto it = mNodeForAddress.find(address); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 484 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), |
| 485 | "Sending transact on unknown address %" PRIu64, address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 486 | |
| 487 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 488 | asyncNumber = it->second.asyncNumber; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 489 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 490 | _l.unlock(); |
| 491 | (void)session->shutdownAndWait(false); |
| 492 | return DEAD_OBJECT; |
| 493 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 497 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 498 | sizeof(RpcWireTransaction) < |
| 499 | data.dataSize(), |
| 500 | "Too much data %zu", data.dataSize()); |
| 501 | |
| 502 | RpcWireHeader command{ |
| 503 | .command = RPC_COMMAND_TRANSACT, |
| 504 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireTransaction) + data.dataSize()), |
| 505 | }; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 506 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 507 | RpcWireTransaction transaction{ |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 508 | .address = RpcWireAddress::fromRaw(address), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 509 | .code = code, |
| 510 | .flags = flags, |
| 511 | .asyncNumber = asyncNumber, |
| 512 | }; |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 513 | CommandData transactionData(sizeof(RpcWireHeader) + sizeof(RpcWireTransaction) + |
| 514 | data.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 515 | if (!transactionData.valid()) { |
| 516 | return NO_MEMORY; |
| 517 | } |
| 518 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 519 | memcpy(transactionData.data() + 0, &command, sizeof(RpcWireHeader)); |
| 520 | memcpy(transactionData.data() + sizeof(RpcWireHeader), &transaction, |
| 521 | sizeof(RpcWireTransaction)); |
| 522 | memcpy(transactionData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireTransaction), data.data(), |
| 523 | data.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 524 | |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 525 | constexpr size_t kWaitMaxUs = 1000000; |
| 526 | constexpr size_t kWaitLogUs = 10000; |
| 527 | size_t waitUs = 0; |
| 528 | |
| 529 | // Oneway calls have no sync point, so if many are sent before, whether this |
| 530 | // is a twoway or oneway transaction, they may have filled up the socket. |
| 531 | // So, make sure we drain them before polling. |
| 532 | std::function<status_t()> drainRefs = [&] { |
| 533 | if (waitUs > kWaitLogUs) { |
| 534 | ALOGE("Cannot send command, trying to process pending refcounts. Waiting %zuus. Too " |
| 535 | "many oneway calls?", |
| 536 | waitUs); |
| 537 | } |
| 538 | |
| 539 | if (waitUs > 0) { |
| 540 | usleep(waitUs); |
| 541 | waitUs = std::min(kWaitMaxUs, waitUs * 2); |
| 542 | } else { |
| 543 | waitUs = 1; |
| 544 | } |
| 545 | |
| 546 | return drainCommands(connection, session, CommandType::CONTROL_ONLY); |
| 547 | }; |
| 548 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 549 | if (status_t status = rpcSend(connection, session, "transaction", transactionData.data(), |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 550 | transactionData.size(), drainRefs); |
| 551 | status != OK) { |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 552 | // TODO(b/167966510): need to undo onBinderLeaving - we know the |
| 553 | // refcount isn't successfully transferred. |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 554 | return status; |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 555 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 556 | |
| 557 | if (flags & IBinder::FLAG_ONEWAY) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 558 | LOG_RPC_DETAIL("Oneway command, so no longer waiting on RpcTransport %p", |
| 559 | connection->rpcTransport.get()); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 560 | |
| 561 | // Do not wait on result. |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 562 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction."); |
| 566 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 567 | return waitForReply(connection, session, reply); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 570 | static void cleanup_reply_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 571 | const binder_size_t* objects, size_t objectsCount) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 572 | (void)p; |
| 573 | delete[] const_cast<uint8_t*>(data - offsetof(RpcWireReply, data)); |
| 574 | (void)dataSize; |
| 575 | LOG_ALWAYS_FATAL_IF(objects != nullptr); |
Yifan Hong | 239a2ca | 2021-06-24 16:05:16 -0700 | [diff] [blame] | 576 | LOG_ALWAYS_FATAL_IF(objectsCount != 0, "%zu objects remaining", objectsCount); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 577 | } |
| 578 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 579 | status_t RpcState::waitForReply(const sp<RpcSession::RpcConnection>& connection, |
| 580 | const sp<RpcSession>& session, Parcel* reply) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 581 | RpcWireHeader command; |
| 582 | while (true) { |
Steven Moreland | ae58f43 | 2021-08-05 17:53:16 -0700 | [diff] [blame] | 583 | if (status_t status = rpcRec(connection, session, "command header (for reply)", &command, |
| 584 | sizeof(command)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 585 | status != OK) |
| 586 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 587 | |
| 588 | if (command.command == RPC_COMMAND_REPLY) break; |
| 589 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 590 | if (status_t status = processCommand(connection, session, command, CommandType::ANY); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 591 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 592 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 595 | CommandData data(command.bodySize); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 596 | if (!data.valid()) return NO_MEMORY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 597 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 598 | if (status_t status = rpcRec(connection, session, "reply body", data.data(), command.bodySize); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 599 | status != OK) |
| 600 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 601 | |
| 602 | if (command.bodySize < sizeof(RpcWireReply)) { |
| 603 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!", |
| 604 | sizeof(RpcWireReply), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 605 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 606 | return BAD_VALUE; |
| 607 | } |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 608 | RpcWireReply* rpcReply = reinterpret_cast<RpcWireReply*>(data.data()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 609 | if (rpcReply->status != OK) return rpcReply->status; |
| 610 | |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 611 | data.release(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 612 | reply->ipcSetDataReference(rpcReply->data, command.bodySize - offsetof(RpcWireReply, data), |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 613 | nullptr, 0, cleanup_reply_data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 614 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 615 | reply->markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 616 | |
| 617 | return OK; |
| 618 | } |
| 619 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 620 | status_t RpcState::sendDecStrongToTarget(const sp<RpcSession::RpcConnection>& connection, |
| 621 | const sp<RpcSession>& session, uint64_t addr, |
| 622 | size_t target) { |
| 623 | RpcDecStrong body = { |
| 624 | .address = RpcWireAddress::fromRaw(addr), |
| 625 | }; |
| 626 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 627 | { |
| 628 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 629 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 630 | auto it = mNodeForAddress.find(addr); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 631 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), |
| 632 | "Sending dec strong on unknown address %" PRIu64, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 633 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 634 | LOG_ALWAYS_FATAL_IF(it->second.timesRecd < target, "Can't dec count of %zu to %zu.", |
| 635 | it->second.timesRecd, target); |
| 636 | |
| 637 | // typically this happens when multiple threads send dec refs at the |
| 638 | // same time - the transactions will get combined automatically |
| 639 | if (it->second.timesRecd == target) return OK; |
| 640 | |
| 641 | body.amount = it->second.timesRecd - target; |
| 642 | it->second.timesRecd = target; |
| 643 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 644 | LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(it), |
| 645 | "Bad state. RpcState shouldn't own received binder"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | RpcWireHeader cmd = { |
| 649 | .command = RPC_COMMAND_DEC_STRONG, |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 650 | .bodySize = sizeof(RpcDecStrong), |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 651 | }; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 652 | if (status_t status = rpcSend(connection, session, "dec ref header", &cmd, sizeof(cmd)); |
| 653 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 654 | return status; |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 655 | |
| 656 | return rpcSend(connection, session, "dec ref body", &body, sizeof(body)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 659 | status_t RpcState::getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection, |
| 660 | const sp<RpcSession>& session, CommandType type) { |
Yifan Hong | 702115c | 2021-06-24 15:39:18 -0700 | [diff] [blame] | 661 | LOG_RPC_DETAIL("getAndExecuteCommand on RpcTransport %p", connection->rpcTransport.get()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 662 | |
| 663 | RpcWireHeader command; |
Steven Moreland | ae58f43 | 2021-08-05 17:53:16 -0700 | [diff] [blame] | 664 | if (status_t status = rpcRec(connection, session, "command header (for server)", &command, |
| 665 | sizeof(command)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 666 | status != OK) |
| 667 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 668 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 669 | return processCommand(connection, session, command, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 672 | status_t RpcState::drainCommands(const sp<RpcSession::RpcConnection>& connection, |
| 673 | const sp<RpcSession>& session, CommandType type) { |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 674 | uint8_t buf; |
Yifan Hong | 218c407 | 2021-08-04 14:59:10 -0700 | [diff] [blame] | 675 | while (connection->rpcTransport->peek(&buf, sizeof(buf)).value_or(0) > 0) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 676 | status_t status = getAndExecuteCommand(connection, session, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 677 | if (status != OK) return status; |
| 678 | } |
| 679 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 682 | status_t RpcState::processCommand(const sp<RpcSession::RpcConnection>& connection, |
| 683 | const sp<RpcSession>& session, const RpcWireHeader& command, |
| 684 | CommandType type) { |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 685 | IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); |
| 686 | IPCThreadState::SpGuard spGuard{ |
| 687 | .address = __builtin_frame_address(0), |
| 688 | .context = "processing binder RPC command", |
| 689 | }; |
| 690 | const IPCThreadState::SpGuard* origGuard; |
| 691 | if (kernelBinderState != nullptr) { |
| 692 | origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); |
| 693 | } |
| 694 | ScopeGuard guardUnguard = [&]() { |
| 695 | if (kernelBinderState != nullptr) { |
| 696 | kernelBinderState->restoreGetCallingSpGuard(origGuard); |
| 697 | } |
| 698 | }; |
| 699 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 700 | switch (command.command) { |
| 701 | case RPC_COMMAND_TRANSACT: |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 702 | if (type != CommandType::ANY) return BAD_TYPE; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 703 | return processTransact(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 704 | case RPC_COMMAND_DEC_STRONG: |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 705 | return processDecStrong(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // We should always know the version of the opposing side, and since the |
| 709 | // RPC-binder-level wire protocol is not self synchronizing, we have no way |
| 710 | // to understand where the current command ends and the next one begins. We |
| 711 | // 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] | 712 | // to kill us, so ending the session for misbehaving client. |
| 713 | ALOGE("Unknown RPC command %d - terminating session", command.command); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 714 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 715 | return DEAD_OBJECT; |
| 716 | } |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 717 | status_t RpcState::processTransact(const sp<RpcSession::RpcConnection>& connection, |
| 718 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 719 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command); |
| 720 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 721 | CommandData transactionData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 722 | if (!transactionData.valid()) { |
| 723 | return NO_MEMORY; |
| 724 | } |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 725 | if (status_t status = rpcRec(connection, session, "transaction body", transactionData.data(), |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 726 | transactionData.size()); |
| 727 | status != OK) |
| 728 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 729 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 730 | return processTransactInternal(connection, session, std::move(transactionData)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 733 | static void do_nothing_to_transact_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 734 | const binder_size_t* objects, size_t objectsCount) { |
| 735 | (void)p; |
| 736 | (void)data; |
| 737 | (void)dataSize; |
| 738 | (void)objects; |
| 739 | (void)objectsCount; |
| 740 | } |
| 741 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 742 | status_t RpcState::processTransactInternal(const sp<RpcSession::RpcConnection>& connection, |
| 743 | const sp<RpcSession>& session, |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 744 | CommandData transactionData) { |
| 745 | // for 'recursive' calls to this, we have already read and processed the |
| 746 | // binder from the transaction data and taken reference counts into account, |
| 747 | // so it is cached here. |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 748 | sp<IBinder> target; |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 749 | processTransactInternalTailCall: |
| 750 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 751 | if (transactionData.size() < sizeof(RpcWireTransaction)) { |
| 752 | ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!", |
| 753 | sizeof(RpcWireTransaction), transactionData.size()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 754 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 755 | return BAD_VALUE; |
| 756 | } |
| 757 | RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data()); |
| 758 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 759 | uint64_t addr = RpcWireAddress::toRaw(transaction->address); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 760 | bool oneway = transaction->flags & IBinder::FLAG_ONEWAY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 761 | |
| 762 | status_t replyStatus = OK; |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 763 | if (addr != 0) { |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 764 | if (!target) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 765 | replyStatus = onBinderEntering(session, addr, &target); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 768 | if (replyStatus != OK) { |
| 769 | // do nothing |
| 770 | } else if (target == nullptr) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 771 | // This can happen if the binder is remote in this process, and |
| 772 | // another thread has called the last decStrong on this binder. |
| 773 | // However, for local binders, it indicates a misbehaving client |
| 774 | // (any binder which is being transacted on should be holding a |
| 775 | // strong ref count), so in either case, terminating the |
| 776 | // session. |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 777 | ALOGE("While transacting, binder has been deleted at address %" PRIu64 ". Terminating!", |
| 778 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 779 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 780 | replyStatus = BAD_VALUE; |
| 781 | } else if (target->localBinder() == nullptr) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 782 | ALOGE("Unknown binder address or non-local binder, not address %" PRIu64 |
| 783 | ". Terminating!", |
| 784 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 785 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 786 | replyStatus = BAD_VALUE; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 787 | } else if (oneway) { |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 788 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 789 | auto it = mNodeForAddress.find(addr); |
| 790 | if (it->second.binder.promote() != target) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 791 | ALOGE("Binder became invalid during transaction. Bad client? %" PRIu64, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 792 | replyStatus = BAD_VALUE; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 793 | } else if (transaction->asyncNumber != it->second.asyncNumber) { |
| 794 | // we need to process some other asynchronous transaction |
| 795 | // first |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 796 | it->second.asyncTodo.push(BinderNode::AsyncTodo{ |
| 797 | .ref = target, |
| 798 | .data = std::move(transactionData), |
| 799 | .asyncNumber = transaction->asyncNumber, |
| 800 | }); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 801 | |
| 802 | size_t numPending = it->second.asyncTodo.size(); |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 803 | LOG_RPC_DETAIL("Enqueuing %" PRIu64 " on %" PRIu64 " (%zu pending)", |
| 804 | transaction->asyncNumber, addr, numPending); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 805 | |
| 806 | constexpr size_t kArbitraryOnewayCallTerminateLevel = 10000; |
| 807 | constexpr size_t kArbitraryOnewayCallWarnLevel = 1000; |
| 808 | constexpr size_t kArbitraryOnewayCallWarnPer = 1000; |
| 809 | |
| 810 | if (numPending >= kArbitraryOnewayCallWarnLevel) { |
| 811 | if (numPending >= kArbitraryOnewayCallTerminateLevel) { |
| 812 | ALOGE("WARNING: %zu pending oneway transactions. Terminating!", numPending); |
| 813 | _l.unlock(); |
| 814 | (void)session->shutdownAndWait(false); |
| 815 | return FAILED_TRANSACTION; |
| 816 | } |
| 817 | |
| 818 | if (numPending % kArbitraryOnewayCallWarnPer == 0) { |
| 819 | ALOGW("Warning: many oneway transactions built up on %p (%zu)", |
| 820 | target.get(), numPending); |
| 821 | } |
| 822 | } |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 823 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | } |
| 827 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 828 | Parcel reply; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 829 | reply.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 830 | |
| 831 | if (replyStatus == OK) { |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 832 | Parcel data; |
| 833 | // transaction->data is owned by this function. Parcel borrows this data and |
| 834 | // only holds onto it for the duration of this function call. Parcel will be |
| 835 | // deleted before the 'transactionData' object. |
| 836 | data.ipcSetDataReference(transaction->data, |
| 837 | transactionData.size() - offsetof(RpcWireTransaction, data), |
| 838 | nullptr /*object*/, 0 /*objectCount*/, |
| 839 | do_nothing_to_transact_data); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 840 | data.markForRpc(session); |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 841 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 842 | if (target) { |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 843 | bool origAllowNested = connection->allowNested; |
| 844 | connection->allowNested = !oneway; |
| 845 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 846 | replyStatus = target->transact(transaction->code, data, &reply, transaction->flags); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 847 | |
| 848 | connection->allowNested = origAllowNested; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 849 | } else { |
| 850 | LOG_RPC_DETAIL("Got special transaction %u", transaction->code); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 851 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 852 | switch (transaction->code) { |
| 853 | case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: { |
| 854 | replyStatus = reply.writeInt32(session->getMaxThreads()); |
| 855 | break; |
| 856 | } |
| 857 | case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: { |
| 858 | // for client connections, this should always report the value |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 859 | // originally returned from the server, so this is asserting |
| 860 | // that it exists |
Steven Moreland | 826367f | 2021-09-10 14:05:31 -0700 | [diff] [blame] | 861 | replyStatus = reply.writeByteVector(session->mId); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 862 | break; |
| 863 | } |
| 864 | default: { |
Steven Moreland | 7b8bc4c | 2021-06-10 22:50:27 +0000 | [diff] [blame] | 865 | sp<RpcServer> server = session->server(); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 866 | if (server) { |
| 867 | switch (transaction->code) { |
| 868 | case RPC_SPECIAL_TRANSACT_GET_ROOT: { |
| 869 | replyStatus = reply.writeStrongBinder(server->getRootObject()); |
| 870 | break; |
| 871 | } |
| 872 | default: { |
| 873 | replyStatus = UNKNOWN_TRANSACTION; |
| 874 | } |
| 875 | } |
| 876 | } else { |
| 877 | ALOGE("Special command sent, but no server object attached."); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 878 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 879 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 884 | // Binder refs are flushed for oneway calls only after all calls which are |
| 885 | // built up are executed. Otherwise, they fill up the binder buffer. |
| 886 | if (addr != 0 && replyStatus == OK && !oneway) { |
| 887 | replyStatus = flushExcessBinderRefs(session, addr, target); |
| 888 | } |
| 889 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 890 | if (oneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 891 | if (replyStatus != OK) { |
| 892 | ALOGW("Oneway call failed with error: %d", replyStatus); |
| 893 | } |
| 894 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 895 | LOG_RPC_DETAIL("Processed async transaction %" PRIu64 " on %" PRIu64, |
| 896 | transaction->asyncNumber, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 897 | |
| 898 | // Check to see if there is another asynchronous transaction to process. |
| 899 | // This behavior differs from binder behavior, since in the binder |
| 900 | // driver, asynchronous transactions will be processed after existing |
| 901 | // pending binder transactions on the queue. The downside of this is |
| 902 | // that asynchronous transactions can be drowned out by synchronous |
| 903 | // transactions. However, we have no easy way to queue these |
| 904 | // transactions after the synchronous transactions we may want to read |
| 905 | // from the wire. So, in socket binder here, we have the opposite |
| 906 | // downside: asynchronous transactions may drown out synchronous |
| 907 | // transactions. |
| 908 | { |
| 909 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 910 | auto it = mNodeForAddress.find(addr); |
| 911 | // last refcount dropped after this transaction happened |
| 912 | if (it == mNodeForAddress.end()) return OK; |
| 913 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 914 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 915 | _l.unlock(); |
| 916 | (void)session->shutdownAndWait(false); |
| 917 | return DEAD_OBJECT; |
| 918 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 919 | |
| 920 | if (it->second.asyncTodo.size() == 0) return OK; |
| 921 | if (it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 922 | LOG_RPC_DETAIL("Found next async transaction %" PRIu64 " on %" PRIu64, |
| 923 | it->second.asyncNumber, addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 924 | |
| 925 | // justification for const_cast (consider avoiding priority_queue): |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 926 | // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 927 | // - gotta go fast |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 928 | auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top()); |
| 929 | |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 930 | // reset up arguments |
| 931 | transactionData = std::move(todo.data); |
Steven Moreland | 3903bf0 | 2021-09-27 16:05:24 -0700 | [diff] [blame] | 932 | LOG_ALWAYS_FATAL_IF(target != todo.ref, |
| 933 | "async list should be associated with a binder"); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 934 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 935 | it->second.asyncTodo.pop(); |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 936 | goto processTransactInternalTailCall; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 937 | } |
| 938 | } |
Steven Moreland | d808331 | 2021-09-22 13:37:10 -0700 | [diff] [blame] | 939 | |
| 940 | // done processing all the async commands on this binder that we can, so |
| 941 | // write decstrongs on the binder |
| 942 | if (addr != 0 && replyStatus == OK) { |
| 943 | return flushExcessBinderRefs(session, addr, target); |
| 944 | } |
| 945 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 946 | return OK; |
| 947 | } |
| 948 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 949 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 950 | sizeof(RpcWireReply) < |
| 951 | reply.dataSize(), |
| 952 | "Too much data for reply %zu", reply.dataSize()); |
| 953 | |
| 954 | RpcWireHeader cmdReply{ |
| 955 | .command = RPC_COMMAND_REPLY, |
| 956 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireReply) + reply.dataSize()), |
| 957 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 958 | RpcWireReply rpcReply{ |
| 959 | .status = replyStatus, |
| 960 | }; |
| 961 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 962 | CommandData replyData(sizeof(RpcWireHeader) + sizeof(RpcWireReply) + reply.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 963 | if (!replyData.valid()) { |
| 964 | return NO_MEMORY; |
| 965 | } |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 966 | memcpy(replyData.data() + 0, &cmdReply, sizeof(RpcWireHeader)); |
| 967 | memcpy(replyData.data() + sizeof(RpcWireHeader), &rpcReply, sizeof(RpcWireReply)); |
| 968 | memcpy(replyData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireReply), reply.data(), |
| 969 | reply.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 970 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 971 | return rpcSend(connection, session, "reply", replyData.data(), replyData.size()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 974 | status_t RpcState::processDecStrong(const sp<RpcSession::RpcConnection>& connection, |
| 975 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 976 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command); |
| 977 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 978 | CommandData commandData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 979 | if (!commandData.valid()) { |
| 980 | return NO_MEMORY; |
| 981 | } |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 982 | if (status_t status = |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 983 | rpcRec(connection, session, "dec ref body", commandData.data(), commandData.size()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 984 | status != OK) |
| 985 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 986 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 987 | if (command.bodySize != sizeof(RpcDecStrong)) { |
| 988 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcDecStrong. Terminating!", |
| 989 | sizeof(RpcDecStrong), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 990 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 991 | return BAD_VALUE; |
| 992 | } |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 993 | RpcDecStrong* body = reinterpret_cast<RpcDecStrong*>(commandData.data()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 994 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 995 | uint64_t addr = RpcWireAddress::toRaw(body->address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 996 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 997 | auto it = mNodeForAddress.find(addr); |
| 998 | if (it == mNodeForAddress.end()) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 999 | ALOGE("Unknown binder address %" PRIu64 " for dec strong.", addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1000 | return OK; |
| 1001 | } |
| 1002 | |
| 1003 | sp<IBinder> target = it->second.binder.promote(); |
| 1004 | if (target == nullptr) { |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1005 | ALOGE("While requesting dec strong, binder has been deleted at address %" PRIu64 |
| 1006 | ". Terminating!", |
| 1007 | addr); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1008 | _l.unlock(); |
| 1009 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1010 | return BAD_VALUE; |
| 1011 | } |
| 1012 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 1013 | if (it->second.timesSent < body->amount) { |
| 1014 | ALOGE("Record of sending binder %zu times, but requested decStrong for %" PRIu64 " of %u", |
| 1015 | it->second.timesSent, addr, body->amount); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1016 | return OK; |
| 1017 | } |
| 1018 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1019 | LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %" PRIu64, |
| 1020 | addr); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1021 | |
Steven Moreland | fd1e8a0 | 2021-07-21 23:30:29 +0000 | [diff] [blame] | 1022 | LOG_RPC_DETAIL("Processing dec strong of %" PRIu64 " by %u from %zu", addr, body->amount, |
| 1023 | it->second.timesSent); |
| 1024 | |
| 1025 | it->second.timesSent -= body->amount; |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1026 | sp<IBinder> tempHold = tryEraseNode(it); |
| 1027 | _l.unlock(); |
| 1028 | tempHold = nullptr; // destructor may make binder calls on this session |
| 1029 | |
| 1030 | return OK; |
| 1031 | } |
| 1032 | |
Steven Moreland | 5623d1a | 2021-09-10 15:45:34 -0700 | [diff] [blame] | 1033 | sp<IBinder> RpcState::tryEraseNode(std::map<uint64_t, BinderNode>::iterator& it) { |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1034 | sp<IBinder> ref; |
| 1035 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1036 | if (it->second.timesSent == 0) { |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1037 | ref = std::move(it->second.sentRef); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1038 | |
| 1039 | if (it->second.timesRecd == 0) { |
Steven Moreland | a6e11cf | 2021-06-04 00:58:31 +0000 | [diff] [blame] | 1040 | LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(), |
| 1041 | "Can't delete binder w/ pending async transactions"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1042 | mNodeForAddress.erase(it); |
| 1043 | } |
| 1044 | } |
| 1045 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 1046 | return ref; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 1049 | bool RpcState::nodeProgressAsyncNumber(BinderNode* node) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 1050 | // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to |
| 1051 | // a single binder |
| 1052 | if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) { |
| 1053 | ALOGE("Out of async transaction IDs. Terminating"); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 1054 | return false; |
| 1055 | } |
| 1056 | node->asyncNumber++; |
| 1057 | return true; |
| 1058 | } |
| 1059 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1060 | } // namespace android |