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