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