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