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) { |
| 276 | LOG_RPC_DETAIL("Sending %s on fd %d: %s", what, connection->fd.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 | |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 285 | if (status_t status = session->mShutdownTrigger->interruptableWriteFully(connection->fd.get(), |
| 286 | data, size); |
| 287 | status != OK) { |
| 288 | LOG_RPC_DETAIL("Failed to write %s (%zu bytes) on fd %d, error: %s", what, size, |
| 289 | connection->fd.get(), statusToString(status).c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 290 | (void)session->shutdownAndWait(false); |
Steven Moreland | 798e0d1 | 2021-07-14 23:19:25 +0000 | [diff] [blame] | 291 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 294 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 297 | status_t RpcState::rpcRec(const sp<RpcSession::RpcConnection>& connection, |
| 298 | const sp<RpcSession>& session, const char* what, void* data, |
| 299 | size_t size) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 300 | if (size > std::numeric_limits<ssize_t>::max()) { |
| 301 | ALOGE("Cannot rec %s at size %zu (too big)", what, size); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 302 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 303 | return BAD_VALUE; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 306 | if (status_t status = |
| 307 | session->mShutdownTrigger->interruptableReadFully(connection->fd.get(), data, size); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 308 | status != OK) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 309 | LOG_RPC_DETAIL("Failed to read %s (%zu bytes) on fd %d, error: %s", what, size, |
| 310 | connection->fd.get(), statusToString(status).c_str()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 311 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 314 | LOG_RPC_DETAIL("Received %s on fd %d: %s", what, connection->fd.get(), |
Steven Moreland | 6212901 | 2021-07-29 12:14:44 -0700 | [diff] [blame] | 315 | android::base::HexString(data, size).c_str()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 316 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Steven Moreland | bf57bce | 2021-07-26 15:26:12 -0700 | [diff] [blame] | 319 | status_t RpcState::readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection, |
| 320 | const sp<RpcSession>& session, uint32_t* version) { |
| 321 | RpcNewSessionResponse response; |
| 322 | if (status_t status = |
| 323 | rpcRec(connection, session, "new session response", &response, sizeof(response)); |
| 324 | status != OK) { |
| 325 | return status; |
| 326 | } |
| 327 | *version = response.version; |
| 328 | return OK; |
| 329 | } |
| 330 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 331 | status_t RpcState::sendConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 332 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 333 | RpcOutgoingConnectionInit init{ |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 334 | .msg = RPC_CONNECTION_INIT_OKAY, |
| 335 | }; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 336 | return rpcSend(connection, session, "connection init", &init, sizeof(init)); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 339 | status_t RpcState::readConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 340 | const sp<RpcSession>& session) { |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 341 | RpcOutgoingConnectionInit init; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 342 | if (status_t status = rpcRec(connection, session, "connection init", &init, sizeof(init)); |
| 343 | status != OK) |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 344 | return status; |
| 345 | |
| 346 | static_assert(sizeof(init.msg) == sizeof(RPC_CONNECTION_INIT_OKAY)); |
| 347 | if (0 != strncmp(init.msg, RPC_CONNECTION_INIT_OKAY, sizeof(init.msg))) { |
| 348 | ALOGE("Connection init message unrecognized %.*s", static_cast<int>(sizeof(init.msg)), |
| 349 | init.msg); |
| 350 | return BAD_VALUE; |
| 351 | } |
| 352 | return OK; |
| 353 | } |
| 354 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 355 | sp<IBinder> RpcState::getRootObject(const sp<RpcSession::RpcConnection>& connection, |
| 356 | const sp<RpcSession>& session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 357 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 358 | data.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 359 | Parcel reply; |
| 360 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 361 | status_t status = transactAddress(connection, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_ROOT, |
| 362 | data, session, &reply, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 363 | if (status != OK) { |
| 364 | ALOGE("Error getting root object: %s", statusToString(status).c_str()); |
| 365 | return nullptr; |
| 366 | } |
| 367 | |
| 368 | return reply.readStrongBinder(); |
| 369 | } |
| 370 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 371 | status_t RpcState::getMaxThreads(const sp<RpcSession::RpcConnection>& connection, |
| 372 | const sp<RpcSession>& session, size_t* maxThreadsOut) { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 373 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 374 | data.markForRpc(session); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 375 | Parcel reply; |
| 376 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 377 | status_t status = |
| 378 | transactAddress(connection, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, |
| 379 | data, session, &reply, 0); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 380 | if (status != OK) { |
| 381 | ALOGE("Error getting max threads: %s", statusToString(status).c_str()); |
| 382 | return status; |
| 383 | } |
| 384 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 385 | int32_t maxThreads; |
| 386 | status = reply.readInt32(&maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 387 | if (status != OK) return status; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 388 | if (maxThreads <= 0) { |
| 389 | ALOGE("Error invalid max maxThreads: %d", maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 390 | return BAD_VALUE; |
| 391 | } |
| 392 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 393 | *maxThreadsOut = maxThreads; |
| 394 | return OK; |
| 395 | } |
| 396 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 397 | status_t RpcState::getSessionId(const sp<RpcSession::RpcConnection>& connection, |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 398 | const sp<RpcSession>& session, RpcAddress* sessionIdOut) { |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 399 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 400 | data.markForRpc(session); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 401 | Parcel reply; |
| 402 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 403 | status_t status = |
| 404 | transactAddress(connection, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_SESSION_ID, |
| 405 | data, session, &reply, 0); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 406 | if (status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 407 | ALOGE("Error getting session ID: %s", statusToString(status).c_str()); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 408 | return status; |
| 409 | } |
| 410 | |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 411 | return sessionIdOut->readFromParcel(reply); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 414 | status_t RpcState::transact(const sp<RpcSession::RpcConnection>& connection, |
| 415 | const sp<IBinder>& binder, uint32_t code, const Parcel& data, |
| 416 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 417 | if (!data.isForRpc()) { |
| 418 | ALOGE("Refusing to send RPC with parcel not crafted for RPC"); |
| 419 | return BAD_TYPE; |
| 420 | } |
| 421 | |
| 422 | if (data.objectsCount() != 0) { |
| 423 | ALOGE("Parcel at %p has attached objects but is being used in an RPC call", &data); |
| 424 | return BAD_TYPE; |
| 425 | } |
| 426 | |
| 427 | RpcAddress address = RpcAddress::zero(); |
| 428 | if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status; |
| 429 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 430 | return transactAddress(connection, address, code, data, session, reply, flags); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 433 | status_t RpcState::transactAddress(const sp<RpcSession::RpcConnection>& connection, |
| 434 | const RpcAddress& address, uint32_t code, const Parcel& data, |
| 435 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 436 | LOG_ALWAYS_FATAL_IF(!data.isForRpc()); |
| 437 | LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0); |
| 438 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 439 | uint64_t asyncNumber = 0; |
| 440 | |
| 441 | if (!address.isZero()) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 442 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 443 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 444 | auto it = mNodeForAddress.find(address); |
| 445 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending transact on unknown address %s", |
| 446 | address.toString().c_str()); |
| 447 | |
| 448 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 449 | asyncNumber = it->second.asyncNumber; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 450 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 451 | _l.unlock(); |
| 452 | (void)session->shutdownAndWait(false); |
| 453 | return DEAD_OBJECT; |
| 454 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 458 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 459 | sizeof(RpcWireTransaction) < |
| 460 | data.dataSize(), |
| 461 | "Too much data %zu", data.dataSize()); |
| 462 | |
| 463 | RpcWireHeader command{ |
| 464 | .command = RPC_COMMAND_TRANSACT, |
| 465 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireTransaction) + data.dataSize()), |
| 466 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 467 | RpcWireTransaction transaction{ |
| 468 | .address = address.viewRawEmbedded(), |
| 469 | .code = code, |
| 470 | .flags = flags, |
| 471 | .asyncNumber = asyncNumber, |
| 472 | }; |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 473 | CommandData transactionData(sizeof(RpcWireHeader) + sizeof(RpcWireTransaction) + |
| 474 | data.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 475 | if (!transactionData.valid()) { |
| 476 | return NO_MEMORY; |
| 477 | } |
| 478 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 479 | memcpy(transactionData.data() + 0, &command, sizeof(RpcWireHeader)); |
| 480 | memcpy(transactionData.data() + sizeof(RpcWireHeader), &transaction, |
| 481 | sizeof(RpcWireTransaction)); |
| 482 | memcpy(transactionData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireTransaction), data.data(), |
| 483 | data.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 484 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 485 | if (status_t status = rpcSend(connection, session, "transaction", transactionData.data(), |
| 486 | transactionData.size()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 487 | status != OK) |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 488 | // TODO(b/167966510): need to undo onBinderLeaving - we know the |
| 489 | // refcount isn't successfully transferred. |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 490 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 491 | |
| 492 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 493 | LOG_RPC_DETAIL("Oneway command, so no longer waiting on %d", connection->fd.get()); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 494 | |
| 495 | // Do not wait on result. |
| 496 | // However, too many oneway calls may cause refcounts to build up and fill up the socket, |
| 497 | // so process those. |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 498 | return drainCommands(connection, session, CommandType::CONTROL_ONLY); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction."); |
| 502 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 503 | return waitForReply(connection, session, reply); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 504 | } |
| 505 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 506 | static void cleanup_reply_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 507 | const binder_size_t* objects, size_t objectsCount) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 508 | (void)p; |
| 509 | delete[] const_cast<uint8_t*>(data - offsetof(RpcWireReply, data)); |
| 510 | (void)dataSize; |
| 511 | LOG_ALWAYS_FATAL_IF(objects != nullptr); |
Yifan Hong | 239a2ca | 2021-06-24 16:05:16 -0700 | [diff] [blame] | 512 | LOG_ALWAYS_FATAL_IF(objectsCount != 0, "%zu objects remaining", objectsCount); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 515 | status_t RpcState::waitForReply(const sp<RpcSession::RpcConnection>& connection, |
| 516 | const sp<RpcSession>& session, Parcel* reply) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 517 | RpcWireHeader command; |
| 518 | while (true) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 519 | if (status_t status = |
| 520 | rpcRec(connection, session, "command header", &command, sizeof(command)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 521 | status != OK) |
| 522 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 523 | |
| 524 | if (command.command == RPC_COMMAND_REPLY) break; |
| 525 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 526 | if (status_t status = processCommand(connection, session, command, CommandType::ANY); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 527 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 528 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 531 | CommandData data(command.bodySize); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 532 | if (!data.valid()) return NO_MEMORY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 533 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 534 | 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] | 535 | status != OK) |
| 536 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 537 | |
| 538 | if (command.bodySize < sizeof(RpcWireReply)) { |
| 539 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!", |
| 540 | sizeof(RpcWireReply), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 541 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 542 | return BAD_VALUE; |
| 543 | } |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 544 | RpcWireReply* rpcReply = reinterpret_cast<RpcWireReply*>(data.data()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 545 | if (rpcReply->status != OK) return rpcReply->status; |
| 546 | |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 547 | data.release(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 548 | reply->ipcSetDataReference(rpcReply->data, command.bodySize - offsetof(RpcWireReply, data), |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 549 | nullptr, 0, cleanup_reply_data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 550 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 551 | reply->markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 552 | |
| 553 | return OK; |
| 554 | } |
| 555 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 556 | status_t RpcState::sendDecStrong(const sp<RpcSession::RpcConnection>& connection, |
| 557 | const sp<RpcSession>& session, const RpcAddress& addr) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 558 | { |
| 559 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 560 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 561 | auto it = mNodeForAddress.find(addr); |
| 562 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending dec strong on unknown address %s", |
| 563 | addr.toString().c_str()); |
| 564 | LOG_ALWAYS_FATAL_IF(it->second.timesRecd <= 0, "Bad dec strong %s", |
| 565 | addr.toString().c_str()); |
| 566 | |
| 567 | it->second.timesRecd--; |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 568 | LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(it), |
| 569 | "Bad state. RpcState shouldn't own received binder"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | RpcWireHeader cmd = { |
| 573 | .command = RPC_COMMAND_DEC_STRONG, |
| 574 | .bodySize = sizeof(RpcWireAddress), |
| 575 | }; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 576 | if (status_t status = rpcSend(connection, session, "dec ref header", &cmd, sizeof(cmd)); |
| 577 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 578 | return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 579 | if (status_t status = rpcSend(connection, session, "dec ref body", &addr.viewRawEmbedded(), |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 580 | sizeof(RpcWireAddress)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 581 | status != OK) |
| 582 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 583 | return OK; |
| 584 | } |
| 585 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 586 | status_t RpcState::getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection, |
| 587 | const sp<RpcSession>& session, CommandType type) { |
| 588 | LOG_RPC_DETAIL("getAndExecuteCommand on fd %d", connection->fd.get()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 589 | |
| 590 | RpcWireHeader command; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 591 | if (status_t status = rpcRec(connection, session, "command header", &command, sizeof(command)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 592 | status != OK) |
| 593 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 594 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 595 | return processCommand(connection, session, command, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 598 | status_t RpcState::drainCommands(const sp<RpcSession::RpcConnection>& connection, |
| 599 | const sp<RpcSession>& session, CommandType type) { |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 600 | uint8_t buf; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 601 | while (0 < TEMP_FAILURE_RETRY( |
| 602 | recv(connection->fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT))) { |
| 603 | status_t status = getAndExecuteCommand(connection, session, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 604 | if (status != OK) return status; |
| 605 | } |
| 606 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Steven Moreland | 19fc9f7 | 2021-06-10 03:57:30 +0000 | [diff] [blame] | 609 | status_t RpcState::processCommand(const sp<RpcSession::RpcConnection>& connection, |
| 610 | const sp<RpcSession>& session, const RpcWireHeader& command, |
| 611 | CommandType type) { |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 612 | IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); |
| 613 | IPCThreadState::SpGuard spGuard{ |
| 614 | .address = __builtin_frame_address(0), |
| 615 | .context = "processing binder RPC command", |
| 616 | }; |
| 617 | const IPCThreadState::SpGuard* origGuard; |
| 618 | if (kernelBinderState != nullptr) { |
| 619 | origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); |
| 620 | } |
| 621 | ScopeGuard guardUnguard = [&]() { |
| 622 | if (kernelBinderState != nullptr) { |
| 623 | kernelBinderState->restoreGetCallingSpGuard(origGuard); |
| 624 | } |
| 625 | }; |
| 626 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 627 | switch (command.command) { |
| 628 | case RPC_COMMAND_TRANSACT: |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 629 | if (type != CommandType::ANY) return BAD_TYPE; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 630 | return processTransact(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 631 | case RPC_COMMAND_DEC_STRONG: |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 632 | return processDecStrong(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // We should always know the version of the opposing side, and since the |
| 636 | // RPC-binder-level wire protocol is not self synchronizing, we have no way |
| 637 | // to understand where the current command ends and the next one begins. We |
| 638 | // 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] | 639 | // to kill us, so ending the session for misbehaving client. |
| 640 | ALOGE("Unknown RPC command %d - terminating session", command.command); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 641 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 642 | return DEAD_OBJECT; |
| 643 | } |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 644 | status_t RpcState::processTransact(const sp<RpcSession::RpcConnection>& connection, |
| 645 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 646 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command); |
| 647 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 648 | CommandData transactionData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 649 | if (!transactionData.valid()) { |
| 650 | return NO_MEMORY; |
| 651 | } |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 652 | if (status_t status = rpcRec(connection, session, "transaction body", transactionData.data(), |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 653 | transactionData.size()); |
| 654 | status != OK) |
| 655 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 656 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 657 | return processTransactInternal(connection, session, std::move(transactionData)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 660 | static void do_nothing_to_transact_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 661 | const binder_size_t* objects, size_t objectsCount) { |
| 662 | (void)p; |
| 663 | (void)data; |
| 664 | (void)dataSize; |
| 665 | (void)objects; |
| 666 | (void)objectsCount; |
| 667 | } |
| 668 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 669 | status_t RpcState::processTransactInternal(const sp<RpcSession::RpcConnection>& connection, |
| 670 | const sp<RpcSession>& session, |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 671 | CommandData transactionData) { |
| 672 | // for 'recursive' calls to this, we have already read and processed the |
| 673 | // binder from the transaction data and taken reference counts into account, |
| 674 | // so it is cached here. |
| 675 | sp<IBinder> targetRef; |
| 676 | processTransactInternalTailCall: |
| 677 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 678 | if (transactionData.size() < sizeof(RpcWireTransaction)) { |
| 679 | ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!", |
| 680 | sizeof(RpcWireTransaction), transactionData.size()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 681 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 682 | return BAD_VALUE; |
| 683 | } |
| 684 | RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data()); |
| 685 | |
| 686 | // TODO(b/182939933): heap allocation just for lookup in mNodeForAddress, |
| 687 | // maybe add an RpcAddress 'view' if the type remains 'heavy' |
| 688 | auto addr = RpcAddress::fromRawEmbedded(&transaction->address); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 689 | bool oneway = transaction->flags & IBinder::FLAG_ONEWAY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 690 | |
| 691 | status_t replyStatus = OK; |
| 692 | sp<IBinder> target; |
| 693 | if (!addr.isZero()) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 694 | if (!targetRef) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 695 | replyStatus = onBinderEntering(session, addr, &target); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 696 | } else { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 697 | target = targetRef; |
| 698 | } |
| 699 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 700 | if (replyStatus != OK) { |
| 701 | // do nothing |
| 702 | } else if (target == nullptr) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 703 | // This can happen if the binder is remote in this process, and |
| 704 | // another thread has called the last decStrong on this binder. |
| 705 | // However, for local binders, it indicates a misbehaving client |
| 706 | // (any binder which is being transacted on should be holding a |
| 707 | // strong ref count), so in either case, terminating the |
| 708 | // session. |
| 709 | ALOGE("While transacting, binder has been deleted at address %s. Terminating!", |
| 710 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 711 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 712 | replyStatus = BAD_VALUE; |
| 713 | } else if (target->localBinder() == nullptr) { |
| 714 | ALOGE("Unknown binder address or non-local binder, not address %s. Terminating!", |
| 715 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 716 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 717 | replyStatus = BAD_VALUE; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 718 | } else if (oneway) { |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 719 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 720 | auto it = mNodeForAddress.find(addr); |
| 721 | if (it->second.binder.promote() != target) { |
| 722 | ALOGE("Binder became invalid during transaction. Bad client? %s", |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 723 | addr.toString().c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 724 | replyStatus = BAD_VALUE; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 725 | } else if (transaction->asyncNumber != it->second.asyncNumber) { |
| 726 | // we need to process some other asynchronous transaction |
| 727 | // first |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 728 | it->second.asyncTodo.push(BinderNode::AsyncTodo{ |
| 729 | .ref = target, |
| 730 | .data = std::move(transactionData), |
| 731 | .asyncNumber = transaction->asyncNumber, |
| 732 | }); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 733 | |
| 734 | size_t numPending = it->second.asyncTodo.size(); |
| 735 | LOG_RPC_DETAIL("Enqueuing %" PRId64 " on %s (%zu pending)", |
| 736 | transaction->asyncNumber, addr.toString().c_str(), numPending); |
| 737 | |
| 738 | constexpr size_t kArbitraryOnewayCallTerminateLevel = 10000; |
| 739 | constexpr size_t kArbitraryOnewayCallWarnLevel = 1000; |
| 740 | constexpr size_t kArbitraryOnewayCallWarnPer = 1000; |
| 741 | |
| 742 | if (numPending >= kArbitraryOnewayCallWarnLevel) { |
| 743 | if (numPending >= kArbitraryOnewayCallTerminateLevel) { |
| 744 | ALOGE("WARNING: %zu pending oneway transactions. Terminating!", numPending); |
| 745 | _l.unlock(); |
| 746 | (void)session->shutdownAndWait(false); |
| 747 | return FAILED_TRANSACTION; |
| 748 | } |
| 749 | |
| 750 | if (numPending % kArbitraryOnewayCallWarnPer == 0) { |
| 751 | ALOGW("Warning: many oneway transactions built up on %p (%zu)", |
| 752 | target.get(), numPending); |
| 753 | } |
| 754 | } |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 755 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | } |
| 759 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 760 | Parcel reply; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 761 | reply.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 762 | |
| 763 | if (replyStatus == OK) { |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 764 | Parcel data; |
| 765 | // transaction->data is owned by this function. Parcel borrows this data and |
| 766 | // only holds onto it for the duration of this function call. Parcel will be |
| 767 | // deleted before the 'transactionData' object. |
| 768 | data.ipcSetDataReference(transaction->data, |
| 769 | transactionData.size() - offsetof(RpcWireTransaction, data), |
| 770 | nullptr /*object*/, 0 /*objectCount*/, |
| 771 | do_nothing_to_transact_data); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 772 | data.markForRpc(session); |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 773 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 774 | if (target) { |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 775 | bool origAllowNested = connection->allowNested; |
| 776 | connection->allowNested = !oneway; |
| 777 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 778 | replyStatus = target->transact(transaction->code, data, &reply, transaction->flags); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 779 | |
| 780 | connection->allowNested = origAllowNested; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 781 | } else { |
| 782 | LOG_RPC_DETAIL("Got special transaction %u", transaction->code); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 783 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 784 | switch (transaction->code) { |
| 785 | case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: { |
| 786 | replyStatus = reply.writeInt32(session->getMaxThreads()); |
| 787 | break; |
| 788 | } |
| 789 | case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: { |
| 790 | // for client connections, this should always report the value |
Steven Moreland | 01a6bad | 2021-06-11 00:59:20 +0000 | [diff] [blame] | 791 | // originally returned from the server, so this is asserting |
| 792 | // that it exists |
| 793 | replyStatus = session->mId.value().writeToParcel(&reply); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 794 | break; |
| 795 | } |
| 796 | default: { |
Steven Moreland | 7b8bc4c | 2021-06-10 22:50:27 +0000 | [diff] [blame] | 797 | sp<RpcServer> server = session->server(); |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 798 | if (server) { |
| 799 | switch (transaction->code) { |
| 800 | case RPC_SPECIAL_TRANSACT_GET_ROOT: { |
| 801 | replyStatus = reply.writeStrongBinder(server->getRootObject()); |
| 802 | break; |
| 803 | } |
| 804 | default: { |
| 805 | replyStatus = UNKNOWN_TRANSACTION; |
| 806 | } |
| 807 | } |
| 808 | } else { |
| 809 | ALOGE("Special command sent, but no server object attached."); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 810 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 811 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | } |
| 815 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame] | 816 | if (oneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 817 | if (replyStatus != OK) { |
| 818 | ALOGW("Oneway call failed with error: %d", replyStatus); |
| 819 | } |
| 820 | |
| 821 | LOG_RPC_DETAIL("Processed async transaction %" PRId64 " on %s", transaction->asyncNumber, |
| 822 | addr.toString().c_str()); |
| 823 | |
| 824 | // Check to see if there is another asynchronous transaction to process. |
| 825 | // This behavior differs from binder behavior, since in the binder |
| 826 | // driver, asynchronous transactions will be processed after existing |
| 827 | // pending binder transactions on the queue. The downside of this is |
| 828 | // that asynchronous transactions can be drowned out by synchronous |
| 829 | // transactions. However, we have no easy way to queue these |
| 830 | // transactions after the synchronous transactions we may want to read |
| 831 | // from the wire. So, in socket binder here, we have the opposite |
| 832 | // downside: asynchronous transactions may drown out synchronous |
| 833 | // transactions. |
| 834 | { |
| 835 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 836 | auto it = mNodeForAddress.find(addr); |
| 837 | // last refcount dropped after this transaction happened |
| 838 | if (it == mNodeForAddress.end()) return OK; |
| 839 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 840 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 841 | _l.unlock(); |
| 842 | (void)session->shutdownAndWait(false); |
| 843 | return DEAD_OBJECT; |
| 844 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 845 | |
| 846 | if (it->second.asyncTodo.size() == 0) return OK; |
| 847 | if (it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) { |
| 848 | LOG_RPC_DETAIL("Found next async transaction %" PRId64 " on %s", |
| 849 | it->second.asyncNumber, addr.toString().c_str()); |
| 850 | |
| 851 | // justification for const_cast (consider avoiding priority_queue): |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 852 | // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 853 | // - gotta go fast |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 854 | auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top()); |
| 855 | |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 856 | // reset up arguments |
| 857 | transactionData = std::move(todo.data); |
| 858 | targetRef = std::move(todo.ref); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 859 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 860 | it->second.asyncTodo.pop(); |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 861 | goto processTransactInternalTailCall; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 862 | } |
| 863 | } |
| 864 | return OK; |
| 865 | } |
| 866 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 867 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 868 | sizeof(RpcWireReply) < |
| 869 | reply.dataSize(), |
| 870 | "Too much data for reply %zu", reply.dataSize()); |
| 871 | |
| 872 | RpcWireHeader cmdReply{ |
| 873 | .command = RPC_COMMAND_REPLY, |
| 874 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireReply) + reply.dataSize()), |
| 875 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 876 | RpcWireReply rpcReply{ |
| 877 | .status = replyStatus, |
| 878 | }; |
| 879 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 880 | CommandData replyData(sizeof(RpcWireHeader) + sizeof(RpcWireReply) + reply.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 881 | if (!replyData.valid()) { |
| 882 | return NO_MEMORY; |
| 883 | } |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 884 | memcpy(replyData.data() + 0, &cmdReply, sizeof(RpcWireHeader)); |
| 885 | memcpy(replyData.data() + sizeof(RpcWireHeader), &rpcReply, sizeof(RpcWireReply)); |
| 886 | memcpy(replyData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireReply), reply.data(), |
| 887 | reply.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 888 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 889 | return rpcSend(connection, session, "reply", replyData.data(), replyData.size()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 890 | } |
| 891 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 892 | status_t RpcState::processDecStrong(const sp<RpcSession::RpcConnection>& connection, |
| 893 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 894 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command); |
| 895 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 896 | CommandData commandData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 897 | if (!commandData.valid()) { |
| 898 | return NO_MEMORY; |
| 899 | } |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 900 | if (status_t status = |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 901 | rpcRec(connection, session, "dec ref body", commandData.data(), commandData.size()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 902 | status != OK) |
| 903 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 904 | |
| 905 | if (command.bodySize < sizeof(RpcWireAddress)) { |
| 906 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireAddress. Terminating!", |
| 907 | sizeof(RpcWireAddress), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 908 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 909 | return BAD_VALUE; |
| 910 | } |
| 911 | RpcWireAddress* address = reinterpret_cast<RpcWireAddress*>(commandData.data()); |
| 912 | |
| 913 | // TODO(b/182939933): heap allocation just for lookup |
| 914 | auto addr = RpcAddress::fromRawEmbedded(address); |
| 915 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 916 | auto it = mNodeForAddress.find(addr); |
| 917 | if (it == mNodeForAddress.end()) { |
| 918 | ALOGE("Unknown binder address %s for dec strong.", addr.toString().c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 919 | return OK; |
| 920 | } |
| 921 | |
| 922 | sp<IBinder> target = it->second.binder.promote(); |
| 923 | if (target == nullptr) { |
| 924 | ALOGE("While requesting dec strong, binder has been deleted at address %s. Terminating!", |
| 925 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 926 | _l.unlock(); |
| 927 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 928 | return BAD_VALUE; |
| 929 | } |
| 930 | |
| 931 | if (it->second.timesSent == 0) { |
| 932 | ALOGE("No record of sending binder, but requested decStrong: %s", addr.toString().c_str()); |
| 933 | return OK; |
| 934 | } |
| 935 | |
| 936 | LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %s", |
| 937 | addr.toString().c_str()); |
| 938 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 939 | it->second.timesSent--; |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 940 | sp<IBinder> tempHold = tryEraseNode(it); |
| 941 | _l.unlock(); |
| 942 | tempHold = nullptr; // destructor may make binder calls on this session |
| 943 | |
| 944 | return OK; |
| 945 | } |
| 946 | |
| 947 | sp<IBinder> RpcState::tryEraseNode(std::map<RpcAddress, BinderNode>::iterator& it) { |
| 948 | sp<IBinder> ref; |
| 949 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 950 | if (it->second.timesSent == 0) { |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 951 | ref = std::move(it->second.sentRef); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 952 | |
| 953 | if (it->second.timesRecd == 0) { |
Steven Moreland | a6e11cf | 2021-06-04 00:58:31 +0000 | [diff] [blame] | 954 | LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(), |
| 955 | "Can't delete binder w/ pending async transactions"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 956 | mNodeForAddress.erase(it); |
| 957 | } |
| 958 | } |
| 959 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 960 | return ref; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 961 | } |
| 962 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 963 | bool RpcState::nodeProgressAsyncNumber(BinderNode* node) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 964 | // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to |
| 965 | // a single binder |
| 966 | if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) { |
| 967 | ALOGE("Out of async transaction IDs. Terminating"); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 968 | return false; |
| 969 | } |
| 970 | node->asyncNumber++; |
| 971 | return true; |
| 972 | } |
| 973 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 974 | } // namespace android |